1 /*****************************************************************************
2 * mod.c: MOD file demuxer (using libmodplug)
3 *****************************************************************************
4 * Copyright (C) 2004 VideoLAN
5 * $Id: mod.c,v 1.2 2004/01/25 20:05:28 hartman Exp $
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
30 #include <vlc/input.h>
32 #include <libmodplug/modplug.h>
35 * - extend demux control to query meta data (demuxer should NEVER touch
37 * - FIXME test endian of samples
41 /*****************************************************************************
43 *****************************************************************************/
44 static int Open ( vlc_object_t * );
45 static void Close ( vlc_object_t * );
48 set_description( _("MOD demuxer (libmodplug)" ) );
49 set_capability( "demux2", 10 );
51 add_bool( "mod-noisereduction", VLC_TRUE, NULL, N_("Noise reduction"), N_("Noise reduction"), VLC_FALSE );
53 add_bool( "mod-reverb", VLC_FALSE, NULL, N_("Reverb"), N_("Reverb"), VLC_FALSE );
54 add_integer_with_range( "mod-reverb-level", 0, 0, 100, NULL, N_("Reverb level (0-100)"), N_("Reverb level (0-100 defaults to 0)"), VLC_FALSE );
55 add_integer_with_range( "mod-reverb-delay", 40, 0, 1000, NULL, N_("Reverb delay (ms)"), N_("Reverb delay in ms (usually 40-200ms)"), VLC_FALSE );
57 add_bool( "mod-megabass", VLC_FALSE, NULL, N_("Mega bass"), N_("Mega bass"), VLC_FALSE );
58 add_integer_with_range( "mod-megabass-level", 0, 0, 100, NULL, N_("Mega bass level (0-100)"), N_("Mega bass level (0-100 defaults to 0)"), VLC_FALSE );
59 add_integer_with_range( "mod-megabass-range", 10, 10, 100, NULL, N_("Mega bass cutt off (Hz)"), N_("Mega bass cutt off (10-100Hz)"), VLC_FALSE );
61 add_bool( "mod-surround", VLC_FALSE, NULL, N_("Surround"), N_("Surround"), VLC_FALSE );
62 add_integer_with_range( "mod-surround-level", 0, 0, 100, NULL, N_("Surround level (0-100)"), N_("Surround level (0-100 defaults to 0)"), VLC_FALSE );
63 add_integer_with_range( "mod-surround-delay", 5, 0, 1000, NULL, N_("Surround delay (ms)"), N_("Surround delay in ms (usually 5-40ms)"), VLC_FALSE );
65 set_callbacks( Open, Close );
66 add_shortcut( "mod" );
69 /*****************************************************************************
71 *****************************************************************************/
86 static int Demux ( demux_t *p_demux );
87 static int Control( demux_t *p_demux, int i_query, va_list args );
89 static const char* mod_ext[] =
91 "mod", "s3m", "xm", "it", "669", "amf", "ams", "dbm", "dmf", "dsm",
92 "far", "mdl", "med", "mtm", "okt", "ptm", "stm", "ult", "umx", "mt2",
96 /*****************************************************************************
98 *****************************************************************************/
99 static int Open( vlc_object_t *p_this )
101 demux_t *p_demux = (demux_t*)p_this;
105 ModPlug_Settings settings;
108 /* We accept file based on extention match */
109 if( strcasecmp( p_demux->psz_demux, "mod" ) )
111 if( ( ext = strchr( p_demux->psz_path, '.' ) ) == NULL || stream_Size( p_demux->s ) == 0 )
113 msg_Warn( p_demux, "MOD module discarded (path=%s)", p_demux->psz_path );
117 for( i = 0; mod_ext[i] != NULL; i++ )
119 if( !strcasecmp( ext, mod_ext[i] ) )
124 if( mod_ext[i] == NULL )
126 msg_Warn( p_demux, "MOD module discarded (extention '%s' unknown)", ext );
129 msg_Dbg( p_demux, "running MOD demuxer (ext=%s)", mod_ext[i] );
132 /* Fill p_demux field */
133 p_demux->pf_demux = Demux;
134 p_demux->pf_control = Control;
135 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
137 msg_Dbg( p_demux, "loading complete file (could be long)" );
138 p_sys->i_data = stream_Size( p_demux->s );
139 p_sys->p_data = malloc( p_sys->i_data );
140 p_sys->i_data = stream_Read( p_demux->s, p_sys->p_data, p_sys->i_data );
141 if( p_sys->i_data <= 0 )
143 msg_Err( p_demux, "failed to read the complete file" );
144 free( p_sys->p_data );
148 /* Create our config variable */
149 var_Create( p_demux, "mod-noisereduction", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
150 var_Create( p_demux, "mod-reverb", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
151 var_Create( p_demux, "mod-reverb-level", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
152 var_Create( p_demux, "mod-reverb-delay", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
153 var_Create( p_demux, "mod-megabass", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
154 var_Create( p_demux, "mod-megabass-level", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
155 var_Create( p_demux, "mod-megabass-range", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
156 var_Create( p_demux, "mod-surround", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
157 var_Create( p_demux, "mod-surround-level", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
158 var_Create( p_demux, "mod-surround-delay", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
160 /* Configure modplug before loading the file */
161 ModPlug_GetSettings( &settings );
162 settings.mFlags = MODPLUG_ENABLE_OVERSAMPLING;
163 settings.mChannels = 2;
165 settings.mFrequency = 44100;
166 settings.mResamplingMode = MODPLUG_RESAMPLE_FIR;
168 var_Get( p_demux, "mod-noisereduction", &val );
169 if( val.b_bool) settings.mFlags |= MODPLUG_ENABLE_NOISE_REDUCTION;
171 var_Get( p_demux, "mod-reverb", &val );
172 if( val.b_bool) settings.mFlags |= MODPLUG_ENABLE_REVERB;
173 var_Get( p_demux, "mod-reverb-level", &val );
174 settings.mReverbDepth = val.i_int;
175 var_Get( p_demux, "mod-reverb-delay", &val );
176 settings.mReverbDelay = val.i_int;
178 var_Get( p_demux, "mod-megabass", &val );
179 if( val.b_bool) settings.mFlags |= MODPLUG_ENABLE_MEGABASS;
180 var_Get( p_demux, "mod-megabass-level", &val );
181 settings.mBassAmount = val.i_int;
182 var_Get( p_demux, "mod-megabass-range", &val );
183 settings.mBassRange = val.i_int;
185 var_Get( p_demux, "mod-surround", &val );
186 if( val.b_bool) settings.mFlags |= MODPLUG_ENABLE_SURROUND;
187 var_Get( p_demux, "mod-surround-level", &val );
188 settings.mSurroundDepth = val.i_int;
189 var_Get( p_demux, "mod-surround-delay", &val );
190 settings.mSurroundDelay = val.i_int;
192 ModPlug_SetSettings( &settings );
194 if( ( p_sys->f = ModPlug_Load( p_sys->p_data, p_sys->i_data ) ) == NULL )
196 msg_Err( p_demux, "failed to understand the file" );
197 /* we try to seek to recover for other plugin */
198 stream_Seek( p_demux->s, 0 );
199 free( p_sys->p_data );
206 p_sys->i_length = ModPlug_GetLength( p_sys->f ) * (int64_t)1000;
208 msg_Dbg( p_demux, "MOD loaded name=%s lenght="I64Fd"ms",
209 ModPlug_GetName( p_sys->f ),
212 #ifdef WORDS_BIGENDIAN
213 es_format_Init( &p_sys->fmt, AUDIO_ES, VLC_FOURCC( 't', 'w', 'o', 's' ) );
215 es_format_Init( &p_sys->fmt, AUDIO_ES, VLC_FOURCC( 'a', 'r', 'a', 'w' ) );
217 p_sys->fmt.audio.i_rate = settings.mFrequency;
218 p_sys->fmt.audio.i_channels = settings.mChannels;
219 p_sys->fmt.audio.i_bitspersample = settings.mBits;
220 p_sys->es = es_out_Add( p_demux->out, &p_sys->fmt );
225 /*****************************************************************************
227 *****************************************************************************/
228 static void Close( vlc_object_t *p_this )
230 demux_t *p_demux = (demux_t*)p_this;
231 demux_sys_t *p_sys = p_demux->p_sys;
233 ModPlug_Unload( p_sys->f );
234 free( p_sys->p_data );
239 /*****************************************************************************
241 *****************************************************************************/
242 static int Demux( demux_t *p_demux )
244 demux_sys_t *p_sys = p_demux->p_sys;
246 int i_bk = ( p_sys->fmt.audio.i_bitspersample / 8 ) *
247 p_sys->fmt.audio.i_channels;
249 p_frame = block_New( p_demux, p_sys->fmt.audio.i_rate / 10 * i_bk );
251 p_frame->i_buffer = ModPlug_Read( p_sys->f, p_frame->p_buffer, p_frame->i_buffer );
252 if( p_frame->i_buffer <= 0 )
255 block_Release( p_frame );
260 es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_sys->i_time );
262 /* We should use p_frame->i_buffer */
263 p_sys->i_time += (int64_t)1000000 * p_frame->i_buffer / i_bk / p_sys->fmt.audio.i_rate;
266 p_frame->i_dts = p_frame->i_pts = p_sys->i_time;
267 es_out_Send( p_demux->out, p_sys->es, p_frame );
272 /*****************************************************************************
274 *****************************************************************************/
275 static int Control( demux_t *p_demux, int i_query, va_list args )
277 demux_sys_t *p_sys = p_demux->p_sys;
283 case DEMUX_GET_POSITION:
284 pf = (double*) va_arg( args, double* );
285 if( p_sys->i_length > 0 )
287 *pf = (double)p_sys->i_time / (double)p_sys->i_length;
292 case DEMUX_SET_POSITION:
293 f = (double) va_arg( args, double );
295 i64 = f * p_sys->i_length;
296 if( i64 >= 0 && i64 <= p_sys->i_length )
298 ModPlug_Seek( p_sys->f, i64 / 1000 );
299 p_sys->i_time = i64 + 1;
300 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
307 pi64 = (int64_t*)va_arg( args, int64_t * );
308 *pi64 = p_sys->i_time;
311 case DEMUX_GET_LENGTH:
312 pi64 = (int64_t*)va_arg( args, int64_t * );
313 *pi64 = p_sys->i_length;
317 i64 = (int64_t)va_arg( args, int64_t );
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 );
329 case DEMUX_GET_FPS: /* meaningless */