ffmpeg: targetos must now be in lower case.
[vlc.git] / modules / demux / voc.c
bloba772c887b13f9ea48c8ecbd9ab46d6a7d7569b55
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 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
29 #include <vlc/vlc.h>
30 #include <vlc_demux.h>
31 #include <vlc_aout.h>
33 #include <vlc_codecs.h>
35 /*****************************************************************************
36 * Module descriptor
37 *****************************************************************************/
38 static int Open ( vlc_object_t * );
39 static void Close( vlc_object_t * );
41 vlc_module_begin();
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 );
47 vlc_module_end();
49 /*****************************************************************************
50 * Local prototypes
51 *****************************************************************************/
52 static int Demux ( demux_t * );
53 static int Control( demux_t *, int i_query, va_list args );
55 struct demux_sys_t
57 es_format_t fmt;
58 es_out_id_t *p_es;
60 int64_t i_block_start;
61 int64_t i_block_end;
63 int64_t i_loop_offset;
64 unsigned i_loop_count;
65 unsigned i_silence_countdown;
67 date_t pts;
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;
78 demux_sys_t *p_sys;
79 uint8_t *p_buf;
80 uint16_t i_data_offset, i_version;
82 if( stream_Peek( p_demux->s, &p_buf, 26 ) < 26 )
83 return VLC_EGENERIC;
85 if( memcmp( p_buf, ct_header, 20 ) )
86 return VLC_EGENERIC;
87 p_buf += 20;
89 i_data_offset = GetWLE( p_buf );
90 if ( i_data_offset < 26 /* not enough room for full VOC header */ )
91 return VLC_EGENERIC;
92 p_buf += 2;
94 i_version = GetWLE( p_buf );
95 if( ( i_version != 0x10A ) && ( i_version != 0x114 ) )
96 return VLC_EGENERIC; /* unknown VOC version */
97 p_buf += 2;
99 if( GetWLE( p_buf ) != (uint16_t)(0x1234 + ~i_version) )
100 return VLC_EGENERIC;
102 /* We have a valid VOC header */
103 msg_Dbg( p_demux, "CT Voice file v%d.%d", i_version >> 8,
104 i_version & 0xff );
106 /* skip VOC header */
107 if( stream_Read( p_demux->s, NULL, i_data_offset ) < i_data_offset )
108 return VLC_EGENERIC;
110 p_demux->pf_demux = Demux;
111 p_demux->pf_control = Control;
112 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
113 if( p_sys == NULL )
114 return VLC_ENOMEM;
116 p_sys->i_silence_countdown = p_sys->i_block_start = p_sys->i_block_end =
117 p_sys->i_loop_count = 0;
118 p_sys->p_es = NULL;
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 );
125 return VLC_SUCCESS;
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 )
144 switch( sr )
146 /*case 8000:
147 return 8000;*/
148 case 11111:
149 return 11025;
151 case 22222:
152 return 22050;
154 case 44444:
155 return 44100;
157 return sr;
160 static int ReadBlockHeader( demux_t *p_demux )
162 es_format_t new_fmt;
163 uint8_t buf[8];
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 );
176 switch( *buf )
178 case 0: /* not possible : caught with earlier stream_Read */
179 goto corrupt;
181 case 1:
182 if( i_block_size < 2 )
183 goto corrupt;
184 i_block_size -= 2;
186 if( stream_Read( p_demux->s, buf, 2 ) < 2 )
187 goto corrupt;
189 if( buf[1] )
191 msg_Err( p_demux, "unsupported compression" );
192 return VLC_EGENERIC;
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;
203 break;
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 ) );
210 break;
212 case 3: /* silence block */
213 if( ( i_block_size != 3 )
214 || ( stream_Read( p_demux->s, buf, 3 ) < 3 ) )
215 goto corrupt;
217 i_block_size = 0;
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;
228 break;
230 case 6: /* repeat block */
231 if( ( i_block_size != 2 )
232 || ( stream_Read( p_demux->s, buf, 2 ) < 2 ) )
233 goto corrupt;
235 i_block_size = 0;
236 p_sys->i_loop_count = GetWLE( buf );
237 p_sys->i_loop_offset = stream_Tell( p_demux->s );
238 break;
240 case 7: /* repeat end block */
241 if( i_block_size != 0 )
242 goto corrupt;
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" );
248 else
249 p_sys->i_loop_count--;
251 break;
253 case 8:
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 ) )
263 goto corrupt;
265 if( buf[2] )
267 msg_Err( p_demux, "unsupported compression" );
268 return VLC_EGENERIC;
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 )
289 goto corrupt;
290 i_block_size -= 2;
292 if( stream_Read( p_demux->s, buf, 2 ) < 2 )
293 goto corrupt;
295 if( buf[1] )
297 msg_Err( p_demux, "unsupported compression" );
298 return VLC_EGENERIC;
301 break;
303 case 9: /* newer data block with channel number and bits resolution */
304 if( i_block_size < 12 )
305 goto corrupt;
306 i_block_size -= 12;
308 if( ( stream_Read( p_demux->s, buf, 8 ) < 8 )
309 || ( stream_Read( p_demux->s, NULL, 4 ) < 4 ) )
310 goto corrupt;
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 )
321 case 8:
322 new_fmt.i_codec = VLC_FOURCC('u','8',' ',' ');
323 break;
325 case 16:
326 new_fmt.i_codec = VLC_FOURCC('u','1','6','l');
327 break;
329 default:
330 msg_Err( p_demux, "unsupported bit res.: %u bits",
331 new_fmt.audio.i_bitspersample );
332 return VLC_EGENERIC;
334 break;
336 case 0x0004: /* signed */
337 switch( new_fmt.audio.i_bitspersample )
339 case 8:
340 new_fmt.i_codec = VLC_FOURCC('s','8',' ',' ');
341 break;
343 case 16:
344 new_fmt.i_codec = VLC_FOURCC('s','1','6','l');
345 break;
347 default:
348 msg_Err( p_demux, "unsupported bit res.: %u bits",
349 new_fmt.audio.i_bitspersample );
350 return VLC_EGENERIC;
352 break;
354 default:
355 msg_Err( p_demux, "unsupported compression" );
356 return VLC_EGENERIC;
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;
365 break;
367 default:
368 msg_Dbg( p_demux, "unknown block type %u - skipping block",
369 (unsigned)*buf);
370 case 4: /* blocks of non-audio types can be skipped */
371 case 5:
372 if( stream_Read( p_demux->s, NULL, i_block_size ) < i_block_size )
373 goto corrupt;
374 i_block_size = 0;
375 break;
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 );
395 p_sys->p_es = NULL;
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 );
406 return VLC_SUCCESS;
408 corrupt:
409 msg_Err( p_demux, "corrupted file - halting demux" );
410 return VLC_EGENERIC;
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;
422 block_t *p_block;
423 int64_t i_offset, i;
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 )
430 return 0;
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 )
437 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" );
444 return 0;
447 else
448 { /* emulates silence from the stream */
449 i = p_sys->i_silence_countdown;
450 if( i > SAMPLES_BUFFER )
451 i = SAMPLES_BUFFER;
453 p_block = block_New( p_demux, i );
454 if( p_block == NULL )
455 return VLC_ENOMEM;
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 );
468 return 1;
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;
478 free( p_sys );
481 /*****************************************************************************
482 * Control:
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,
489 p_sys->i_block_end,
490 p_sys->fmt.i_bitrate,
491 p_sys->fmt.audio.i_blockalign,
492 i_query, args );