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

xa.c

00001 /*****************************************************************************
00002  * xa.c : xa file input module for vlc
00003  *****************************************************************************
00004  * Copyright (C) 2005 the VideoLAN team
00005  * $Id: xa.c 11664 2005-07-09 06:17:09Z courmisch $
00006  *
00007  * Authors: Remi Denis-Courmont <rem # videolan.org>
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 #include <vlc/aout.h>
00032 
00033 #include <codecs.h>
00034 
00035 /*****************************************************************************
00036  * Module descriptor
00037  *****************************************************************************/
00038 static int  Open ( vlc_object_t * );
00039 static void Close( vlc_object_t * );
00040 
00041 vlc_module_begin();
00042     set_description( _("XA demuxer") );
00043     set_category( CAT_INPUT );
00044     set_subcategory( SUBCAT_INPUT_DEMUX );
00045     set_capability( "demux2", 10 );
00046     set_callbacks( Open, Close );
00047 vlc_module_end();
00048 
00049 /*****************************************************************************
00050  * Local prototypes
00051  *****************************************************************************/
00052 static int Demux  ( demux_t * );
00053 static int Control( demux_t *, int i_query, va_list args );
00054 
00055 struct demux_sys_t
00056 {
00057     es_format_t     fmt;
00058     es_out_id_t     *p_es;
00059 
00060     int64_t         i_data_offset;
00061     unsigned int    i_data_size;
00062 
00063     date_t          pts;
00064 };
00065 
00066 typedef struct xa_header_t
00067 {
00068     char     xa_id[4];
00069     uint32_t iSize;
00070 
00071     uint16_t wFormatTag;
00072     uint16_t nChannels;
00073     uint32_t nSamplesPerSec;
00074     uint32_t nAvgBytesPerSec;
00075     uint16_t nBlockAlign;
00076     uint16_t wBitsPerSample;
00077 } xa_header_t;
00078 
00079 
00080 /*****************************************************************************
00081  * Open: check file and initializes structures
00082  *****************************************************************************/
00083 static int Open( vlc_object_t * p_this )
00084 {
00085     demux_t     *p_demux = (demux_t*)p_this;
00086     demux_sys_t *p_sys;
00087     xa_header_t p_xa;
00088     uint8_t     *p_buf;
00089 
00090     /* XA file heuristic */
00091     if( stream_Peek( p_demux->s, &p_buf, sizeof( p_xa ) )
00092             < (signed)sizeof( p_xa ) )
00093         return VLC_EGENERIC;
00094 
00095     memcpy( &p_xa, p_buf, sizeof( p_xa ) );
00096     if( ( strncmp( p_xa.xa_id, "XAI", 4 )
00097        && strncmp( p_xa.xa_id, "XAJ", 4 ) )
00098      || ( GetWLE( &p_xa.wFormatTag  ) != 0x0001)
00099      || ( GetWLE( &p_xa.wBitsPerSample ) != 16) )
00100         return VLC_EGENERIC;
00101 
00102     p_demux->pf_demux   = Demux;
00103     p_demux->pf_control = Control;
00104     p_demux->p_sys      = p_sys = malloc( sizeof( demux_sys_t ) );
00105     p_sys->p_es         = NULL;
00106 
00107     /* skip XA header -- cannot fail */
00108     stream_Read( p_demux->s, NULL, sizeof( p_xa ) );
00109 
00110     es_format_Init( &p_sys->fmt, AUDIO_ES, VLC_FOURCC('X','A','J',0) );
00111 
00112     msg_Dbg( p_demux, "assuming EA ADPCM audio codec" );
00113     p_sys->fmt.audio.i_rate = GetDWLE( &p_xa.nSamplesPerSec );
00114     p_sys->fmt.audio.i_bytes_per_frame = 15 * GetWLE( &p_xa.nChannels );
00115     p_sys->fmt.audio.i_frame_length = 28; /* 28 samples of 4 bits each */
00116 
00117     p_sys->fmt.audio.i_channels = GetWLE ( &p_xa.nChannels );
00118     p_sys->fmt.audio.i_blockalign = p_sys->fmt.audio.i_bytes_per_frame;
00119     p_sys->fmt.audio.i_bitspersample = 16;
00120     p_sys->fmt.i_bitrate = (p_sys->fmt.audio.i_rate
00121                             * p_sys->fmt.audio.i_bytes_per_frame * 8)
00122                             / p_sys->fmt.audio.i_frame_length;
00123     p_sys->fmt.i_extra = 0;
00124     p_sys->fmt.p_extra = NULL;
00125 
00126     p_sys->i_data_offset = stream_Tell( p_demux->s );
00127     /* FIXME: better computation */
00128     p_sys->i_data_size = p_xa.iSize * 15 / 56;
00129 
00130     msg_Dbg( p_demux, "fourcc: %4.4s, channels: %d, "
00131              "freq: %d Hz, bitrate: %dKo/s, blockalign: %d",
00132              (char *)&p_sys->fmt.i_codec, p_sys->fmt.audio.i_channels,
00133              p_sys->fmt.audio.i_rate, p_sys->fmt.i_bitrate / 8192,
00134              p_sys->fmt.audio.i_blockalign );
00135 
00136     p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
00137 
00138     date_Init( &p_sys->pts, p_sys->fmt.audio.i_rate, 1 );
00139     date_Set( &p_sys->pts, 1 );
00140 
00141     return VLC_SUCCESS;
00142 }
00143 
00144 /*****************************************************************************
00145  * Demux: read packet and send them to decoders
00146  *****************************************************************************
00147  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
00148  *****************************************************************************/
00149 static int Demux( demux_t *p_demux )
00150 {
00151     demux_sys_t *p_sys = p_demux->p_sys;
00152     block_t     *p_block;
00153     int64_t     i_offset;
00154 
00155     i_offset = stream_Tell( p_demux->s );
00156 
00157     if( p_sys->i_data_size > 0 &&
00158         i_offset >= p_sys->i_data_offset + p_sys->i_data_size )
00159     {
00160         /* EOF */
00161         return 0;
00162     }
00163 
00164     p_block = stream_Block( p_demux->s, p_sys->fmt.audio.i_bytes_per_frame );
00165     if( p_block == NULL )
00166     {
00167         msg_Warn( p_demux, "cannot read data" );
00168         return 0;
00169     }
00170 
00171     p_block->i_dts = p_block->i_pts =
00172         date_Increment( &p_sys->pts, p_sys->fmt.audio.i_frame_length );
00173 
00174     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
00175 
00176     es_out_Send( p_demux->out, p_sys->p_es, p_block );
00177 
00178     return 1;
00179 }
00180 
00181 /*****************************************************************************
00182  * Close: frees unused data
00183  *****************************************************************************/
00184 static void Close ( vlc_object_t * p_this )
00185 {
00186     demux_sys_t *p_sys  = ((demux_t *)p_this)->p_sys;
00187 
00188     free( p_sys );
00189 }
00190 
00191 /*****************************************************************************
00192  * Control:
00193  *****************************************************************************/
00194 static int Control( demux_t *p_demux, int i_query, va_list args )
00195 {
00196     demux_sys_t *p_sys  = p_demux->p_sys;
00197 
00198     return demux2_vaControlHelper( p_demux->s, p_sys->i_data_offset,
00199                                    p_sys->i_data_size ? p_sys->i_data_offset
00200                                    + p_sys->i_data_size : -1,
00201                                    p_sys->fmt.i_bitrate,
00202                                    p_sys->fmt.audio.i_blockalign,
00203                                    i_query, args );
00204 }

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