1 /*****************************************************************************
2 * mod.c: MOD file demuxer (using libmodplug)
3 *****************************************************************************
4 * Copyright (C) 2004-2009 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 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_demux.h>
37 #include <libmodplug/modplug.h>
40 * - extend demux control to query meta data (demuxer should NEVER touch
42 * - FIXME test endian of samples
46 /*****************************************************************************
48 *****************************************************************************/
49 static int Open ( vlc_object_t * );
50 static void Close ( vlc_object_t * );
52 #define NOISE_LONGTEXT N_("Enable noise reduction algorithm.")
53 #define REVERB_LONGTEXT N_("Enable reverberation" )
54 #define REVERB_LEVEL_LONGTEXT N_( "Reverberation level (from 0 " \
55 "to 100, default value is 0)." )
56 #define REVERB_DELAY_LONGTEXT N_("Reverberation delay, in ms." \
57 " Usual values are from to 40 to 200ms." )
58 #define MEGABASS_LONGTEXT N_( "Enable megabass mode" )
59 #define MEGABASS_LEVEL_LONGTEXT N_("Megabass mode level (from 0 to 100, " \
60 "default value is 0)." )
61 #define MEGABASS_RANGE_LONGTEXT N_("Megabass mode cutoff frequency, in Hz. " \
62 "This is the maximum frequency for which the megabass " \
63 "effect applies. Valid values are from 10 to 100 Hz." )
64 #define SURROUND_LEVEL_LONGTEXT N_( "Surround effect level (from 0 to 100, " \
65 "default value is 0)." )
66 #define SURROUND_DELAY_LONGTEXT N_("Surround delay, in ms. Usual values are " \
71 set_description( N_("MOD demuxer (libmodplug)" ) )
72 set_capability( "demux", 10 )
73 set_category( CAT_INPUT )
74 set_subcategory( SUBCAT_INPUT_DEMUX )
76 add_bool( "mod-noisereduction", true, NULL, N_("Noise reduction"),
77 NOISE_LONGTEXT, false )
79 add_bool( "mod-reverb", false, NULL, N_("Reverb"),
80 REVERB_LONGTEXT, false )
81 add_integer_with_range( "mod-reverb-level", 0, 0, 100, NULL,
82 N_("Reverberation level"), REVERB_LEVEL_LONGTEXT, true )
83 add_integer_with_range( "mod-reverb-delay", 40, 0, 1000, NULL,
84 N_("Reverberation delay"), REVERB_DELAY_LONGTEXT, true )
86 add_bool( "mod-megabass", false, NULL, N_("Mega bass"),
87 MEGABASS_LONGTEXT, false )
88 add_integer_with_range( "mod-megabass-level", 0, 0, 100, NULL,
89 N_("Mega bass level"), MEGABASS_LEVEL_LONGTEXT, true )
90 add_integer_with_range( "mod-megabass-range", 10, 10, 100, NULL,
91 N_("Mega bass cutoff"), MEGABASS_RANGE_LONGTEXT, true )
93 add_bool( "mod-surround", false, NULL, N_("Surround"), N_("Surround"),
95 add_integer_with_range( "mod-surround-level", 0, 0, 100, NULL,
96 N_("Surround level"), SURROUND_LEVEL_LONGTEXT, true )
97 add_integer_with_range( "mod-surround-delay", 5, 0, 1000, NULL,
98 N_("Surround delay (ms)"), SURROUND_DELAY_LONGTEXT, true )
100 set_callbacks( Open, Close )
101 add_shortcut( "mod" )
104 /*****************************************************************************
106 *****************************************************************************/
121 static int Demux ( demux_t *p_demux );
122 static int Control( demux_t *p_demux, int i_query, va_list args );
124 static const char *ppsz_mod_ext[] =
126 "mod", "s3m", "xm", "it", "669", "amf", "ams", "dbm", "dmf", "dsm",
127 "far", "mdl", "med", "mtm", "okt", "ptm", "stm", "ult", "umx", "mt2",
131 /* We load the complete file in memory, put a higher bound
132 * of 500 Mo (which is really big anyway) */
133 #define MOD_MAX_FILE_SIZE (500*1000*1000)
135 /*****************************************************************************
137 *****************************************************************************/
138 static int Open( vlc_object_t *p_this )
140 demux_t *p_demux = (demux_t*)p_this;
142 ModPlug_Settings settings;
144 /* We accept file based on extension match */
145 if( !p_demux->b_force )
147 const char *psz_ext = strrchr( p_demux->psz_path, '.' );
153 psz_ext++; /* skip . */
154 for( i = 0; ppsz_mod_ext[i] != NULL; i++ )
156 if( !strcasecmp( psz_ext, ppsz_mod_ext[i] ) )
159 if( ppsz_mod_ext[i] == NULL )
161 msg_Dbg( p_demux, "running MOD demuxer (ext=%s)", ppsz_mod_ext[i] );
164 const int64_t i_size = stream_Size( p_demux->s );
165 if( i_size <= 0 || i_size >= MOD_MAX_FILE_SIZE )
168 /* Fill p_demux field */
169 p_demux->pf_demux = Demux;
170 p_demux->pf_control = Control;
171 p_demux->p_sys = p_sys = malloc( sizeof( *p_sys ) );
175 msg_Dbg( p_demux, "loading complete file (could be long)" );
176 p_sys->i_data = i_size;
177 p_sys->p_data = malloc( p_sys->i_data );
179 p_sys->i_data = stream_Read( p_demux->s, p_sys->p_data, p_sys->i_data );
180 if( p_sys->i_data <= 0 || !p_sys->p_data )
182 msg_Err( p_demux, "failed to read the complete file" );
183 free( p_sys->p_data );
188 /* Configure modplug before loading the file */
189 ModPlug_GetSettings( &settings );
190 settings.mFlags = MODPLUG_ENABLE_OVERSAMPLING;
191 settings.mChannels = 2;
193 settings.mFrequency = 44100;
194 settings.mResamplingMode = MODPLUG_RESAMPLE_FIR;
196 if( var_CreateGetBool( p_demux, "mod-noisereduction" ) )
197 settings.mFlags |= MODPLUG_ENABLE_NOISE_REDUCTION;
199 if( var_CreateGetBool( p_demux, "mod-reverb" ) )
200 settings.mFlags |= MODPLUG_ENABLE_REVERB;
201 settings.mReverbDepth = var_CreateGetInteger( p_demux, "mod-reverb-level" );
202 settings.mReverbDelay = var_CreateGetInteger( p_demux, "mod-reverb-delay" );
204 if( var_CreateGetBool( p_demux, "mod-megabass" ) )
205 settings.mFlags |= MODPLUG_ENABLE_MEGABASS;
206 settings.mBassAmount = var_CreateGetInteger( p_demux, "mod-megabass-level" );
207 settings.mBassRange = var_CreateGetInteger( p_demux, "mod-megabass-range" );
209 if( var_CreateGetBool( p_demux, "mod-surround" ) )
210 settings.mFlags |= MODPLUG_ENABLE_SURROUND;
211 settings.mSurroundDepth = var_CreateGetInteger( p_demux, "mod-surround-level" );
212 settings.mSurroundDelay = var_CreateGetInteger( p_demux, "mod-surround-delay" );
214 ModPlug_SetSettings( &settings );
216 if( ( p_sys->f = ModPlug_Load( p_sys->p_data, p_sys->i_data ) ) == NULL )
218 msg_Err( p_demux, "failed to understand the file" );
219 /* we try to seek to recover for other plugin */
220 stream_Seek( p_demux->s, 0 );
221 free( p_sys->p_data );
228 p_sys->i_length = ModPlug_GetLength( p_sys->f ) * INT64_C(1000);
230 msg_Dbg( p_demux, "MOD loaded name=%s lenght=%"PRId64"ms",
231 ModPlug_GetName( p_sys->f ),
234 #ifdef WORDS_BIGENDIAN
235 es_format_Init( &p_sys->fmt, AUDIO_ES, VLC_FOURCC( 't', 'w', 'o', 's' ) );
237 es_format_Init( &p_sys->fmt, AUDIO_ES, VLC_FOURCC( 'a', 'r', 'a', 'w' ) );
239 p_sys->fmt.audio.i_rate = settings.mFrequency;
240 p_sys->fmt.audio.i_channels = settings.mChannels;
241 p_sys->fmt.audio.i_bitspersample = settings.mBits;
242 p_sys->es = es_out_Add( p_demux->out, &p_sys->fmt );
247 /*****************************************************************************
249 *****************************************************************************/
250 static void Close( vlc_object_t *p_this )
252 demux_t *p_demux = (demux_t*)p_this;
253 demux_sys_t *p_sys = p_demux->p_sys;
255 ModPlug_Unload( p_sys->f );
256 free( p_sys->p_data );
261 /*****************************************************************************
263 *****************************************************************************/
264 static int Demux( demux_t *p_demux )
266 demux_sys_t *p_sys = p_demux->p_sys;
268 int i_bk = ( p_sys->fmt.audio.i_bitspersample / 8 ) *
269 p_sys->fmt.audio.i_channels;
272 p_frame = block_New( p_demux, p_sys->fmt.audio.i_rate / 10 * i_bk );
276 i_read = ModPlug_Read( p_sys->f, p_frame->p_buffer, p_frame->i_buffer );
280 block_Release( p_frame );
283 p_frame->i_buffer = i_read;
286 es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_sys->i_time );
288 /* We should use p_frame->i_buffer */
289 p_sys->i_time += INT64_C(1000000) * p_frame->i_buffer / i_bk / p_sys->fmt.audio.i_rate;
292 p_frame->i_dts = p_frame->i_pts = p_sys->i_time;
293 es_out_Send( p_demux->out, p_sys->es, p_frame );
298 /*****************************************************************************
300 *****************************************************************************/
301 static int Control( demux_t *p_demux, int i_query, va_list args )
303 demux_sys_t *p_sys = p_demux->p_sys;
309 case DEMUX_GET_POSITION:
310 pf = (double*) va_arg( args, double* );
311 if( p_sys->i_length > 0 )
313 *pf = (double)p_sys->i_time / (double)p_sys->i_length;
318 case DEMUX_SET_POSITION:
319 f = (double) va_arg( args, double );
321 i64 = f * p_sys->i_length;
322 if( i64 >= 0 && i64 <= p_sys->i_length )
324 ModPlug_Seek( p_sys->f, i64 / 1000 );
325 p_sys->i_time = i64 + 1;
332 pi64 = (int64_t*)va_arg( args, int64_t * );
333 *pi64 = p_sys->i_time;
336 case DEMUX_GET_LENGTH:
337 pi64 = (int64_t*)va_arg( args, int64_t * );
338 *pi64 = p_sys->i_length;
342 i64 = (int64_t)va_arg( args, int64_t );
344 if( i64 >= 0 && i64 <= p_sys->i_length )
346 ModPlug_Seek( p_sys->f, i64 / 1000 );
347 p_sys->i_time = i64 + 1;
353 case DEMUX_HAS_UNSUPPORTED_META:
355 bool *pb_bool = (bool*)va_arg( args, bool* );
356 *pb_bool = false; /* FIXME I am not sure of this one */
361 vlc_meta_t *p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t* );
362 const char *psz_name = ModPlug_GetName( p_sys->f );
363 if( psz_name && *psz_name )
364 vlc_meta_SetTitle( p_meta, psz_name );
368 case DEMUX_GET_FPS: /* meaningless */