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
8 * Authors: Christophe Massiot <massiot@via.ecp.fr>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_codec.h>
39 #define MPEG_FRAME_SIZE 1152
40 #define MAX_CODED_FRAME_SIZE 1792
42 /****************************************************************************
44 ****************************************************************************/
45 static int OpenEncoder ( vlc_object_t
* );
46 static void CloseEncoder ( vlc_object_t
* );
47 static block_t
*Encode ( encoder_t
*, block_t
* );
49 /*****************************************************************************
51 *****************************************************************************/
52 #define ENC_CFG_PREFIX "sout-twolame-"
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." )
68 static const int pi_stereo_values
[] = { 0, 1, 2 };
69 static const char *const ppsz_stereo_descriptions
[] =
70 { N_("Stereo"), N_("Dual mono"), N_("Joint stereo") };
74 set_shortname( "Twolame")
75 set_description( N_("Libtwolame audio encoder") )
76 set_capability( "encoder", 120 )
77 set_callbacks( OpenEncoder
, CloseEncoder
)
78 set_category( CAT_INPUT
)
79 set_subcategory( SUBCAT_INPUT_ACODEC
)
81 add_float( ENC_CFG_PREFIX
"quality", 0.0, ENC_QUALITY_TEXT
,
82 ENC_QUALITY_LONGTEXT
, false )
83 add_integer( ENC_CFG_PREFIX
"mode", 0, ENC_MODE_TEXT
,
84 ENC_MODE_LONGTEXT
, false )
85 change_integer_list( pi_stereo_values
, ppsz_stereo_descriptions
);
86 add_bool( ENC_CFG_PREFIX
"vbr", false, ENC_VBR_TEXT
,
87 ENC_VBR_LONGTEXT
, false )
88 add_integer( ENC_CFG_PREFIX
"psy", 3, ENC_PSY_TEXT
,
89 ENC_PSY_LONGTEXT
, false )
92 static const char *const ppsz_enc_options
[] = {
93 "quality", "mode", "vbr", "psy", NULL
96 /*****************************************************************************
97 * encoder_sys_t : twolame encoder descriptor
98 *****************************************************************************/
104 int16_t p_buffer
[MPEG_FRAME_SIZE
* 2];
109 * libtwolame properties
111 twolame_options
*p_twolame
;
112 unsigned char p_out_buffer
[MAX_CODED_FRAME_SIZE
];
115 /*****************************************************************************
116 * OpenEncoder: probe the encoder and return score
117 *****************************************************************************/
118 static const uint16_t mpa_bitrate_tab
[2][15] =
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}
124 static const uint16_t mpa_freq_tab
[6] =
125 { 44100, 48000, 32000, 22050, 24000, 16000 };
127 static int OpenEncoder( vlc_object_t
*p_this
)
129 encoder_t
*p_enc
= (encoder_t
*)p_this
;
130 encoder_sys_t
*p_sys
;
133 if( p_enc
->fmt_out
.i_codec
!= VLC_CODEC_MP2
&&
134 p_enc
->fmt_out
.i_codec
!= VLC_CODEC_MPGA
&&
135 p_enc
->fmt_out
.i_codec
!= VLC_FOURCC( 'm', 'p', '2', 'a' ) &&
141 if( p_enc
->fmt_in
.audio
.i_channels
> 2 )
143 msg_Err( p_enc
, "doesn't support > 2 channels" );
147 for ( i_frequency
= 0; i_frequency
< 6; i_frequency
++ )
149 if ( p_enc
->fmt_out
.audio
.i_rate
== mpa_freq_tab
[i_frequency
] )
152 if ( i_frequency
== 6 )
154 msg_Err( p_enc
, "MPEG audio doesn't support frequency=%d",
155 p_enc
->fmt_out
.audio
.i_rate
);
159 /* Allocate the memory needed to store the decoder's structure */
160 if( ( p_sys
= (encoder_sys_t
*)malloc(sizeof(encoder_sys_t
)) ) == NULL
)
162 p_enc
->p_sys
= p_sys
;
164 p_enc
->fmt_in
.i_codec
= VLC_CODEC_S16N
;
166 p_enc
->fmt_out
.i_cat
= AUDIO_ES
;
167 p_enc
->fmt_out
.i_codec
= VLC_CODEC_MPGA
;
169 config_ChainParse( p_enc
, ENC_CFG_PREFIX
, ppsz_enc_options
, p_enc
->p_cfg
);
171 p_sys
->p_twolame
= twolame_init();
174 twolame_set_in_samplerate( p_sys
->p_twolame
, p_enc
->fmt_out
.audio
.i_rate
);
175 twolame_set_out_samplerate( p_sys
->p_twolame
, p_enc
->fmt_out
.audio
.i_rate
);
177 if( var_GetBool( p_enc
, ENC_CFG_PREFIX
"vbr" ) )
179 float f_quality
= var_GetFloat( p_enc
, ENC_CFG_PREFIX
"quality" );
180 if ( f_quality
> 50.f
) f_quality
= 50.f
;
181 if ( f_quality
< 0.f
) f_quality
= 0.f
;
182 twolame_set_VBR( p_sys
->p_twolame
, 1 );
183 twolame_set_VBR_q( p_sys
->p_twolame
, f_quality
);
188 for ( i
= 1; i
< 14; i
++ )
190 if ( p_enc
->fmt_out
.i_bitrate
/ 1000
191 <= mpa_bitrate_tab
[i_frequency
/ 3][i
] )
194 if ( p_enc
->fmt_out
.i_bitrate
/ 1000
195 != mpa_bitrate_tab
[i_frequency
/ 3][i
] )
197 msg_Warn( p_enc
, "MPEG audio doesn't support bitrate=%d, using %d",
198 p_enc
->fmt_out
.i_bitrate
,
199 mpa_bitrate_tab
[i_frequency
/ 3][i
] * 1000 );
200 p_enc
->fmt_out
.i_bitrate
= mpa_bitrate_tab
[i_frequency
/ 3][i
]
204 twolame_set_bitrate( p_sys
->p_twolame
,
205 p_enc
->fmt_out
.i_bitrate
/ 1000 );
208 if ( p_enc
->fmt_in
.audio
.i_channels
== 1 )
210 twolame_set_num_channels( p_sys
->p_twolame
, 1 );
211 twolame_set_mode( p_sys
->p_twolame
, TWOLAME_MONO
);
215 twolame_set_num_channels( p_sys
->p_twolame
, 2 );
216 switch( var_GetInteger( p_enc
, ENC_CFG_PREFIX
"mode" ) )
219 twolame_set_mode( p_sys
->p_twolame
, TWOLAME_DUAL_CHANNEL
);
222 twolame_set_mode( p_sys
->p_twolame
, TWOLAME_JOINT_STEREO
);
226 twolame_set_mode( p_sys
->p_twolame
, TWOLAME_STEREO
);
231 twolame_set_psymodel( p_sys
->p_twolame
,
232 var_GetInteger( p_enc
, ENC_CFG_PREFIX
"psy" ) );
234 if ( twolame_init_params( p_sys
->p_twolame
) )
236 msg_Err( p_enc
, "twolame initialization failed" );
237 return -VLC_EGENERIC
;
240 p_enc
->pf_encode_audio
= Encode
;
242 p_sys
->i_nb_samples
= 0;
247 /****************************************************************************
248 * Encode: the whole thing
249 ****************************************************************************
250 * This function spits out MPEG packets.
251 ****************************************************************************/
252 static void Bufferize( encoder_t
*p_enc
, int16_t *p_in
, int i_nb_samples
)
254 encoder_sys_t
*p_sys
= p_enc
->p_sys
;
255 const unsigned i_offset
= p_sys
->i_nb_samples
* p_enc
->fmt_in
.audio
.i_channels
;
256 const unsigned i_len
= ARRAY_SIZE(p_sys
->p_buffer
);
258 if( i_offset
>= i_len
)
260 msg_Err( p_enc
, "buffer full" );
264 unsigned i_copy
= i_nb_samples
* p_enc
->fmt_in
.audio
.i_channels
;
265 if( i_copy
+ i_offset
> i_len
)
267 msg_Err( p_enc
, "dropping samples" );
268 i_copy
= i_len
- i_offset
;
271 memcpy( p_sys
->p_buffer
+ i_offset
, p_in
, i_copy
* sizeof(int16_t) );
274 static block_t
*Encode( encoder_t
*p_enc
, block_t
*p_aout_buf
)
276 encoder_sys_t
*p_sys
= p_enc
->p_sys
;
277 block_t
*p_chain
= NULL
;
279 if( unlikely( !p_aout_buf
) ) {
283 i_used
= twolame_encode_flush( p_sys
->p_twolame
,
284 p_sys
->p_out_buffer
, MAX_CODED_FRAME_SIZE
);
288 p_block
= block_Alloc( i_used
);
291 memcpy( p_block
->p_buffer
, p_sys
->p_out_buffer
, i_used
);
292 p_block
->i_length
= vlc_tick_from_samples( MPEG_FRAME_SIZE
,
293 p_enc
->fmt_out
.audio
.i_rate
);
294 p_block
->i_dts
= p_block
->i_pts
= p_sys
->i_pts
;
295 p_sys
->i_pts
+= p_block
->i_length
;
300 int16_t *p_buffer
= (int16_t *)p_aout_buf
->p_buffer
;
301 int i_nb_samples
= p_aout_buf
->i_nb_samples
;
303 p_sys
->i_pts
= p_aout_buf
->i_pts
-
304 vlc_tick_from_samples( p_sys
->i_nb_samples
,
305 p_enc
->fmt_out
.audio
.i_rate
);
307 while ( p_sys
->i_nb_samples
+ i_nb_samples
>= MPEG_FRAME_SIZE
)
312 Bufferize( p_enc
, p_buffer
, MPEG_FRAME_SIZE
- p_sys
->i_nb_samples
);
313 i_nb_samples
-= MPEG_FRAME_SIZE
- p_sys
->i_nb_samples
;
314 p_buffer
+= (MPEG_FRAME_SIZE
- p_sys
->i_nb_samples
) * 2;
316 i_used
= twolame_encode_buffer_interleaved( p_sys
->p_twolame
,
317 p_sys
->p_buffer
, MPEG_FRAME_SIZE
,
318 p_sys
->p_out_buffer
, MAX_CODED_FRAME_SIZE
);
319 /* On error, buffer samples and return what was already encoded */
322 msg_Err( p_enc
, "encoder error: %d", i_used
);
326 p_sys
->i_nb_samples
= 0;
327 p_block
= block_Alloc( i_used
);
331 block_ChainRelease( p_chain
);
334 memcpy( p_block
->p_buffer
, p_sys
->p_out_buffer
, i_used
);
335 p_block
->i_length
= vlc_tick_from_samples( MPEG_FRAME_SIZE
,
336 p_enc
->fmt_out
.audio
.i_rate
);
337 p_block
->i_dts
= p_block
->i_pts
= p_sys
->i_pts
;
338 p_sys
->i_pts
+= p_block
->i_length
;
339 block_ChainAppend( &p_chain
, p_block
);
344 Bufferize( p_enc
, p_buffer
, i_nb_samples
);
345 p_sys
->i_nb_samples
+= i_nb_samples
;
351 /*****************************************************************************
352 * CloseEncoder: twolame encoder destruction
353 *****************************************************************************/
354 static void CloseEncoder( vlc_object_t
*p_this
)
356 encoder_t
*p_enc
= (encoder_t
*)p_this
;
357 encoder_sys_t
*p_sys
= p_enc
->p_sys
;
359 twolame_close( &p_sys
->p_twolame
);