1 /*****************************************************************************
2 * voc.c : Creative Voice File (.VOC) demux module for vlc
3 *****************************************************************************
4 * Copyright (C) 2005 Rémi Denis-Courmont
7 * Authors: Rémi 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
29 #include <vlc_demux.h>
32 #include <vlc_codecs.h>
34 /*****************************************************************************
36 *****************************************************************************/
37 static int Open ( vlc_object_t * );
38 static void Close( vlc_object_t * );
41 set_description( _("VOC demuxer") );
42 set_category( CAT_INPUT );
43 set_subcategory( SUBCAT_INPUT_DEMUX );
44 set_capability( "demux2", 10 );
45 set_callbacks( Open, Close );
48 /*****************************************************************************
50 *****************************************************************************/
51 static int Demux ( demux_t * );
52 static int Control( demux_t *, int i_query, va_list args );
59 int64_t i_block_start;
62 int64_t i_loop_offset;
63 unsigned i_loop_count;
64 unsigned i_silence_countdown;
69 static const char ct_header[] = "Creative Voice File\x1a";
71 /*****************************************************************************
72 * Open: check file and initializes structures
73 *****************************************************************************/
74 static int Open( vlc_object_t * p_this )
76 demux_t *p_demux = (demux_t*)p_this;
79 uint16_t i_data_offset, i_version;
81 if( stream_Peek( p_demux->s, &p_buf, 26 ) < 26 )
84 if( memcmp( p_buf, ct_header, 20 ) )
88 i_data_offset = GetWLE( p_buf );
89 if ( i_data_offset < 26 /* not enough room for full VOC header */ )
93 i_version = GetWLE( p_buf );
94 if( ( i_version != 0x10A ) && ( i_version != 0x114 ) )
95 return VLC_EGENERIC; /* unknown VOC version */
98 if( GetWLE( p_buf ) != (uint16_t)(0x1234 + ~i_version) )
101 /* We have a valid VOC header */
102 msg_Dbg( p_demux, "CT Voice file v%d.%d", i_version >> 8,
105 /* skip VOC header */
106 if( stream_Read( p_demux->s, NULL, i_data_offset ) < i_data_offset )
109 p_demux->pf_demux = Demux;
110 p_demux->pf_control = Control;
111 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
115 p_sys->i_silence_countdown = p_sys->i_block_start = p_sys->i_block_end =
116 p_sys->i_loop_count = 0;
119 date_Init( &p_sys->pts, 1, 1 );
120 date_Set( &p_sys->pts, 1 );
122 es_format_Init( &p_sys->fmt, AUDIO_ES, 0 );
128 static int fmtcmp( es_format_t *ofmt, es_format_t *nfmt )
130 return (ofmt->audio.i_bitspersample != nfmt->audio.i_bitspersample)
131 || (ofmt->audio.i_rate != nfmt->audio.i_rate)
132 || (ofmt->audio.i_channels != nfmt->audio.i_channels);
137 * Converts old-style VOC sample rates to commonly used ones
138 * so as not to confuse sound card drivers.
139 * (I assume 16k, 24k and 32k are never found in .VOC files)
141 static unsigned int fix_voc_sr( unsigned int sr )
159 static int ReadBlockHeader( demux_t *p_demux )
163 int32_t i_block_size;
164 demux_sys_t *p_sys = p_demux->p_sys;
166 if( stream_Read( p_demux->s, buf, 4 ) < 4 )
167 return VLC_EGENERIC; /* EOF */
169 i_block_size = GetDWLE( buf ) >> 8;
170 msg_Dbg( p_demux, "new block: type: %u, size: %u",
171 (unsigned)*buf, i_block_size );
173 es_format_Init( &new_fmt, AUDIO_ES, 0 );
177 case 0: /* not possible : caught with earlier stream_Read */
181 if( i_block_size < 2 )
185 if( stream_Read( p_demux->s, buf, 2 ) < 2 )
190 msg_Err( p_demux, "unsupported compression" );
194 new_fmt.i_codec = VLC_FOURCC('u','8',' ',' ');
195 new_fmt.audio.i_rate = fix_voc_sr( 1000000L / (256L - buf[0]) );
196 new_fmt.audio.i_bytes_per_frame = 1;
197 new_fmt.audio.i_frame_length = 1;
198 new_fmt.audio.i_channels = 1;
199 new_fmt.audio.i_blockalign = 1;
200 new_fmt.audio.i_bitspersample = 8;
201 new_fmt.i_bitrate = new_fmt.audio.i_rate * 8;
204 case 2: /* data block with same format as the previous one */
205 if( p_sys->p_es == NULL )
206 goto corrupt; /* no previous block! */
208 memcpy( &new_fmt, &p_sys->fmt, sizeof( new_fmt ) );
211 case 3: /* silence block */
212 if( ( i_block_size != 3 )
213 || ( stream_Read( p_demux->s, buf, 3 ) < 3 ) )
217 p_sys->i_silence_countdown = GetWLE( buf );
219 new_fmt.i_codec = VLC_FOURCC('u','8',' ',' ');
220 new_fmt.audio.i_rate = fix_voc_sr( 1000000L / (256L - buf[0]) );
221 new_fmt.audio.i_bytes_per_frame = 1;
222 new_fmt.audio.i_frame_length = 1;
223 new_fmt.audio.i_channels = 1;
224 new_fmt.audio.i_blockalign = 1;
225 new_fmt.audio.i_bitspersample = 8;
226 new_fmt.i_bitrate = new_fmt.audio.i_rate * 8;
229 case 6: /* repeat block */
230 if( ( i_block_size != 2 )
231 || ( stream_Read( p_demux->s, buf, 2 ) < 2 ) )
235 p_sys->i_loop_count = GetWLE( buf );
236 p_sys->i_loop_offset = stream_Tell( p_demux->s );
239 case 7: /* repeat end block */
240 if( i_block_size != 0 )
243 if( p_sys->i_loop_count > 0 )
245 if( stream_Seek( p_demux->s, p_sys->i_loop_offset ) )
246 msg_Warn( p_demux, "cannot loop: seek failed" );
248 p_sys->i_loop_count--;
254 * Block 8 is a big kludge to add stereo support to block 1 :
255 * A block of type 8 is always followed by a block of type 1
256 * and specifies the number of channels in that 1-block
257 * (normally block 1 are always mono). In practice, block type 9
258 * is used for stereo rather than 8
260 if( ( i_block_size != 4 )
261 || ( stream_Read( p_demux->s, buf, 4 ) < 4 ) )
266 msg_Err( p_demux, "unsupported compression" );
270 new_fmt.i_codec = VLC_FOURCC('u','8',' ',' ');
271 new_fmt.audio.i_channels = buf[3] + 1; /* can't be nul */
272 new_fmt.audio.i_rate = 256000000L /
273 ((65536L - GetWLE(buf)) * new_fmt.audio.i_channels);
274 new_fmt.audio.i_bytes_per_frame = new_fmt.audio.i_channels;
275 new_fmt.audio.i_frame_length = 1;
276 new_fmt.audio.i_blockalign = new_fmt.audio.i_bytes_per_frame;
277 new_fmt.audio.i_bitspersample = 8 * new_fmt.audio.i_bytes_per_frame;
278 new_fmt.i_bitrate = new_fmt.audio.i_rate * 8;
280 /* read subsequent block 1 */
281 if( stream_Read( p_demux->s, buf, 4 ) < 4 )
282 return VLC_EGENERIC; /* EOF */
284 i_block_size = GetDWLE( buf ) >> 8;
285 msg_Dbg( p_demux, "new block: type: %u, size: %u",
286 (unsigned)*buf, i_block_size );
287 if( i_block_size < 2 )
291 if( stream_Read( p_demux->s, buf, 2 ) < 2 )
296 msg_Err( p_demux, "unsupported compression" );
302 case 9: /* newer data block with channel number and bits resolution */
303 if( i_block_size < 12 )
307 if( ( stream_Read( p_demux->s, buf, 8 ) < 8 )
308 || ( stream_Read( p_demux->s, NULL, 4 ) < 4 ) )
311 new_fmt.audio.i_rate = GetDWLE( buf );
312 new_fmt.audio.i_bitspersample = buf[4];
313 new_fmt.audio.i_channels = buf[5];
315 switch( GetWLE( &buf[6] ) ) /* format */
317 case 0x0000: /* PCM */
318 switch( new_fmt.audio.i_bitspersample )
321 new_fmt.i_codec = VLC_FOURCC('u','8',' ',' ');
325 new_fmt.i_codec = VLC_FOURCC('u','1','6','l');
329 msg_Err( p_demux, "unsupported bit res.: %u bits",
330 new_fmt.audio.i_bitspersample );
335 case 0x0004: /* signed */
336 switch( new_fmt.audio.i_bitspersample )
339 new_fmt.i_codec = VLC_FOURCC('s','8',' ',' ');
343 new_fmt.i_codec = VLC_FOURCC('s','1','6','l');
347 msg_Err( p_demux, "unsupported bit res.: %u bits",
348 new_fmt.audio.i_bitspersample );
354 msg_Err( p_demux, "unsupported compression" );
358 new_fmt.audio.i_bytes_per_frame = new_fmt.audio.i_channels
359 * (new_fmt.audio.i_bitspersample / 8);
360 new_fmt.audio.i_frame_length = 1;
361 new_fmt.audio.i_blockalign = new_fmt.audio.i_bytes_per_frame;
362 new_fmt.i_bitrate = 8 * new_fmt.audio.i_rate
363 * new_fmt.audio.i_bytes_per_frame;
367 msg_Dbg( p_demux, "unknown block type %u - skipping block",
369 case 4: /* blocks of non-audio types can be skipped */
371 if( stream_Read( p_demux->s, NULL, i_block_size ) < i_block_size )
377 p_sys->i_block_start = stream_Tell( p_demux->s );
378 p_sys->i_block_end = p_sys->i_block_start + i_block_size;
380 if( i_block_size || p_sys->i_silence_countdown )
382 /* we've read a block with data in it - update decoder */
383 msg_Dbg( p_demux, "fourcc: %4.4s, channels: %d, "
384 "freq: %d Hz, bitrate: %dKo/s, blockalign: %d, "
385 "bits/samples: %d", (char *)&new_fmt.i_codec,
386 new_fmt.audio.i_channels, new_fmt.audio.i_rate,
387 new_fmt.i_bitrate / 8192, new_fmt.audio.i_blockalign,
388 new_fmt.audio.i_bitspersample );
390 if( ( p_sys->p_es != NULL ) && fmtcmp( &p_sys->fmt, &new_fmt ) )
392 msg_Dbg( p_demux, "codec change needed" );
393 es_out_Del( p_demux->out, p_sys->p_es );
397 if( p_sys->p_es == NULL )
399 memcpy( &p_sys->fmt, &new_fmt, sizeof( p_sys->fmt ) );
400 date_Change( &p_sys->pts, p_sys->fmt.audio.i_rate, 1 );
401 p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
408 msg_Err( p_demux, "corrupted file - halting demux" );
412 /*****************************************************************************
413 * Demux: read packet and send them to decoders
414 *****************************************************************************
415 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
416 *****************************************************************************/
417 #define SAMPLES_BUFFER 1000
418 static int Demux( demux_t *p_demux )
420 demux_sys_t *p_sys = p_demux->p_sys;
424 i_offset = stream_Tell( p_demux->s );
426 while( ( i_offset >= p_sys->i_block_end )
427 && ( p_sys->i_silence_countdown == 0 ) )
428 if( ReadBlockHeader( p_demux ) != VLC_SUCCESS )
431 if( p_sys->i_silence_countdown == 0 )
433 i = ( p_sys->i_block_end - i_offset )
434 / p_sys->fmt.audio.i_bytes_per_frame;
435 if( i > SAMPLES_BUFFER )
438 p_block = stream_Block( p_demux->s,
439 p_sys->fmt.audio.i_bytes_per_frame * i );
440 if( p_block == NULL )
442 msg_Warn( p_demux, "cannot read data" );
447 { /* emulates silence from the stream */
448 i = p_sys->i_silence_countdown;
449 if( i > SAMPLES_BUFFER )
452 p_block = block_New( p_demux, i );
453 if( p_block == NULL )
456 memset( p_block->p_buffer, 0, i );
457 p_sys->i_silence_countdown -= i;
460 p_block->i_dts = p_block->i_pts =
461 date_Increment( &p_sys->pts, p_sys->fmt.audio.i_frame_length * i );
463 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
465 es_out_Send( p_demux->out, p_sys->p_es, p_block );
470 /*****************************************************************************
471 * Close: frees unused data
472 *****************************************************************************/
473 static void Close ( vlc_object_t * p_this )
475 demux_sys_t *p_sys = ((demux_t *)p_this)->p_sys;
480 /*****************************************************************************
482 *****************************************************************************/
483 static int Control( demux_t *p_demux, int i_query, va_list args )
485 demux_sys_t *p_sys = p_demux->p_sys;
487 return demux2_vaControlHelper( p_demux->s, p_sys->i_block_start,
489 p_sys->fmt.i_bitrate,
490 p_sys->fmt.audio.i_blockalign,