1 /*****************************************************************************
2 * xa.c : xa file demux module for vlc
3 *****************************************************************************
4 * Copyright (C) 2005 Rémi Denis-Courmont
7 * Authors: Rémi Denis-Courmont <rem # videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_demux.h>
36 /*****************************************************************************
38 *****************************************************************************/
39 static int Open ( vlc_object_t * );
40 static void Close( vlc_object_t * );
43 set_description( N_("XA demuxer") )
44 set_category( CAT_INPUT )
45 set_subcategory( SUBCAT_INPUT_DEMUX )
46 set_capability( "demux", 10 )
47 set_callbacks( Open, Close )
50 /*****************************************************************************
52 *****************************************************************************/
53 static int Demux ( demux_t * );
54 static int Control( demux_t *, int i_query, va_list args );
60 int64_t i_data_offset;
61 unsigned int i_data_size;
62 unsigned int i_block_frames;
63 unsigned int i_frame_size;
64 unsigned int i_bitrate;
69 typedef struct xa_header_t
76 uint32_t nSamplesPerSec;
77 uint32_t nAvgBytesPerSec;
79 uint16_t wBitsPerSample;
82 #define FRAME_LENGTH 28 /* samples per frame */
84 /*****************************************************************************
85 * Open: check file and initializes structures
86 *****************************************************************************/
87 static int Open( vlc_object_t * p_this )
89 demux_t *p_demux = (demux_t*)p_this;
94 /* XA file heuristic */
95 if( stream_Peek( p_demux->s, &p_buf, sizeof( p_xa ) )
96 < (signed)sizeof( p_xa ) )
99 memcpy( &p_xa, p_buf, sizeof( p_xa ) );
100 if( ( strncmp( p_xa.xa_id, "XAI", 4 )
101 && strncmp( p_xa.xa_id, "XAJ", 4 ) )
102 || ( GetWLE( &p_xa.wFormatTag ) != 0x0001)
103 || ( GetWLE( &p_xa.wBitsPerSample ) != 16) )
106 p_sys = malloc( sizeof( demux_sys_t ) );
107 if( unlikely( p_sys == NULL ) )
110 p_demux->pf_demux = Demux;
111 p_demux->pf_control = Control;
112 p_demux->p_sys = p_sys;
115 /* skip XA header -- cannot fail */
116 stream_Read( p_demux->s, NULL, sizeof( p_xa ) );
119 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC('X','A','J',0) );
121 msg_Dbg( p_demux, "assuming EA ADPCM audio codec" );
122 fmt.audio.i_rate = GetDWLE( &p_xa.nSamplesPerSec );
123 fmt.audio.i_bytes_per_frame = 15 * GetWLE( &p_xa.nChannels );
124 fmt.audio.i_frame_length = FRAME_LENGTH;
126 fmt.audio.i_channels = GetWLE ( &p_xa.nChannels );
127 fmt.audio.i_blockalign = fmt.audio.i_bytes_per_frame;
128 fmt.audio.i_bitspersample = 16;
129 fmt.i_bitrate = (fmt.audio.i_rate * fmt.audio.i_bytes_per_frame * 8)
130 / fmt.audio.i_frame_length;
132 p_sys->i_data_offset = stream_Tell( p_demux->s );
133 /* FIXME: better computation */
134 p_sys->i_data_size = p_xa.iSize * 15 / 56;
135 /* How many frames per block (1:1 is too CPU intensive) */
136 p_sys->i_block_frames = fmt.audio.i_rate / (FRAME_LENGTH * 20) + 1;
137 p_sys->i_frame_size = fmt.audio.i_bytes_per_frame;
138 p_sys->i_bitrate = fmt.i_bitrate;
140 msg_Dbg( p_demux, "fourcc: %4.4s, channels: %d, "
141 "freq: %d Hz, bitrate: %dKo/s, blockalign: %d",
142 (char *)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate,
143 fmt.i_bitrate / 8192, fmt.audio.i_blockalign );
145 p_sys->p_es = es_out_Add( p_demux->out, &fmt );
147 date_Init( &p_sys->pts, fmt.audio.i_rate, 1 );
148 date_Set( &p_sys->pts, VLC_TS_0 );
153 /*****************************************************************************
154 * Demux: read packet and send them to decoders
155 *****************************************************************************
156 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
157 *****************************************************************************/
158 static int Demux( demux_t *p_demux )
160 demux_sys_t *p_sys = p_demux->p_sys;
163 unsigned i_frames = p_sys->i_block_frames;
165 i_offset = stream_Tell( p_demux->s );
167 if( p_sys->i_data_size > 0 &&
168 i_offset >= p_sys->i_data_offset + p_sys->i_data_size )
174 p_block = stream_Block( p_demux->s, p_sys->i_frame_size * i_frames );
175 if( p_block == NULL )
177 msg_Warn( p_demux, "cannot read data" );
181 i_frames = p_block->i_buffer / p_sys->i_frame_size;
182 p_block->i_dts = p_block->i_pts = date_Get( &p_sys->pts );
183 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
184 es_out_Send( p_demux->out, p_sys->p_es, p_block );
186 date_Increment( &p_sys->pts, i_frames * FRAME_LENGTH );
191 /*****************************************************************************
192 * Close: frees unused data
193 *****************************************************************************/
194 static void Close ( vlc_object_t * p_this )
196 demux_sys_t *p_sys = ((demux_t *)p_this)->p_sys;
201 /*****************************************************************************
203 *****************************************************************************/
204 static int Control( demux_t *p_demux, int i_query, va_list args )
206 demux_sys_t *p_sys = p_demux->p_sys;
208 return demux_vaControlHelper( p_demux->s, p_sys->i_data_offset,
209 p_sys->i_data_size ? p_sys->i_data_offset
210 + p_sys->i_data_size : -1,
211 p_sys->i_bitrate, p_sys->i_frame_size,