]> git.sesse.net Git - vlc/blob - modules/demux/mod.c
Mod: reindent.
[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  * Konstanty Bialkowski <konstanty@ieee.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_demux.h>
36 #include <vlc_meta.h>
37 #include <assert.h>
38
39 #include <libmodplug/modplug.h>
40
41 /* TODO:
42  *  - extend demux control to query meta data (demuxer should NEVER touch
43  *      playlist itself)
44  *  - FIXME test endian of samples
45  *  - ...
46  */
47
48 /*****************************************************************************
49  * Module descriptor
50  *****************************************************************************/
51 static int  Open    ( vlc_object_t * );
52 static void Close  ( vlc_object_t * );
53
54 #define NOISE_LONGTEXT N_("Enable noise reduction algorithm.")
55 #define REVERB_LONGTEXT N_("Enable reverberation" )
56 #define REVERB_LEVEL_LONGTEXT N_( "Reverberation level (from 0 " \
57                 "to 100, default value is 0)." )
58 #define REVERB_DELAY_LONGTEXT N_("Reverberation delay, in ms." \
59                 " Usual values are from to 40 to 200ms." )
60 #define MEGABASS_LONGTEXT N_( "Enable megabass mode" )
61 #define MEGABASS_LEVEL_LONGTEXT N_("Megabass mode level (from 0 to 100, " \
62                 "default value is 0)." )
63 #define MEGABASS_RANGE_LONGTEXT N_("Megabass mode cutoff frequency, in Hz. " \
64                 "This is the maximum frequency for which the megabass " \
65                 "effect applies. Valid values are from 10 to 100 Hz." )
66 #define SURROUND_LEVEL_LONGTEXT N_( "Surround effect level (from 0 to 100, " \
67                 "default value is 0)." )
68 #define SURROUND_DELAY_LONGTEXT N_("Surround delay, in ms. Usual values are " \
69                 "from 5 to 40 ms." )
70
71 vlc_module_begin ()
72     set_shortname( "MOD")
73     set_description( N_("MOD demuxer (libmodplug)" ) )
74     set_capability( "demux", 10 )
75     set_category( CAT_INPUT )
76     set_subcategory( SUBCAT_INPUT_DEMUX )
77
78     add_bool( "mod-noisereduction", true, NULL, N_("Noise reduction"),
79               NOISE_LONGTEXT, false )
80
81     add_bool( "mod-reverb", false, NULL, N_("Reverb"),
82               REVERB_LONGTEXT, false )
83     add_integer_with_range( "mod-reverb-level", 0, 0, 100, NULL,
84              N_("Reverberation level"), REVERB_LEVEL_LONGTEXT, true )
85     add_integer_with_range( "mod-reverb-delay", 40, 0, 1000, NULL,
86              N_("Reverberation delay"), REVERB_DELAY_LONGTEXT, true )
87
88     add_bool( "mod-megabass", false, NULL, N_("Mega bass"),
89                     MEGABASS_LONGTEXT, false )
90     add_integer_with_range( "mod-megabass-level", 0, 0, 100, NULL,
91               N_("Mega bass level"), MEGABASS_LEVEL_LONGTEXT, true )
92     add_integer_with_range( "mod-megabass-range", 10, 10, 100, NULL,
93               N_("Mega bass cutoff"), MEGABASS_RANGE_LONGTEXT, true )
94
95     add_bool( "mod-surround", false, NULL, N_("Surround"), N_("Surround"),
96                false )
97     add_integer_with_range( "mod-surround-level", 0, 0, 100, NULL,
98               N_("Surround level"), SURROUND_LEVEL_LONGTEXT, true )
99     add_integer_with_range( "mod-surround-delay", 5, 0, 1000, NULL,
100               N_("Surround delay (ms)"), SURROUND_DELAY_LONGTEXT, true )
101
102     set_callbacks( Open, Close )
103     add_shortcut( "mod" )
104 vlc_module_end ()
105
106 /*****************************************************************************
107  * Local prototypes
108  *****************************************************************************/
109
110 struct demux_sys_t
111 {
112     es_format_t  fmt;
113     es_out_id_t *es;
114
115     date_t      pts;
116     int64_t     i_length;
117
118     int         i_data;
119     uint8_t     *p_data;
120     ModPlugFile *f;
121 };
122
123 static int Demux  ( demux_t *p_demux );
124 static int Control( demux_t *p_demux, int i_query, va_list args );
125
126 static int Validate( demux_t *p_demux, const char *psz_ext );
127
128 static const char *ppsz_mod_ext[] =
129 {
130     "mod", "s3m", "xm",  "it",  "669", "amf", "ams", "dbm", "dmf", "dsm",
131     "far", "mdl", "med", "mtm", "okt", "ptm", "stm", "ult", "umx", "mt2",
132     "psm", "abc", NULL
133 };
134
135
136 /* We load the complete file in memory, put a higher bound
137  * of 500 Mo (which is really big anyway) */
138 #define MOD_MAX_FILE_SIZE (500*1000*1000)
139
140 /*****************************************************************************
141  * Open
142  *****************************************************************************/
143 static int Open( vlc_object_t *p_this )
144 {
145     demux_t     *p_demux = (demux_t*)p_this;
146     demux_sys_t *p_sys;
147     ModPlug_Settings settings;
148
149     /* We accept file based on extension match */
150     if( !p_demux->b_force )
151     {
152         const char *psz_ext = strrchr( p_demux->psz_path, '.' );
153         int i;
154
155         if( !psz_ext )
156             return VLC_EGENERIC;
157
158         psz_ext++;  /* skip . */
159         for( i = 0; ppsz_mod_ext[i] != NULL; i++ )
160         {
161             if( !strcasecmp( psz_ext, ppsz_mod_ext[i] ) )
162                 break;
163         }
164         if( ppsz_mod_ext[i] == NULL )
165             return VLC_EGENERIC;
166         if( Validate( p_demux, ppsz_mod_ext[i] ) )
167         {
168             msg_Warn( p_demux, "MOD validation failed (ext=%s)", ppsz_mod_ext[i]);
169             return VLC_EGENERIC;
170         }
171         msg_Dbg( p_demux, "running MOD demuxer (ext=%s)", ppsz_mod_ext[i] );
172     }
173
174     const int64_t i_size = stream_Size( p_demux->s );
175     if( i_size <= 0 || i_size >= MOD_MAX_FILE_SIZE )
176         return VLC_EGENERIC;
177
178     /* Fill p_demux field */
179     p_demux->pf_demux = Demux;
180     p_demux->pf_control = Control;
181     p_demux->p_sys = p_sys = malloc( sizeof( *p_sys ) );
182     if( !p_sys )
183         return VLC_ENOMEM;
184
185     msg_Dbg( p_demux, "loading complete file (could be long)" );
186     p_sys->i_data = i_size;
187     p_sys->p_data = malloc( p_sys->i_data );
188     if( p_sys->p_data )
189         p_sys->i_data = stream_Read( p_demux->s, p_sys->p_data, p_sys->i_data );
190     if( p_sys->i_data <= 0 || !p_sys->p_data )
191     {
192         msg_Err( p_demux, "failed to read the complete file" );
193         free( p_sys->p_data );
194         free( p_sys );
195         return VLC_EGENERIC;
196     }
197
198     /* Configure modplug before loading the file */
199     ModPlug_GetSettings( &settings );
200     settings.mFlags = MODPLUG_ENABLE_OVERSAMPLING;
201     settings.mChannels = 2;
202     settings.mBits = 16;
203     settings.mFrequency = 44100;
204     settings.mResamplingMode = MODPLUG_RESAMPLE_FIR;
205
206     if( var_CreateGetBool( p_demux, "mod-noisereduction" ) )
207         settings.mFlags |= MODPLUG_ENABLE_NOISE_REDUCTION;
208
209     if( var_CreateGetBool( p_demux, "mod-reverb" ) )
210         settings.mFlags |= MODPLUG_ENABLE_REVERB;
211     settings.mReverbDepth = var_CreateGetInteger( p_demux, "mod-reverb-level" );
212     settings.mReverbDelay = var_CreateGetInteger( p_demux, "mod-reverb-delay" );
213
214     if( var_CreateGetBool( p_demux, "mod-megabass" ) )
215         settings.mFlags |= MODPLUG_ENABLE_MEGABASS;
216     settings.mBassAmount = var_CreateGetInteger( p_demux, "mod-megabass-level" );
217     settings.mBassRange = var_CreateGetInteger( p_demux, "mod-megabass-range" );
218
219     if( var_CreateGetBool( p_demux, "mod-surround" ) )
220         settings.mFlags |= MODPLUG_ENABLE_SURROUND;
221     settings.mSurroundDepth = var_CreateGetInteger( p_demux, "mod-surround-level" );
222     settings.mSurroundDelay = var_CreateGetInteger( p_demux, "mod-surround-delay" );
223
224     ModPlug_SetSettings( &settings );
225
226     if( ( p_sys->f = ModPlug_Load( p_sys->p_data, p_sys->i_data ) ) == NULL )
227     {
228         msg_Err( p_demux, "failed to understand the file" );
229         /* we try to seek to recover for other plugin */
230         stream_Seek( p_demux->s, 0 );
231         free( p_sys->p_data );
232         free( p_sys );
233         return VLC_EGENERIC;
234     }
235
236     /* init time */
237     date_Init( &p_sys->pts, settings.mFrequency, 1 );
238     date_Set( &p_sys->pts, 1 );
239     p_sys->i_length = ModPlug_GetLength( p_sys->f ) * INT64_C(1000);
240
241     msg_Dbg( p_demux, "MOD loaded name=%s lenght=%"PRId64"ms",
242              ModPlug_GetName( p_sys->f ),
243              p_sys->i_length );
244
245 #ifdef WORDS_BIGENDIAN
246     es_format_Init( &p_sys->fmt, AUDIO_ES, VLC_FOURCC( 't', 'w', 'o', 's' ) );
247 #else
248     es_format_Init( &p_sys->fmt, AUDIO_ES, VLC_FOURCC( 'a', 'r', 'a', 'w' ) );
249 #endif
250     p_sys->fmt.audio.i_rate = settings.mFrequency;
251     p_sys->fmt.audio.i_channels = settings.mChannels;
252     p_sys->fmt.audio.i_bitspersample = settings.mBits;
253     p_sys->es = es_out_Add( p_demux->out, &p_sys->fmt );
254
255     return VLC_SUCCESS;
256 }
257
258 /*****************************************************************************
259  * Close
260  *****************************************************************************/
261 static void Close( vlc_object_t *p_this )
262 {
263     demux_t     *p_demux = (demux_t*)p_this;
264     demux_sys_t *p_sys = p_demux->p_sys;
265
266     ModPlug_Unload( p_sys->f );
267     free( p_sys->p_data );
268     free( p_sys );
269 }
270
271
272 /*****************************************************************************
273  * Demux:
274  *****************************************************************************/
275 static int Demux( demux_t *p_demux )
276 {
277     demux_sys_t *p_sys = p_demux->p_sys;
278     block_t     *p_frame;
279     const int i_bk = ( p_sys->fmt.audio.i_bitspersample / 8 ) *
280                        p_sys->fmt.audio.i_channels;
281
282     p_frame = block_New( p_demux, p_sys->fmt.audio.i_rate / 10 * i_bk );
283     if( !p_frame )
284         return -1;
285
286     const int i_read = ModPlug_Read( p_sys->f, p_frame->p_buffer, p_frame->i_buffer );
287     if( i_read <= 0 )
288     {
289         /* EOF */
290         block_Release( p_frame );
291         return 0;
292     }
293     p_frame->i_buffer = i_read;
294     p_frame->i_dts =
295     p_frame->i_pts = date_Increment( &p_sys->pts, p_frame->i_buffer / i_bk );
296
297     /* Set PCR */
298     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_frame->i_pts );
299
300     /* Send data */
301     es_out_Send( p_demux->out, p_sys->es, p_frame );
302
303     return 1;
304 }
305
306 /*****************************************************************************
307  * Control:
308  *****************************************************************************/
309 static int Control( demux_t *p_demux, int i_query, va_list args )
310 {
311     demux_sys_t *p_sys = p_demux->p_sys;
312     double f, *pf;
313     int64_t i64, *pi64;
314
315     switch( i_query )
316     {
317     case DEMUX_GET_POSITION:
318         pf = (double*) va_arg( args, double* );
319         if( p_sys->i_length > 0 )
320         {
321             *pf = (double)date_Get( &p_sys->pts ) / (double)p_sys->i_length;
322             return VLC_SUCCESS;
323         }
324         return VLC_EGENERIC;
325
326     case DEMUX_SET_POSITION:
327         f = (double) va_arg( args, double );
328
329         i64 = f * p_sys->i_length;
330         if( i64 >= 0 && i64 <= p_sys->i_length )
331         {
332             ModPlug_Seek( p_sys->f, i64 / 1000 );
333             date_Set( &p_sys->pts, i64 + 1 );
334
335             return VLC_SUCCESS;
336         }
337         return VLC_EGENERIC;
338
339     case DEMUX_GET_TIME:
340         pi64 = (int64_t*)va_arg( args, int64_t * );
341         *pi64 = date_Get( &p_sys->pts );
342         return VLC_SUCCESS;
343
344     case DEMUX_GET_LENGTH:
345         pi64 = (int64_t*)va_arg( args, int64_t * );
346         *pi64 = p_sys->i_length;
347         return VLC_SUCCESS;
348
349     case DEMUX_SET_TIME:
350         i64 = (int64_t)va_arg( args, int64_t );
351
352         if( i64 >= 0 && i64 <= p_sys->i_length )
353         {
354             ModPlug_Seek( p_sys->f, i64 / 1000 );
355             date_Set( &p_sys->pts, i64 + 1 );
356
357             return VLC_SUCCESS;
358         }
359         return VLC_EGENERIC;
360
361     case DEMUX_HAS_UNSUPPORTED_META:
362     {
363         bool *pb_bool = (bool*)va_arg( args, bool* );
364         *pb_bool = false; /* FIXME I am not sure of this one */
365         return VLC_SUCCESS;
366     }
367     case DEMUX_GET_META:
368     {
369         vlc_meta_t *p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t* );
370         unsigned i_num_samples = ModPlug_NumSamples( p_sys->f ),
371                  i_num_instruments = ModPlug_NumInstruments( p_sys->f );
372         unsigned i_num_patterns = ModPlug_NumPatterns( p_sys->f ),
373                  i_num_channels = ModPlug_NumChannels( p_sys->f );
374         //      unsigned modType = ModPlug_GetModuleType( p_sys->f );
375
376         char psz_temp[2048]; /* 32 * 240 max, but only need start  */
377         char *psz_module_info, *psz_instrument_info;
378         unsigned i_temp_index = 0;
379         const char *psz_name = ModPlug_GetName( p_sys->f );
380         if( psz_name && *psz_name )
381             vlc_meta_SetTitle( p_meta, psz_name );
382
383         /* Comment field from artist - not in every type of MOD */
384         psz_name = ModPlug_GetMessage( p_sys->f );
385         if (psz_name && *psz_name )
386             vlc_meta_SetDescription( p_meta, psz_name );
387
388         /* Instruments only in newer MODs - so don't show if 0 */
389         if ( asprintf( &psz_instrument_info, ", %i Instruments",
390                        i_num_instruments ) )
391         {
392             if ( asprintf( &psz_module_info, "%i Channels, %i Patterns\n"
393                 "%i Samples%s\n",
394                 i_num_channels, i_num_patterns, i_num_samples,
395                 ( i_num_instruments ? psz_instrument_info : "" ) ))
396             {
397                 vlc_meta_AddExtra( p_meta, "Module Information",
398                         psz_module_info );
399                 free( psz_module_info );
400             }
401
402             free( psz_instrument_info );
403         }
404
405         /* Make list of instruments (XM, IT, etc) */
406         if ( i_num_instruments )
407         {
408             i_temp_index = 0;
409             for ( int i = 0; i < i_num_instruments && i_temp_index < sizeof(psz_temp); i++ )
410             {
411                 char lBuffer[33];
412                 ModPlug_InstrumentName( p_sys->f, i, lBuffer );
413                 if ( !lBuffer[0] ) continue; // don't add empty fields.
414                 i_temp_index += snprintf( &psz_temp[i_temp_index], sizeof(psz_temp) - i_temp_index, "%s\n", lBuffer );
415             }
416
417             vlc_meta_AddExtra( p_meta, "Instruments", psz_temp );
418         }
419
420         /* Make list of samples */
421         for ( int i = 0; i < i_num_samples && i_temp_index < sizeof(psz_temp); i++ )
422         {
423             char lBuffer[33];
424             ModPlug_SampleName( p_sys->f, i, lBuffer );
425             if ( !lBuffer[0] ) continue; // don't add empty fields.
426             i_temp_index += snprintf( &psz_temp[i_temp_index], sizeof(psz_temp) - i_temp_index, "%s\n", lBuffer );
427         }
428
429         vlc_meta_AddExtra( p_meta, "Samples", psz_temp );
430
431         return VLC_SUCCESS;
432     }
433
434     case DEMUX_GET_FPS: /* meaningless */
435     default:
436         return VLC_EGENERIC;
437     }
438 }
439
440 /*****************************************************************************
441  * Validate: try to ensure it is really a mod file.
442  * The tests are not robust enough to replace extension checks in the general
443  * cases.
444  * TODO: maybe it should return a score, which will be used to bypass the
445  * extension checks when high enough.
446  *****************************************************************************/
447 static int Validate( demux_t *p_demux, const char *psz_ext )
448 {
449     static const struct
450     {
451         int i_offset;
452         const char *psz_marker;
453     } p_marker[] = {
454         {  0, "ziRCONia" },             /* MMCMP files */
455         {  0, "Extended Module" },      /* XM */
456         { 44, "SCRM" },                 /* S3M */
457         {  0, "IMPM" },                 /* IT */
458         {  0, "MThd" },                 /* MID */
459         {  0, "GF1PATCH110" },          /* PAT */
460         { 20, "!SCREAM!" },             /* STM */
461         { 20, "!Scream!" },             /* STM */
462         { 20, "BMOD2STM" },             /* STM */
463         {  0, "MMD0" },                 /* MED v0 */
464         {  0, "MMD1" },                 /* MED v1 */
465         {  0, "MMD2" },                 /* MED v2 */
466         {  0, "MMD3" },                 /* MED v3 */
467         {  0, "MTM" },                  /* MTM */
468         {  0, "DMDL" },                 /* MDL */
469         {  0, "DBM0" },                 /* DBM */
470         {  0, "if" },                   /* 669 */
471         {  0, "JN" },                   /* 669 */
472         {  0, "FAR\xfe" },              /* FAR */
473         {  0, "Extreme" },              /* AMS */
474         {  0, "OKTASONGCMOD" },         /* OKT */
475         { 44, "PTMF" },                 /* PTM */
476         {  0, "MAS_UTrack_V00" },       /* Ult */
477         {  0, "DDMF" },                 /* DMF */
478         {  8, "DSMFSONG" },             /* DSM */
479         {  0, "\xc1\x83\x2a\x9e" },     /* UMX */
480         {  0, "ASYLUM Music Format V1.0" }, /* AMF Type 0 */
481         {  0, "AMF" },                  /* AMF */
482         {  0, "PSM\xfe" },              /* PSM */
483         {  0, "PSM " },                 /* PSM */
484         {  0, "MT20" },                 /* MT2 */
485
486         { 1080, "M.K." },               /* MOD */
487         { 1080, "M!K!" },
488         { 1080, "M&K!" },
489         { 1080, "N.T." },
490         { 1080, "CD81" },
491         { 1080, "OKTA" },
492         { 1080, "16CN" },
493         { 1080, "32CN" },
494         { 1080, "FLT" },
495         { 1080, "TDZ" },
496         { 1081, "CHN" },
497         { 1082, "CH" },
498
499         {  -1, NULL }
500     };
501
502     const uint8_t *p_peek;
503     const int i_peek = stream_Peek( p_demux->s, &p_peek, 2048 );
504     if( i_peek < 4 )
505         return VLC_EGENERIC;
506
507     for( int i = 0; p_marker[i].i_offset >= 0; i++ )
508     {
509         const char *psz_marker = p_marker[i].psz_marker;
510         const int i_size = strlen( psz_marker );
511         const int i_offset = p_marker[i].i_offset;
512
513         if( i_peek < i_offset + i_size )
514             continue;
515
516         if( !memcmp( &p_peek[i_offset], psz_marker, i_size ) )
517             return VLC_SUCCESS;
518     }
519
520     /* The only two format left untested are ABC and MOD(old version)
521      * ant they are difficult to test :( */
522
523     /* Check for ABC
524      * TODO i_peek = 2048 is too big for such files */
525     if( !strcasecmp( psz_ext, "abc" ) )
526     {
527         bool b_k = false;
528         bool b_tx = false;
529
530         for( int i = 0; i < i_peek-1; i++ )
531         {
532             b_k |= p_peek[i+0] == 'K' && p_peek[i+1] == ':';
533             b_tx |= ( p_peek[i+0] == 'X' || p_peek[i+0] == 'T') && p_peek[i+1] == ':';
534         }
535         if( !b_k || !b_tx )
536             return VLC_EGENERIC;
537         return VLC_SUCCESS;
538     }
539
540     /* Check for MOD */
541     if( !strcasecmp( psz_ext, "mod" ) && i_peek >= 20 + 15 * 30 )
542     {
543         /* Check that the name is correctly null padded */
544         const uint8_t *p = memchr( p_peek, '\0', 20 );
545         if( p )
546         {
547             for( ; p < &p_peek[20]; p++ )
548             {
549                 if( *p )
550                     return VLC_EGENERIC;
551             }
552         }
553
554         for( int i = 0; i < 15; i++ )
555         {
556             const uint8_t *p_sample = &p_peek[20 + i*30];
557
558             /* Check correct null padding */
559             const uint8_t *p = memchr( &p_sample[0], '\0', 22 );
560             if( p )
561             {
562                 for( ; p < &p_sample[22]; p++ )
563                 {
564                     if( *p )
565                         return VLC_EGENERIC;
566                 }
567             }
568
569             if( p_sample[25] > 64 ) /* Volume value */
570                 return VLC_EGENERIC;
571         }
572         return VLC_SUCCESS;
573     }
574     return VLC_EGENERIC;
575 }