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>
38 #include <libmodplug/modplug.h>
41 * - extend demux control to query meta data (demuxer should NEVER touch
43 * - FIXME test endian of samples
47 /*****************************************************************************
49 *****************************************************************************/
50 static int Open ( vlc_object_t * );
51 static void Close ( vlc_object_t * );
53 #define NOISE_LONGTEXT N_("Enable noise reduction algorithm.")
54 #define REVERB_LONGTEXT N_("Enable reverberation" )
55 #define REVERB_LEVEL_LONGTEXT N_( "Reverberation level (from 0 " \
56 "to 100, default value is 0)." )
57 #define REVERB_DELAY_LONGTEXT N_("Reverberation delay, in ms." \
58 " Usual values are from to 40 to 200ms." )
59 #define MEGABASS_LONGTEXT N_( "Enable megabass mode" )
60 #define MEGABASS_LEVEL_LONGTEXT N_("Megabass mode level (from 0 to 100, " \
61 "default value is 0)." )
62 #define MEGABASS_RANGE_LONGTEXT N_("Megabass mode cutoff frequency, in Hz. " \
63 "This is the maximum frequency for which the megabass " \
64 "effect applies. Valid values are from 10 to 100 Hz." )
65 #define SURROUND_LEVEL_LONGTEXT N_( "Surround effect level (from 0 to 100, " \
66 "default value is 0)." )
67 #define SURROUND_DELAY_LONGTEXT N_("Surround delay, in ms. Usual values are " \
72 set_description( N_("MOD demuxer (libmodplug)" ) )
73 set_capability( "demux", 10 )
74 set_category( CAT_INPUT )
75 set_subcategory( SUBCAT_INPUT_DEMUX )
77 add_bool( "mod-noisereduction", true, NULL, N_("Noise reduction"),
78 NOISE_LONGTEXT, false )
80 add_bool( "mod-reverb", false, NULL, N_("Reverb"),
81 REVERB_LONGTEXT, false )
82 add_integer_with_range( "mod-reverb-level", 0, 0, 100, NULL,
83 N_("Reverberation level"), REVERB_LEVEL_LONGTEXT, true )
84 add_integer_with_range( "mod-reverb-delay", 40, 0, 1000, NULL,
85 N_("Reverberation delay"), REVERB_DELAY_LONGTEXT, true )
87 add_bool( "mod-megabass", false, NULL, N_("Mega bass"),
88 MEGABASS_LONGTEXT, false )
89 add_integer_with_range( "mod-megabass-level", 0, 0, 100, NULL,
90 N_("Mega bass level"), MEGABASS_LEVEL_LONGTEXT, true )
91 add_integer_with_range( "mod-megabass-range", 10, 10, 100, NULL,
92 N_("Mega bass cutoff"), MEGABASS_RANGE_LONGTEXT, true )
94 add_bool( "mod-surround", false, NULL, N_("Surround"), N_("Surround"),
96 add_integer_with_range( "mod-surround-level", 0, 0, 100, NULL,
97 N_("Surround level"), SURROUND_LEVEL_LONGTEXT, true )
98 add_integer_with_range( "mod-surround-delay", 5, 0, 1000, NULL,
99 N_("Surround delay (ms)"), SURROUND_DELAY_LONGTEXT, true )
101 set_callbacks( Open, Close )
102 add_shortcut( "mod" )
105 /*****************************************************************************
107 *****************************************************************************/
122 static int Demux ( demux_t *p_demux );
123 static int Control( demux_t *p_demux, int i_query, va_list args );
125 static int Validate( demux_t *p_demux, const char *psz_ext );
127 static const char *ppsz_mod_ext[] =
129 "mod", "s3m", "xm", "it", "669", "amf", "ams", "dbm", "dmf", "dsm",
130 "far", "mdl", "med", "mtm", "okt", "ptm", "stm", "ult", "umx", "mt2",
135 /* We load the complete file in memory, put a higher bound
136 * of 500 Mo (which is really big anyway) */
137 #define MOD_MAX_FILE_SIZE (500*1000*1000)
139 /*****************************************************************************
141 *****************************************************************************/
142 static int Open( vlc_object_t *p_this )
144 demux_t *p_demux = (demux_t*)p_this;
146 ModPlug_Settings settings;
148 /* We accept file based on extension match */
149 if( !p_demux->b_force )
151 const char *psz_ext = strrchr( p_demux->psz_path, '.' );
157 psz_ext++; /* skip . */
158 for( i = 0; ppsz_mod_ext[i] != NULL; i++ )
160 if( !strcasecmp( psz_ext, ppsz_mod_ext[i] ) )
163 if( ppsz_mod_ext[i] == NULL )
165 if( Validate( p_demux, ppsz_mod_ext[i] ) )
167 msg_Warn( p_demux, "MOD validation failed (ext=%s)", ppsz_mod_ext[i]);
170 msg_Dbg( p_demux, "running MOD demuxer (ext=%s)", ppsz_mod_ext[i] );
173 const int64_t i_size = stream_Size( p_demux->s );
174 if( i_size <= 0 || i_size >= MOD_MAX_FILE_SIZE )
177 /* Fill p_demux field */
178 p_demux->pf_demux = Demux;
179 p_demux->pf_control = Control;
180 p_demux->p_sys = p_sys = malloc( sizeof( *p_sys ) );
184 msg_Dbg( p_demux, "loading complete file (could be long)" );
185 p_sys->i_data = i_size;
186 p_sys->p_data = malloc( p_sys->i_data );
188 p_sys->i_data = stream_Read( p_demux->s, p_sys->p_data, p_sys->i_data );
189 if( p_sys->i_data <= 0 || !p_sys->p_data )
191 msg_Err( p_demux, "failed to read the complete file" );
192 free( p_sys->p_data );
197 /* Configure modplug before loading the file */
198 ModPlug_GetSettings( &settings );
199 settings.mFlags = MODPLUG_ENABLE_OVERSAMPLING;
200 settings.mChannels = 2;
202 settings.mFrequency = 44100;
203 settings.mResamplingMode = MODPLUG_RESAMPLE_FIR;
205 if( var_CreateGetBool( p_demux, "mod-noisereduction" ) )
206 settings.mFlags |= MODPLUG_ENABLE_NOISE_REDUCTION;
208 if( var_CreateGetBool( p_demux, "mod-reverb" ) )
209 settings.mFlags |= MODPLUG_ENABLE_REVERB;
210 settings.mReverbDepth = var_CreateGetInteger( p_demux, "mod-reverb-level" );
211 settings.mReverbDelay = var_CreateGetInteger( p_demux, "mod-reverb-delay" );
213 if( var_CreateGetBool( p_demux, "mod-megabass" ) )
214 settings.mFlags |= MODPLUG_ENABLE_MEGABASS;
215 settings.mBassAmount = var_CreateGetInteger( p_demux, "mod-megabass-level" );
216 settings.mBassRange = var_CreateGetInteger( p_demux, "mod-megabass-range" );
218 if( var_CreateGetBool( p_demux, "mod-surround" ) )
219 settings.mFlags |= MODPLUG_ENABLE_SURROUND;
220 settings.mSurroundDepth = var_CreateGetInteger( p_demux, "mod-surround-level" );
221 settings.mSurroundDelay = var_CreateGetInteger( p_demux, "mod-surround-delay" );
223 ModPlug_SetSettings( &settings );
225 if( ( p_sys->f = ModPlug_Load( p_sys->p_data, p_sys->i_data ) ) == NULL )
227 msg_Err( p_demux, "failed to understand the file" );
228 /* we try to seek to recover for other plugin */
229 stream_Seek( p_demux->s, 0 );
230 free( p_sys->p_data );
237 p_sys->i_length = ModPlug_GetLength( p_sys->f ) * INT64_C(1000);
239 msg_Dbg( p_demux, "MOD loaded name=%s lenght=%"PRId64"ms",
240 ModPlug_GetName( p_sys->f ),
243 #ifdef WORDS_BIGENDIAN
244 es_format_Init( &p_sys->fmt, AUDIO_ES, VLC_FOURCC( 't', 'w', 'o', 's' ) );
246 es_format_Init( &p_sys->fmt, AUDIO_ES, VLC_FOURCC( 'a', 'r', 'a', 'w' ) );
248 p_sys->fmt.audio.i_rate = settings.mFrequency;
249 p_sys->fmt.audio.i_channels = settings.mChannels;
250 p_sys->fmt.audio.i_bitspersample = settings.mBits;
251 p_sys->es = es_out_Add( p_demux->out, &p_sys->fmt );
256 /*****************************************************************************
258 *****************************************************************************/
259 static void Close( vlc_object_t *p_this )
261 demux_t *p_demux = (demux_t*)p_this;
262 demux_sys_t *p_sys = p_demux->p_sys;
264 ModPlug_Unload( p_sys->f );
265 free( p_sys->p_data );
270 /*****************************************************************************
272 *****************************************************************************/
273 static int Demux( demux_t *p_demux )
275 demux_sys_t *p_sys = p_demux->p_sys;
277 int i_bk = ( p_sys->fmt.audio.i_bitspersample / 8 ) *
278 p_sys->fmt.audio.i_channels;
281 p_frame = block_New( p_demux, p_sys->fmt.audio.i_rate / 10 * i_bk );
285 i_read = ModPlug_Read( p_sys->f, p_frame->p_buffer, p_frame->i_buffer );
289 block_Release( p_frame );
292 p_frame->i_buffer = i_read;
295 es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_sys->i_time );
297 /* We should use p_frame->i_buffer */
298 p_sys->i_time += INT64_C(1000000) * p_frame->i_buffer / i_bk / p_sys->fmt.audio.i_rate;
301 p_frame->i_dts = p_frame->i_pts = p_sys->i_time;
302 es_out_Send( p_demux->out, p_sys->es, p_frame );
307 /*****************************************************************************
309 *****************************************************************************/
310 static int Control( demux_t *p_demux, int i_query, va_list args )
312 demux_sys_t *p_sys = p_demux->p_sys;
318 case DEMUX_GET_POSITION:
319 pf = (double*) va_arg( args, double* );
320 if( p_sys->i_length > 0 )
322 *pf = (double)p_sys->i_time / (double)p_sys->i_length;
327 case DEMUX_SET_POSITION:
328 f = (double) va_arg( args, double );
330 i64 = f * p_sys->i_length;
331 if( i64 >= 0 && i64 <= p_sys->i_length )
333 ModPlug_Seek( p_sys->f, i64 / 1000 );
334 p_sys->i_time = i64 + 1;
341 pi64 = (int64_t*)va_arg( args, int64_t * );
342 *pi64 = p_sys->i_time;
345 case DEMUX_GET_LENGTH:
346 pi64 = (int64_t*)va_arg( args, int64_t * );
347 *pi64 = p_sys->i_length;
351 i64 = (int64_t)va_arg( args, int64_t );
353 if( i64 >= 0 && i64 <= p_sys->i_length )
355 ModPlug_Seek( p_sys->f, i64 / 1000 );
356 p_sys->i_time = i64 + 1;
362 case DEMUX_HAS_UNSUPPORTED_META:
364 bool *pb_bool = (bool*)va_arg( args, bool* );
365 *pb_bool = false; /* FIXME I am not sure of this one */
370 vlc_meta_t *p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t* );
371 const char *psz_name = ModPlug_GetName( p_sys->f );
372 if( psz_name && *psz_name )
373 vlc_meta_SetTitle( p_meta, psz_name );
377 case DEMUX_GET_FPS: /* meaningless */
383 /*****************************************************************************
384 * Validate: try to ensure it is really a mod file.
385 * The tests are not robust enough to replace extension checks in the general
387 * TODO: maybe it should return a score, which will be used to bypass the
388 * extension checks when high enough.
389 *****************************************************************************/
390 static int Validate( demux_t *p_demux, const char *psz_ext )
395 const char *psz_marker;
397 { 0, "ziRCONia" }, /* MMCMP files */
398 { 0, "Extended Module" }, /* XM */
399 { 44, "SCRM" }, /* S3M */
400 { 0, "IMPM" }, /* IT */
401 { 0, "MThd" }, /* MID */
402 { 0, "GF1PATCH110" }, /* PAT */
403 { 20, "!SCREAM!" }, /* STM */
404 { 20, "!Scream!" }, /* STM */
405 { 20, "BMOD2STM" }, /* STM */
406 { 0, "MMD0" }, /* MED */
407 { 0, "MMD1" }, /* MED */
408 { 0, "MTM" }, /* MTM */
409 { 0, "DMDL" }, /* MDL */
410 { 0, "DBM0" }, /* DBM */
411 { 0, "if" }, /* 669 */
412 { 0, "JN" }, /* 669 */
413 { 0, "FAR\xfe" }, /* FAR */
414 { 0, "Extreme" }, /* AMS */
415 { 0, "OKTASONGCMOD" }, /* OKT */
416 { 44, "PTMF" }, /* PTM */
417 { 0, "MAS_UTrack_V00" }, /* Ult */
418 { 0, "DDMF" }, /* DMF */
419 { 8, "DSMFSONG" }, /* DSM */
420 { 0, "\xc1\x83\x2a\x9e" }, /* UMX */
421 { 0, "ASYLUM Music Format V1.0" }, /* AMF */
422 { 0, "PSM\xfe" }, /* PSM */
423 { 0, "PSM " }, /* PSM */
424 { 0, "MT20" }, /* MT2 */
426 { 1080, "M.K." }, /* MOD */
442 const uint8_t *p_peek;
443 const int i_peek = stream_Peek( p_demux->s, &p_peek, 2048 );
447 for( int i = 0; p_marker[i].i_offset >= 0; i++ )
449 const char *psz_marker = p_marker[i].psz_marker;
450 const int i_size = strlen( psz_marker );
451 const int i_offset = p_marker[i].i_offset;
453 if( i_peek < i_offset + i_size )
456 if( !memcmp( &p_peek[i_offset], psz_marker, i_size ) )
460 /* The only two format left untested are ABC and MOD(old version)
461 * ant they are difficult to test :( */
464 * TODO i_peek = 2048 is too big for such files */
465 if( !strcasecmp( psz_ext, "abc" ) )
470 for( int i = 0; i < i_peek-1; i++ )
472 b_k |= p_peek[i+0] == 'K' && p_peek[i+1] == ':';
473 b_tx |= ( p_peek[i+0] == 'X' || p_peek[i+0] == 'T') && p_peek[i+1] == ':';
481 if( !strcasecmp( psz_ext, "mod" ) && i_peek >= 20 + 15 * 30 )
483 /* Check that the name is correctly null padded */
484 const uint8_t *p = memchr( p_peek, '\0', 20 );
487 for( ; p < &p_peek[20]; p++ )
494 for( int i = 0; i < 15; i++ )
496 const uint8_t *p_sample = &p_peek[20 + i*30];
498 /* Check correct null padding */
499 const uint8_t *p = memchr( &p_sample[0], '\0', 22 );
502 for( ; p < &p_sample[22]; p++ )
509 if( p_sample[25] > 64 ) /* Volume value */