]> git.sesse.net Git - vlc/blob - modules/demux/mod.c
Added mod title support.
[vlc] / modules / demux / mod.c
1 /*****************************************************************************
2  * mod.c: MOD file demuxer (using libmodplug)
3  *****************************************************************************
4  * Copyright (C) 2004-2009 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_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_demux.h>
35 #include <vlc_meta.h>
36
37 #include <libmodplug/modplug.h>
38
39 /* TODO:
40  *  - extend demux control to query meta data (demuxer should NEVER touch
41  *      playlist itself)
42  *  - FIXME test endian of samples
43  *  - ...
44  */
45
46 /*****************************************************************************
47  * Module descriptor
48  *****************************************************************************/
49 static int  Open    ( vlc_object_t * );
50 static void Close  ( vlc_object_t * );
51
52 #define NOISE_LONGTEXT N_("Enable noise reduction algorithm.")
53 #define REVERB_LONGTEXT N_("Enable reverberation" )
54 #define REVERB_LEVEL_LONGTEXT N_( "Reverberation level (from 0 " \
55                 "to 100, default value is 0)." )
56 #define REVERB_DELAY_LONGTEXT N_("Reverberation delay, in ms." \
57                 " Usual values are from to 40 to 200ms." )
58 #define MEGABASS_LONGTEXT N_( "Enable megabass mode" )
59 #define MEGABASS_LEVEL_LONGTEXT N_("Megabass mode level (from 0 to 100, " \
60                 "default value is 0)." )
61 #define MEGABASS_RANGE_LONGTEXT N_("Megabass mode cutoff frequency, in Hz. " \
62                 "This is the maximum frequency for which the megabass " \
63                 "effect applies. Valid values are from 10 to 100 Hz." )
64 #define SURROUND_LEVEL_LONGTEXT N_( "Surround effect level (from 0 to 100, " \
65                 "default value is 0)." )
66 #define SURROUND_DELAY_LONGTEXT N_("Surround delay, in ms. Usual values are " \
67                 "from 5 to 40 ms." )
68
69 vlc_module_begin ()
70     set_shortname( "MOD")
71     set_description( N_("MOD demuxer (libmodplug)" ) )
72     set_capability( "demux", 10 )
73     set_category( CAT_INPUT )
74     set_subcategory( SUBCAT_INPUT_DEMUX )
75
76     add_bool( "mod-noisereduction", true, NULL, N_("Noise reduction"),
77               NOISE_LONGTEXT, false )
78
79     add_bool( "mod-reverb", false, NULL, N_("Reverb"),
80               REVERB_LONGTEXT, false )
81     add_integer_with_range( "mod-reverb-level", 0, 0, 100, NULL,
82              N_("Reverberation level"), REVERB_LEVEL_LONGTEXT, true )
83     add_integer_with_range( "mod-reverb-delay", 40, 0, 1000, NULL,
84              N_("Reverberation delay"), REVERB_DELAY_LONGTEXT, true )
85
86     add_bool( "mod-megabass", false, NULL, N_("Mega bass"),
87                     MEGABASS_LONGTEXT, false )
88     add_integer_with_range( "mod-megabass-level", 0, 0, 100, NULL,
89               N_("Mega bass level"), MEGABASS_LEVEL_LONGTEXT, true )
90     add_integer_with_range( "mod-megabass-range", 10, 10, 100, NULL,
91               N_("Mega bass cutoff"), MEGABASS_RANGE_LONGTEXT, true )
92
93     add_bool( "mod-surround", false, NULL, N_("Surround"), N_("Surround"),
94                false )
95     add_integer_with_range( "mod-surround-level", 0, 0, 100, NULL,
96               N_("Surround level"), SURROUND_LEVEL_LONGTEXT, true )
97     add_integer_with_range( "mod-surround-delay", 5, 0, 1000, NULL,
98               N_("Surround delay (ms)"), SURROUND_DELAY_LONGTEXT, true )
99
100     set_callbacks( Open, Close )
101     add_shortcut( "mod" )
102 vlc_module_end ()
103
104 /*****************************************************************************
105  * Local prototypes
106  *****************************************************************************/
107
108 struct demux_sys_t
109 {
110     es_format_t  fmt;
111     es_out_id_t *es;
112
113     int64_t     i_time;
114     int64_t     i_length;
115
116     int         i_data;
117     uint8_t     *p_data;
118     ModPlugFile *f;
119 };
120
121 static int Demux  ( demux_t *p_demux );
122 static int Control( demux_t *p_demux, int i_query, va_list args );
123
124 static const char *ppsz_mod_ext[] =
125 {
126     "mod", "s3m", "xm",  "it",  "669", "amf", "ams", "dbm", "dmf", "dsm",
127     "far", "mdl", "med", "mtm", "okt", "ptm", "stm", "ult", "umx", "mt2",
128     "psm", NULL
129 };
130
131 /* We load the complete file in memory, put a higher bound
132  * of 500 Mo (which is really big anyway) */
133 #define MOD_MAX_FILE_SIZE (500*1000*1000)
134
135 /*****************************************************************************
136  * Open
137  *****************************************************************************/
138 static int Open( vlc_object_t *p_this )
139 {
140     demux_t     *p_demux = (demux_t*)p_this;
141     demux_sys_t *p_sys;
142     ModPlug_Settings settings;
143
144     /* We accept file based on extension match */
145     if( !p_demux->b_force )
146     {
147         const char *psz_ext = strrchr( p_demux->psz_path, '.' );
148         int i;
149
150         if( !psz_ext )
151             return VLC_EGENERIC;
152
153         psz_ext++;  /* skip . */
154         for( i = 0; ppsz_mod_ext[i] != NULL; i++ )
155         {
156             if( !strcasecmp( psz_ext, ppsz_mod_ext[i] ) )
157                 break;
158         }
159         if( ppsz_mod_ext[i] == NULL )
160             return VLC_EGENERIC;
161         msg_Dbg( p_demux, "running MOD demuxer (ext=%s)", ppsz_mod_ext[i] );
162     }
163
164     const int64_t i_size = stream_Size( p_demux->s );
165     if( i_size <= 0 || i_size >= MOD_MAX_FILE_SIZE )
166         return VLC_EGENERIC;
167
168     /* Fill p_demux field */
169     p_demux->pf_demux = Demux;
170     p_demux->pf_control = Control;
171     p_demux->p_sys = p_sys = malloc( sizeof( *p_sys ) );
172     if( !p_sys )
173         return VLC_ENOMEM;
174
175     msg_Dbg( p_demux, "loading complete file (could be long)" );
176     p_sys->i_data = i_size;
177     p_sys->p_data = malloc( p_sys->i_data );
178     if( p_sys->p_data )
179         p_sys->i_data = stream_Read( p_demux->s, p_sys->p_data, p_sys->i_data );
180     if( p_sys->i_data <= 0 || !p_sys->p_data )
181     {
182         msg_Err( p_demux, "failed to read the complete file" );
183         free( p_sys->p_data );
184         free( p_sys );
185         return VLC_EGENERIC;
186     }
187
188     /* Configure modplug before loading the file */
189     ModPlug_GetSettings( &settings );
190     settings.mFlags = MODPLUG_ENABLE_OVERSAMPLING;
191     settings.mChannels = 2;
192     settings.mBits = 16;
193     settings.mFrequency = 44100;
194     settings.mResamplingMode = MODPLUG_RESAMPLE_FIR;
195
196     if( var_CreateGetBool( p_demux, "mod-noisereduction" ) )
197         settings.mFlags |= MODPLUG_ENABLE_NOISE_REDUCTION;
198
199     if( var_CreateGetBool( p_demux, "mod-reverb" ) )
200         settings.mFlags |= MODPLUG_ENABLE_REVERB;
201     settings.mReverbDepth = var_CreateGetInteger( p_demux, "mod-reverb-level" );
202     settings.mReverbDelay = var_CreateGetInteger( p_demux, "mod-reverb-delay" );
203
204     if( var_CreateGetBool( p_demux, "mod-megabass" ) )
205         settings.mFlags |= MODPLUG_ENABLE_MEGABASS;
206     settings.mBassAmount = var_CreateGetInteger( p_demux, "mod-megabass-level" );
207     settings.mBassRange = var_CreateGetInteger( p_demux, "mod-megabass-range" );
208
209     if( var_CreateGetBool( p_demux, "mod-surround" ) )
210         settings.mFlags |= MODPLUG_ENABLE_SURROUND;
211     settings.mSurroundDepth = var_CreateGetInteger( p_demux, "mod-surround-level" );
212     settings.mSurroundDelay = var_CreateGetInteger( p_demux, "mod-surround-delay" );
213
214     ModPlug_SetSettings( &settings );
215
216     if( ( p_sys->f = ModPlug_Load( p_sys->p_data, p_sys->i_data ) ) == NULL )
217     {
218         msg_Err( p_demux, "failed to understand the file" );
219         /* we try to seek to recover for other plugin */
220         stream_Seek( p_demux->s, 0 );
221         free( p_sys->p_data );
222         free( p_sys );
223         return VLC_EGENERIC;
224     }
225
226     /* init time */
227     p_sys->i_time  = 1;
228     p_sys->i_length = ModPlug_GetLength( p_sys->f ) * INT64_C(1000);
229
230     msg_Dbg( p_demux, "MOD loaded name=%s lenght=%"PRId64"ms",
231              ModPlug_GetName( p_sys->f ),
232              p_sys->i_length );
233
234 #ifdef WORDS_BIGENDIAN
235     es_format_Init( &p_sys->fmt, AUDIO_ES, VLC_FOURCC( 't', 'w', 'o', 's' ) );
236 #else
237     es_format_Init( &p_sys->fmt, AUDIO_ES, VLC_FOURCC( 'a', 'r', 'a', 'w' ) );
238 #endif
239     p_sys->fmt.audio.i_rate = settings.mFrequency;
240     p_sys->fmt.audio.i_channels = settings.mChannels;
241     p_sys->fmt.audio.i_bitspersample = settings.mBits;
242     p_sys->es = es_out_Add( p_demux->out, &p_sys->fmt );
243
244     return VLC_SUCCESS;
245 }
246
247 /*****************************************************************************
248  * Close
249  *****************************************************************************/
250 static void Close( vlc_object_t *p_this )
251 {
252     demux_t     *p_demux = (demux_t*)p_this;
253     demux_sys_t *p_sys = p_demux->p_sys;
254
255     ModPlug_Unload( p_sys->f );
256     free( p_sys->p_data );
257     free( p_sys );
258 }
259
260
261 /*****************************************************************************
262  * Demux:
263  *****************************************************************************/
264 static int Demux( demux_t *p_demux )
265 {
266     demux_sys_t *p_sys = p_demux->p_sys;
267     block_t     *p_frame;
268     int         i_bk = ( p_sys->fmt.audio.i_bitspersample / 8 ) *
269                        p_sys->fmt.audio.i_channels;
270     int         i_read;
271
272     p_frame = block_New( p_demux, p_sys->fmt.audio.i_rate / 10 * i_bk );
273     if( !p_frame )
274         return -1;
275
276     i_read = ModPlug_Read( p_sys->f, p_frame->p_buffer, p_frame->i_buffer );
277     if( i_read <= 0 )
278     {
279         /* EOF */
280         block_Release( p_frame );
281         return 0;
282     }
283     p_frame->i_buffer = i_read;
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_C(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
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
349             return VLC_SUCCESS;
350         }
351         return VLC_EGENERIC;
352
353     case DEMUX_HAS_UNSUPPORTED_META:
354     {
355         bool *pb_bool = (bool*)va_arg( args, bool* );
356         *pb_bool = false; /* FIXME I am not sure of this one */
357         return VLC_SUCCESS;
358     }
359     case DEMUX_GET_META:
360     {
361         vlc_meta_t *p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t* );
362         const char *psz_name = ModPlug_GetName( p_sys->f );
363         if( psz_name && *psz_name )
364             vlc_meta_SetTitle( p_meta, psz_name );
365         return VLC_SUCCESS;
366     }
367
368     case DEMUX_GET_FPS: /* meaningless */
369     default:
370         return VLC_EGENERIC;
371     }
372 }
373