1 /*****************************************************************************
2 * mod.c: MOD file demuxer (using libmodplug)
3 *****************************************************************************
4 * Copyright (C) 2004 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
29 #include <vlc_demux.h>
31 #include <libmodplug/modplug.h>
34 * - extend demux control to query meta data (demuxer should NEVER touch
36 * - FIXME test endian of samples
40 /*****************************************************************************
42 *****************************************************************************/
43 static int Open ( vlc_object_t * );
44 static void Close ( vlc_object_t * );
46 #define NOISE_LONGTEXT N_("Enable noise reduction algorithm.")
47 #define REVERB_LONGTEXT N_("Enable reverberation" )
48 #define REVERB_LEVEL_LONGTEXT N_( "Reverberation level (from 0 " \
49 "to 100, default value is 0)." )
50 #define REVERB_DELAY_LONGTEXT N_("Reverberation delay, in ms." \
51 " Usual values are from to 40 to 200ms." )
52 #define MEGABASS_LONGTEXT N_( "Enable megabass mode" )
53 #define MEGABASS_LEVEL_LONGTEXT N_("Megabass mode level (from 0 to 100, " \
54 "default value is 0)." )
55 #define MEGABASS_RANGE_LONGTEXT N_("Megabass mode cutoff frequency, in Hz. " \
56 "This is the maximum frequency for which the megabass " \
57 "effect applies. Valid values are from 10 to 100 Hz." )
58 #define SURROUND_LEVEL_LONGTEXT N_( "Surround effect level (from 0 to 100, " \
59 "default value is 0)." )
60 #define SURROUND_DELAY_LONGTEXT N_("Surround delay, in ms. Usual values are " \
64 set_shortname( "MOD");
65 set_description( _("MOD demuxer (libmodplug)" ) );
66 set_capability( "demux2", 10 );
67 set_category( CAT_INPUT );
68 set_subcategory( SUBCAT_INPUT_DEMUX );
70 add_bool( "mod-noisereduction", VLC_TRUE, NULL, N_("Noise reduction"),
71 NOISE_LONGTEXT, VLC_FALSE );
73 add_bool( "mod-reverb", VLC_FALSE, NULL, N_("Reverb"),
74 REVERB_LONGTEXT, VLC_FALSE );
75 add_integer_with_range( "mod-reverb-level", 0, 0, 100, NULL,
76 N_("Reverberation level"), REVERB_LEVEL_LONGTEXT, VLC_TRUE );
77 add_integer_with_range( "mod-reverb-delay", 40, 0, 1000, NULL,
78 N_("Reverberation delay"), REVERB_DELAY_LONGTEXT, VLC_TRUE );
80 add_bool( "mod-megabass", VLC_FALSE, NULL, N_("Mega bass"),
81 MEGABASS_LONGTEXT, VLC_FALSE );
82 add_integer_with_range( "mod-megabass-level", 0, 0, 100, NULL,
83 N_("Mega bass level"), MEGABASS_LEVEL_LONGTEXT, VLC_TRUE );
84 add_integer_with_range( "mod-megabass-range", 10, 10, 100, NULL,
85 N_("Mega bass cutoff"), MEGABASS_RANGE_LONGTEXT, VLC_TRUE );
87 add_bool( "mod-surround", VLC_FALSE, NULL, N_("Surround"), N_("Surround"),
89 add_integer_with_range( "mod-surround-level", 0, 0, 100, NULL,
90 N_("Surround level"), SURROUND_LEVEL_LONGTEXT, VLC_TRUE );
91 add_integer_with_range( "mod-surround-delay", 5, 0, 1000, NULL,
92 N_("Surround delay (ms)"), SURROUND_DELAY_LONGTEXT, VLC_TRUE );
94 set_callbacks( Open, Close );
95 add_shortcut( "mod" );
98 /*****************************************************************************
100 *****************************************************************************/
115 static int Demux ( demux_t *p_demux );
116 static int Control( demux_t *p_demux, int i_query, va_list args );
118 static const char* mod_ext[] =
120 "mod", "s3m", "xm", "it", "669", "amf", "ams", "dbm", "dmf", "dsm",
121 "far", "mdl", "med", "mtm", "okt", "ptm", "stm", "ult", "umx", "mt2",
125 /*****************************************************************************
127 *****************************************************************************/
128 static int Open( vlc_object_t *p_this )
130 demux_t *p_demux = (demux_t*)p_this;
134 ModPlug_Settings settings;
137 /* We accept file based on extension match */
138 if( strcasecmp( p_demux->psz_demux, "mod" ) )
140 if( ( ext = strrchr( p_demux->psz_path, '.' ) ) == NULL ||
141 stream_Size( p_demux->s ) == 0 ) return VLC_EGENERIC;
144 for( i = 0; mod_ext[i] != NULL; i++ )
146 if( !strcasecmp( ext, mod_ext[i] ) )
151 if( mod_ext[i] == NULL ) return VLC_EGENERIC;
152 msg_Dbg( p_demux, "running MOD demuxer (ext=%s)", mod_ext[i] );
155 /* Fill p_demux field */
156 p_demux->pf_demux = Demux;
157 p_demux->pf_control = Control;
158 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
160 msg_Dbg( p_demux, "loading complete file (could be long)" );
161 p_sys->i_data = stream_Size( p_demux->s );
162 p_sys->p_data = malloc( p_sys->i_data );
163 p_sys->i_data = stream_Read( p_demux->s, p_sys->p_data, p_sys->i_data );
164 if( p_sys->i_data <= 0 )
166 msg_Err( p_demux, "failed to read the complete file" );
167 free( p_sys->p_data );
171 /* Create our config variable */
172 var_Create( p_demux, "mod-noisereduction", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
173 var_Create( p_demux, "mod-reverb", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
174 var_Create( p_demux, "mod-reverb-level", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
175 var_Create( p_demux, "mod-reverb-delay", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
176 var_Create( p_demux, "mod-megabass", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
177 var_Create( p_demux, "mod-megabass-level", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
178 var_Create( p_demux, "mod-megabass-range", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
179 var_Create( p_demux, "mod-surround", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
180 var_Create( p_demux, "mod-surround-level", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
181 var_Create( p_demux, "mod-surround-delay", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
183 /* Configure modplug before loading the file */
184 ModPlug_GetSettings( &settings );
185 settings.mFlags = MODPLUG_ENABLE_OVERSAMPLING;
186 settings.mChannels = 2;
188 settings.mFrequency = 44100;
189 settings.mResamplingMode = MODPLUG_RESAMPLE_FIR;
191 var_Get( p_demux, "mod-noisereduction", &val );
192 if( val.b_bool) settings.mFlags |= MODPLUG_ENABLE_NOISE_REDUCTION;
194 var_Get( p_demux, "mod-reverb", &val );
195 if( val.b_bool) settings.mFlags |= MODPLUG_ENABLE_REVERB;
196 var_Get( p_demux, "mod-reverb-level", &val );
197 settings.mReverbDepth = val.i_int;
198 var_Get( p_demux, "mod-reverb-delay", &val );
199 settings.mReverbDelay = val.i_int;
201 var_Get( p_demux, "mod-megabass", &val );
202 if( val.b_bool) settings.mFlags |= MODPLUG_ENABLE_MEGABASS;
203 var_Get( p_demux, "mod-megabass-level", &val );
204 settings.mBassAmount = val.i_int;
205 var_Get( p_demux, "mod-megabass-range", &val );
206 settings.mBassRange = val.i_int;
208 var_Get( p_demux, "mod-surround", &val );
209 if( val.b_bool) settings.mFlags |= MODPLUG_ENABLE_SURROUND;
210 var_Get( p_demux, "mod-surround-level", &val );
211 settings.mSurroundDepth = val.i_int;
212 var_Get( p_demux, "mod-surround-delay", &val );
213 settings.mSurroundDelay = val.i_int;
215 ModPlug_SetSettings( &settings );
217 if( ( p_sys->f = ModPlug_Load( p_sys->p_data, p_sys->i_data ) ) == NULL )
219 msg_Err( p_demux, "failed to understand the file" );
220 /* we try to seek to recover for other plugin */
221 stream_Seek( p_demux->s, 0 );
222 free( p_sys->p_data );
229 p_sys->i_length = ModPlug_GetLength( p_sys->f ) * (int64_t)1000;
231 msg_Dbg( p_demux, "MOD loaded name=%s lenght="I64Fd"ms",
232 ModPlug_GetName( p_sys->f ),
235 #ifdef WORDS_BIGENDIAN
236 es_format_Init( &p_sys->fmt, AUDIO_ES, VLC_FOURCC( 't', 'w', 'o', 's' ) );
238 es_format_Init( &p_sys->fmt, AUDIO_ES, VLC_FOURCC( 'a', 'r', 'a', 'w' ) );
240 p_sys->fmt.audio.i_rate = settings.mFrequency;
241 p_sys->fmt.audio.i_channels = settings.mChannels;
242 p_sys->fmt.audio.i_bitspersample = settings.mBits;
243 p_sys->es = es_out_Add( p_demux->out, &p_sys->fmt );
248 /*****************************************************************************
250 *****************************************************************************/
251 static void Close( vlc_object_t *p_this )
253 demux_t *p_demux = (demux_t*)p_this;
254 demux_sys_t *p_sys = p_demux->p_sys;
256 ModPlug_Unload( p_sys->f );
257 free( p_sys->p_data );
262 /*****************************************************************************
264 *****************************************************************************/
265 static int Demux( demux_t *p_demux )
267 demux_sys_t *p_sys = p_demux->p_sys;
269 int i_bk = ( p_sys->fmt.audio.i_bitspersample / 8 ) *
270 p_sys->fmt.audio.i_channels;
272 p_frame = block_New( p_demux, p_sys->fmt.audio.i_rate / 10 * i_bk );
274 p_frame->i_buffer = ModPlug_Read( p_sys->f, p_frame->p_buffer, p_frame->i_buffer );
275 if( p_frame->i_buffer <= 0 )
278 block_Release( p_frame );
283 es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_sys->i_time );
285 /* We should use p_frame->i_buffer */
286 p_sys->i_time += (int64_t)1000000 * p_frame->i_buffer / i_bk / p_sys->fmt.audio.i_rate;
289 p_frame->i_dts = p_frame->i_pts = p_sys->i_time;
290 es_out_Send( p_demux->out, p_sys->es, p_frame );
295 /*****************************************************************************
297 *****************************************************************************/
298 static int Control( demux_t *p_demux, int i_query, va_list args )
300 demux_sys_t *p_sys = p_demux->p_sys;
306 case DEMUX_GET_POSITION:
307 pf = (double*) va_arg( args, double* );
308 if( p_sys->i_length > 0 )
310 *pf = (double)p_sys->i_time / (double)p_sys->i_length;
315 case DEMUX_SET_POSITION:
316 f = (double) va_arg( args, double );
318 i64 = f * p_sys->i_length;
319 if( i64 >= 0 && i64 <= p_sys->i_length )
321 ModPlug_Seek( p_sys->f, i64 / 1000 );
322 p_sys->i_time = i64 + 1;
323 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
330 pi64 = (int64_t*)va_arg( args, int64_t * );
331 *pi64 = p_sys->i_time;
334 case DEMUX_GET_LENGTH:
335 pi64 = (int64_t*)va_arg( args, int64_t * );
336 *pi64 = p_sys->i_length;
340 i64 = (int64_t)va_arg( args, int64_t );
342 if( i64 >= 0 && i64 <= p_sys->i_length )
344 ModPlug_Seek( p_sys->f, i64 / 1000 );
345 p_sys->i_time = i64 + 1;
346 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
352 case DEMUX_GET_FPS: /* meaningless */