]> git.sesse.net Git - vlc/blob - modules/demux/mod.c
Improved a bit mod detection checks.
[vlc] / modules / demux / mod.c
1 /*****************************************************************************
2  * mod.c: MOD file demuxer (using libmodplug)
3  *****************************************************************************
4  * Copyright (C) 2004-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_demux.h>
35 #include <vlc_meta.h>
36 #include <assert.h>
37
38 #include <libmodplug/modplug.h>
39
40 /* TODO:
41  *  - extend demux control to query meta data (demuxer should NEVER touch
42  *      playlist itself)
43  *  - FIXME test endian of samples
44  *  - ...
45  */
46
47 /*****************************************************************************
48  * Module descriptor
49  *****************************************************************************/
50 static int  Open    ( vlc_object_t * );
51 static void Close  ( vlc_object_t * );
52
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 " \
68                 "from 5 to 40 ms." )
69
70 vlc_module_begin ()
71     set_shortname( "MOD")
72     set_description( N_("MOD demuxer (libmodplug)" ) )
73     set_capability( "demux", 10 )
74     set_category( CAT_INPUT )
75     set_subcategory( SUBCAT_INPUT_DEMUX )
76
77     add_bool( "mod-noisereduction", true, NULL, N_("Noise reduction"),
78               NOISE_LONGTEXT, false )
79
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 )
86
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 )
93
94     add_bool( "mod-surround", false, NULL, N_("Surround"), N_("Surround"),
95                false )
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 )
100
101     set_callbacks( Open, Close )
102     add_shortcut( "mod" )
103 vlc_module_end ()
104
105 /*****************************************************************************
106  * Local prototypes
107  *****************************************************************************/
108
109 struct demux_sys_t
110 {
111     es_format_t  fmt;
112     es_out_id_t *es;
113
114     int64_t     i_time;
115     int64_t     i_length;
116
117     int         i_data;
118     uint8_t     *p_data;
119     ModPlugFile *f;
120 };
121
122 static int Demux  ( demux_t *p_demux );
123 static int Control( demux_t *p_demux, int i_query, va_list args );
124
125 static int Validate( demux_t *p_demux, const char *psz_ext );
126
127 static const char *ppsz_mod_ext[] =
128 {
129     "mod", "s3m", "xm",  "it",  "669", "amf", "ams", "dbm", "dmf", "dsm",
130     "far", "mdl", "med", "mtm", "okt", "ptm", "stm", "ult", "umx", "mt2",
131     "psm", "abc", NULL
132 };
133
134
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)
138
139 /*****************************************************************************
140  * Open
141  *****************************************************************************/
142 static int Open( vlc_object_t *p_this )
143 {
144     demux_t     *p_demux = (demux_t*)p_this;
145     demux_sys_t *p_sys;
146     ModPlug_Settings settings;
147
148     /* We accept file based on extension match */
149     if( !p_demux->b_force )
150     {
151         const char *psz_ext = strrchr( p_demux->psz_path, '.' );
152         int i;
153
154         if( !psz_ext )
155             return VLC_EGENERIC;
156
157         psz_ext++;  /* skip . */
158         for( i = 0; ppsz_mod_ext[i] != NULL; i++ )
159         {
160             if( !strcasecmp( psz_ext, ppsz_mod_ext[i] ) )
161                 break;
162         }
163         if( ppsz_mod_ext[i] == NULL )
164             return VLC_EGENERIC;
165         if( Validate( p_demux, ppsz_mod_ext[i] ) )
166         {
167             msg_Warn( p_demux, "MOD validation failed (ext=%s)", ppsz_mod_ext[i]);
168             return VLC_EGENERIC;
169         }
170         msg_Dbg( p_demux, "running MOD demuxer (ext=%s)", ppsz_mod_ext[i] );
171     }
172
173     const int64_t i_size = stream_Size( p_demux->s );
174     if( i_size <= 0 || i_size >= MOD_MAX_FILE_SIZE )
175         return VLC_EGENERIC;
176
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 ) );
181     if( !p_sys )
182         return VLC_ENOMEM;
183
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 );
187     if( p_sys->p_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 )
190     {
191         msg_Err( p_demux, "failed to read the complete file" );
192         free( p_sys->p_data );
193         free( p_sys );
194         return VLC_EGENERIC;
195     }
196
197     /* Configure modplug before loading the file */
198     ModPlug_GetSettings( &settings );
199     settings.mFlags = MODPLUG_ENABLE_OVERSAMPLING;
200     settings.mChannels = 2;
201     settings.mBits = 16;
202     settings.mFrequency = 44100;
203     settings.mResamplingMode = MODPLUG_RESAMPLE_FIR;
204
205     if( var_CreateGetBool( p_demux, "mod-noisereduction" ) )
206         settings.mFlags |= MODPLUG_ENABLE_NOISE_REDUCTION;
207
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" );
212
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" );
217
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" );
222
223     ModPlug_SetSettings( &settings );
224
225     if( ( p_sys->f = ModPlug_Load( p_sys->p_data, p_sys->i_data ) ) == NULL )
226     {
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 );
231         free( p_sys );
232         return VLC_EGENERIC;
233     }
234
235     /* init time */
236     p_sys->i_time  = 1;
237     p_sys->i_length = ModPlug_GetLength( p_sys->f ) * INT64_C(1000);
238
239     msg_Dbg( p_demux, "MOD loaded name=%s lenght=%"PRId64"ms",
240              ModPlug_GetName( p_sys->f ),
241              p_sys->i_length );
242
243 #ifdef WORDS_BIGENDIAN
244     es_format_Init( &p_sys->fmt, AUDIO_ES, VLC_FOURCC( 't', 'w', 'o', 's' ) );
245 #else
246     es_format_Init( &p_sys->fmt, AUDIO_ES, VLC_FOURCC( 'a', 'r', 'a', 'w' ) );
247 #endif
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 );
252
253     return VLC_SUCCESS;
254 }
255
256 /*****************************************************************************
257  * Close
258  *****************************************************************************/
259 static void Close( vlc_object_t *p_this )
260 {
261     demux_t     *p_demux = (demux_t*)p_this;
262     demux_sys_t *p_sys = p_demux->p_sys;
263
264     ModPlug_Unload( p_sys->f );
265     free( p_sys->p_data );
266     free( p_sys );
267 }
268
269
270 /*****************************************************************************
271  * Demux:
272  *****************************************************************************/
273 static int Demux( demux_t *p_demux )
274 {
275     demux_sys_t *p_sys = p_demux->p_sys;
276     block_t     *p_frame;
277     int         i_bk = ( p_sys->fmt.audio.i_bitspersample / 8 ) *
278                        p_sys->fmt.audio.i_channels;
279     int         i_read;
280
281     p_frame = block_New( p_demux, p_sys->fmt.audio.i_rate / 10 * i_bk );
282     if( !p_frame )
283         return -1;
284
285     i_read = ModPlug_Read( p_sys->f, p_frame->p_buffer, p_frame->i_buffer );
286     if( i_read <= 0 )
287     {
288         /* EOF */
289         block_Release( p_frame );
290         return 0;
291     }
292     p_frame->i_buffer = i_read;
293
294     /* Set PCR */
295     es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_sys->i_time );
296
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;
299
300     /* Send data */
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 );
303
304     return 1;
305 }
306
307 /*****************************************************************************
308  * Control:
309  *****************************************************************************/
310 static int Control( demux_t *p_demux, int i_query, va_list args )
311 {
312     demux_sys_t *p_sys = p_demux->p_sys;
313     double f, *pf;
314     int64_t i64, *pi64;
315
316     switch( i_query )
317     {
318     case DEMUX_GET_POSITION:
319         pf = (double*) va_arg( args, double* );
320         if( p_sys->i_length > 0 )
321         {
322             *pf = (double)p_sys->i_time / (double)p_sys->i_length;
323             return VLC_SUCCESS;
324         }
325         return VLC_EGENERIC;
326
327     case DEMUX_SET_POSITION:
328         f = (double) va_arg( args, double );
329
330         i64 = f * p_sys->i_length;
331         if( i64 >= 0 && i64 <= p_sys->i_length )
332         {
333             ModPlug_Seek( p_sys->f, i64 / 1000 );
334             p_sys->i_time = i64 + 1;
335
336             return VLC_SUCCESS;
337         }
338         return VLC_EGENERIC;
339
340     case DEMUX_GET_TIME:
341         pi64 = (int64_t*)va_arg( args, int64_t * );
342         *pi64 = p_sys->i_time;
343         return VLC_SUCCESS;
344
345     case DEMUX_GET_LENGTH:
346         pi64 = (int64_t*)va_arg( args, int64_t * );
347         *pi64 = p_sys->i_length;
348         return VLC_SUCCESS;
349
350     case DEMUX_SET_TIME:
351         i64 = (int64_t)va_arg( args, int64_t );
352
353         if( i64 >= 0 && i64 <= p_sys->i_length )
354         {
355             ModPlug_Seek( p_sys->f, i64 / 1000 );
356             p_sys->i_time = i64 + 1;
357
358             return VLC_SUCCESS;
359         }
360         return VLC_EGENERIC;
361
362     case DEMUX_HAS_UNSUPPORTED_META:
363     {
364         bool *pb_bool = (bool*)va_arg( args, bool* );
365         *pb_bool = false; /* FIXME I am not sure of this one */
366         return VLC_SUCCESS;
367     }
368     case DEMUX_GET_META:
369     {
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 );
374         return VLC_SUCCESS;
375     }
376
377     case DEMUX_GET_FPS: /* meaningless */
378     default:
379         return VLC_EGENERIC;
380     }
381 }
382
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
386  * cases.
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 )
391 {
392     static const struct
393     {
394         int i_offset;
395         const char *psz_marker;
396     } p_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 */
425
426         { 1080, "M.K." },               /* MOD */
427         { 1080, "M!K!" },
428         { 1080, "M&K!" },
429         { 1080, "N.T." },
430         { 1080, "CD81" },
431         { 1080, "OKTA" },
432         { 1080, "16CN" },
433         { 1080, "32CN" },
434         { 1080, "FLT" },
435         { 1080, "TDZ" },
436         { 1081, "CHN" },
437         { 1082, "CH" },
438
439         {  -1, NULL }
440     };
441
442     const uint8_t *p_peek;
443     const int i_peek = stream_Peek( p_demux->s, &p_peek, 2048 );
444     if( i_peek < 4 )
445         return VLC_EGENERIC;
446
447     for( int i = 0; p_marker[i].i_offset >= 0; i++ )
448     {
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;
452
453         if( i_peek < i_offset + i_size )
454             continue;
455
456         if( !memcmp( &p_peek[i_offset], psz_marker, i_size ) )
457             return VLC_SUCCESS;
458     }
459
460     /* The only two format left untested are ABC and MOD(old version)
461      * ant they are difficult to test :( */
462
463     /* Check for ABC
464      * TODO i_peek = 2048 is too big for such files */
465     if( !strcasecmp( psz_ext, "abc" ) )
466     {
467         bool b_k = false;
468         bool b_tx = false;
469
470         for( int i = 0; i < i_peek-1; i++ )
471         {
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] == ':';
474         }
475         if( !b_k || !b_tx )
476             return VLC_EGENERIC;
477         return VLC_SUCCESS;
478     }
479
480     /* Check for MOD */
481     if( !strcasecmp( psz_ext, "mod" ) && i_peek >= 20 + 15 * 30 )
482     {
483         /* Check that the name is correctly null padded */
484         const uint8_t *p = memchr( p_peek, '\0', 20 );
485         if( p )
486         {
487             for( ; p < &p_peek[20]; p++ )
488             {
489                 if( *p )
490                     return VLC_EGENERIC;
491             }
492         }
493
494         for( int i = 0; i < 15; i++ )
495         {
496             const uint8_t *p_sample = &p_peek[20 + i*30];
497
498             /* Check correct null padding */
499             const uint8_t *p = memchr( &p_sample[0], '\0', 22 );
500             if( p )
501             {
502                 for( ; p < &p_sample[22]; p++ )
503                 {
504                     if( *p )
505                         return VLC_EGENERIC;
506                 }
507             }
508
509             if( p_sample[25] > 64 ) /* Volume value */
510                 return VLC_EGENERIC;
511         }
512         return VLC_SUCCESS;
513     }
514     return VLC_EGENERIC;
515 }