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