1 /*****************************************************************************
2 * xa.c : xa file demux module for vlc
3 *****************************************************************************
4 * Copyright (C) 2005 Rémi Denis-Courmont
6 * Authors: Rémi Denis-Courmont
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_demux.h>
36 /*****************************************************************************
38 *****************************************************************************/
39 static int Open ( vlc_object_t
* );
42 set_description( N_("XA demuxer") )
43 set_category( CAT_INPUT
)
44 set_subcategory( SUBCAT_INPUT_DEMUX
)
45 set_capability( "demux", 10 )
49 /*****************************************************************************
51 *****************************************************************************/
52 static int Demux ( demux_t
* );
53 static int Control( demux_t
*, int i_query
, va_list args
);
59 unsigned int i_data_size
;
60 unsigned int i_block_frames
;
61 unsigned int i_frame_size
;
62 unsigned int i_bitrate
;
67 typedef struct xa_header_t
74 uint32_t nSamplesPerSec
;
75 uint32_t nAvgBytesPerSec
;
77 uint16_t wBitsPerSample
;
80 #define HEADER_LENGTH 24
82 static_assert(offsetof(xa_header_t
, wBitsPerSample
) == 22, "Bad padding");
84 #define FRAME_LENGTH 28 /* samples per frame */
86 /*****************************************************************************
87 * Open: check file and initializes structures
88 *****************************************************************************/
89 static int Open( vlc_object_t
* p_this
)
91 demux_t
*p_demux
= (demux_t
*)p_this
;
94 /* XA file heuristic */
95 if( vlc_stream_Peek( p_demux
->s
, &peek
, 10 ) < 10 )
97 if( memcmp( peek
, "XAI", 4 ) && memcmp( peek
, "XAJ", 4 ) &&
98 memcmp( peek
, "XA\0", 4 ) )
100 if( GetWLE( peek
+ 8 ) != 1 ) /* format tag */
103 demux_sys_t
*p_sys
= vlc_obj_malloc( p_this
, sizeof (*p_sys
) );
104 if( unlikely( p_sys
== NULL
) )
110 if( vlc_stream_Read( p_demux
->s
, &xa
, HEADER_LENGTH
) < HEADER_LENGTH
)
114 es_format_Init( &fmt
, AUDIO_ES
, VLC_CODEC_ADPCM_XA_EA
);
116 msg_Dbg( p_demux
, "assuming EA ADPCM audio codec" );
117 fmt
.audio
.i_rate
= GetDWLE( &xa
.nSamplesPerSec
);
118 fmt
.audio
.i_bytes_per_frame
= 15 * GetWLE( &xa
.nChannels
);
119 fmt
.audio
.i_frame_length
= FRAME_LENGTH
;
121 fmt
.audio
.i_channels
= GetWLE ( &xa
.nChannels
);
122 fmt
.audio
.i_blockalign
= fmt
.audio
.i_bytes_per_frame
;
123 fmt
.audio
.i_bitspersample
= GetWLE( &xa
.wBitsPerSample
);
124 fmt
.i_bitrate
= (fmt
.audio
.i_rate
* fmt
.audio
.i_bytes_per_frame
* 8)
125 / fmt
.audio
.i_frame_length
;
127 /* FIXME: better computation */
128 p_sys
->i_data_size
= xa
.iSize
* 15 / 56;
129 /* How many frames per block (1:1 is too CPU intensive) */
130 p_sys
->i_block_frames
= fmt
.audio
.i_rate
/ (FRAME_LENGTH
* 20) + 1;
131 p_sys
->i_frame_size
= fmt
.audio
.i_bytes_per_frame
;
132 p_sys
->i_bitrate
= fmt
.i_bitrate
;
134 msg_Dbg( p_demux
, "fourcc: %4.4s, channels: %d, "
135 "freq: %d Hz, bitrate: %dKo/s, blockalign: %d",
136 (char *)&fmt
.i_codec
, fmt
.audio
.i_channels
, fmt
.audio
.i_rate
,
137 fmt
.i_bitrate
/ 8192, fmt
.audio
.i_blockalign
);
139 if( fmt
.audio
.i_rate
== 0 || fmt
.audio
.i_channels
== 0
140 || fmt
.audio
.i_bitspersample
!= 16 )
143 p_sys
->p_es
= es_out_Add( p_demux
->out
, &fmt
);
144 if( unlikely(p_sys
->p_es
== NULL
) )
147 date_Init( &p_sys
->pts
, fmt
.audio
.i_rate
, 1 );
148 date_Set( &p_sys
->pts
, VLC_TICK_0
);
150 p_demux
->pf_demux
= Demux
;
151 p_demux
->pf_control
= Control
;
152 p_demux
->p_sys
= p_sys
;
156 /*****************************************************************************
157 * Demux: read packet and send them to decoders
158 *****************************************************************************
159 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
160 *****************************************************************************/
161 static int Demux( demux_t
*p_demux
)
163 demux_sys_t
*p_sys
= p_demux
->p_sys
;
165 int64_t i_offset
= vlc_stream_Tell( p_demux
->s
);
166 unsigned i_frames
= p_sys
->i_block_frames
;
168 if( p_sys
->i_data_size
> 0 &&
169 (i_offset
- HEADER_LENGTH
) >= p_sys
->i_data_size
)
171 return VLC_DEMUXER_EOF
;
174 p_block
= vlc_stream_Block( p_demux
->s
, p_sys
->i_frame_size
* i_frames
);
175 if( p_block
== NULL
)
177 msg_Warn( p_demux
, "cannot read data" );
178 return VLC_DEMUXER_EOF
;
181 i_frames
= p_block
->i_buffer
/ p_sys
->i_frame_size
;
182 p_block
->i_dts
= p_block
->i_pts
= date_Get( &p_sys
->pts
);
183 es_out_SetPCR( p_demux
->out
, p_block
->i_pts
);
184 es_out_Send( p_demux
->out
, p_sys
->p_es
, p_block
);
186 date_Increment( &p_sys
->pts
, i_frames
* FRAME_LENGTH
);
188 return VLC_DEMUXER_SUCCESS
;
191 /*****************************************************************************
193 *****************************************************************************/
194 static int Control( demux_t
*p_demux
, int i_query
, va_list args
)
196 demux_sys_t
*p_sys
= p_demux
->p_sys
;
198 return demux_vaControlHelper( p_demux
->s
, HEADER_LENGTH
,
199 p_sys
->i_data_size
? (int64_t)HEADER_LENGTH
+ p_sys
->i_data_size
: -1,
200 p_sys
->i_bitrate
, p_sys
->i_frame_size
,