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