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 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_demux.h>
37 #include <vlc_codecs.h>
39 /*****************************************************************************
41 *****************************************************************************/
42 static int Open ( vlc_object_t
* );
43 static void Close( vlc_object_t
* );
46 set_description( N_("VOC demuxer") );
47 set_category( CAT_INPUT
);
48 set_subcategory( SUBCAT_INPUT_DEMUX
);
49 set_capability( "demux", 10 );
50 set_callbacks( Open
, Close
);
53 /*****************************************************************************
55 *****************************************************************************/
56 static int Demux ( demux_t
* );
57 static int Control( demux_t
*, int i_query
, va_list args
);
64 int64_t i_block_start
;
67 int64_t i_loop_offset
;
68 unsigned i_loop_count
;
69 unsigned i_silence_countdown
;
74 static const char ct_header
[] = "Creative Voice File\x1a";
76 /*****************************************************************************
77 * Open: check file and initializes structures
78 *****************************************************************************/
79 static int Open( vlc_object_t
* p_this
)
81 demux_t
*p_demux
= (demux_t
*)p_this
;
84 uint16_t i_data_offset
, i_version
;
86 if( stream_Peek( p_demux
->s
, &p_buf
, 26 ) < 26 )
89 if( memcmp( p_buf
, ct_header
, 20 ) )
93 i_data_offset
= GetWLE( p_buf
);
94 if ( i_data_offset
< 26 /* not enough room for full VOC header */ )
98 i_version
= GetWLE( p_buf
);
99 if( ( i_version
!= 0x10A ) && ( i_version
!= 0x114 ) )
100 return VLC_EGENERIC
; /* unknown VOC version */
103 if( GetWLE( p_buf
) != (uint16_t)(0x1234 + ~i_version
) )
106 /* We have a valid VOC header */
107 msg_Dbg( p_demux
, "CT Voice file v%d.%d", i_version
>> 8,
110 /* skip VOC header */
111 if( stream_Read( p_demux
->s
, NULL
, i_data_offset
) < i_data_offset
)
114 p_demux
->pf_demux
= Demux
;
115 p_demux
->pf_control
= Control
;
116 p_demux
->p_sys
= p_sys
= malloc( sizeof( demux_sys_t
) );
120 p_sys
->i_silence_countdown
= p_sys
->i_block_start
= p_sys
->i_block_end
=
121 p_sys
->i_loop_count
= 0;
124 date_Init( &p_sys
->pts
, 1, 1 );
125 date_Set( &p_sys
->pts
, 1 );
127 es_format_Init( &p_sys
->fmt
, AUDIO_ES
, 0 );
133 static int fmtcmp( es_format_t
*ofmt
, es_format_t
*nfmt
)
135 return (ofmt
->audio
.i_bitspersample
!= nfmt
->audio
.i_bitspersample
)
136 || (ofmt
->audio
.i_rate
!= nfmt
->audio
.i_rate
)
137 || (ofmt
->audio
.i_channels
!= nfmt
->audio
.i_channels
);
142 * Converts old-style VOC sample rates to commonly used ones
143 * so as not to confuse sound card drivers.
144 * (I assume 16k, 24k and 32k are never found in .VOC files)
146 static unsigned int fix_voc_sr( unsigned int sr
)
164 static int ReadBlockHeader( demux_t
*p_demux
)
168 int32_t i_block_size
;
169 demux_sys_t
*p_sys
= p_demux
->p_sys
;
171 if( stream_Read( p_demux
->s
, buf
, 4 ) < 4 )
172 return VLC_EGENERIC
; /* EOF */
174 i_block_size
= GetDWLE( buf
) >> 8;
175 msg_Dbg( p_demux
, "new block: type: %u, size: %u",
176 (unsigned)*buf
, i_block_size
);
178 es_format_Init( &new_fmt
, AUDIO_ES
, 0 );
182 case 0: /* not possible : caught with earlier stream_Read */
186 if( i_block_size
< 2 )
190 if( stream_Read( p_demux
->s
, buf
, 2 ) < 2 )
195 msg_Err( p_demux
, "unsupported compression" );
199 new_fmt
.i_codec
= VLC_FOURCC('u','8',' ',' ');
200 new_fmt
.audio
.i_rate
= fix_voc_sr( 1000000L / (256L - buf
[0]) );
201 new_fmt
.audio
.i_bytes_per_frame
= 1;
202 new_fmt
.audio
.i_frame_length
= 1;
203 new_fmt
.audio
.i_channels
= 1;
204 new_fmt
.audio
.i_blockalign
= 1;
205 new_fmt
.audio
.i_bitspersample
= 8;
206 new_fmt
.i_bitrate
= new_fmt
.audio
.i_rate
* 8;
209 case 2: /* data block with same format as the previous one */
210 if( p_sys
->p_es
== NULL
)
211 goto corrupt
; /* no previous block! */
213 memcpy( &new_fmt
, &p_sys
->fmt
, sizeof( new_fmt
) );
216 case 3: /* silence block */
217 if( ( i_block_size
!= 3 )
218 || ( stream_Read( p_demux
->s
, buf
, 3 ) < 3 ) )
222 p_sys
->i_silence_countdown
= GetWLE( buf
);
224 new_fmt
.i_codec
= VLC_FOURCC('u','8',' ',' ');
225 new_fmt
.audio
.i_rate
= fix_voc_sr( 1000000L / (256L - buf
[0]) );
226 new_fmt
.audio
.i_bytes_per_frame
= 1;
227 new_fmt
.audio
.i_frame_length
= 1;
228 new_fmt
.audio
.i_channels
= 1;
229 new_fmt
.audio
.i_blockalign
= 1;
230 new_fmt
.audio
.i_bitspersample
= 8;
231 new_fmt
.i_bitrate
= new_fmt
.audio
.i_rate
* 8;
234 case 6: /* repeat block */
235 if( ( i_block_size
!= 2 )
236 || ( stream_Read( p_demux
->s
, buf
, 2 ) < 2 ) )
240 p_sys
->i_loop_count
= GetWLE( buf
);
241 p_sys
->i_loop_offset
= stream_Tell( p_demux
->s
);
244 case 7: /* repeat end block */
245 if( i_block_size
!= 0 )
248 if( p_sys
->i_loop_count
> 0 )
250 if( stream_Seek( p_demux
->s
, p_sys
->i_loop_offset
) )
251 msg_Warn( p_demux
, "cannot loop: seek failed" );
253 p_sys
->i_loop_count
--;
259 * Block 8 is a big kludge to add stereo support to block 1 :
260 * A block of type 8 is always followed by a block of type 1
261 * and specifies the number of channels in that 1-block
262 * (normally block 1 are always mono). In practice, block type 9
263 * is used for stereo rather than 8
265 if( ( i_block_size
!= 4 )
266 || ( stream_Read( p_demux
->s
, buf
, 4 ) < 4 ) )
271 msg_Err( p_demux
, "unsupported compression" );
275 new_fmt
.i_codec
= VLC_FOURCC('u','8',' ',' ');
276 new_fmt
.audio
.i_channels
= buf
[3] + 1; /* can't be nul */
277 new_fmt
.audio
.i_rate
= 256000000L /
278 ((65536L - GetWLE(buf
)) * new_fmt
.audio
.i_channels
);
279 new_fmt
.audio
.i_bytes_per_frame
= new_fmt
.audio
.i_channels
;
280 new_fmt
.audio
.i_frame_length
= 1;
281 new_fmt
.audio
.i_blockalign
= new_fmt
.audio
.i_bytes_per_frame
;
282 new_fmt
.audio
.i_bitspersample
= 8 * new_fmt
.audio
.i_bytes_per_frame
;
283 new_fmt
.i_bitrate
= new_fmt
.audio
.i_rate
* 8;
285 /* read subsequent block 1 */
286 if( stream_Read( p_demux
->s
, buf
, 4 ) < 4 )
287 return VLC_EGENERIC
; /* EOF */
289 i_block_size
= GetDWLE( buf
) >> 8;
290 msg_Dbg( p_demux
, "new block: type: %u, size: %u",
291 (unsigned)*buf
, i_block_size
);
292 if( i_block_size
< 2 )
296 if( stream_Read( p_demux
->s
, buf
, 2 ) < 2 )
301 msg_Err( p_demux
, "unsupported compression" );
307 case 9: /* newer data block with channel number and bits resolution */
308 if( i_block_size
< 12 )
312 if( ( stream_Read( p_demux
->s
, buf
, 8 ) < 8 )
313 || ( stream_Read( p_demux
->s
, NULL
, 4 ) < 4 ) )
316 new_fmt
.audio
.i_rate
= GetDWLE( buf
);
317 new_fmt
.audio
.i_bitspersample
= buf
[4];
318 new_fmt
.audio
.i_channels
= buf
[5];
320 switch( GetWLE( &buf
[6] ) ) /* format */
322 case 0x0000: /* PCM */
323 switch( new_fmt
.audio
.i_bitspersample
)
326 new_fmt
.i_codec
= VLC_FOURCC('u','8',' ',' ');
330 new_fmt
.i_codec
= VLC_FOURCC('u','1','6','l');
334 msg_Err( p_demux
, "unsupported bit res.: %u bits",
335 new_fmt
.audio
.i_bitspersample
);
340 case 0x0004: /* signed */
341 switch( new_fmt
.audio
.i_bitspersample
)
344 new_fmt
.i_codec
= VLC_FOURCC('s','8',' ',' ');
348 new_fmt
.i_codec
= VLC_FOURCC('s','1','6','l');
352 msg_Err( p_demux
, "unsupported bit res.: %u bits",
353 new_fmt
.audio
.i_bitspersample
);
359 msg_Err( p_demux
, "unsupported compression" );
363 new_fmt
.audio
.i_bytes_per_frame
= new_fmt
.audio
.i_channels
364 * (new_fmt
.audio
.i_bitspersample
/ 8);
365 new_fmt
.audio
.i_frame_length
= 1;
366 new_fmt
.audio
.i_blockalign
= new_fmt
.audio
.i_bytes_per_frame
;
367 new_fmt
.i_bitrate
= 8 * new_fmt
.audio
.i_rate
368 * new_fmt
.audio
.i_bytes_per_frame
;
372 msg_Dbg( p_demux
, "unknown block type %u - skipping block",
374 case 4: /* blocks of non-audio types can be skipped */
376 if( stream_Read( p_demux
->s
, NULL
, i_block_size
) < i_block_size
)
382 p_sys
->i_block_start
= stream_Tell( p_demux
->s
);
383 p_sys
->i_block_end
= p_sys
->i_block_start
+ i_block_size
;
385 if( i_block_size
|| p_sys
->i_silence_countdown
)
387 /* we've read a block with data in it - update decoder */
388 msg_Dbg( p_demux
, "fourcc: %4.4s, channels: %d, "
389 "freq: %d Hz, bitrate: %dKo/s, blockalign: %d, "
390 "bits/samples: %d", (char *)&new_fmt
.i_codec
,
391 new_fmt
.audio
.i_channels
, new_fmt
.audio
.i_rate
,
392 new_fmt
.i_bitrate
/ 8192, new_fmt
.audio
.i_blockalign
,
393 new_fmt
.audio
.i_bitspersample
);
395 if( ( p_sys
->p_es
!= NULL
) && fmtcmp( &p_sys
->fmt
, &new_fmt
) )
397 msg_Dbg( p_demux
, "codec change needed" );
398 es_out_Del( p_demux
->out
, p_sys
->p_es
);
402 if( p_sys
->p_es
== NULL
)
404 memcpy( &p_sys
->fmt
, &new_fmt
, sizeof( p_sys
->fmt
) );
405 date_Change( &p_sys
->pts
, p_sys
->fmt
.audio
.i_rate
, 1 );
406 p_sys
->p_es
= es_out_Add( p_demux
->out
, &p_sys
->fmt
);
413 msg_Err( p_demux
, "corrupted file - halting demux" );
417 /*****************************************************************************
418 * Demux: read packet and send them to decoders
419 *****************************************************************************
420 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
421 *****************************************************************************/
422 #define SAMPLES_BUFFER 1000
423 static int Demux( demux_t
*p_demux
)
425 demux_sys_t
*p_sys
= p_demux
->p_sys
;
429 i_offset
= stream_Tell( p_demux
->s
);
431 while( ( i_offset
>= p_sys
->i_block_end
)
432 && ( p_sys
->i_silence_countdown
== 0 ) )
433 if( ReadBlockHeader( p_demux
) != VLC_SUCCESS
)
436 if( p_sys
->i_silence_countdown
== 0 )
438 i
= ( p_sys
->i_block_end
- i_offset
)
439 / p_sys
->fmt
.audio
.i_bytes_per_frame
;
440 if( i
> SAMPLES_BUFFER
)
443 p_block
= stream_Block( p_demux
->s
,
444 p_sys
->fmt
.audio
.i_bytes_per_frame
* i
);
445 if( p_block
== NULL
)
447 msg_Warn( p_demux
, "cannot read data" );
452 { /* emulates silence from the stream */
453 i
= p_sys
->i_silence_countdown
;
454 if( i
> SAMPLES_BUFFER
)
457 p_block
= block_New( p_demux
, i
);
458 if( p_block
== NULL
)
461 memset( p_block
->p_buffer
, 0, i
);
462 p_sys
->i_silence_countdown
-= i
;
465 p_block
->i_dts
= p_block
->i_pts
=
466 date_Increment( &p_sys
->pts
, p_sys
->fmt
.audio
.i_frame_length
* i
);
468 es_out_Control( p_demux
->out
, ES_OUT_SET_PCR
, p_block
->i_pts
);
470 es_out_Send( p_demux
->out
, p_sys
->p_es
, p_block
);
475 /*****************************************************************************
476 * Close: frees unused data
477 *****************************************************************************/
478 static void Close ( vlc_object_t
* p_this
)
480 demux_sys_t
*p_sys
= ((demux_t
*)p_this
)->p_sys
;
485 /*****************************************************************************
487 *****************************************************************************/
488 static int Control( demux_t
*p_demux
, int i_query
, va_list args
)
490 demux_sys_t
*p_sys
= p_demux
->p_sys
;
492 return demux_vaControlHelper( p_demux
->s
, p_sys
->i_block_start
,
494 p_sys
->fmt
.i_bitrate
,
495 p_sys
->fmt
.audio
.i_blockalign
,