chromecast: test encoder modules
[vlc.git] / modules / demux / voc.c
blob33e6ecf958b7521319de8c28f2dc488cb31df3ae
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 it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * 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>
36 /*****************************************************************************
37 * Module descriptor
38 *****************************************************************************/
39 static int Open ( vlc_object_t * );
40 static void Close( vlc_object_t * );
42 vlc_module_begin ()
43 set_description( N_("VOC demuxer") )
44 set_category( CAT_INPUT )
45 set_subcategory( SUBCAT_INPUT_DEMUX )
46 set_capability( "demux", 10 )
47 set_callbacks( Open, Close )
48 vlc_module_end ()
50 /*****************************************************************************
51 * Local prototypes
52 *****************************************************************************/
53 static int Demux ( demux_t * );
54 static int Control( demux_t *, int i_query, va_list args );
56 struct demux_sys_t
58 es_format_t fmt;
59 es_out_id_t *p_es;
61 int64_t i_block_start;
62 int64_t i_block_end;
64 int64_t i_loop_offset;
65 unsigned i_loop_count;
66 unsigned i_silence_countdown;
68 date_t pts;
71 static const char ct_header[] = "Creative Voice File\x1a";
73 /*****************************************************************************
74 * Open: check file and initializes structures
75 *****************************************************************************/
76 static int Open( vlc_object_t * p_this )
78 demux_t *p_demux = (demux_t*)p_this;
79 demux_sys_t *p_sys;
80 const uint8_t *p_buf;
81 uint16_t i_data_offset, i_version;
83 if( vlc_stream_Peek( p_demux->s, &p_buf, 26 ) < 26 )
84 return VLC_EGENERIC;
86 if( memcmp( p_buf, ct_header, 20 ) )
87 return VLC_EGENERIC;
88 p_buf += 20;
90 i_data_offset = GetWLE( p_buf );
91 if ( i_data_offset < 26 /* not enough room for full VOC header */ )
92 return VLC_EGENERIC;
93 p_buf += 2;
95 i_version = GetWLE( p_buf );
96 if( ( i_version != 0x10A ) && ( i_version != 0x114 ) )
97 return VLC_EGENERIC; /* unknown VOC version */
98 p_buf += 2;
100 if( GetWLE( p_buf ) != (uint16_t)(0x1234 + ~i_version) )
101 return VLC_EGENERIC;
103 /* We have a valid VOC header */
104 msg_Dbg( p_demux, "CT Voice file v%d.%d", i_version >> 8,
105 i_version & 0xff );
107 /* skip VOC header */
108 if( vlc_stream_Read( p_demux->s, NULL, i_data_offset ) < i_data_offset )
109 return VLC_EGENERIC;
111 p_demux->pf_demux = Demux;
112 p_demux->pf_control = Control;
113 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
114 if( p_sys == NULL )
115 return VLC_ENOMEM;
117 p_sys->i_silence_countdown = p_sys->i_block_start = p_sys->i_block_end =
118 p_sys->i_loop_count = 0;
119 p_sys->p_es = NULL;
121 date_Init( &p_sys->pts, 1, 1 );
122 date_Set( &p_sys->pts, 1 );
124 es_format_Init( &p_sys->fmt, AUDIO_ES, 0 );
126 return VLC_SUCCESS;
130 static int fmtcmp( es_format_t *ofmt, es_format_t *nfmt )
132 return (ofmt->audio.i_bitspersample != nfmt->audio.i_bitspersample)
133 || (ofmt->audio.i_rate != nfmt->audio.i_rate)
134 || (ofmt->audio.i_channels != nfmt->audio.i_channels);
139 * Converts old-style VOC sample rates to commonly used ones
140 * so as not to confuse sound card drivers.
141 * (I assume 16k, 24k and 32k are never found in .VOC files)
143 static unsigned int fix_voc_sr( unsigned int sr )
145 switch( sr )
147 /*case 8000:
148 return 8000;*/
149 case 11111:
150 return 11025;
152 case 22222:
153 return 22050;
155 case 44444:
156 return 44100;
158 return sr;
161 static int ReadBlockHeader( demux_t *p_demux )
163 es_format_t new_fmt;
164 uint8_t buf[8];
165 int32_t i_block_size;
166 demux_sys_t *p_sys = p_demux->p_sys;
168 if( vlc_stream_Read( p_demux->s, buf, 4 ) < 4 )
169 return VLC_EGENERIC; /* EOF */
171 i_block_size = GetDWLE( buf ) >> 8;
172 msg_Dbg( p_demux, "new block: type: %u, size: %u",
173 (unsigned)*buf, i_block_size );
175 es_format_Init( &new_fmt, AUDIO_ES, 0 );
177 switch( *buf )
179 case 0: /* not possible : caught with earlier vlc_stream_Read */
180 goto corrupt;
182 case 1:
183 if( i_block_size < 2 )
184 goto corrupt;
185 i_block_size -= 2;
187 if( vlc_stream_Read( p_demux->s, buf, 2 ) < 2 )
188 goto corrupt;
190 switch( buf[1] ) /* codec id */
192 case 0x0:
193 new_fmt.i_codec = VLC_CODEC_U8;
194 new_fmt.audio.i_bytes_per_frame = 1;
195 new_fmt.audio.i_bitspersample = 8;
196 break;
197 case 0x1:
198 new_fmt.i_codec = VLC_CODEC_ADPCM_SBPRO_4;
199 new_fmt.audio.i_bytes_per_frame = 1;
200 new_fmt.audio.i_bitspersample = 4;
201 break;
202 case 0x2:
203 new_fmt.i_codec = VLC_CODEC_ADPCM_SBPRO_3;
204 new_fmt.audio.i_bytes_per_frame = 3;
205 new_fmt.audio.i_bitspersample = 3;
206 break;
207 case 0x3:
208 new_fmt.i_codec = VLC_CODEC_ADPCM_SBPRO_2;
209 new_fmt.audio.i_bytes_per_frame = 1;
210 new_fmt.audio.i_bitspersample = 2;
211 break;
212 case 0x4:
213 new_fmt.i_codec = VLC_CODEC_S16L;
214 new_fmt.audio.i_bytes_per_frame = 2;
215 new_fmt.audio.i_bitspersample = 16;
216 break;
217 case 0x6:
218 new_fmt.i_codec = VLC_CODEC_ALAW;
219 new_fmt.audio.i_bytes_per_frame = 1;
220 new_fmt.audio.i_bitspersample = 8;
221 break;
222 case 0x7:
223 new_fmt.i_codec = VLC_CODEC_MULAW;
224 new_fmt.audio.i_bytes_per_frame = 1;
225 new_fmt.audio.i_bitspersample = 8;
226 break;
227 default:
228 msg_Err( p_demux, "unsupported compression 0x%"PRIx8, buf[1] );
229 return VLC_EGENERIC;
232 new_fmt.audio.i_channels = 1;
233 new_fmt.audio.i_bytes_per_frame *= new_fmt.audio.i_channels;
234 new_fmt.audio.i_blockalign = new_fmt.audio.i_bytes_per_frame;
236 new_fmt.audio.i_frame_length = new_fmt.audio.i_bytes_per_frame * 8
237 / new_fmt.audio.i_bitspersample;
239 new_fmt.audio.i_rate = fix_voc_sr( 1000000L / (256L - buf[0]) );
240 new_fmt.i_bitrate = new_fmt.audio.i_rate * new_fmt.audio.i_bitspersample
241 * new_fmt.audio.i_channels;
243 break;
245 case 2: /* data block with same format as the previous one */
246 if( p_sys->p_es == NULL )
247 goto corrupt; /* no previous block! */
249 memcpy( &new_fmt, &p_sys->fmt, sizeof( new_fmt ) );
250 break;
252 case 3: /* silence block */
253 if( ( i_block_size != 3 )
254 || ( vlc_stream_Read( p_demux->s, buf, 3 ) < 3 ) )
255 goto corrupt;
257 i_block_size = 0;
258 p_sys->i_silence_countdown = GetWLE( buf );
260 new_fmt.i_codec = VLC_CODEC_U8;
261 new_fmt.audio.i_rate = fix_voc_sr( 1000000L / (256L - buf[0]) );
262 new_fmt.audio.i_bytes_per_frame = 1;
263 new_fmt.audio.i_frame_length = 1;
264 new_fmt.audio.i_channels = 1;
265 new_fmt.audio.i_blockalign = 1;
266 new_fmt.audio.i_bitspersample = 8;
267 new_fmt.i_bitrate = new_fmt.audio.i_rate * 8;
268 break;
270 case 6: /* repeat block */
271 if( ( i_block_size != 2 )
272 || ( vlc_stream_Read( p_demux->s, buf, 2 ) < 2 ) )
273 goto corrupt;
275 i_block_size = 0;
276 p_sys->i_loop_count = GetWLE( buf );
277 p_sys->i_loop_offset = vlc_stream_Tell( p_demux->s );
278 break;
280 case 7: /* repeat end block */
281 if( i_block_size != 0 )
282 goto corrupt;
284 if( p_sys->i_loop_count > 0 )
286 if( vlc_stream_Seek( p_demux->s, p_sys->i_loop_offset ) )
287 msg_Warn( p_demux, "cannot loop: seek failed" );
288 else
289 p_sys->i_loop_count--;
291 break;
293 case 8:
295 * Block 8 is a big kludge to add stereo support to block 1 :
296 * A block of type 8 is always followed by a block of type 1
297 * and specifies the number of channels in that 1-block
298 * (normally block 1 are always mono). In practice, block type 9
299 * is used for stereo rather than 8
301 if( ( i_block_size != 4 )
302 || ( vlc_stream_Read( p_demux->s, buf, 4 ) < 4 ) )
303 goto corrupt;
305 if( buf[2] )
307 msg_Err( p_demux, "unsupported compression" );
308 return VLC_EGENERIC;
311 new_fmt.i_codec = VLC_CODEC_U8;
312 if (buf[3] >= 32)
313 goto corrupt;
314 new_fmt.audio.i_channels = buf[3] + 1; /* can't be nul */
315 new_fmt.audio.i_rate = 256000000L /
316 ((65536L - GetWLE(buf)) * new_fmt.audio.i_channels);
317 new_fmt.audio.i_bytes_per_frame = new_fmt.audio.i_channels;
318 new_fmt.audio.i_frame_length = 1;
319 new_fmt.audio.i_blockalign = new_fmt.audio.i_bytes_per_frame;
320 new_fmt.audio.i_bitspersample = 8 * new_fmt.audio.i_bytes_per_frame;
321 new_fmt.i_bitrate = new_fmt.audio.i_rate * 8;
323 /* read subsequent block 1 */
324 if( vlc_stream_Read( p_demux->s, buf, 4 ) < 4 )
325 return VLC_EGENERIC; /* EOF */
327 i_block_size = GetDWLE( buf ) >> 8;
328 msg_Dbg( p_demux, "new block: type: %u, size: %u",
329 (unsigned)*buf, i_block_size );
330 if( i_block_size < 2 )
331 goto corrupt;
332 i_block_size -= 2;
334 if( vlc_stream_Read( p_demux->s, buf, 2 ) < 2 )
335 goto corrupt;
337 if( buf[1] )
339 msg_Err( p_demux, "unsupported compression" );
340 return VLC_EGENERIC;
343 break;
345 case 9: /* newer data block with channel number and bits resolution */
346 if( i_block_size < 12 )
347 goto corrupt;
348 i_block_size -= 12;
350 if( ( vlc_stream_Read( p_demux->s, buf, 8 ) < 8 )
351 || ( vlc_stream_Read( p_demux->s, NULL, 4 ) < 4 ) )
352 goto corrupt;
354 new_fmt.audio.i_rate = GetDWLE( buf );
355 if( !new_fmt.audio.i_rate )
356 goto corrupt;
357 new_fmt.audio.i_bitspersample = buf[4];
358 new_fmt.audio.i_channels = buf[5];
360 switch( GetWLE( &buf[6] ) ) /* format */
362 case 0x0000: /* PCM */
363 switch( new_fmt.audio.i_bitspersample )
365 case 8:
366 new_fmt.i_codec = VLC_CODEC_U8;
367 break;
369 case 16:
370 new_fmt.i_codec = VLC_CODEC_U16L;
371 break;
373 default:
374 msg_Err( p_demux, "unsupported bit res.: %u bits",
375 new_fmt.audio.i_bitspersample );
376 return VLC_EGENERIC;
378 break;
380 case 0x0004: /* signed */
381 switch( new_fmt.audio.i_bitspersample )
383 case 8:
384 new_fmt.i_codec = VLC_CODEC_S8;
385 break;
387 case 16:
388 new_fmt.i_codec = VLC_CODEC_S16L;
389 break;
391 default:
392 msg_Err( p_demux, "unsupported bit res.: %u bits",
393 new_fmt.audio.i_bitspersample );
394 return VLC_EGENERIC;
396 break;
398 default:
399 msg_Err( p_demux, "unsupported compression" );
400 return VLC_EGENERIC;
403 if( new_fmt.audio.i_channels == 0 )
405 msg_Err( p_demux, "0 channels detected" );
406 return VLC_EGENERIC;
409 new_fmt.audio.i_bytes_per_frame = new_fmt.audio.i_channels
410 * (new_fmt.audio.i_bitspersample / 8);
411 new_fmt.audio.i_frame_length = 1;
412 new_fmt.audio.i_blockalign = new_fmt.audio.i_bytes_per_frame;
413 new_fmt.i_bitrate = 8 * new_fmt.audio.i_rate
414 * new_fmt.audio.i_bytes_per_frame;
415 break;
417 default:
418 msg_Dbg( p_demux, "unknown block type %u - skipping block",
419 (unsigned)*buf);
420 /* fall through */
421 case 4: /* blocks of non-audio types can be skipped */
422 case 5:
423 if( vlc_stream_Read( p_demux->s, NULL,
424 i_block_size ) < i_block_size )
425 goto corrupt;
426 i_block_size = 0;
427 break;
430 p_sys->i_block_start = vlc_stream_Tell( p_demux->s );
431 p_sys->i_block_end = p_sys->i_block_start + i_block_size;
433 if( i_block_size || p_sys->i_silence_countdown )
435 /* we've read a block with data in it - update decoder */
436 msg_Dbg( p_demux, "fourcc: %4.4s, channels: %d, "
437 "freq: %d Hz, bitrate: %dKo/s, blockalign: %d, "
438 "bits/samples: %d", (char *)&new_fmt.i_codec,
439 new_fmt.audio.i_channels, new_fmt.audio.i_rate,
440 new_fmt.i_bitrate / 8192, new_fmt.audio.i_blockalign,
441 new_fmt.audio.i_bitspersample );
443 if( ( p_sys->p_es != NULL ) && fmtcmp( &p_sys->fmt, &new_fmt ) )
445 msg_Dbg( p_demux, "codec change needed" );
446 es_out_Del( p_demux->out, p_sys->p_es );
447 p_sys->p_es = NULL;
450 if( p_sys->p_es == NULL )
452 memcpy( &p_sys->fmt, &new_fmt, sizeof( p_sys->fmt ) );
453 date_Change( &p_sys->pts, p_sys->fmt.audio.i_rate, 1 );
454 p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
458 return VLC_SUCCESS;
460 corrupt:
461 msg_Err( p_demux, "corrupted file - halting demux" );
462 return VLC_EGENERIC;
465 /*****************************************************************************
466 * Demux: read packet and send them to decoders
467 *****************************************************************************
468 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
469 *****************************************************************************/
470 #define MAX_READ_FRAME 1000
471 static int Demux( demux_t *p_demux )
473 demux_sys_t *p_sys = p_demux->p_sys;
474 block_t *p_block;
475 int64_t i_read_frames;
477 if( p_sys->i_silence_countdown == 0 )
479 int64_t i_offset = vlc_stream_Tell( p_demux->s );
480 if( i_offset >= p_sys->i_block_end )
482 if( ReadBlockHeader( p_demux ) != VLC_SUCCESS )
483 return 0;
484 return 1;
487 i_read_frames = ( p_sys->i_block_end - i_offset )
488 / p_sys->fmt.audio.i_bytes_per_frame;
490 if( i_read_frames > MAX_READ_FRAME )
491 i_read_frames = MAX_READ_FRAME;
493 p_block = vlc_stream_Block( p_demux->s,
494 p_sys->fmt.audio.i_bytes_per_frame
495 * i_read_frames );
496 if( p_block == NULL )
498 msg_Warn( p_demux, "cannot read data" );
499 return 0;
502 else
503 { /* emulates silence from the stream */
504 i_read_frames = p_sys->i_silence_countdown;
505 if( i_read_frames > MAX_READ_FRAME )
506 i_read_frames = MAX_READ_FRAME;
508 p_block = block_Alloc( i_read_frames );
509 if( p_block == NULL )
510 return VLC_ENOMEM;
512 memset( p_block->p_buffer, 0, i_read_frames );
513 p_sys->i_silence_countdown -= i_read_frames;
516 p_block->i_dts = p_block->i_pts = VLC_TS_0 + date_Get( &p_sys->pts );
517 p_block->i_nb_samples = i_read_frames * p_sys->fmt.audio.i_frame_length;
518 date_Increment( &p_sys->pts, p_block->i_nb_samples );
519 es_out_SetPCR( p_demux->out, p_block->i_pts );
520 es_out_Send( p_demux->out, p_sys->p_es, p_block );
522 return 1;
525 /*****************************************************************************
526 * Close: frees unused data
527 *****************************************************************************/
528 static void Close ( vlc_object_t * p_this )
530 demux_sys_t *p_sys = ((demux_t *)p_this)->p_sys;
532 free( p_sys );
535 /*****************************************************************************
536 * Control:
537 *****************************************************************************/
538 static int Control( demux_t *p_demux, int i_query, va_list args )
540 demux_sys_t *p_sys = p_demux->p_sys;
542 return demux_vaControlHelper( p_demux->s, p_sys->i_block_start,
543 p_sys->i_block_end,
544 p_sys->fmt.i_bitrate,
545 p_sys->fmt.audio.i_blockalign,
546 i_query, args );