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