]> git.sesse.net Git - vlc/blob - modules/demux/xa.c
7cc44b87707007483996205d45747384ec51d0d9
[vlc] / modules / demux / xa.c
1 /*****************************************************************************
2  * xa.c : xa file input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2005 VideoLAN
5  * $Id$
6  *
7  * Authors: Remi Denis-Courmont <rem # via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
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 General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28
29 #include <vlc/vlc.h>
30 #include <vlc/input.h>
31 #include <vlc/aout.h>
32
33 #include <codecs.h>
34
35 /*****************************************************************************
36  * Module descriptor
37  *****************************************************************************/
38 static int  Open ( vlc_object_t * );
39 static void Close( vlc_object_t * );
40
41 vlc_module_begin();
42     set_description( _("XA demuxer") );
43     set_category( CAT_INPUT );
44     set_subcategory( SUBCAT_INPUT_DEMUX );
45     set_capability( "demux2", 10 );
46     set_callbacks( Open, Close );
47 vlc_module_end();
48
49 /*****************************************************************************
50  * Local prototypes
51  *****************************************************************************/
52 static int Demux  ( demux_t * );
53 static int Control( demux_t *, int i_query, va_list args );
54
55 struct demux_sys_t
56 {
57     es_format_t     fmt;
58     es_out_id_t     *p_es;
59
60     int64_t         i_data_offset;
61     unsigned int    i_data_size;
62
63     date_t          pts;
64 };
65
66 typedef struct xa_header_y
67 {
68     uint8_t  xa_id[4];
69     uint32_t iSize;
70
71     uint16_t wFormatTag;
72     uint16_t nChannels;
73     uint32_t nSamplesPerSec;
74     uint32_t nAvgBytesPerSec;
75     uint16_t nBlockAlign;
76     uint16_t wBitsPerSample;
77 } xa_header_t;
78
79
80 /*****************************************************************************
81  * Open: check file and initializes structures
82  *****************************************************************************/
83 static int Open( vlc_object_t * p_this )
84 {
85     demux_t     *p_demux = (demux_t*)p_this;
86     demux_sys_t *p_sys;
87     xa_header_t p_xa;
88     uint8_t     *p_buf;
89
90     /* XA file heuristic */
91     if( stream_Peek( p_demux->s, &p_buf, sizeof( p_xa ) )
92             < (signed)sizeof( p_xa ) )
93         return VLC_EGENERIC;
94
95     memcpy( &p_xa, p_buf, sizeof( p_xa ) );
96     if( ( strncmp( p_xa.xa_id, "XAI", 4 )
97        && strncmp( p_xa.xa_id, "XAJ", 4 ) )
98      || ( GetWLE( &p_xa.wFormatTag  ) != 0x0001)
99      || ( GetWLE( &p_xa.wBitsPerSample ) != 16) )
100         return VLC_EGENERIC;
101
102     p_demux->pf_demux   = Demux;
103     p_demux->pf_control = Control;
104     p_demux->p_sys      = p_sys = malloc( sizeof( demux_sys_t ) );
105     p_sys->p_es         = NULL;
106
107     /* skip XA header -- cannot fail */
108     stream_Read( p_demux->s, NULL, sizeof( p_xa ) );
109
110     es_format_Init( &p_sys->fmt, AUDIO_ES, 0 );
111
112     msg_Dbg( p_demux, "assuming EA ADPCM audio format" );
113     p_sys->fmt.i_codec = VLC_FOURCC('X','A','J',0);
114     p_sys->fmt.audio.i_rate = GetDWLE( &p_xa.nSamplesPerSec );
115     p_sys->fmt.audio.i_bytes_per_frame = 15 * GetWLE( &p_xa.nChannels );
116     p_sys->fmt.audio.i_frame_length = 28; /* 28 samples of 4 bits each */
117
118     p_sys->fmt.audio.i_channels = GetWLE ( &p_xa.nChannels );
119     p_sys->fmt.audio.i_blockalign = p_sys->fmt.audio.i_bytes_per_frame;
120     p_sys->fmt.audio.i_bitspersample = 4;
121     p_sys->fmt.i_bitrate = (p_sys->fmt.audio.i_rate
122                             * p_sys->fmt.audio.i_bytes_per_frame * 8)
123                             / p_sys->fmt.audio.i_frame_length;
124     p_sys->fmt.i_extra = 0;
125     p_sys->fmt.p_extra = NULL;
126
127     p_sys->i_data_offset = stream_Tell( p_demux->s );
128     /* FIXME: better computation */
129     p_sys->i_data_size = p_xa.iSize * 15 / 56;
130
131     msg_Dbg( p_demux, "fourcc: %4.4s, channels: %d, "
132              "freq: %d Hz, bitrate: %dKo/s, blockalign: %d",
133              (char *)&p_sys->fmt.i_codec, p_sys->fmt.audio.i_channels,
134              p_sys->fmt.audio.i_rate, p_sys->fmt.i_bitrate / 8192,
135              p_sys->fmt.audio.i_blockalign );
136
137     p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
138
139     date_Init( &p_sys->pts, p_sys->fmt.audio.i_rate, 1 );
140     date_Set( &p_sys->pts, 1 );
141
142     return VLC_SUCCESS;
143 }
144
145 /*****************************************************************************
146  * Demux: read packet and send them to decoders
147  *****************************************************************************
148  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
149  *****************************************************************************/
150 static int Demux( demux_t *p_demux )
151 {
152     demux_sys_t *p_sys = p_demux->p_sys;
153     block_t     *p_block;
154     int64_t     i_offset;
155
156     i_offset = stream_Tell( p_demux->s );
157
158     if( p_sys->i_data_size > 0 &&
159         i_offset >= p_sys->i_data_offset + p_sys->i_data_size )
160     {
161         /* EOF */
162         return 0;
163     }
164
165     p_block = stream_Block( p_demux->s, p_sys->fmt.audio.i_bytes_per_frame );
166     if( p_block == NULL )
167     {
168         msg_Warn( p_demux, "cannot read data" );
169         return 0;
170     }
171
172     p_block->i_dts = p_block->i_pts =
173         date_Increment( &p_sys->pts, p_sys->fmt.audio.i_frame_length );
174
175     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
176
177     es_out_Send( p_demux->out, p_sys->p_es, p_block );
178
179     return 1;
180 }
181
182 /*****************************************************************************
183  * Close: frees unused data
184  *****************************************************************************/
185 static void Close ( vlc_object_t * p_this )
186 {
187     demux_sys_t *p_sys  = ((demux_t *)p_this)->p_sys;
188
189     free( p_sys );
190 }
191
192 /*****************************************************************************
193  * Control:
194  *****************************************************************************/
195 static int Control( demux_t *p_demux, int i_query, va_list args )
196 {
197     demux_sys_t *p_sys  = p_demux->p_sys;
198
199     if( i_query == DEMUX_SET_POSITION )
200         return -1; /* FIXME: progressive encoding - seeking not supported */
201
202     return demux2_vaControlHelper( p_demux->s, 0, -1,
203                                    p_sys->fmt.i_bitrate,
204                                    p_sys->fmt.audio.i_blockalign,
205                                    i_query, args );
206 }