1 /*****************************************************************************
2 * voc.c : Creative Voice File (.VOC) demux module for vlc
3 *****************************************************************************
4 * Copyright (C) 2005 VideoLAN
7 * Authors: Remi Denis-Courmont <rem # videolan.org>
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 );
60 int64_t i_block_start;
63 int64_t i_loop_offset;
64 unsigned i_loop_count;
65 unsigned i_silence_countdown;
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;
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 p_demux->pf_demux = Demux;
111 p_demux->pf_control = Control;
112 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
116 p_sys->i_silence_countdown = p_sys->i_block_start = p_sys->i_block_end =
117 p_sys->i_loop_count = 0;
120 date_Init( &p_sys->pts, 1, 1 );
121 date_Set( &p_sys->pts, 1 );
123 es_format_Init( &p_sys->fmt, AUDIO_ES, 0 );
129 static int fmtcmp( es_format_t *ofmt, es_format_t *nfmt )
131 return (ofmt->audio.i_bitspersample != nfmt->audio.i_bitspersample)
132 || (ofmt->audio.i_rate != nfmt->audio.i_rate)
133 || (ofmt->audio.i_channels != nfmt->audio.i_channels);
137 static int ReadBlockHeader( demux_t *p_demux )
141 int32_t i_block_size;
142 demux_sys_t *p_sys = p_demux->p_sys;
144 if( stream_Read( p_demux->s, buf, 4 ) < 4 )
145 return VLC_EGENERIC; /* EOF */
147 i_block_size = GetDWLE( buf ) >> 8;
148 msg_Dbg( p_demux, "new block: type: %u, size: %u",
149 (unsigned)*buf, i_block_size );
151 es_format_Init( &new_fmt, AUDIO_ES, 0 );
155 case 0: /* not possible : caught with earlier stream_Read */
159 if( i_block_size < 2 )
163 if( stream_Read( p_demux->s, buf, 2 ) < 2 )
166 new_fmt.audio.i_rate = 1000000L / (256L - buf[0]);
169 msg_Err( p_demux, "Unsupported compression" );
173 new_fmt.i_codec = VLC_FOURCC('u','8',' ',' ');
174 new_fmt.audio.i_bytes_per_frame = 1;
175 new_fmt.audio.i_frame_length = 1;
176 new_fmt.audio.i_channels = 1;
177 new_fmt.audio.i_blockalign = 1;
178 new_fmt.audio.i_bitspersample = 8;
179 new_fmt.i_bitrate = p_sys->fmt.audio.i_rate * 8;
182 case 2: /* data block with same format as the previous one */
183 if( p_sys->p_es == NULL )
184 goto corrupt; /* no previous block! */
186 memcpy( &new_fmt, &p_sys->fmt, sizeof( new_fmt ) );
189 case 3: /* silence block */
190 if( ( i_block_size != 3 )
191 || ( stream_Read( p_demux->s, buf, 3 ) < 3 ) )
195 p_sys->i_silence_countdown = GetWLE( buf );
197 new_fmt.audio.i_rate = 1000000L / (256L - buf[2]);
198 new_fmt.i_codec = VLC_FOURCC('u','8',' ',' ');
199 new_fmt.audio.i_bytes_per_frame = 1;
200 new_fmt.audio.i_frame_length = 1;
201 new_fmt.audio.i_channels = 1;
202 new_fmt.audio.i_blockalign = 1;
203 new_fmt.audio.i_bitspersample = 8;
204 new_fmt.i_bitrate = p_sys->fmt.audio.i_rate * 8;
207 case 6: /* repeat block */
208 if( ( i_block_size != 2 )
209 || ( stream_Read( p_demux->s, buf, 2 ) < 2 ) )
213 p_sys->i_loop_count = GetWLE( buf );
214 p_sys->i_loop_offset = stream_Tell( p_demux->s );
217 case 7: /* repeat end block */
218 if( i_block_size != 0 )
221 if( p_sys->i_loop_count > 0 )
223 if( stream_Seek( p_demux->s, p_sys->i_loop_offset ) )
224 msg_Warn( p_demux, "cannot loop: seek failed" );
226 p_sys->i_loop_count--;
230 /* FIXME: support block type 8 properly */
232 msg_Err( p_demux, "Unimplemented block type 8" );
236 if( i_block_size < 12 )
240 if( ( stream_Read( p_demux->s, buf, 8 ) < 8 )
241 || ( stream_Read( p_demux->s, NULL, 4 ) < 4 ) )
244 new_fmt.audio.i_rate = GetDWLE( buf );
245 new_fmt.audio.i_bitspersample = buf[4];
246 new_fmt.audio.i_channels = buf[5];
248 switch( GetWLE( &buf[6] ) ) /* format */
250 case 0x0000: /* PCM */
251 switch( new_fmt.audio.i_bitspersample )
254 new_fmt.i_codec = VLC_FOURCC('u','8',' ',' ');
258 new_fmt.i_codec = VLC_FOURCC('u','1','6','l');
262 msg_Err( p_demux, "Unsupported bit res.: %u bits",
263 new_fmt.audio.i_bitspersample );
268 case 0x0004: /* signed */
269 switch( new_fmt.audio.i_bitspersample )
272 new_fmt.i_codec = VLC_FOURCC('s','8',' ',' ');
276 new_fmt.i_codec = VLC_FOURCC('s','1','6','l');
280 msg_Err( p_demux, "Unsupported bit res.: %u bits",
281 new_fmt.audio.i_bitspersample );
287 msg_Err( p_demux, "Unsupported compression" );
291 new_fmt.audio.i_bytes_per_frame = new_fmt.audio.i_channels
292 * (new_fmt.audio.i_bitspersample / 8);
293 new_fmt.audio.i_frame_length = 1;
294 new_fmt.audio.i_blockalign = p_sys->fmt.audio.i_bytes_per_frame;
295 new_fmt.i_bitrate = 8 * new_fmt.audio.i_rate
296 * new_fmt.audio.i_bytes_per_frame;
300 msg_Dbg( p_demux, "Unknown block type %u - skipping block",
302 case 4: /* blocks of non-audio types can be skipped */
304 if( stream_Read( p_demux->s, NULL, i_block_size ) < i_block_size )
310 p_sys->i_block_start = stream_Tell( p_demux->s );
311 p_sys->i_block_end = p_sys->i_block_start + i_block_size;
313 if( i_block_size || p_sys->i_silence_countdown )
315 /* we've read a block with data in it - update decoder */
316 msg_Dbg( p_demux, "fourcc: %4.4s, channels: %d, "
317 "freq: %d Hz, bitrate: %dKo/s, blockalign: %d, "
318 "bits/samples: %d", (char *)&new_fmt.i_codec,
319 new_fmt.audio.i_channels, new_fmt.audio.i_rate,
320 new_fmt.i_bitrate / 8192, new_fmt.audio.i_blockalign,
321 new_fmt.audio.i_bitspersample );
323 if( ( p_sys->p_es != NULL ) && fmtcmp( &p_sys->fmt, &new_fmt ) )
325 msg_Dbg( p_demux, "codec change needed" );
326 es_out_Del( p_demux->out, p_sys->p_es );
330 if( p_sys->p_es == NULL )
332 memcpy( &p_sys->fmt, &new_fmt, sizeof( p_sys->fmt ) );
333 date_Change( &p_sys->pts, p_sys->fmt.audio.i_rate, 1 );
334 p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
341 msg_Err( p_demux, "corrupted file - halting demux" );
345 /*****************************************************************************
346 * Demux: read packet and send them to decoders
347 *****************************************************************************
348 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
349 *****************************************************************************/
350 static int Demux( demux_t *p_demux )
352 demux_sys_t *p_sys = p_demux->p_sys;
356 i_offset = stream_Tell( p_demux->s );
358 while( ( i_offset >= p_sys->i_block_end )
359 && ( p_sys->i_silence_countdown == 0 ) )
360 if( ReadBlockHeader( p_demux ) != VLC_SUCCESS )
363 if( p_sys->i_silence_countdown == 0 )
365 i = ( p_sys->i_block_end - i_offset )
366 / p_sys->fmt.audio.i_bytes_per_frame;
370 p_block = stream_Block( p_demux->s,
371 p_sys->fmt.audio.i_bytes_per_frame * i );
372 if( p_block == NULL )
374 msg_Warn( p_demux, "cannot read data" );
379 { /* emulates silence from the stream */
380 i = p_sys->i_silence_countdown;
384 p_block = block_New( p_demux, i );
385 if( p_block == NULL )
388 memset( p_block->p_buffer, 0, i );
389 p_sys->i_silence_countdown -= i;
392 p_block->i_dts = p_block->i_pts =
393 date_Increment( &p_sys->pts, p_sys->fmt.audio.i_frame_length * i );
395 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
397 es_out_Send( p_demux->out, p_sys->p_es, p_block );
402 /*****************************************************************************
403 * Close: frees unused data
404 *****************************************************************************/
405 static void Close ( vlc_object_t * p_this )
407 demux_sys_t *p_sys = ((demux_t *)p_this)->p_sys;
412 /*****************************************************************************
414 *****************************************************************************/
415 static int Control( demux_t *p_demux, int i_query, va_list args )
417 demux_sys_t *p_sys = p_demux->p_sys;
419 return demux2_vaControlHelper( p_demux->s, p_sys->i_block_start,
421 p_sys->fmt.i_bitrate,
422 p_sys->fmt.audio.i_blockalign,