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