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