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

mpc.c

00001 /*****************************************************************************
00002  * mpc.c : MPC stream input module for vlc
00003  *****************************************************************************
00004  * Copyright (C) 2001 the VideoLAN team
00005  * $Id: a52.c 11699 2005-07-11 08:52:14Z sam $
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 <vlc/vlc.h>
00028 #include <vlc/input.h>
00029 #include <vlc_codec.h>
00030 #include <math.h>
00031 
00032 #include <mpcdec/mpcdec.h>
00033 
00034 /* TODO:
00035  *  - test stream version 4..6
00036  *  - test fixed float version
00037  *  - ...
00038  *
00039  *  XXX:
00040  *  It is done the ugly way (the demux does the decode stage... but it works
00041  */
00042 
00043 /*****************************************************************************
00044  * Module descriptor
00045  *****************************************************************************/
00046 #define REPLAYGAIN_TYPE_TEXT N_("Replay Gain type" )
00047 #define REPLAYGAIN_TYPE_LONGTEXT N_( "Replay Gain type" )
00048 
00049 static int  Open  ( vlc_object_t * );
00050 static void Close ( vlc_object_t * );
00051 
00052 static int  pi_replaygain_type[] = { 0, 1, 2 };
00053 static char *ppsz_replaygain_type[] = { N_("None"), N_("Title"), N_("Album") };
00054 
00055 vlc_module_begin();
00056     set_shortname( "MPC" );
00057     set_description( _("MPC demuxer") );
00058     set_category( CAT_INPUT );
00059     set_subcategory( SUBCAT_INPUT_DEMUX );
00060     set_capability( "demux2", 145 );
00061 
00062     add_integer( "mpc-replaygain-type", 2, NULL,
00063                 REPLAYGAIN_TYPE_TEXT, REPLAYGAIN_TYPE_LONGTEXT, VLC_FALSE );
00064         change_integer_list( pi_replaygain_type, ppsz_replaygain_type, 0 );
00065 
00066     set_callbacks( Open, Close );
00067     add_shortcut( "mpc" );
00068 vlc_module_end();
00069 
00070 /*****************************************************************************
00071  * Local prototypes
00072  *****************************************************************************/
00073 static int Demux  ( demux_t * );
00074 static int Control( demux_t *, int, va_list );
00075 
00076 struct demux_sys_t
00077 {
00078     /* */
00079     es_out_id_t *p_es;
00080 
00081     /* */
00082     mpc_decoder    decoder;
00083     mpc_reader     reader;
00084     mpc_streaminfo info;
00085 
00086     /* */
00087     vlc_meta_t     *p_meta;
00088     int64_t        i_position;
00089 };
00090 
00091 mpc_int32_t ReaderRead( void *p_private, void *dst, mpc_int32_t i_size );
00092 mpc_bool_t  ReaderSeek( void *p_private, mpc_int32_t i_offset );
00093 mpc_int32_t ReaderTell( void *p_private);
00094 mpc_int32_t ReaderGetSize( void *p_private );
00095 mpc_bool_t  ReaderCanSeek( void *p_private );
00096 
00097 /*****************************************************************************
00098  * Open: initializes ES structures
00099  *****************************************************************************/
00100 static int Open( vlc_object_t * p_this )
00101 {
00102     demux_t     *p_demux = (demux_t*)p_this;
00103     demux_sys_t *p_sys;
00104     char        psz_info[4096];
00105     es_format_t fmt;
00106     uint8_t     *p_peek;
00107     module_t    *p_id3;
00108 
00109     if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
00110         return VLC_EGENERIC;
00111 
00112     if( memcmp( p_peek, "MP+", 3 ) )
00113     {
00114         /* for v4..6 we check extention file */
00115         const int i_version = (GetDWLE( p_peek ) >> 11)&0x3ff;
00116 
00117         if( i_version  < 4 || i_version > 6 )
00118             return VLC_EGENERIC;
00119 
00120         if( !p_demux->psz_demux || strcmp( p_demux->psz_demux, "mpc" ) )
00121         {
00122             /* Check file name extension */
00123             int i_len;
00124             if( !p_demux->psz_path )
00125                 return VLC_EGENERIC;
00126 
00127             i_len = strlen( p_demux->psz_path );
00128             if( i_len < 4 ||
00129                 ( strcasecmp( &p_demux->psz_path[i_len-4], ".mpc" ) &&
00130                   strcasecmp( &p_demux->psz_path[i_len-4], ".mp+" ) &&
00131                   strcasecmp( &p_demux->psz_path[i_len-4], ".mpp" ) ) )
00132                 return VLC_EGENERIC;
00133         }
00134     }
00135 
00136     /* */
00137     p_sys = malloc( sizeof( demux_sys_t ) );
00138     memset( p_sys, 0, sizeof(demux_sys_t) );
00139 
00140     p_sys->i_position = 0;
00141 
00142     p_sys->reader.read = ReaderRead;
00143     p_sys->reader.seek = ReaderSeek;
00144     p_sys->reader.tell = ReaderTell;
00145     p_sys->reader.get_size = ReaderGetSize;
00146     p_sys->reader.canseek = ReaderCanSeek;
00147     p_sys->reader.data = p_demux;
00148 
00149     /* Load info */
00150     mpc_streaminfo_init( &p_sys->info );
00151     if( mpc_streaminfo_read( &p_sys->info, &p_sys->reader ) != ERROR_CODE_OK )
00152     {
00153         /* invalid file */
00154         free( p_sys );
00155         return VLC_EGENERIC;
00156     }
00157 
00158     /* */
00159     mpc_decoder_setup( &p_sys->decoder, &p_sys->reader );
00160     if( !mpc_decoder_initialize( &p_sys->decoder, &p_sys->info ) )
00161     {
00162         /* */
00163         free( p_sys );
00164         return VLC_EGENERIC;
00165     }
00166 
00167     /* Handle reaply gain */
00168     if( p_sys->info.peak_title != 32767 )
00169     {
00170         int i_type = var_CreateGetInteger( p_demux, "mpc-replaygain-type" );
00171         int gain;
00172         int peak;
00173 
00174         if( i_type == 2 )       // album
00175         {
00176             gain = p_sys->info.gain_album;
00177             peak = p_sys->info.peak_album;
00178         }
00179         else if( i_type == 1 )  // title
00180         {
00181             gain = p_sys->info.gain_title;
00182             peak = p_sys->info.peak_title;
00183         }
00184         else
00185         {
00186             gain = 0;
00187             peak = 0;
00188         }
00189 
00190         if( gain )
00191         {
00192             double g = pow( 10, (double)gain / 2000.0 );
00193             double gmax = (double)32767.0 / (peak+1);
00194             if( g > gmax )
00195                 g = gmax;
00196 
00197             msg_Dbg( p_demux, "Using reaply gain factor %f", g );
00198             mpc_decoder_scale_output( &p_sys->decoder, g );
00199         }
00200     }
00201 
00202     /* Fill p_demux fields */
00203     p_demux->pf_demux = Demux;
00204     p_demux->pf_control = Control;
00205     p_demux->p_sys = p_sys;
00206 
00207     /* */
00208 #ifndef MPC_FIXED_POINT
00209     es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'f', 'l', '3', '2' ) );
00210 #else
00211 #   ifdef WORDS_BIGENDIAN
00212     es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 's', '3', '2', 'b' ) );
00213 #   else
00214     es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 's', '3', '2', 'l' ) );
00215 #   endif
00216 #endif
00217     fmt.audio.i_channels = p_sys->info.channels;
00218     fmt.audio.i_rate = p_sys->info.sample_freq;
00219     fmt.audio.i_blockalign = 4*fmt.audio.i_channels;
00220     fmt.audio.i_bitspersample = 32;
00221     fmt.i_bitrate = fmt.i_bitrate * fmt.audio.i_channels *
00222                     fmt.audio.i_bitspersample;
00223     p_sys->p_es = es_out_Add( p_demux->out, &fmt );
00224 
00225 
00226     /* Parse possible id3 header */
00227     if( ( p_id3 = module_Need( p_demux, "id3", NULL, 0 ) ) )
00228     {
00229         p_sys->p_meta = (vlc_meta_t *)p_demux->p_private;
00230         p_demux->p_private = NULL;
00231         module_Unneed( p_demux, p_id3 );
00232     }
00233 
00234     if( !p_sys->p_meta )
00235         p_sys->p_meta = vlc_meta_New();
00236 
00237     sprintf( psz_info, "Musepack v%d", p_sys->info.stream_version );
00238     vlc_meta_Add( p_sys->p_meta, VLC_META_CODEC_NAME, psz_info );
00239 
00240     return VLC_SUCCESS;
00241 }
00242 
00243 /*****************************************************************************
00244  * Close: frees unused data
00245  *****************************************************************************/
00246 static void Close( vlc_object_t * p_this )
00247 {
00248     demux_t        *p_demux = (demux_t*)p_this;
00249     demux_sys_t    *p_sys = p_demux->p_sys;
00250 
00251     free( p_sys );
00252 }
00253 
00254 /*****************************************************************************
00255  * Demux:
00256  *****************************************************************************
00257  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
00258  *****************************************************************************/
00259 static int Demux( demux_t *p_demux )
00260 {
00261     demux_sys_t *p_sys = p_demux->p_sys;
00262     block_t     *p_data;
00263     int i_ret;
00264 
00265     p_data = block_New( p_demux,
00266                         MPC_DECODER_BUFFER_LENGTH*sizeof(MPC_SAMPLE_FORMAT) );
00267     i_ret = mpc_decoder_decode( &p_sys->decoder,
00268                                (MPC_SAMPLE_FORMAT*)p_data->p_buffer,
00269                                NULL, NULL );
00270     if( i_ret <= 0 )
00271     {
00272         block_Release( p_data );
00273         return i_ret < 0 ? -1 : 0;
00274     }
00275 
00276     /* */
00277     p_data->i_buffer = i_ret * sizeof(MPC_SAMPLE_FORMAT) * p_sys->info.channels;
00278     p_data->i_dts = p_data->i_pts =
00279             1 + I64C(1000000) * p_sys->i_position / p_sys->info.sample_freq;
00280 
00281     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_data->i_dts );
00282 
00283     es_out_Send( p_demux->out, p_sys->p_es, p_data );
00284 
00285     /* */
00286     p_sys->i_position += i_ret;
00287 
00288     return 1;
00289 }
00290 
00291 /*****************************************************************************
00292  * Control:
00293  *****************************************************************************/
00294 static int Control( demux_t *p_demux, int i_query, va_list args )
00295 {
00296     demux_sys_t *p_sys = p_demux->p_sys;
00297     double   f, *pf;
00298     int64_t i64, *pi64;
00299     vlc_meta_t **pp_meta;
00300 
00301     switch( i_query )
00302     {
00303         case DEMUX_GET_META:
00304             pp_meta = (vlc_meta_t **)va_arg( args, vlc_meta_t** );
00305             if( p_sys->p_meta )
00306                 *pp_meta = vlc_meta_Duplicate( p_sys->p_meta );
00307             else
00308                 *pp_meta = NULL;
00309             return VLC_SUCCESS;
00310 
00311         case DEMUX_GET_LENGTH:
00312             pi64 = (int64_t*)va_arg( args, int64_t * );
00313             *pi64 = I64C(1000000) * p_sys->info.pcm_samples /
00314                         p_sys->info.sample_freq;
00315             return VLC_SUCCESS;
00316 
00317         case DEMUX_GET_POSITION:
00318             pf = (double*)va_arg( args, double * );
00319             if( p_sys->info.pcm_samples > 0 )
00320                 *pf = (double) p_sys->i_position /
00321                       (double)p_sys->info.pcm_samples;
00322             else
00323                 *pf = 0.0;
00324             return VLC_SUCCESS;
00325 
00326         case DEMUX_GET_TIME:
00327             pi64 = (int64_t*)va_arg( args, int64_t * );
00328             *pi64 = I64C(1000000) * p_sys->i_position /
00329                         p_sys->info.sample_freq;
00330             return VLC_SUCCESS;
00331 
00332         case DEMUX_SET_POSITION:
00333             f = (double)va_arg( args, double );
00334             i64 = (int64_t)(f * p_sys->info.pcm_samples);
00335             if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
00336             {
00337                 p_sys->i_position = i64;
00338                 return VLC_SUCCESS;
00339             }
00340             return VLC_EGENERIC;
00341 
00342         case DEMUX_SET_TIME:
00343             i64 = (int64_t)va_arg( args, int64_t );
00344             if( mpc_decoder_seek_sample( &p_sys->decoder, i64 ) )
00345             {
00346                 p_sys->i_position = i64;
00347                 return VLC_SUCCESS;
00348             }
00349             return VLC_EGENERIC;
00350 
00351         default:
00352             return VLC_EGENERIC;
00353     }
00354 }
00355 
00356 mpc_int32_t ReaderRead( void *p_private, void *dst, mpc_int32_t i_size )
00357 {
00358     demux_t *p_demux = (demux_t*)p_private;
00359     return stream_Read( p_demux->s, dst, i_size );
00360 }
00361 
00362 mpc_bool_t ReaderSeek( void *p_private, mpc_int32_t i_offset )
00363 {
00364     demux_t *p_demux = (demux_t*)p_private;
00365     return !stream_Seek( p_demux->s, i_offset );
00366 }
00367 
00368 mpc_int32_t ReaderTell( void *p_private)
00369 {
00370     demux_t *p_demux = (demux_t*)p_private;
00371     return stream_Tell( p_demux->s );
00372 }
00373 
00374 mpc_int32_t ReaderGetSize( void *p_private )
00375 {
00376     demux_t *p_demux = (demux_t*)p_private;
00377     return stream_Size( p_demux->s );
00378 }
00379 
00380 mpc_bool_t ReaderCanSeek( void *p_private )
00381 {
00382     demux_t *p_demux = (demux_t*)p_private;
00383     vlc_bool_t b_canseek;
00384 
00385     stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_canseek );
00386     return b_canseek;
00387 }
00388 

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