xspf: new playlist design
[vlc.git] / modules / demux / voc.c
blob3cf8887d44aa26bc0edd1ba31771d221662054c0
1 /*****************************************************************************
2 * voc.c : Creative Voice File (.VOC) demux module for vlc
3 *****************************************************************************
4 * Copyright (C) 2005 Rémi Denis-Courmont
5 * $Id$
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 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_demux.h>
35 #include <vlc_aout.h>
37 #include <vlc_codecs.h>
39 /*****************************************************************************
40 * Module descriptor
41 *****************************************************************************/
42 static int Open ( vlc_object_t * );
43 static void Close( vlc_object_t * );
45 vlc_module_begin ()
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 )
51 vlc_module_end ()
53 /*****************************************************************************
54 * Local prototypes
55 *****************************************************************************/
56 static int Demux ( demux_t * );
57 static int Control( demux_t *, int i_query, va_list args );
59 struct demux_sys_t
61 es_format_t fmt;
62 es_out_id_t *p_es;
64 int64_t i_block_start;
65 int64_t i_block_end;
67 int64_t i_loop_offset;
68 unsigned i_loop_count;
69 unsigned i_silence_countdown;
71 date_t pts;
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;
82 demux_sys_t *p_sys;
83 const uint8_t *p_buf;
84 uint16_t i_data_offset, i_version;
86 if( stream_Peek( p_demux->s, &p_buf, 26 ) < 26 )
87 return VLC_EGENERIC;
89 if( memcmp( p_buf, ct_header, 20 ) )
90 return VLC_EGENERIC;
91 p_buf += 20;
93 i_data_offset = GetWLE( p_buf );
94 if ( i_data_offset < 26 /* not enough room for full VOC header */ )
95 return VLC_EGENERIC;
96 p_buf += 2;
98 i_version = GetWLE( p_buf );
99 if( ( i_version != 0x10A ) && ( i_version != 0x114 ) )
100 return VLC_EGENERIC; /* unknown VOC version */
101 p_buf += 2;
103 if( GetWLE( p_buf ) != (uint16_t)(0x1234 + ~i_version) )
104 return VLC_EGENERIC;
106 /* We have a valid VOC header */
107 msg_Dbg( p_demux, "CT Voice file v%d.%d", i_version >> 8,
108 i_version & 0xff );
110 /* skip VOC header */
111 if( stream_Read( p_demux->s, NULL, i_data_offset ) < i_data_offset )
112 return VLC_EGENERIC;
114 p_demux->pf_demux = Demux;
115 p_demux->pf_control = Control;
116 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
117 if( p_sys == NULL )
118 return VLC_ENOMEM;
120 p_sys->i_silence_countdown = p_sys->i_block_start = p_sys->i_block_end =
121 p_sys->i_loop_count = 0;
122 p_sys->p_es = NULL;
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 );
129 return VLC_SUCCESS;
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 )
148 switch( sr )
150 /*case 8000:
151 return 8000;*/
152 case 11111:
153 return 11025;
155 case 22222:
156 return 22050;
158 case 44444:
159 return 44100;
161 return sr;
164 static int ReadBlockHeader( demux_t *p_demux )
166 es_format_t new_fmt;
167 uint8_t buf[8];
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 );
180 switch( *buf )
182 case 0: /* not possible : caught with earlier stream_Read */
183 goto corrupt;
185 case 1:
186 if( i_block_size < 2 )
187 goto corrupt;
188 i_block_size -= 2;
190 if( stream_Read( p_demux->s, buf, 2 ) < 2 )
191 goto corrupt;
193 if( buf[1] )
195 msg_Err( p_demux, "unsupported compression" );
196 return VLC_EGENERIC;
199 new_fmt.i_codec = VLC_CODEC_U8;
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;
207 break;
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 ) );
214 break;
216 case 3: /* silence block */
217 if( ( i_block_size != 3 )
218 || ( stream_Read( p_demux->s, buf, 3 ) < 3 ) )
219 goto corrupt;
221 i_block_size = 0;
222 p_sys->i_silence_countdown = GetWLE( buf );
224 new_fmt.i_codec = VLC_CODEC_U8;
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;
232 break;
234 case 6: /* repeat block */
235 if( ( i_block_size != 2 )
236 || ( stream_Read( p_demux->s, buf, 2 ) < 2 ) )
237 goto corrupt;
239 i_block_size = 0;
240 p_sys->i_loop_count = GetWLE( buf );
241 p_sys->i_loop_offset = stream_Tell( p_demux->s );
242 break;
244 case 7: /* repeat end block */
245 if( i_block_size != 0 )
246 goto corrupt;
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" );
252 else
253 p_sys->i_loop_count--;
255 break;
257 case 8:
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 ) )
267 goto corrupt;
269 if( buf[2] )
271 msg_Err( p_demux, "unsupported compression" );
272 return VLC_EGENERIC;
275 new_fmt.i_codec = VLC_CODEC_U8;
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 )
293 goto corrupt;
294 i_block_size -= 2;
296 if( stream_Read( p_demux->s, buf, 2 ) < 2 )
297 goto corrupt;
299 if( buf[1] )
301 msg_Err( p_demux, "unsupported compression" );
302 return VLC_EGENERIC;
305 break;
307 case 9: /* newer data block with channel number and bits resolution */
308 if( i_block_size < 12 )
309 goto corrupt;
310 i_block_size -= 12;
312 if( ( stream_Read( p_demux->s, buf, 8 ) < 8 )
313 || ( stream_Read( p_demux->s, NULL, 4 ) < 4 ) )
314 goto corrupt;
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 )
325 case 8:
326 new_fmt.i_codec = VLC_CODEC_U8;
327 break;
329 case 16:
330 new_fmt.i_codec = VLC_CODEC_U16L;
331 break;
333 default:
334 msg_Err( p_demux, "unsupported bit res.: %u bits",
335 new_fmt.audio.i_bitspersample );
336 return VLC_EGENERIC;
338 break;
340 case 0x0004: /* signed */
341 switch( new_fmt.audio.i_bitspersample )
343 case 8:
344 new_fmt.i_codec = VLC_CODEC_S8;
345 break;
347 case 16:
348 new_fmt.i_codec = VLC_CODEC_S16L;
349 break;
351 default:
352 msg_Err( p_demux, "unsupported bit res.: %u bits",
353 new_fmt.audio.i_bitspersample );
354 return VLC_EGENERIC;
356 break;
358 default:
359 msg_Err( p_demux, "unsupported compression" );
360 return VLC_EGENERIC;
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;
369 break;
371 default:
372 msg_Dbg( p_demux, "unknown block type %u - skipping block",
373 (unsigned)*buf);
374 case 4: /* blocks of non-audio types can be skipped */
375 case 5:
376 if( stream_Read( p_demux->s, NULL, i_block_size ) < i_block_size )
377 goto corrupt;
378 i_block_size = 0;
379 break;
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 );
399 p_sys->p_es = NULL;
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 );
410 return VLC_SUCCESS;
412 corrupt:
413 msg_Err( p_demux, "corrupted file - halting demux" );
414 return VLC_EGENERIC;
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;
426 block_t *p_block;
427 int64_t i_offset, i;
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 )
434 return 0;
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 )
441 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" );
448 return 0;
451 else
452 { /* emulates silence from the stream */
453 i = p_sys->i_silence_countdown;
454 if( i > SAMPLES_BUFFER )
455 i = SAMPLES_BUFFER;
457 p_block = block_New( p_demux, i );
458 if( p_block == NULL )
459 return VLC_ENOMEM;
461 memset( p_block->p_buffer, 0, i );
462 p_sys->i_silence_countdown -= i;
465 p_block->i_dts = p_block->i_pts = VLC_TS_0 + date_Get( &p_sys->pts );
467 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
469 es_out_Send( p_demux->out, p_sys->p_es, p_block );
471 date_Increment( &p_sys->pts, p_sys->fmt.audio.i_frame_length * i );
473 return 1;
476 /*****************************************************************************
477 * Close: frees unused data
478 *****************************************************************************/
479 static void Close ( vlc_object_t * p_this )
481 demux_sys_t *p_sys = ((demux_t *)p_this)->p_sys;
483 free( p_sys );
486 /*****************************************************************************
487 * Control:
488 *****************************************************************************/
489 static int Control( demux_t *p_demux, int i_query, va_list args )
491 demux_sys_t *p_sys = p_demux->p_sys;
493 return demux_vaControlHelper( p_demux->s, p_sys->i_block_start,
494 p_sys->i_block_end,
495 p_sys->fmt.i_bitrate,
496 p_sys->fmt.audio.i_blockalign,
497 i_query, args );