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