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