]> git.sesse.net Git - vlc/blob - modules/demux/mod.c
input options whitelisting, step 2 (refs #1371)
[vlc] / modules / demux / mod.c
1 /*****************************************************************************
2  * mod.c: MOD file demuxer (using libmodplug)
3  *****************************************************************************
4  * Copyright (C) 2004 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 #include <vlc/vlc.h>
29 #include <vlc_demux.h>
30
31 #include <libmodplug/modplug.h>
32
33 /* TODO:
34  *  - extend demux control to query meta data (demuxer should NEVER touch
35  *      playlist itself)
36  *  - FIXME test endian of samples
37  *  - ...
38  */
39
40 /*****************************************************************************
41  * Module descriptor
42  *****************************************************************************/
43 static int  Open    ( vlc_object_t * );
44 static void Close  ( vlc_object_t * );
45
46 #define NOISE_LONGTEXT N_("Enable noise reduction algorithm.")
47 #define REVERB_LONGTEXT N_("Enable reverberation" )
48 #define REVERB_LEVEL_LONGTEXT N_( "Reverberation level (from 0 " \
49                 "to 100, default value is 0)." )
50 #define REVERB_DELAY_LONGTEXT N_("Reverberation delay, in ms." \
51                 " Usual values are from to 40 to 200ms." )
52 #define MEGABASS_LONGTEXT N_( "Enable megabass mode" )
53 #define MEGABASS_LEVEL_LONGTEXT N_("Megabass mode level (from 0 to 100, " \
54                 "default value is 0)." )
55 #define MEGABASS_RANGE_LONGTEXT N_("Megabass mode cutoff frequency, in Hz. " \
56                 "This is the maximum frequency for which the megabass " \
57                 "effect applies. Valid values are from 10 to 100 Hz." )
58 #define SURROUND_LEVEL_LONGTEXT N_( "Surround effect level (from 0 to 100, " \
59                 "default value is 0)." )
60 #define SURROUND_DELAY_LONGTEXT N_("Surround delay, in ms. Usual values are " \
61                 "from 5 to 40 ms." )
62
63 vlc_module_begin();
64     set_shortname( "MOD");
65     set_description( _("MOD demuxer (libmodplug)" ) );
66     set_capability( "demux2", 10 );
67     set_category( CAT_INPUT );
68     set_subcategory( SUBCAT_INPUT_DEMUX );
69
70     add_bool( "mod-noisereduction", VLC_TRUE, NULL, N_("Noise reduction"),
71               NOISE_LONGTEXT, VLC_FALSE );
72         change_safe();
73
74     add_bool( "mod-reverb", VLC_FALSE, NULL, N_("Reverb"),
75               REVERB_LONGTEXT, VLC_FALSE );
76         change_safe();
77     add_integer_with_range( "mod-reverb-level", 0, 0, 100, NULL,
78              N_("Reverberation level"), REVERB_LEVEL_LONGTEXT, VLC_TRUE );
79         change_safe();
80     add_integer_with_range( "mod-reverb-delay", 40, 0, 1000, NULL,
81              N_("Reverberation delay"), REVERB_DELAY_LONGTEXT, VLC_TRUE );
82         change_safe();
83
84     add_bool( "mod-megabass", VLC_FALSE, NULL, N_("Mega bass"),
85                     MEGABASS_LONGTEXT, VLC_FALSE );
86         change_safe();
87     add_integer_with_range( "mod-megabass-level", 0, 0, 100, NULL,
88               N_("Mega bass level"), MEGABASS_LEVEL_LONGTEXT, VLC_TRUE );
89         change_safe();
90     add_integer_with_range( "mod-megabass-range", 10, 10, 100, NULL,
91               N_("Mega bass cutoff"), MEGABASS_RANGE_LONGTEXT, VLC_TRUE );
92         change_safe();
93
94     add_bool( "mod-surround", VLC_FALSE, NULL, N_("Surround"), N_("Surround"),
95                VLC_FALSE );
96         change_safe();
97     add_integer_with_range( "mod-surround-level", 0, 0, 100, NULL,
98               N_("Surround level"), SURROUND_LEVEL_LONGTEXT, VLC_TRUE );
99         change_safe();
100     add_integer_with_range( "mod-surround-delay", 5, 0, 1000, NULL,
101               N_("Surround delay (ms)"), SURROUND_DELAY_LONGTEXT, VLC_TRUE );
102         change_safe();
103
104     set_callbacks( Open, Close );
105     add_shortcut( "mod" );
106 vlc_module_end();
107
108 /*****************************************************************************
109  * Local prototypes
110  *****************************************************************************/
111
112 struct demux_sys_t
113 {
114     es_format_t  fmt;
115     es_out_id_t *es;
116
117     int64_t     i_time;
118     int64_t     i_length;
119
120     int         i_data;
121     uint8_t     *p_data;
122     ModPlugFile *f;
123 };
124
125 static int Demux  ( demux_t *p_demux );
126 static int Control( demux_t *p_demux, int i_query, va_list args );
127
128 static const char* 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", NULL
133 };
134
135 /*****************************************************************************
136  * Open
137  *****************************************************************************/
138 static int Open( vlc_object_t *p_this )
139 {
140     demux_t     *p_demux = (demux_t*)p_this;
141     demux_sys_t *p_sys;
142     ModPlug_Settings settings;
143     vlc_value_t val;
144
145     /* We accept file based on extension match */
146     if( !p_demux->b_force )
147     {
148         char *ext;
149         int i;
150         if( ( ext = strrchr( p_demux->psz_path, '.' ) ) == NULL ||
151             stream_Size( p_demux->s ) == 0 ) return VLC_EGENERIC;
152
153         ext++;  /* skip . */
154         for( i = 0; mod_ext[i] != NULL; i++ )
155         {
156             if( !strcasecmp( ext, mod_ext[i] ) )
157             {
158                 break;
159             }
160         }
161         if( mod_ext[i] == NULL ) return VLC_EGENERIC;
162         msg_Dbg( p_demux, "running MOD demuxer (ext=%s)", mod_ext[i] );
163     }
164
165     /* Fill p_demux field */
166     p_demux->pf_demux = Demux;
167     p_demux->pf_control = Control;
168     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
169
170     msg_Dbg( p_demux, "loading complete file (could be long)" );
171     p_sys->i_data = stream_Size( p_demux->s );
172     p_sys->p_data = malloc( p_sys->i_data );
173     p_sys->i_data = stream_Read( p_demux->s, p_sys->p_data, p_sys->i_data );
174     if( p_sys->i_data <= 0 )
175     {
176         msg_Err( p_demux, "failed to read the complete file" );
177         free( p_sys->p_data );
178         free( p_sys );
179         return VLC_EGENERIC;
180     }
181     /* Create our config variable */
182     var_Create( p_demux, "mod-noisereduction", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
183     var_Create( p_demux, "mod-reverb", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
184     var_Create( p_demux, "mod-reverb-level", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
185     var_Create( p_demux, "mod-reverb-delay", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
186     var_Create( p_demux, "mod-megabass", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
187     var_Create( p_demux, "mod-megabass-level", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
188     var_Create( p_demux, "mod-megabass-range", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
189     var_Create( p_demux, "mod-surround", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
190     var_Create( p_demux, "mod-surround-level", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
191     var_Create( p_demux, "mod-surround-delay", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
192
193     /* Configure modplug before loading the file */
194     ModPlug_GetSettings( &settings );
195     settings.mFlags = MODPLUG_ENABLE_OVERSAMPLING;
196     settings.mChannels = 2;
197     settings.mBits = 16;
198     settings.mFrequency = 44100;
199     settings.mResamplingMode = MODPLUG_RESAMPLE_FIR;
200
201     var_Get( p_demux, "mod-noisereduction", &val );
202     if( val.b_bool) settings.mFlags |= MODPLUG_ENABLE_NOISE_REDUCTION;
203
204     var_Get( p_demux, "mod-reverb", &val );
205     if( val.b_bool) settings.mFlags |= MODPLUG_ENABLE_REVERB;
206     var_Get( p_demux, "mod-reverb-level", &val );
207     settings.mReverbDepth = val.i_int;
208     var_Get( p_demux, "mod-reverb-delay", &val );
209     settings.mReverbDelay = val.i_int;
210
211     var_Get( p_demux, "mod-megabass", &val );
212     if( val.b_bool) settings.mFlags |= MODPLUG_ENABLE_MEGABASS;
213     var_Get( p_demux, "mod-megabass-level", &val );
214     settings.mBassAmount = val.i_int;
215     var_Get( p_demux, "mod-megabass-range", &val );
216     settings.mBassRange = val.i_int;
217
218     var_Get( p_demux, "mod-surround", &val );
219     if( val.b_bool) settings.mFlags |= MODPLUG_ENABLE_SURROUND;
220     var_Get( p_demux, "mod-surround-level", &val );
221     settings.mSurroundDepth = val.i_int;
222     var_Get( p_demux, "mod-surround-delay", &val );
223     settings.mSurroundDelay = val.i_int;
224
225     ModPlug_SetSettings( &settings );
226
227     if( ( p_sys->f = ModPlug_Load( p_sys->p_data, p_sys->i_data ) ) == NULL )
228     {
229         msg_Err( p_demux, "failed to understand the file" );
230         /* we try to seek to recover for other plugin */
231         stream_Seek( p_demux->s, 0 );
232         free( p_sys->p_data );
233         free( p_sys );
234         return VLC_EGENERIC;
235     }
236
237     /* init time */
238     p_sys->i_time  = 1;
239     p_sys->i_length = ModPlug_GetLength( p_sys->f ) * (int64_t)1000;
240
241     msg_Dbg( p_demux, "MOD loaded name=%s lenght="I64Fd"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     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
284     p_frame->i_buffer = ModPlug_Read( p_sys->f, p_frame->p_buffer, p_frame->i_buffer );
285     if( p_frame->i_buffer <= 0 )
286     {
287         /* EOF */
288         block_Release( p_frame );
289         return 0;
290     }
291
292     /* Set PCR */
293     es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_sys->i_time );
294
295     /* We should use p_frame->i_buffer */
296     p_sys->i_time += (int64_t)1000000 * p_frame->i_buffer / i_bk / p_sys->fmt.audio.i_rate;
297
298     /* Send data */
299     p_frame->i_dts = p_frame->i_pts = p_sys->i_time;
300     es_out_Send( p_demux->out, p_sys->es, p_frame );
301
302     return 1;
303 }
304
305 /*****************************************************************************
306  * Control:
307  *****************************************************************************/
308 static int Control( demux_t *p_demux, int i_query, va_list args )
309 {
310     demux_sys_t *p_sys = p_demux->p_sys;
311     double f, *pf;
312     int64_t i64, *pi64;
313
314     switch( i_query )
315     {
316         case DEMUX_GET_POSITION:
317             pf = (double*) va_arg( args, double* );
318             if( p_sys->i_length > 0 )
319             {
320                 *pf = (double)p_sys->i_time / (double)p_sys->i_length;
321                 return VLC_SUCCESS;
322             }
323             return VLC_EGENERIC;
324
325         case DEMUX_SET_POSITION:
326             f = (double) va_arg( args, double );
327
328             i64 = f * p_sys->i_length;
329             if( i64 >= 0 && i64 <= p_sys->i_length )
330             {
331                 ModPlug_Seek( p_sys->f, i64 / 1000 );
332                 p_sys->i_time = i64 + 1;
333                 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
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 = p_sys->i_time;
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                 p_sys->i_time = i64 + 1;
356                 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
357
358                 return VLC_SUCCESS;
359             }
360             return VLC_EGENERIC;
361
362         case DEMUX_GET_FPS: /* meaningless */
363         default:
364             return VLC_EGENERIC;
365     }
366 }
367