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