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

aiff.c

00001 /*****************************************************************************
00002  * aiff.c: Audio Interchange File Format demuxer
00003  *****************************************************************************
00004  * Copyright (C) 2004 the VideoLAN team
00005  * $Id: aiff.c 11709 2005-07-11 16:20:33Z massiot $
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 /* TODO:
00033  *  - ...
00034  */
00035 
00036 /*****************************************************************************
00037  * Module descriptor
00038  *****************************************************************************/
00039 static int  Open    ( vlc_object_t * );
00040 static void Close  ( vlc_object_t * );
00041 
00042 vlc_module_begin();
00043     set_category( CAT_INPUT );
00044     set_subcategory( SUBCAT_INPUT_DEMUX );
00045     set_description( _("AIFF demuxer" ) );
00046     set_capability( "demux2", 10 );
00047     set_callbacks( Open, Close );
00048     add_shortcut( "aiff" );
00049 vlc_module_end();
00050 
00051 /*****************************************************************************
00052  * Local prototypes
00053  *****************************************************************************/
00054 
00055 struct demux_sys_t
00056 {
00057     es_format_t  fmt;
00058     es_out_id_t *es;
00059 
00060     int64_t     i_ssnd_pos;
00061     int64_t     i_ssnd_size;
00062     int         i_ssnd_offset;
00063     int         i_ssnd_blocksize;
00064 
00065     /* real data start */
00066     int64_t     i_ssnd_start;
00067     int64_t     i_ssnd_end;
00068 
00069     int         i_ssnd_fsize;
00070 
00071     int64_t     i_time;
00072 };
00073 
00074 static int Demux  ( demux_t *p_demux );
00075 static int Control( demux_t *p_demux, int i_query, va_list args );
00076 
00077 /* GetF80BE: read a 80 bits float in big endian */
00078 static unsigned int GetF80BE( uint8_t p[10] )
00079 {
00080     unsigned int i_mantissa = GetDWBE( &p[2] );
00081     int          i_exp = 30 - p[1];
00082     unsigned int i_last = 0;
00083 
00084     while( i_exp-- )
00085     {
00086         i_last = i_mantissa;
00087         i_mantissa >>= 1;
00088     }
00089     if( i_last&0x01 )
00090     {
00091         i_mantissa++;
00092     }
00093     return i_mantissa;
00094 }
00095 
00096 /*****************************************************************************
00097  * Open
00098  *****************************************************************************/
00099 static int Open( vlc_object_t *p_this )
00100 {
00101     demux_t     *p_demux = (demux_t*)p_this;
00102     demux_sys_t *p_sys;
00103 
00104     uint8_t     *p_peek;
00105 
00106     if( stream_Peek( p_demux->s, &p_peek, 12 ) < 12 ) return VLC_EGENERIC;
00107     if( strncmp( (char *)&p_peek[0], "FORM", 4 ) || strncmp( (char *)&p_peek[8], "AIFF", 4 ) )
00108     {
00109         return VLC_EGENERIC;
00110     }
00111 
00112     /* skip aiff header */
00113     stream_Read( p_demux->s, NULL, 12 );
00114 
00115     /* Fill p_demux field */
00116     p_demux->pf_demux = Demux;
00117     p_demux->pf_control = Control;
00118     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
00119 
00120     es_format_Init( &p_sys->fmt, UNKNOWN_ES, 0 );
00121     p_sys->i_time = 1;
00122     p_sys->i_ssnd_pos = -1;
00123 
00124     for( ;; )
00125     {
00126         uint32_t i_size;
00127 
00128         if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
00129         {
00130             msg_Dbg( p_demux, "cannot peek()" );
00131             goto error;
00132         }
00133         i_size = GetDWBE( &p_peek[4] );
00134 
00135         msg_Dbg( p_demux, "chunk fcc=%4.4s size=%d", p_peek, i_size );
00136 
00137         if( !strncmp( (char *)&p_peek[0], "COMM", 4 ) )
00138         {
00139             if( stream_Peek( p_demux->s, &p_peek, 18 + 8 ) < 18 + 8 )
00140             {
00141                 msg_Dbg( p_demux, "cannot peek()" );
00142                 goto error;
00143             }
00144             es_format_Init( &p_sys->fmt, AUDIO_ES, VLC_FOURCC( 't', 'w', 'o', 's' ) );
00145             p_sys->fmt.audio.i_channels = GetWBE( &p_peek[8] );
00146             p_sys->fmt.audio.i_bitspersample = GetWBE( &p_peek[14] );
00147             p_sys->fmt.audio.i_rate     = GetF80BE( &p_peek[16] );
00148 
00149             msg_Dbg( p_demux, "COMM: channels=%d samples_frames=%d bits=%d rate=%d",
00150                      GetWBE( &p_peek[8] ), GetDWBE( &p_peek[10] ), GetWBE( &p_peek[14] ), GetF80BE( &p_peek[16] ) );
00151         }
00152         else if( !strncmp( (char *)&p_peek[0], "SSND", 4 ) )
00153         {
00154             if( stream_Peek( p_demux->s, &p_peek, 8 + 8 ) < 8 + 8 )
00155             {
00156                 msg_Dbg( p_demux, "cannot peek()" );
00157                 goto error;
00158             }
00159 
00160             p_sys->i_ssnd_pos = stream_Tell( p_demux->s );
00161             p_sys->i_ssnd_size = i_size;
00162             p_sys->i_ssnd_offset = GetDWBE( &p_peek[8] );
00163             p_sys->i_ssnd_blocksize = GetDWBE( &p_peek[12] );
00164 
00165             msg_Dbg( p_demux, "SSND: (offset=%d blocksize=%d)",
00166                      p_sys->i_ssnd_offset, p_sys->i_ssnd_blocksize );
00167         }
00168         if( p_sys->i_ssnd_pos >= 12 && p_sys->fmt.i_cat == AUDIO_ES )
00169         {
00170             /* We have found the 2 needed chunks */
00171             break;
00172         }
00173 
00174         /* Skip this chunk */
00175         i_size += 8;
00176         if( stream_Read( p_demux->s, NULL, i_size ) != (int)i_size )
00177         {
00178             msg_Warn( p_demux, "incomplete file" );
00179             goto error;
00180         }
00181     }
00182 
00183     p_sys->i_ssnd_start = p_sys->i_ssnd_pos + 16 + p_sys->i_ssnd_offset;
00184     p_sys->i_ssnd_end   = p_sys->i_ssnd_start + p_sys->i_ssnd_size;
00185 
00186     p_sys->i_ssnd_fsize = p_sys->fmt.audio.i_channels *
00187                           ( p_sys->fmt.audio.i_bitspersample + 7 ) / 8;
00188 
00189     if( p_sys->i_ssnd_fsize <= 0 )
00190     {
00191         msg_Err( p_demux, "invalid audio parameters" );
00192         goto error;
00193     }
00194 
00195     if( p_sys->i_ssnd_size <= 0 )
00196     {
00197         /* unknown */
00198         p_sys->i_ssnd_end = 0;
00199     }
00200 
00201     /* seek into SSND chunk */
00202     if( stream_Seek( p_demux->s, p_sys->i_ssnd_start ) )
00203     {
00204         msg_Err( p_demux, "cannot seek to data chunk" );
00205         goto error;
00206     }
00207 
00208     /* */
00209     p_sys->es = es_out_Add( p_demux->out, &p_sys->fmt );
00210 
00211     return VLC_SUCCESS;
00212 
00213 error:
00214     free( p_sys );
00215     return VLC_EGENERIC;
00216 }
00217 
00218 /*****************************************************************************
00219  * Close
00220  *****************************************************************************/
00221 static void Close( vlc_object_t *p_this )
00222 {
00223     demux_t     *p_demux = (demux_t*)p_this;
00224     demux_sys_t *p_sys = p_demux->p_sys;
00225 
00226     free( p_sys );
00227 }
00228 
00229 
00230 /*****************************************************************************
00231  * Demux:
00232  *****************************************************************************/
00233 static int Demux( demux_t *p_demux )
00234 {
00235     demux_sys_t *p_sys = p_demux->p_sys;
00236     int64_t     i_tell = stream_Tell( p_demux->s );
00237 
00238     block_t     *p_block;
00239     int         i_read;
00240 
00241     if( p_sys->i_ssnd_end > 0 && i_tell >= p_sys->i_ssnd_end )
00242     {
00243         /* EOF */
00244         return 0;
00245     }
00246 
00247     /* Set PCR */
00248     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time);
00249 
00250     /* we will read 100ms at once */
00251     i_read = p_sys->i_ssnd_fsize * ( p_sys->fmt.audio.i_rate / 10 );
00252     if( p_sys->i_ssnd_end > 0 && p_sys->i_ssnd_end - i_tell < i_read )
00253     {
00254         i_read = p_sys->i_ssnd_end - i_tell;
00255     }
00256     if( ( p_block = stream_Block( p_demux->s, i_read ) ) == NULL )
00257     {
00258         return 0;
00259     }
00260 
00261     p_block->i_dts =
00262     p_block->i_pts = p_sys->i_time;
00263 
00264     p_sys->i_time += (int64_t)1000000 *
00265                      p_block->i_buffer /
00266                      p_sys->i_ssnd_fsize /
00267                      p_sys->fmt.audio.i_rate;
00268 
00269     /* */
00270     es_out_Send( p_demux->out, p_sys->es, p_block );
00271     return 1;
00272 }
00273 
00274 /*****************************************************************************
00275  * Control:
00276  *****************************************************************************/
00277 static int Control( demux_t *p_demux, int i_query, va_list args )
00278 {
00279     demux_sys_t *p_sys = p_demux->p_sys;
00280     double f, *pf;
00281     int64_t *pi64;
00282 
00283     switch( i_query )
00284     {
00285         case DEMUX_GET_POSITION:
00286         {
00287             int64_t i_start = p_sys->i_ssnd_start;
00288             int64_t i_end   = p_sys->i_ssnd_end > 0 ? p_sys->i_ssnd_end : stream_Size( p_demux->s );
00289             int64_t i_tell  = stream_Tell( p_demux->s );
00290 
00291             pf = (double*) va_arg( args, double* );
00292 
00293             if( i_start < i_end )
00294             {
00295                 *pf = (double)(i_tell - i_start)/(double)(i_end - i_start);
00296                 return VLC_SUCCESS;
00297             }
00298             return VLC_EGENERIC;
00299         }
00300 
00301         case DEMUX_SET_POSITION:
00302         {
00303             int64_t i_start = p_sys->i_ssnd_start;
00304             int64_t i_end  = p_sys->i_ssnd_end > 0 ? p_sys->i_ssnd_end : stream_Size( p_demux->s );
00305 
00306             f = (double) va_arg( args, double );
00307 
00308             if( i_start < i_end )
00309             {
00310                 int     i_frame = (f * ( i_end - i_start )) / p_sys->i_ssnd_fsize;
00311                 int64_t i_new   = i_start + i_frame * p_sys->i_ssnd_fsize;
00312 
00313                 if( stream_Seek( p_demux->s, i_new ) )
00314                 {
00315                     return VLC_EGENERIC;
00316                 }
00317                 p_sys->i_time = 1 + (int64_t)1000000 * i_frame / p_sys->fmt.audio.i_rate;
00318                 return VLC_SUCCESS;
00319             }
00320             return VLC_EGENERIC;
00321         }
00322 
00323         case DEMUX_GET_TIME:
00324             pi64 = (int64_t*)va_arg( args, int64_t * );
00325             *pi64 = p_sys->i_time;
00326             return VLC_SUCCESS;
00327 
00328         case DEMUX_GET_LENGTH:
00329         {
00330             int64_t i_end  = p_sys->i_ssnd_end > 0 ? p_sys->i_ssnd_end : stream_Size( p_demux->s );
00331 
00332             pi64 = (int64_t*)va_arg( args, int64_t * );
00333             if( p_sys->i_ssnd_start < i_end )
00334             {
00335                 *pi64 = (int64_t)1000000 * ( i_end - p_sys->i_ssnd_start ) / p_sys->i_ssnd_fsize / p_sys->fmt.audio.i_rate;
00336                 return VLC_SUCCESS;
00337             }
00338             return VLC_EGENERIC;
00339         }
00340         case DEMUX_SET_TIME:
00341         case DEMUX_GET_FPS:
00342         default:
00343             return VLC_EGENERIC;
00344     }
00345 }

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