]> git.sesse.net Git - vlc/blob - modules/demux/mod.c
Use var_InheritString for --decklink-video-connection.
[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         if( !p_demux->psz_file )
153             return VLC_EGENERIC;
154         const char *psz_ext = strrchr( p_demux->psz_file, '.' );
155         int i;
156
157         if( !psz_ext )
158             return VLC_EGENERIC;
159
160         psz_ext++;  /* skip . */
161         for( i = 0; ppsz_mod_ext[i] != NULL; i++ )
162         {
163             if( !strcasecmp( psz_ext, ppsz_mod_ext[i] ) )
164                 break;
165         }
166         if( ppsz_mod_ext[i] == NULL )
167             return VLC_EGENERIC;
168         if( Validate( p_demux, ppsz_mod_ext[i] ) )
169         {
170             msg_Warn( p_demux, "MOD validation failed (ext=%s)", ppsz_mod_ext[i]);
171             return VLC_EGENERIC;
172         }
173         msg_Dbg( p_demux, "running MOD demuxer (ext=%s)", ppsz_mod_ext[i] );
174     }
175
176     const int64_t i_size = stream_Size( p_demux->s );
177     if( i_size <= 0 || i_size >= MOD_MAX_FILE_SIZE )
178         return VLC_EGENERIC;
179
180     /* Fill p_demux field */
181     p_demux->pf_demux = Demux;
182     p_demux->pf_control = Control;
183     p_demux->p_sys = p_sys = malloc( sizeof( *p_sys ) );
184     if( !p_sys )
185         return VLC_ENOMEM;
186
187     msg_Dbg( p_demux, "loading complete file (could be long)" );
188     p_sys->i_data = i_size;
189     p_sys->p_data = malloc( p_sys->i_data );
190     if( p_sys->p_data )
191         p_sys->i_data = stream_Read( p_demux->s, p_sys->p_data, p_sys->i_data );
192     if( p_sys->i_data <= 0 || !p_sys->p_data )
193     {
194         msg_Err( p_demux, "failed to read the complete file" );
195         free( p_sys->p_data );
196         free( p_sys );
197         return VLC_EGENERIC;
198     }
199
200     /* Configure modplug before loading the file */
201     ModPlug_GetSettings( &settings );
202     settings.mFlags = MODPLUG_ENABLE_OVERSAMPLING;
203     settings.mChannels = 2;
204     settings.mBits = 16;
205     settings.mFrequency = 44100;
206     settings.mResamplingMode = MODPLUG_RESAMPLE_FIR;
207
208     if( var_CreateGetBool( p_demux, "mod-noisereduction" ) )
209         settings.mFlags |= MODPLUG_ENABLE_NOISE_REDUCTION;
210
211     if( var_CreateGetBool( p_demux, "mod-reverb" ) )
212         settings.mFlags |= MODPLUG_ENABLE_REVERB;
213     settings.mReverbDepth = var_CreateGetInteger( p_demux, "mod-reverb-level" );
214     settings.mReverbDelay = var_CreateGetInteger( p_demux, "mod-reverb-delay" );
215
216     if( var_CreateGetBool( p_demux, "mod-megabass" ) )
217         settings.mFlags |= MODPLUG_ENABLE_MEGABASS;
218     settings.mBassAmount = var_CreateGetInteger( p_demux, "mod-megabass-level" );
219     settings.mBassRange = var_CreateGetInteger( p_demux, "mod-megabass-range" );
220
221     if( var_CreateGetBool( p_demux, "mod-surround" ) )
222         settings.mFlags |= MODPLUG_ENABLE_SURROUND;
223     settings.mSurroundDepth = var_CreateGetInteger( p_demux, "mod-surround-level" );
224     settings.mSurroundDelay = var_CreateGetInteger( p_demux, "mod-surround-delay" );
225
226     ModPlug_SetSettings( &settings );
227
228     if( ( p_sys->f = ModPlug_Load( p_sys->p_data, p_sys->i_data ) ) == NULL )
229     {
230         msg_Err( p_demux, "failed to understand the file" );
231         /* we try to seek to recover for other plugin */
232         stream_Seek( p_demux->s, 0 );
233         free( p_sys->p_data );
234         free( p_sys );
235         return VLC_EGENERIC;
236     }
237
238     /* init time */
239     date_Init( &p_sys->pts, settings.mFrequency, 1 );
240     date_Set( &p_sys->pts, 0 );
241     p_sys->i_length = ModPlug_GetLength( p_sys->f ) * INT64_C(1000);
242
243     msg_Dbg( p_demux, "MOD loaded name=%s lenght=%"PRId64"ms",
244              ModPlug_GetName( p_sys->f ),
245              p_sys->i_length );
246
247 #ifdef WORDS_BIGENDIAN
248     es_format_Init( &p_sys->fmt, AUDIO_ES, VLC_FOURCC( 't', 'w', 'o', 's' ) );
249 #else
250     es_format_Init( &p_sys->fmt, AUDIO_ES, VLC_FOURCC( 'a', 'r', 'a', 'w' ) );
251 #endif
252     p_sys->fmt.audio.i_rate = settings.mFrequency;
253     p_sys->fmt.audio.i_channels = settings.mChannels;
254     p_sys->fmt.audio.i_bitspersample = settings.mBits;
255     p_sys->es = es_out_Add( p_demux->out, &p_sys->fmt );
256
257     return VLC_SUCCESS;
258 }
259
260 /*****************************************************************************
261  * Close
262  *****************************************************************************/
263 static void Close( vlc_object_t *p_this )
264 {
265     demux_t     *p_demux = (demux_t*)p_this;
266     demux_sys_t *p_sys = p_demux->p_sys;
267
268     ModPlug_Unload( p_sys->f );
269     free( p_sys->p_data );
270     free( p_sys );
271 }
272
273
274 /*****************************************************************************
275  * Demux:
276  *****************************************************************************/
277 static int Demux( demux_t *p_demux )
278 {
279     demux_sys_t *p_sys = p_demux->p_sys;
280     block_t     *p_frame;
281     const int i_bk = ( p_sys->fmt.audio.i_bitspersample / 8 ) *
282                        p_sys->fmt.audio.i_channels;
283
284     p_frame = block_New( p_demux, p_sys->fmt.audio.i_rate / 10 * i_bk );
285     if( !p_frame )
286         return -1;
287
288     const int i_read = ModPlug_Read( p_sys->f, p_frame->p_buffer, p_frame->i_buffer );
289     if( i_read <= 0 )
290     {
291         /* EOF */
292         block_Release( p_frame );
293         return 0;
294     }
295     p_frame->i_buffer = i_read;
296     p_frame->i_dts =
297     p_frame->i_pts = VLC_TS_0 + date_Get( &p_sys->pts );
298
299     /* Set PCR */
300     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_frame->i_pts );
301
302     /* Send data */
303     es_out_Send( p_demux->out, p_sys->es, p_frame );
304
305     date_Increment( &p_sys->pts, i_read / i_bk );
306
307     return 1;
308 }
309
310 /*****************************************************************************
311  * Control:
312  *****************************************************************************/
313 static int Control( demux_t *p_demux, int i_query, va_list args )
314 {
315     demux_sys_t *p_sys = p_demux->p_sys;
316     double f, *pf;
317     int64_t i64, *pi64;
318
319     switch( i_query )
320     {
321     case DEMUX_GET_POSITION:
322         pf = (double*) va_arg( args, double* );
323         if( p_sys->i_length > 0 )
324         {
325             double current = date_Get( &p_sys->pts );
326             double length = p_sys->i_length;
327             *pf = current / length;
328             return VLC_SUCCESS;
329         }
330         return VLC_EGENERIC;
331
332     case DEMUX_SET_POSITION:
333         f = (double) va_arg( args, double );
334
335         i64 = f * p_sys->i_length;
336         if( i64 >= 0 && i64 <= p_sys->i_length )
337         {
338             ModPlug_Seek( p_sys->f, i64 / 1000 );
339             date_Set( &p_sys->pts, i64 );
340
341             return VLC_SUCCESS;
342         }
343         return VLC_EGENERIC;
344
345     case DEMUX_GET_TIME:
346         pi64 = (int64_t*)va_arg( args, int64_t * );
347         *pi64 = date_Get( &p_sys->pts );
348         return VLC_SUCCESS;
349
350     case DEMUX_GET_LENGTH:
351         pi64 = (int64_t*)va_arg( args, int64_t * );
352         *pi64 = p_sys->i_length;
353         return VLC_SUCCESS;
354
355     case DEMUX_SET_TIME:
356         i64 = (int64_t)va_arg( args, int64_t );
357
358         if( i64 >= 0 && i64 <= p_sys->i_length )
359         {
360             ModPlug_Seek( p_sys->f, i64 / 1000 );
361             date_Set( &p_sys->pts, i64 );
362
363             return VLC_SUCCESS;
364         }
365         return VLC_EGENERIC;
366
367     case DEMUX_HAS_UNSUPPORTED_META:
368     {
369         bool *pb_bool = (bool*)va_arg( args, bool* );
370         *pb_bool = false; /* FIXME I am not sure of this one */
371         return VLC_SUCCESS;
372     }
373     case DEMUX_GET_META:
374     {
375         vlc_meta_t *p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t* );
376         unsigned i_num_samples = ModPlug_NumSamples( p_sys->f ),
377                  i_num_instruments = ModPlug_NumInstruments( p_sys->f );
378         unsigned i_num_patterns = ModPlug_NumPatterns( p_sys->f ),
379                  i_num_channels = ModPlug_NumChannels( p_sys->f );
380         //      unsigned modType = ModPlug_GetModuleType( p_sys->f );
381
382         char psz_temp[2048]; /* 32 * 240 max, but only need start  */
383         char *psz_module_info, *psz_instrument_info;
384         unsigned i_temp_index = 0;
385         const char *psz_name = ModPlug_GetName( p_sys->f );
386         if( psz_name && *psz_name )
387             vlc_meta_SetTitle( p_meta, psz_name );
388
389         /* Comment field from artist - not in every type of MOD */
390         psz_name = ModPlug_GetMessage( p_sys->f );
391         if( psz_name && *psz_name )
392             vlc_meta_SetDescription( p_meta, psz_name );
393
394         /* Instruments only in newer MODs - so don't show if 0 */
395         if( asprintf( &psz_instrument_info, ", %i Instruments",
396                       i_num_instruments ) >= 0 )
397         {
398             if( asprintf( &psz_module_info,
399                           "%i Channels, %i Patterns\n"
400                           "%i Samples%s\n",
401                           i_num_channels, i_num_patterns, i_num_samples,
402                           ( i_num_instruments ? psz_instrument_info : "" ) ) >= 0 )
403             {
404                 vlc_meta_AddExtra( p_meta, "Module Information",
405                                    psz_module_info );
406                 free( psz_module_info );
407             }
408
409             free( psz_instrument_info );
410         }
411
412         /* Make list of instruments (XM, IT, etc) */
413         if( i_num_instruments )
414         {
415             i_temp_index = 0;
416             for( unsigned i = 0; i < i_num_instruments && i_temp_index < sizeof(psz_temp); i++ )
417             {
418                 char lBuffer[33];
419                 ModPlug_InstrumentName( p_sys->f, i, lBuffer );
420                 if ( !lBuffer[0] ) continue; // don't add empty fields.
421                 i_temp_index += snprintf( &psz_temp[i_temp_index], sizeof(psz_temp) - i_temp_index, "%s\n", lBuffer );
422             }
423
424             vlc_meta_AddExtra( p_meta, "Instruments", psz_temp );
425         }
426
427         /* Make list of samples */
428         for( unsigned int i = 0; i < i_num_samples && i_temp_index < sizeof(psz_temp); i++ )
429         {
430             char psz_buffer[33];
431             ModPlug_SampleName( p_sys->f, i, psz_buffer );
432             if ( !psz_buffer[0] ) continue; // don't add empty fields.
433             i_temp_index += snprintf( &psz_temp[i_temp_index], sizeof(psz_temp) - i_temp_index, "%s\n", psz_buffer );
434         }
435
436         vlc_meta_AddExtra( p_meta, "Samples", psz_temp );
437
438         return VLC_SUCCESS;
439     }
440
441     case DEMUX_GET_FPS: /* meaningless */
442     default:
443         return VLC_EGENERIC;
444     }
445 }
446
447 /*****************************************************************************
448  * Validate: try to ensure it is really a mod file.
449  * The tests are not robust enough to replace extension checks in the general
450  * cases.
451  * TODO: maybe it should return a score, which will be used to bypass the
452  * extension checks when high enough.
453  *****************************************************************************/
454 static int Validate( demux_t *p_demux, const char *psz_ext )
455 {
456     static const struct
457     {
458         int i_offset;
459         const char *psz_marker;
460     } p_marker[] = {
461         {  0, "ziRCONia" },             /* MMCMP files */
462         {  0, "Extended Module" },      /* XM */
463         { 44, "SCRM" },                 /* S3M */
464         {  0, "IMPM" },                 /* IT */
465         {  0, "MThd" },                 /* MID */
466         {  0, "GF1PATCH110" },          /* PAT */
467         { 20, "!SCREAM!" },             /* STM */
468         { 20, "!Scream!" },             /* STM */
469         { 20, "BMOD2STM" },             /* STM */
470         {  0, "MMD0" },                 /* MED v0 */
471         {  0, "MMD1" },                 /* MED v1 */
472         {  0, "MMD2" },                 /* MED v2 */
473         {  0, "MMD3" },                 /* MED v3 */
474         {  0, "MTM" },                  /* MTM */
475         {  0, "DMDL" },                 /* MDL */
476         {  0, "DBM0" },                 /* DBM */
477         {  0, "if" },                   /* 669 */
478         {  0, "JN" },                   /* 669 */
479         {  0, "FAR\xfe" },              /* FAR */
480         {  0, "Extreme" },              /* AMS */
481         {  0, "OKTASONGCMOD" },         /* OKT */
482         { 44, "PTMF" },                 /* PTM */
483         {  0, "MAS_UTrack_V00" },       /* Ult */
484         {  0, "DDMF" },                 /* DMF */
485         {  8, "DSMFSONG" },             /* DSM */
486         {  0, "\xc1\x83\x2a\x9e" },     /* UMX */
487         {  0, "ASYLUM Music Format V1.0" }, /* AMF Type 0 */
488         {  0, "AMF" },                  /* AMF */
489         {  0, "PSM\xfe" },              /* PSM */
490         {  0, "PSM " },                 /* PSM */
491         {  0, "MT20" },                 /* MT2 */
492
493         { 1080, "M.K." },               /* MOD */
494         { 1080, "M!K!" },
495         { 1080, "M&K!" },
496         { 1080, "N.T." },
497         { 1080, "CD81" },
498         { 1080, "OKTA" },
499         { 1080, "16CN" },
500         { 1080, "32CN" },
501         { 1080, "FLT" },
502         { 1080, "TDZ" },
503         { 1081, "CHN" },
504         { 1082, "CH" },
505
506         {  -1, NULL }
507     };
508
509     const uint8_t *p_peek;
510     const int i_peek = stream_Peek( p_demux->s, &p_peek, 2048 );
511     if( i_peek < 4 )
512         return VLC_EGENERIC;
513
514     for( int i = 0; p_marker[i].i_offset >= 0; i++ )
515     {
516         const char *psz_marker = p_marker[i].psz_marker;
517         const int i_size = strlen( psz_marker );
518         const int i_offset = p_marker[i].i_offset;
519
520         if( i_peek < i_offset + i_size )
521             continue;
522
523         if( !memcmp( &p_peek[i_offset], psz_marker, i_size ) )
524             return VLC_SUCCESS;
525     }
526
527     /* The only two format left untested are ABC and MOD(old version)
528      * ant they are difficult to test :( */
529
530     /* Check for ABC
531      * TODO i_peek = 2048 is too big for such files */
532     if( !strcasecmp( psz_ext, "abc" ) )
533     {
534         bool b_k = false;
535         bool b_tx = false;
536
537         for( int i = 0; i < i_peek-1; i++ )
538         {
539             b_k |= p_peek[i+0] == 'K' && p_peek[i+1] == ':';
540             b_tx |= ( p_peek[i+0] == 'X' || p_peek[i+0] == 'T') && p_peek[i+1] == ':';
541         }
542         if( !b_k || !b_tx )
543             return VLC_EGENERIC;
544         return VLC_SUCCESS;
545     }
546
547     /* Check for MOD */
548     if( !strcasecmp( psz_ext, "mod" ) && i_peek >= 20 + 15 * 30 )
549     {
550         /* Check that the name is correctly null padded */
551         const uint8_t *p = memchr( p_peek, '\0', 20 );
552         if( p )
553         {
554             for( ; p < &p_peek[20]; p++ )
555             {
556                 if( *p )
557                     return VLC_EGENERIC;
558             }
559         }
560
561         for( int i = 0; i < 15; i++ )
562         {
563             const uint8_t *p_sample = &p_peek[20 + i*30];
564
565             /* Check correct null padding */
566             const uint8_t *p = memchr( &p_sample[0], '\0', 22 );
567             if( p )
568             {
569                 for( ; p < &p_sample[22]; p++ )
570                 {
571                     if( *p )
572                         return VLC_EGENERIC;
573                 }
574             }
575
576             if( p_sample[25] > 64 ) /* Volume value */
577                 return VLC_EGENERIC;
578         }
579         return VLC_SUCCESS;
580     }
581     return VLC_EGENERIC;
582 }