chromecast: refactor, make place for SPUs
[vlc.git] / modules / demux / xa.c
blobc55632e3b15df845c7edb4ed67e3cf15d57ef272
1 /*****************************************************************************
2 * xa.c : xa file demux module for vlc
3 *****************************************************************************
4 * Copyright (C) 2005 Rémi Denis-Courmont
5 * $Id$
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 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include <assert.h>
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_demux.h>
37 /*****************************************************************************
38 * Module descriptor
39 *****************************************************************************/
40 static int Open ( vlc_object_t * );
41 static void Close( vlc_object_t * );
43 vlc_module_begin ()
44 set_description( N_("XA demuxer") )
45 set_category( CAT_INPUT )
46 set_subcategory( SUBCAT_INPUT_DEMUX )
47 set_capability( "demux", 10 )
48 set_callbacks( Open, Close )
49 vlc_module_end ()
51 /*****************************************************************************
52 * Local prototypes
53 *****************************************************************************/
54 static int Demux ( demux_t * );
55 static int Control( demux_t *, int i_query, va_list args );
57 struct demux_sys_t
59 es_out_id_t *p_es;
61 unsigned int i_data_size;
62 unsigned int i_block_frames;
63 unsigned int i_frame_size;
64 unsigned int i_bitrate;
66 date_t pts;
69 typedef struct xa_header_t
71 char xa_id[4];
72 uint32_t iSize;
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;
82 #define HEADER_LENGTH 24
84 static_assert(offsetof(xa_header_t, wBitsPerSample) == 22, "Bad padding");
86 #define FRAME_LENGTH 28 /* samples per frame */
88 /*****************************************************************************
89 * Open: check file and initializes structures
90 *****************************************************************************/
91 static int Open( vlc_object_t * p_this )
93 demux_t *p_demux = (demux_t*)p_this;
94 const uint8_t *peek;
96 /* XA file heuristic */
97 if( vlc_stream_Peek( p_demux->s, &peek, 10 ) < 10 )
98 return VLC_EGENERIC;
99 if( memcmp( peek, "XAI", 4 ) && memcmp( peek, "XAJ", 4 ) &&
100 memcmp( peek, "XA\0", 4 ) )
101 return VLC_EGENERIC;
102 if( GetWLE( peek + 8 ) != 1 ) /* format tag */
103 return VLC_EGENERIC;
105 demux_sys_t *p_sys = malloc( sizeof( demux_sys_t ) );
106 if( unlikely( p_sys == NULL ) )
107 return VLC_ENOMEM;
109 /* read XA header*/
110 xa_header_t xa;
112 if( vlc_stream_Read( p_demux->s, &xa, HEADER_LENGTH ) < HEADER_LENGTH )
114 free( p_sys );
115 return VLC_EGENERIC;
118 es_format_t fmt;
119 es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_ADPCM_XA_EA );
121 msg_Dbg( p_demux, "assuming EA ADPCM audio codec" );
122 fmt.audio.i_rate = GetDWLE( &xa.nSamplesPerSec );
123 fmt.audio.i_bytes_per_frame = 15 * GetWLE( &xa.nChannels );
124 fmt.audio.i_frame_length = FRAME_LENGTH;
126 fmt.audio.i_channels = GetWLE ( &xa.nChannels );
127 fmt.audio.i_blockalign = fmt.audio.i_bytes_per_frame;
128 fmt.audio.i_bitspersample = GetWLE( &xa.wBitsPerSample );
129 fmt.i_bitrate = (fmt.audio.i_rate * fmt.audio.i_bytes_per_frame * 8)
130 / fmt.audio.i_frame_length;
132 /* FIXME: better computation */
133 p_sys->i_data_size = xa.iSize * 15 / 56;
134 /* How many frames per block (1:1 is too CPU intensive) */
135 p_sys->i_block_frames = fmt.audio.i_rate / (FRAME_LENGTH * 20) + 1;
136 p_sys->i_frame_size = fmt.audio.i_bytes_per_frame;
137 p_sys->i_bitrate = fmt.i_bitrate;
139 msg_Dbg( p_demux, "fourcc: %4.4s, channels: %d, "
140 "freq: %d Hz, bitrate: %dKo/s, blockalign: %d",
141 (char *)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate,
142 fmt.i_bitrate / 8192, fmt.audio.i_blockalign );
144 if( fmt.audio.i_rate == 0 || fmt.audio.i_channels == 0
145 || fmt.audio.i_bitspersample != 16 )
147 free( p_sys );
148 return VLC_EGENERIC;
151 p_sys->p_es = es_out_Add( p_demux->out, &fmt );
153 date_Init( &p_sys->pts, fmt.audio.i_rate, 1 );
154 date_Set( &p_sys->pts, VLC_TS_0 );
156 p_demux->pf_demux = Demux;
157 p_demux->pf_control = Control;
158 p_demux->p_sys = p_sys;
159 return VLC_SUCCESS;
162 /*****************************************************************************
163 * Demux: read packet and send them to decoders
164 *****************************************************************************
165 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
166 *****************************************************************************/
167 static int Demux( demux_t *p_demux )
169 demux_sys_t *p_sys = p_demux->p_sys;
170 block_t *p_block;
171 int64_t i_offset = vlc_stream_Tell( p_demux->s );
172 unsigned i_frames = p_sys->i_block_frames;
174 if( p_sys->i_data_size > 0 &&
175 (i_offset - HEADER_LENGTH) >= p_sys->i_data_size )
177 /* EOF */
178 return 0;
181 p_block = vlc_stream_Block( p_demux->s, p_sys->i_frame_size * i_frames );
182 if( p_block == NULL )
184 msg_Warn( p_demux, "cannot read data" );
185 return 0;
188 i_frames = p_block->i_buffer / p_sys->i_frame_size;
189 p_block->i_dts = p_block->i_pts = date_Get( &p_sys->pts );
190 es_out_SetPCR( p_demux->out, p_block->i_pts );
191 es_out_Send( p_demux->out, p_sys->p_es, p_block );
193 date_Increment( &p_sys->pts, i_frames * FRAME_LENGTH );
195 return 1;
198 /*****************************************************************************
199 * Close: frees unused data
200 *****************************************************************************/
201 static void Close ( vlc_object_t * p_this )
203 demux_sys_t *p_sys = ((demux_t *)p_this)->p_sys;
205 free( p_sys );
208 /*****************************************************************************
209 * Control:
210 *****************************************************************************/
211 static int Control( demux_t *p_demux, int i_query, va_list args )
213 demux_sys_t *p_sys = p_demux->p_sys;
215 return demux_vaControlHelper( p_demux->s, HEADER_LENGTH,
216 p_sys->i_data_size ? (int64_t)HEADER_LENGTH + p_sys->i_data_size : -1,
217 p_sys->i_bitrate, p_sys->i_frame_size,
218 i_query, args );