1 /*****************************************************************************
2 * voc.c : Creative Voice File (.VOC) demux module for vlc
3 *****************************************************************************
4 * Copyright (C) 2005 VideoLAN
5 * $Id: xa.c 10182 2005-03-07 15:04:53Z courmisch $
7 * Authors: Remi Denis-Courmont <rem # via.ecp.fr>
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
30 #include <vlc/input.h>
35 /*****************************************************************************
37 *****************************************************************************/
38 static int Open ( vlc_object_t * );
39 static void Close( vlc_object_t * );
42 set_description( _("VOC demuxer") );
43 set_category( CAT_INPUT );
44 set_subcategory( SUBCAT_INPUT_DEMUX );
45 set_capability( "demux2", 10 );
46 set_callbacks( Open, Close );
49 /*****************************************************************************
51 *****************************************************************************/
52 static int Demux ( demux_t * );
53 static int Control( demux_t *, int i_query, va_list args );
61 int64_t i_data_offset;
62 unsigned int i_data_size;
70 static const char ct_header[] = "Creative Voice File\x1a";
72 /*****************************************************************************
73 * Open: check file and initializes structures
74 *****************************************************************************/
75 static int Open( vlc_object_t * p_this )
77 demux_t *p_demux = (demux_t*)p_this;
79 uint8_t *p_buf, buf[8];
80 uint16_t i_data_offset, i_version;
82 if( stream_Peek( p_demux->s, &p_buf, 26 ) < 26 )
85 if( memcmp( p_buf, ct_header, 20 ) )
89 i_data_offset = GetWLE( p_buf );
90 if ( i_data_offset < 26 /* not enough room for full VOC header */ )
94 i_version = GetWLE( p_buf );
95 if( ( i_version != 0x10A ) && ( i_version != 0x114 ) )
96 return VLC_EGENERIC; /* unknown VOC version */
99 if( GetWLE( p_buf ) != (uint16_t)(0x1234 + ~i_version) )
102 /* We have a valid VOC header */
103 msg_Dbg( p_demux, "CT Voice file v%d.%d", i_version >> 8,
106 /* skip VOC header */
107 if( stream_Read( p_demux->s, NULL, i_data_offset ) < i_data_offset )
110 /* read first block header */
111 /* FIXME: we only support one-block files at the moment */
112 if( stream_Read( p_demux->s, buf, 4 ) < 4 )
115 p_demux->pf_demux = Demux;
116 p_demux->pf_control = Control;
117 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
119 p_sys->i_block_size = GetDWLE( buf ) >> 8;
120 msg_Dbg( p_demux, "block size: %u bytes", p_sys->i_block_size );
122 es_format_Init( &p_sys->fmt, AUDIO_ES, 0 );
127 if( p_sys->i_block_size < 2 )
129 p_sys->i_block_size -= 2;
131 if( stream_Read( p_demux->s, buf, 2 ) < 2 )
134 p_sys->fmt.audio.i_rate = 1000000L / (256L - buf[0]);
137 msg_Err( p_demux, "Unsupported compression" );
141 p_sys->fmt.i_codec = VLC_FOURCC('u','8',' ',' ');
142 p_sys->fmt.audio.i_bytes_per_frame = 1;
143 p_sys->fmt.audio.i_frame_length = 1;
144 p_sys->fmt.audio.i_channels = 1;
145 p_sys->fmt.audio.i_blockalign = 1;
146 p_sys->fmt.audio.i_bitspersample = 8;
147 p_sys->fmt.i_bitrate = p_sys->fmt.audio.i_rate * 8;
151 if( p_sys->i_block_size < 12 )
153 p_sys->i_block_size -= 12;
155 if( ( stream_Read( p_demux->s, buf, 8 ) < 8 )
156 || ( stream_Read( p_demux->s, NULL, 4 ) < 4 ) )
159 p_sys->fmt.audio.i_rate = GetDWLE( buf );
160 p_sys->fmt.audio.i_bitspersample = buf[4];
161 p_sys->fmt.audio.i_channels = buf[5];
163 switch( GetWLE( &buf[6] ) ) /* format */
165 case 0x0000: /* PCM */
166 switch( p_sys->fmt.audio.i_bitspersample )
169 p_sys->fmt.i_codec = VLC_FOURCC('u','8',' ',' ');
173 p_sys->fmt.i_codec = VLC_FOURCC('u','1','6','l');
177 msg_Err( p_demux, "Unsupported bit res.: %u bits",
178 p_sys->fmt.audio.i_bitspersample );
183 case 0x0004: /* signed */
184 switch( p_sys->fmt.audio.i_bitspersample )
187 p_sys->fmt.i_codec = VLC_FOURCC('s','8',' ',' ');
191 p_sys->fmt.i_codec = VLC_FOURCC('s','1','6','l');
195 msg_Err( p_demux, "Unsupported bit res.: %u bits",
196 p_sys->fmt.audio.i_bitspersample );
202 msg_Err( p_demux, "Unsupported compression" );
206 p_sys->fmt.audio.i_bytes_per_frame = p_sys->fmt.audio.i_channels
207 * (p_sys->fmt.audio.i_bitspersample / 8);
208 p_sys->fmt.audio.i_frame_length = 1;
209 p_sys->fmt.audio.i_blockalign = p_sys->fmt.audio.i_bytes_per_frame;
210 p_sys->fmt.i_bitrate = 8 * p_sys->fmt.audio.i_rate
211 * p_sys->fmt.audio.i_bytes_per_frame;
215 msg_Dbg( p_demux, "Unsupported block type %u", (unsigned)*p_buf);
218 p_sys->fmt.i_extra = 0;
219 p_sys->fmt.p_extra = NULL;
221 msg_Dbg( p_demux, "fourcc: %4.4s, channels: %d, "
222 "freq: %d Hz, bitrate: %dKo/s, blockalign: %d, bits/samples: %d",
223 (char *)&p_sys->fmt.i_codec, p_sys->fmt.audio.i_channels,
224 p_sys->fmt.audio.i_rate, p_sys->fmt.i_bitrate / 8192,
225 p_sys->fmt.audio.i_blockalign, p_sys->fmt.audio.i_bitspersample
228 p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
230 date_Init( &p_sys->pts, p_sys->fmt.audio.i_rate, 1 );
231 date_Set( &p_sys->pts, 1 );
236 msg_Err( p_demux, "Unsupported or malformatted file" );
241 /*****************************************************************************
242 * Demux: read packet and send them to decoders
243 *****************************************************************************
244 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
245 *****************************************************************************/
246 static int Demux( demux_t *p_demux )
248 demux_sys_t *p_sys = p_demux->p_sys;
253 i_offset = stream_Tell( p_demux->s );
255 if( p_sys->i_data_size > 0 &&
256 i_offset >= p_sys->i_data_offset + p_sys->i_data_size )
262 p_block = stream_Block( p_demux->s, p_sys->fmt.audio.i_bytes_per_frame );
263 if( p_block == NULL )
265 msg_Warn( p_demux, "cannot read data" );
269 p_block->i_dts = p_block->i_pts =
270 date_Increment( &p_sys->pts, p_sys->fmt.audio.i_frame_length );
272 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
274 es_out_Send( p_demux->out, p_sys->p_es, p_block );
279 /*****************************************************************************
280 * Close: frees unused data
281 *****************************************************************************/
282 static void Close ( vlc_object_t * p_this )
284 demux_sys_t *p_sys = ((demux_t *)p_this)->p_sys;
289 /*****************************************************************************
291 *****************************************************************************/
292 static int Control( demux_t *p_demux, int i_query, va_list args )
294 demux_sys_t *p_sys = p_demux->p_sys;
296 if( i_query == DEMUX_SET_POSITION )
297 return VLC_EGENERIC; /* not implemented yet */
299 return demux2_vaControlHelper( p_demux->s, 0, -1, p_sys->fmt.i_bitrate,
300 p_sys->fmt.audio.i_blockalign,