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 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
30 #include <vlc_demux.h>
33 #include <vlc_codecs.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
);
138 * Converts old-style VOC sample rates to commonly used ones
139 * so as not to confuse sound card drivers.
140 * (I assume 16k, 24k and 32k are never found in .VOC files)
142 static unsigned int fix_voc_sr( unsigned int sr
)
160 static int ReadBlockHeader( demux_t
*p_demux
)
164 int32_t i_block_size
;
165 demux_sys_t
*p_sys
= p_demux
->p_sys
;
167 if( stream_Read( p_demux
->s
, buf
, 4 ) < 4 )
168 return VLC_EGENERIC
; /* EOF */
170 i_block_size
= GetDWLE( buf
) >> 8;
171 msg_Dbg( p_demux
, "new block: type: %u, size: %u",
172 (unsigned)*buf
, i_block_size
);
174 es_format_Init( &new_fmt
, AUDIO_ES
, 0 );
178 case 0: /* not possible : caught with earlier stream_Read */
182 if( i_block_size
< 2 )
186 if( stream_Read( p_demux
->s
, buf
, 2 ) < 2 )
191 msg_Err( p_demux
, "unsupported compression" );
195 new_fmt
.i_codec
= VLC_FOURCC('u','8',' ',' ');
196 new_fmt
.audio
.i_rate
= fix_voc_sr( 1000000L / (256L - buf
[0]) );
197 new_fmt
.audio
.i_bytes_per_frame
= 1;
198 new_fmt
.audio
.i_frame_length
= 1;
199 new_fmt
.audio
.i_channels
= 1;
200 new_fmt
.audio
.i_blockalign
= 1;
201 new_fmt
.audio
.i_bitspersample
= 8;
202 new_fmt
.i_bitrate
= new_fmt
.audio
.i_rate
* 8;
205 case 2: /* data block with same format as the previous one */
206 if( p_sys
->p_es
== NULL
)
207 goto corrupt
; /* no previous block! */
209 memcpy( &new_fmt
, &p_sys
->fmt
, sizeof( new_fmt
) );
212 case 3: /* silence block */
213 if( ( i_block_size
!= 3 )
214 || ( stream_Read( p_demux
->s
, buf
, 3 ) < 3 ) )
218 p_sys
->i_silence_countdown
= GetWLE( buf
);
220 new_fmt
.i_codec
= VLC_FOURCC('u','8',' ',' ');
221 new_fmt
.audio
.i_rate
= fix_voc_sr( 1000000L / (256L - buf
[0]) );
222 new_fmt
.audio
.i_bytes_per_frame
= 1;
223 new_fmt
.audio
.i_frame_length
= 1;
224 new_fmt
.audio
.i_channels
= 1;
225 new_fmt
.audio
.i_blockalign
= 1;
226 new_fmt
.audio
.i_bitspersample
= 8;
227 new_fmt
.i_bitrate
= new_fmt
.audio
.i_rate
* 8;
230 case 6: /* repeat block */
231 if( ( i_block_size
!= 2 )
232 || ( stream_Read( p_demux
->s
, buf
, 2 ) < 2 ) )
236 p_sys
->i_loop_count
= GetWLE( buf
);
237 p_sys
->i_loop_offset
= stream_Tell( p_demux
->s
);
240 case 7: /* repeat end block */
241 if( i_block_size
!= 0 )
244 if( p_sys
->i_loop_count
> 0 )
246 if( stream_Seek( p_demux
->s
, p_sys
->i_loop_offset
) )
247 msg_Warn( p_demux
, "cannot loop: seek failed" );
249 p_sys
->i_loop_count
--;
255 * Block 8 is a big kludge to add stereo support to block 1 :
256 * A block of type 8 is always followed by a block of type 1
257 * and specifies the number of channels in that 1-block
258 * (normally block 1 are always mono). In practice, block type 9
259 * is used for stereo rather than 8
261 if( ( i_block_size
!= 4 )
262 || ( stream_Read( p_demux
->s
, buf
, 4 ) < 4 ) )
267 msg_Err( p_demux
, "unsupported compression" );
271 new_fmt
.i_codec
= VLC_FOURCC('u','8',' ',' ');
272 new_fmt
.audio
.i_channels
= buf
[3] + 1; /* can't be nul */
273 new_fmt
.audio
.i_rate
= 256000000L /
274 ((65536L - GetWLE(buf
)) * new_fmt
.audio
.i_channels
);
275 new_fmt
.audio
.i_bytes_per_frame
= new_fmt
.audio
.i_channels
;
276 new_fmt
.audio
.i_frame_length
= 1;
277 new_fmt
.audio
.i_blockalign
= new_fmt
.audio
.i_bytes_per_frame
;
278 new_fmt
.audio
.i_bitspersample
= 8 * new_fmt
.audio
.i_bytes_per_frame
;
279 new_fmt
.i_bitrate
= new_fmt
.audio
.i_rate
* 8;
281 /* read subsequent block 1 */
282 if( stream_Read( p_demux
->s
, buf
, 4 ) < 4 )
283 return VLC_EGENERIC
; /* EOF */
285 i_block_size
= GetDWLE( buf
) >> 8;
286 msg_Dbg( p_demux
, "new block: type: %u, size: %u",
287 (unsigned)*buf
, i_block_size
);
288 if( i_block_size
< 2 )
292 if( stream_Read( p_demux
->s
, buf
, 2 ) < 2 )
297 msg_Err( p_demux
, "unsupported compression" );
303 case 9: /* newer data block with channel number and bits resolution */
304 if( i_block_size
< 12 )
308 if( ( stream_Read( p_demux
->s
, buf
, 8 ) < 8 )
309 || ( stream_Read( p_demux
->s
, NULL
, 4 ) < 4 ) )
312 new_fmt
.audio
.i_rate
= GetDWLE( buf
);
313 new_fmt
.audio
.i_bitspersample
= buf
[4];
314 new_fmt
.audio
.i_channels
= buf
[5];
316 switch( GetWLE( &buf
[6] ) ) /* format */
318 case 0x0000: /* PCM */
319 switch( new_fmt
.audio
.i_bitspersample
)
322 new_fmt
.i_codec
= VLC_FOURCC('u','8',' ',' ');
326 new_fmt
.i_codec
= VLC_FOURCC('u','1','6','l');
330 msg_Err( p_demux
, "unsupported bit res.: %u bits",
331 new_fmt
.audio
.i_bitspersample
);
336 case 0x0004: /* signed */
337 switch( new_fmt
.audio
.i_bitspersample
)
340 new_fmt
.i_codec
= VLC_FOURCC('s','8',' ',' ');
344 new_fmt
.i_codec
= VLC_FOURCC('s','1','6','l');
348 msg_Err( p_demux
, "unsupported bit res.: %u bits",
349 new_fmt
.audio
.i_bitspersample
);
355 msg_Err( p_demux
, "unsupported compression" );
359 new_fmt
.audio
.i_bytes_per_frame
= new_fmt
.audio
.i_channels
360 * (new_fmt
.audio
.i_bitspersample
/ 8);
361 new_fmt
.audio
.i_frame_length
= 1;
362 new_fmt
.audio
.i_blockalign
= new_fmt
.audio
.i_bytes_per_frame
;
363 new_fmt
.i_bitrate
= 8 * new_fmt
.audio
.i_rate
364 * new_fmt
.audio
.i_bytes_per_frame
;
368 msg_Dbg( p_demux
, "unknown block type %u - skipping block",
370 case 4: /* blocks of non-audio types can be skipped */
372 if( stream_Read( p_demux
->s
, NULL
, i_block_size
) < i_block_size
)
378 p_sys
->i_block_start
= stream_Tell( p_demux
->s
);
379 p_sys
->i_block_end
= p_sys
->i_block_start
+ i_block_size
;
381 if( i_block_size
|| p_sys
->i_silence_countdown
)
383 /* we've read a block with data in it - update decoder */
384 msg_Dbg( p_demux
, "fourcc: %4.4s, channels: %d, "
385 "freq: %d Hz, bitrate: %dKo/s, blockalign: %d, "
386 "bits/samples: %d", (char *)&new_fmt
.i_codec
,
387 new_fmt
.audio
.i_channels
, new_fmt
.audio
.i_rate
,
388 new_fmt
.i_bitrate
/ 8192, new_fmt
.audio
.i_blockalign
,
389 new_fmt
.audio
.i_bitspersample
);
391 if( ( p_sys
->p_es
!= NULL
) && fmtcmp( &p_sys
->fmt
, &new_fmt
) )
393 msg_Dbg( p_demux
, "codec change needed" );
394 es_out_Del( p_demux
->out
, p_sys
->p_es
);
398 if( p_sys
->p_es
== NULL
)
400 memcpy( &p_sys
->fmt
, &new_fmt
, sizeof( p_sys
->fmt
) );
401 date_Change( &p_sys
->pts
, p_sys
->fmt
.audio
.i_rate
, 1 );
402 p_sys
->p_es
= es_out_Add( p_demux
->out
, &p_sys
->fmt
);
409 msg_Err( p_demux
, "corrupted file - halting demux" );
413 /*****************************************************************************
414 * Demux: read packet and send them to decoders
415 *****************************************************************************
416 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
417 *****************************************************************************/
418 #define SAMPLES_BUFFER 1000
419 static int Demux( demux_t
*p_demux
)
421 demux_sys_t
*p_sys
= p_demux
->p_sys
;
425 i_offset
= stream_Tell( p_demux
->s
);
427 while( ( i_offset
>= p_sys
->i_block_end
)
428 && ( p_sys
->i_silence_countdown
== 0 ) )
429 if( ReadBlockHeader( p_demux
) != VLC_SUCCESS
)
432 if( p_sys
->i_silence_countdown
== 0 )
434 i
= ( p_sys
->i_block_end
- i_offset
)
435 / p_sys
->fmt
.audio
.i_bytes_per_frame
;
436 if( i
> SAMPLES_BUFFER
)
439 p_block
= stream_Block( p_demux
->s
,
440 p_sys
->fmt
.audio
.i_bytes_per_frame
* i
);
441 if( p_block
== NULL
)
443 msg_Warn( p_demux
, "cannot read data" );
448 { /* emulates silence from the stream */
449 i
= p_sys
->i_silence_countdown
;
450 if( i
> SAMPLES_BUFFER
)
453 p_block
= block_New( p_demux
, i
);
454 if( p_block
== NULL
)
457 memset( p_block
->p_buffer
, 0, i
);
458 p_sys
->i_silence_countdown
-= i
;
461 p_block
->i_dts
= p_block
->i_pts
=
462 date_Increment( &p_sys
->pts
, p_sys
->fmt
.audio
.i_frame_length
* i
);
464 es_out_Control( p_demux
->out
, ES_OUT_SET_PCR
, p_block
->i_pts
);
466 es_out_Send( p_demux
->out
, p_sys
->p_es
, p_block
);
471 /*****************************************************************************
472 * Close: frees unused data
473 *****************************************************************************/
474 static void Close ( vlc_object_t
* p_this
)
476 demux_sys_t
*p_sys
= ((demux_t
*)p_this
)->p_sys
;
481 /*****************************************************************************
483 *****************************************************************************/
484 static int Control( demux_t
*p_demux
, int i_query
, va_list args
)
486 demux_sys_t
*p_sys
= p_demux
->p_sys
;
488 return demux2_vaControlHelper( p_demux
->s
, p_sys
->i_block_start
,
490 p_sys
->fmt
.i_bitrate
,
491 p_sys
->fmt
.audio
.i_blockalign
,