Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

mod.c

00001 /*****************************************************************************
00002  * mod.c: MOD file demuxer (using libmodplug)
00003  *****************************************************************************
00004  * Copyright (C) 2004 the VideoLAN team
00005  * $Id: mod.c 11664 2005-07-09 06:17:09Z courmisch $
00006  *
00007  * Authors: Laurent Aimar <[email protected]>
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00022  *****************************************************************************/
00023 
00024 /*****************************************************************************
00025  * Preamble
00026  *****************************************************************************/
00027 #include <stdlib.h>                                      /* malloc(), free() */
00028 
00029 #include <vlc/vlc.h>
00030 #include <vlc/input.h>
00031 
00032 #include <libmodplug/modplug.h>
00033 
00034 /* TODO:
00035  *  - extend demux control to query meta data (demuxer should NEVER touch
00036  *      playlist itself)
00037  *  - FIXME test endian of samples
00038  *  - ...
00039  */
00040 
00041 /*****************************************************************************
00042  * Module descriptor
00043  *****************************************************************************/
00044 static int  Open    ( vlc_object_t * );
00045 static void Close  ( vlc_object_t * );
00046 
00047 vlc_module_begin();
00048     set_shortname( "MOD");
00049     set_description( _("MOD demuxer (libmodplug)" ) );
00050     set_capability( "demux2", 10 );
00051     set_category( CAT_INPUT );
00052     set_subcategory( SUBCAT_INPUT_DEMUX );
00053 
00054     add_bool( "mod-noisereduction", VLC_TRUE, NULL, N_("Noise reduction"), N_("Noise reduction"), VLC_FALSE );
00055 
00056     add_bool( "mod-reverb", VLC_FALSE, NULL, N_("Reverb"), N_("Reverb"), VLC_FALSE );
00057     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 );
00058     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 );
00059 
00060     add_bool( "mod-megabass", VLC_FALSE, NULL, N_("Mega bass"), N_("Mega bass"), VLC_FALSE );
00061     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 );
00062     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 );
00063 
00064     add_bool( "mod-surround", VLC_FALSE, NULL, N_("Surround"), N_("Surround"), VLC_FALSE );
00065     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 );
00066     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 );
00067 
00068     set_callbacks( Open, Close );
00069     add_shortcut( "mod" );
00070 vlc_module_end();
00071 
00072 /*****************************************************************************
00073  * Local prototypes
00074  *****************************************************************************/
00075 
00076 struct demux_sys_t
00077 {
00078     es_format_t  fmt;
00079     es_out_id_t *es;
00080 
00081     int64_t     i_time;
00082     int64_t     i_length;
00083 
00084     int         i_data;
00085     uint8_t     *p_data;
00086     ModPlugFile *f;
00087 };
00088 
00089 static int Demux  ( demux_t *p_demux );
00090 static int Control( demux_t *p_demux, int i_query, va_list args );
00091 
00092 static const char* mod_ext[] =
00093 {
00094     "mod", "s3m", "xm",  "it",  "669", "amf", "ams", "dbm", "dmf", "dsm",
00095     "far", "mdl", "med", "mtm", "okt", "ptm", "stm", "ult", "umx", "mt2",
00096     "psm", NULL
00097 };
00098 
00099 /*****************************************************************************
00100  * Open
00101  *****************************************************************************/
00102 static int Open( vlc_object_t *p_this )
00103 {
00104     demux_t     *p_demux = (demux_t*)p_this;
00105     demux_sys_t *p_sys;
00106     char        *ext;
00107     int         i;
00108     ModPlug_Settings settings;
00109     vlc_value_t val;
00110 
00111     /* We accept file based on extention match */
00112     if( strcasecmp( p_demux->psz_demux, "mod" ) )
00113     {
00114         if( ( ext = strchr( p_demux->psz_path, '.' ) ) == NULL ||
00115             stream_Size( p_demux->s ) == 0 ) return VLC_EGENERIC;
00116 
00117         ext++;  /* skip . */
00118         for( i = 0; mod_ext[i] != NULL; i++ )
00119         {
00120             if( !strcasecmp( ext, mod_ext[i] ) )
00121             {
00122                 break;
00123             }
00124         }
00125         if( mod_ext[i] == NULL ) return VLC_EGENERIC;
00126         msg_Dbg( p_demux, "running MOD demuxer (ext=%s)", mod_ext[i] );
00127     }
00128 
00129     /* Fill p_demux field */
00130     p_demux->pf_demux = Demux;
00131     p_demux->pf_control = Control;
00132     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
00133 
00134     msg_Dbg( p_demux, "loading complete file (could be long)" );
00135     p_sys->i_data = stream_Size( p_demux->s );
00136     p_sys->p_data = malloc( p_sys->i_data );
00137     p_sys->i_data = stream_Read( p_demux->s, p_sys->p_data, p_sys->i_data );
00138     if( p_sys->i_data <= 0 )
00139     {
00140         msg_Err( p_demux, "failed to read the complete file" );
00141         free( p_sys->p_data );
00142         free( p_sys );
00143         return VLC_EGENERIC;
00144     }
00145     /* Create our config variable */
00146     var_Create( p_demux, "mod-noisereduction", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
00147     var_Create( p_demux, "mod-reverb", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
00148     var_Create( p_demux, "mod-reverb-level", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
00149     var_Create( p_demux, "mod-reverb-delay", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
00150     var_Create( p_demux, "mod-megabass", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
00151     var_Create( p_demux, "mod-megabass-level", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
00152     var_Create( p_demux, "mod-megabass-range", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
00153     var_Create( p_demux, "mod-surround", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
00154     var_Create( p_demux, "mod-surround-level", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
00155     var_Create( p_demux, "mod-surround-delay", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
00156 
00157     /* Configure modplug before loading the file */
00158     ModPlug_GetSettings( &settings );
00159     settings.mFlags = MODPLUG_ENABLE_OVERSAMPLING;
00160     settings.mChannels = 2;
00161     settings.mBits = 16;
00162     settings.mFrequency = 44100;
00163     settings.mResamplingMode = MODPLUG_RESAMPLE_FIR;
00164 
00165     var_Get( p_demux, "mod-noisereduction", &val );
00166     if( val.b_bool) settings.mFlags |= MODPLUG_ENABLE_NOISE_REDUCTION;
00167 
00168     var_Get( p_demux, "mod-reverb", &val );
00169     if( val.b_bool) settings.mFlags |= MODPLUG_ENABLE_REVERB;
00170     var_Get( p_demux, "mod-reverb-level", &val );
00171     settings.mReverbDepth = val.i_int;
00172     var_Get( p_demux, "mod-reverb-delay", &val );
00173     settings.mReverbDelay = val.i_int;
00174 
00175     var_Get( p_demux, "mod-megabass", &val );
00176     if( val.b_bool) settings.mFlags |= MODPLUG_ENABLE_MEGABASS;
00177     var_Get( p_demux, "mod-megabass-level", &val );
00178     settings.mBassAmount = val.i_int;
00179     var_Get( p_demux, "mod-megabass-range", &val );
00180     settings.mBassRange = val.i_int;
00181 
00182     var_Get( p_demux, "mod-surround", &val );
00183     if( val.b_bool) settings.mFlags |= MODPLUG_ENABLE_SURROUND;
00184     var_Get( p_demux, "mod-surround-level", &val );
00185     settings.mSurroundDepth = val.i_int;
00186     var_Get( p_demux, "mod-surround-delay", &val );
00187     settings.mSurroundDelay = val.i_int;
00188 
00189     ModPlug_SetSettings( &settings );
00190 
00191     if( ( p_sys->f = ModPlug_Load( p_sys->p_data, p_sys->i_data ) ) == NULL )
00192     {
00193         msg_Err( p_demux, "failed to understand the file" );
00194         /* we try to seek to recover for other plugin */
00195         stream_Seek( p_demux->s, 0 );
00196         free( p_sys->p_data );
00197         free( p_sys );
00198         return VLC_EGENERIC;
00199     }
00200 
00201     /* init time */
00202     p_sys->i_time  = 1;
00203     p_sys->i_length = ModPlug_GetLength( p_sys->f ) * (int64_t)1000;
00204 
00205     msg_Dbg( p_demux, "MOD loaded name=%s lenght="I64Fd"ms",
00206              ModPlug_GetName( p_sys->f ),
00207              p_sys->i_length );
00208 
00209 #ifdef WORDS_BIGENDIAN
00210     es_format_Init( &p_sys->fmt, AUDIO_ES, VLC_FOURCC( 't', 'w', 'o', 's' ) );
00211 #else
00212     es_format_Init( &p_sys->fmt, AUDIO_ES, VLC_FOURCC( 'a', 'r', 'a', 'w' ) );
00213 #endif
00214     p_sys->fmt.audio.i_rate = settings.mFrequency;
00215     p_sys->fmt.audio.i_channels = settings.mChannels;
00216     p_sys->fmt.audio.i_bitspersample = settings.mBits;
00217     p_sys->es = es_out_Add( p_demux->out, &p_sys->fmt );
00218 
00219     return VLC_SUCCESS;
00220 }
00221 
00222 /*****************************************************************************
00223  * Close
00224  *****************************************************************************/
00225 static void Close( vlc_object_t *p_this )
00226 {
00227     demux_t     *p_demux = (demux_t*)p_this;
00228     demux_sys_t *p_sys = p_demux->p_sys;
00229 
00230     ModPlug_Unload( p_sys->f );
00231     free( p_sys->p_data );
00232     free( p_sys );
00233 }
00234 
00235 
00236 /*****************************************************************************
00237  * Demux:
00238  *****************************************************************************/
00239 static int Demux( demux_t *p_demux )
00240 {
00241     demux_sys_t *p_sys = p_demux->p_sys;
00242     block_t     *p_frame;
00243     int         i_bk = ( p_sys->fmt.audio.i_bitspersample / 8 ) *
00244                        p_sys->fmt.audio.i_channels;
00245 
00246     p_frame = block_New( p_demux, p_sys->fmt.audio.i_rate / 10 * i_bk );
00247 
00248     p_frame->i_buffer = ModPlug_Read( p_sys->f, p_frame->p_buffer, p_frame->i_buffer );
00249     if( p_frame->i_buffer <= 0 )
00250     {
00251         /* EOF */
00252         block_Release( p_frame );
00253         return 0;
00254     }
00255 
00256     /* Set PCR */
00257     es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_sys->i_time );
00258 
00259     /* We should use p_frame->i_buffer */
00260     p_sys->i_time += (int64_t)1000000 * p_frame->i_buffer / i_bk / p_sys->fmt.audio.i_rate;
00261 
00262     /* Send data */
00263     p_frame->i_dts = p_frame->i_pts = p_sys->i_time;
00264     es_out_Send( p_demux->out, p_sys->es, p_frame );
00265 
00266     return 1;
00267 }
00268 
00269 /*****************************************************************************
00270  * Control:
00271  *****************************************************************************/
00272 static int Control( demux_t *p_demux, int i_query, va_list args )
00273 {
00274     demux_sys_t *p_sys = p_demux->p_sys;
00275     double f, *pf;
00276     int64_t i64, *pi64;
00277 
00278     switch( i_query )
00279     {
00280         case DEMUX_GET_POSITION:
00281             pf = (double*) va_arg( args, double* );
00282             if( p_sys->i_length > 0 )
00283             {
00284                 *pf = (double)p_sys->i_time / (double)p_sys->i_length;
00285                 return VLC_SUCCESS;
00286             }
00287             return VLC_EGENERIC;
00288 
00289         case DEMUX_SET_POSITION:
00290             f = (double) va_arg( args, double );
00291 
00292             i64 = f * p_sys->i_length;
00293             if( i64 >= 0 && i64 <= p_sys->i_length )
00294             {
00295                 ModPlug_Seek( p_sys->f, i64 / 1000 );
00296                 p_sys->i_time = i64 + 1;
00297                 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
00298 
00299                 return VLC_SUCCESS;
00300             }
00301             return VLC_EGENERIC;
00302 
00303         case DEMUX_GET_TIME:
00304             pi64 = (int64_t*)va_arg( args, int64_t * );
00305             *pi64 = p_sys->i_time;
00306             return VLC_SUCCESS;
00307 
00308         case DEMUX_GET_LENGTH:
00309             pi64 = (int64_t*)va_arg( args, int64_t * );
00310             *pi64 = p_sys->i_length;
00311             return VLC_SUCCESS;
00312 
00313         case DEMUX_SET_TIME:
00314             i64 = (int64_t)va_arg( args, int64_t );
00315 
00316             if( i64 >= 0 && i64 <= p_sys->i_length )
00317             {
00318                 ModPlug_Seek( p_sys->f, i64 / 1000 );
00319                 p_sys->i_time = i64 + 1;
00320                 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
00321 
00322                 return VLC_SUCCESS;
00323             }
00324             return VLC_EGENERIC;
00325 
00326         case DEMUX_GET_FPS: /* meaningless */
00327         default:
00328             return VLC_EGENERIC;
00329     }
00330 }
00331 

Generated on Tue Dec 20 10:14:34 2005 for vlc-0.8.4a by  doxygen 1.4.2