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