]> git.sesse.net Git - vlc/blob - modules/demux/voc.c
update module LIST file.
[vlc] / modules / demux / voc.c
1 /*****************************************************************************
2  * voc.c : Creative Voice File (.VOC) demux module for vlc
3  *****************************************************************************
4  * Copyright (C) 2005 Rémi Denis-Courmont
5  * $Id$
6  *
7  * Authors: Rémi Denis-Courmont <rem # videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc/vlc.h>
33 #include <vlc_demux.h>
34 #include <vlc_aout.h>
35
36 #include <vlc_codecs.h>
37
38 /*****************************************************************************
39  * Module descriptor
40  *****************************************************************************/
41 static int  Open ( vlc_object_t * );
42 static void Close( vlc_object_t * );
43
44 vlc_module_begin();
45     set_description( _("VOC demuxer") );
46     set_category( CAT_INPUT );
47     set_subcategory( SUBCAT_INPUT_DEMUX );
48     set_capability( "demux2", 10 );
49     set_callbacks( Open, Close );
50 vlc_module_end();
51
52 /*****************************************************************************
53  * Local prototypes
54  *****************************************************************************/
55 static int Demux  ( demux_t * );
56 static int Control( demux_t *, int i_query, va_list args );
57
58 struct demux_sys_t
59 {
60     es_format_t     fmt;
61     es_out_id_t     *p_es;
62
63     int64_t         i_block_start;
64     int64_t         i_block_end;
65
66     int64_t         i_loop_offset;
67     unsigned        i_loop_count;
68     unsigned        i_silence_countdown;
69
70     date_t          pts;
71 };
72
73 static const char ct_header[] = "Creative Voice File\x1a";
74
75 /*****************************************************************************
76  * Open: check file and initializes structures
77  *****************************************************************************/
78 static int Open( vlc_object_t * p_this )
79 {
80     demux_t     *p_demux = (demux_t*)p_this;
81     demux_sys_t *p_sys;
82     const uint8_t *p_buf;
83     uint16_t    i_data_offset, i_version;
84
85     if( stream_Peek( p_demux->s, &p_buf, 26 ) < 26 )
86         return VLC_EGENERIC;
87
88     if( memcmp( p_buf, ct_header, 20 ) )
89         return VLC_EGENERIC;
90     p_buf += 20;
91
92     i_data_offset = GetWLE( p_buf );
93     if ( i_data_offset < 26 /* not enough room for full VOC header */ )
94         return VLC_EGENERIC;
95     p_buf += 2;
96
97     i_version = GetWLE( p_buf );
98     if( ( i_version != 0x10A ) && ( i_version != 0x114 ) )
99         return VLC_EGENERIC; /* unknown VOC version */
100     p_buf += 2;
101
102     if( GetWLE( p_buf ) != (uint16_t)(0x1234 + ~i_version) )
103         return VLC_EGENERIC;
104
105     /* We have a valid VOC header */
106     msg_Dbg( p_demux, "CT Voice file v%d.%d", i_version >> 8,
107              i_version & 0xff );
108
109     /* skip VOC header */
110     if( stream_Read( p_demux->s, NULL, i_data_offset ) < i_data_offset )
111         return VLC_EGENERIC;
112
113     p_demux->pf_demux   = Demux;
114     p_demux->pf_control = Control;
115     p_demux->p_sys      = p_sys = malloc( sizeof( demux_sys_t ) );
116     if( p_sys == NULL )
117         return VLC_ENOMEM;
118
119     p_sys->i_silence_countdown = p_sys->i_block_start = p_sys->i_block_end =
120     p_sys->i_loop_count = 0;
121     p_sys->p_es = NULL;
122
123     date_Init( &p_sys->pts, 1, 1 );
124     date_Set( &p_sys->pts, 1 );
125
126     es_format_Init( &p_sys->fmt, AUDIO_ES, 0 );
127
128     return VLC_SUCCESS;
129 }
130
131
132 static int fmtcmp( es_format_t *ofmt, es_format_t *nfmt )
133 {
134     return (ofmt->audio.i_bitspersample != nfmt->audio.i_bitspersample)
135         || (ofmt->audio.i_rate != nfmt->audio.i_rate)
136         || (ofmt->audio.i_channels != nfmt->audio.i_channels);
137 }
138
139
140 /*
141  * Converts old-style VOC sample rates to commonly used ones
142  * so as not to confuse sound card drivers.
143  * (I assume 16k, 24k and 32k are never found in .VOC files)
144  */
145 static unsigned int fix_voc_sr( unsigned int sr )
146 {
147     switch( sr )
148     {
149         /*case 8000:
150             return 8000;*/
151         case 11111:
152             return 11025;
153
154         case 22222:
155             return 22050;
156
157         case 44444:
158             return 44100;
159     }
160     return sr;
161 }
162
163 static int ReadBlockHeader( demux_t *p_demux )
164 {
165     es_format_t     new_fmt;
166     uint8_t buf[8];
167     int32_t i_block_size;
168     demux_sys_t *p_sys = p_demux->p_sys;
169
170     if( stream_Read( p_demux->s, buf, 4 ) < 4 )
171         return VLC_EGENERIC; /* EOF */
172
173     i_block_size = GetDWLE( buf ) >> 8;
174     msg_Dbg( p_demux, "new block: type: %u, size: %u",
175              (unsigned)*buf, i_block_size );
176
177     es_format_Init( &new_fmt, AUDIO_ES, 0 );
178
179     switch( *buf )
180     {
181         case 0: /* not possible : caught with earlier stream_Read */
182             goto corrupt;
183
184         case 1:
185             if( i_block_size < 2 )
186                 goto corrupt;
187             i_block_size -= 2;
188
189             if( stream_Read( p_demux->s, buf, 2 ) < 2 )
190                 goto corrupt;
191
192             if( buf[1] )
193             {
194                 msg_Err( p_demux, "unsupported compression" );
195                 return VLC_EGENERIC;
196             }
197
198             new_fmt.i_codec = VLC_FOURCC('u','8',' ',' ');
199             new_fmt.audio.i_rate = fix_voc_sr( 1000000L / (256L - buf[0]) );
200             new_fmt.audio.i_bytes_per_frame = 1;
201             new_fmt.audio.i_frame_length = 1;
202             new_fmt.audio.i_channels = 1;
203             new_fmt.audio.i_blockalign = 1;
204             new_fmt.audio.i_bitspersample = 8;
205             new_fmt.i_bitrate = new_fmt.audio.i_rate * 8;
206             break;
207
208         case 2: /* data block with same format as the previous one */
209             if( p_sys->p_es == NULL )
210                 goto corrupt; /* no previous block! */
211
212             memcpy( &new_fmt, &p_sys->fmt, sizeof( new_fmt ) );
213             break;
214
215         case 3: /* silence block */
216             if( ( i_block_size != 3 )
217              || ( stream_Read( p_demux->s, buf, 3 ) < 3 ) )
218                 goto corrupt;
219
220             i_block_size = 0;
221             p_sys->i_silence_countdown = GetWLE( buf );
222
223             new_fmt.i_codec = VLC_FOURCC('u','8',' ',' ');
224             new_fmt.audio.i_rate = fix_voc_sr( 1000000L / (256L - buf[0]) );
225             new_fmt.audio.i_bytes_per_frame = 1;
226             new_fmt.audio.i_frame_length = 1;
227             new_fmt.audio.i_channels = 1;
228             new_fmt.audio.i_blockalign = 1;
229             new_fmt.audio.i_bitspersample = 8;
230             new_fmt.i_bitrate = new_fmt.audio.i_rate * 8;
231             break;
232
233         case 6: /* repeat block */
234             if( ( i_block_size != 2 )
235              || ( stream_Read( p_demux->s, buf, 2 ) < 2 ) )
236                 goto corrupt;
237
238             i_block_size = 0;
239             p_sys->i_loop_count = GetWLE( buf );
240             p_sys->i_loop_offset = stream_Tell( p_demux->s );
241             break;
242
243         case 7: /* repeat end block */
244             if( i_block_size != 0 )
245                 goto corrupt;
246
247             if( p_sys->i_loop_count > 0 )
248             {
249                 if( stream_Seek( p_demux->s, p_sys->i_loop_offset ) )
250                     msg_Warn( p_demux, "cannot loop: seek failed" );
251                 else
252                     p_sys->i_loop_count--;
253             }
254             break;
255
256         case 8:
257             /*
258              * Block 8 is a big kludge to add stereo support to block 1 :
259              * A block of type 8 is always followed by a block of type 1
260              * and specifies the number of channels in that 1-block
261              * (normally block 1 are always mono). In practice, block type 9
262              * is used for stereo rather than 8
263              */
264             if( ( i_block_size != 4 )
265              || ( stream_Read( p_demux->s, buf, 4 ) < 4 ) )
266                 goto corrupt;
267
268             if( buf[2] )
269             {
270                 msg_Err( p_demux, "unsupported compression" );
271                 return VLC_EGENERIC;
272             }
273
274             new_fmt.i_codec = VLC_FOURCC('u','8',' ',' ');
275             new_fmt.audio.i_channels = buf[3] + 1; /* can't be nul */
276             new_fmt.audio.i_rate = 256000000L /
277                           ((65536L - GetWLE(buf)) * new_fmt.audio.i_channels);
278             new_fmt.audio.i_bytes_per_frame = new_fmt.audio.i_channels;
279             new_fmt.audio.i_frame_length = 1;
280             new_fmt.audio.i_blockalign = new_fmt.audio.i_bytes_per_frame;
281             new_fmt.audio.i_bitspersample = 8 * new_fmt.audio.i_bytes_per_frame;
282             new_fmt.i_bitrate = new_fmt.audio.i_rate * 8;
283
284             /* read subsequent block 1 */
285             if( stream_Read( p_demux->s, buf, 4 ) < 4 )
286                 return VLC_EGENERIC; /* EOF */
287  
288             i_block_size = GetDWLE( buf ) >> 8;
289             msg_Dbg( p_demux, "new block: type: %u, size: %u",
290                     (unsigned)*buf, i_block_size );
291             if( i_block_size < 2 )
292                 goto corrupt;
293             i_block_size -= 2;
294
295             if( stream_Read( p_demux->s, buf, 2 ) < 2 )
296                 goto corrupt;
297
298             if( buf[1] )
299             {
300                 msg_Err( p_demux, "unsupported compression" );
301                 return VLC_EGENERIC;
302             }
303
304             break;
305
306         case 9: /* newer data block with channel number and bits resolution */
307             if( i_block_size < 12 )
308                 goto corrupt;
309             i_block_size -= 12;
310
311             if( ( stream_Read( p_demux->s, buf, 8 ) < 8 )
312              || ( stream_Read( p_demux->s, NULL, 4 ) < 4 ) )
313                 goto corrupt;
314
315             new_fmt.audio.i_rate = GetDWLE( buf );
316             new_fmt.audio.i_bitspersample = buf[4];
317             new_fmt.audio.i_channels = buf[5];
318
319             switch( GetWLE( &buf[6] ) ) /* format */
320             {
321                 case 0x0000: /* PCM */
322                     switch( new_fmt.audio.i_bitspersample )
323                     {
324                         case 8:
325                             new_fmt.i_codec = VLC_FOURCC('u','8',' ',' ');
326                             break;
327
328                         case 16:
329                             new_fmt.i_codec = VLC_FOURCC('u','1','6','l');
330                             break;
331
332                         default:
333                             msg_Err( p_demux, "unsupported bit res.: %u bits",
334                                      new_fmt.audio.i_bitspersample );
335                             return VLC_EGENERIC;
336                     }
337                     break;
338
339                 case 0x0004: /* signed */
340                     switch( new_fmt.audio.i_bitspersample )
341                     {
342                         case 8:
343                             new_fmt.i_codec = VLC_FOURCC('s','8',' ',' ');
344                             break;
345
346                         case 16:
347                             new_fmt.i_codec = VLC_FOURCC('s','1','6','l');
348                             break;
349
350                         default:
351                             msg_Err( p_demux, "unsupported bit res.: %u bits",
352                                      new_fmt.audio.i_bitspersample );
353                             return VLC_EGENERIC;
354                     }
355                     break;
356
357                 default:
358                     msg_Err( p_demux, "unsupported compression" );
359                     return VLC_EGENERIC;
360             }
361
362             new_fmt.audio.i_bytes_per_frame = new_fmt.audio.i_channels
363                 * (new_fmt.audio.i_bitspersample / 8);
364             new_fmt.audio.i_frame_length = 1;
365             new_fmt.audio.i_blockalign = new_fmt.audio.i_bytes_per_frame;
366             new_fmt.i_bitrate = 8 * new_fmt.audio.i_rate
367                                      * new_fmt.audio.i_bytes_per_frame;
368             break;
369
370         default:
371             msg_Dbg( p_demux, "unknown block type %u - skipping block",
372                      (unsigned)*buf);
373         case 4: /* blocks of non-audio types can be skipped */
374         case 5:
375             if( stream_Read( p_demux->s, NULL, i_block_size ) < i_block_size )
376                 goto corrupt;
377             i_block_size = 0;
378             break;
379     }
380
381     p_sys->i_block_start = stream_Tell( p_demux->s );
382     p_sys->i_block_end = p_sys->i_block_start + i_block_size;
383
384     if( i_block_size || p_sys->i_silence_countdown )
385     {
386         /* we've read a block with data in it - update decoder */
387         msg_Dbg( p_demux, "fourcc: %4.4s, channels: %d, "
388                  "freq: %d Hz, bitrate: %dKo/s, blockalign: %d, "
389                  "bits/samples: %d", (char *)&new_fmt.i_codec,
390                  new_fmt.audio.i_channels, new_fmt.audio.i_rate,
391                  new_fmt.i_bitrate / 8192, new_fmt.audio.i_blockalign,
392                  new_fmt.audio.i_bitspersample );
393
394         if( ( p_sys->p_es != NULL ) && fmtcmp( &p_sys->fmt, &new_fmt ) )
395         {
396             msg_Dbg( p_demux, "codec change needed" );
397             es_out_Del( p_demux->out, p_sys->p_es );
398             p_sys->p_es = NULL;
399         }
400
401         if( p_sys->p_es == NULL )
402         {
403             memcpy( &p_sys->fmt, &new_fmt, sizeof( p_sys->fmt ) );
404             date_Change( &p_sys->pts, p_sys->fmt.audio.i_rate, 1 );
405             p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
406         }
407     }
408
409     return VLC_SUCCESS;
410
411 corrupt:
412     msg_Err( p_demux, "corrupted file - halting demux" );
413     return VLC_EGENERIC;
414 }
415
416 /*****************************************************************************
417  * Demux: read packet and send them to decoders
418  *****************************************************************************
419  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
420  *****************************************************************************/
421 #define SAMPLES_BUFFER 1000
422 static int Demux( demux_t *p_demux )
423 {
424     demux_sys_t *p_sys = p_demux->p_sys;
425     block_t     *p_block;
426     int64_t     i_offset, i;
427
428     i_offset = stream_Tell( p_demux->s );
429
430     while( ( i_offset >= p_sys->i_block_end )
431          && ( p_sys->i_silence_countdown == 0 ) )
432         if( ReadBlockHeader( p_demux ) != VLC_SUCCESS )
433             return 0;
434
435     if( p_sys->i_silence_countdown == 0 )
436     {
437         i = ( p_sys->i_block_end - i_offset )
438             / p_sys->fmt.audio.i_bytes_per_frame;
439         if( i > SAMPLES_BUFFER )
440             i = SAMPLES_BUFFER;
441
442         p_block = stream_Block( p_demux->s,
443                                 p_sys->fmt.audio.i_bytes_per_frame * i );
444         if( p_block == NULL )
445         {
446             msg_Warn( p_demux, "cannot read data" );
447             return 0;
448         }
449     }
450     else
451     {   /* emulates silence from the stream */
452         i = p_sys->i_silence_countdown;
453         if( i > SAMPLES_BUFFER )
454             i = SAMPLES_BUFFER;
455
456         p_block = block_New( p_demux, i );
457         if( p_block == NULL )
458             return VLC_ENOMEM;
459
460         memset( p_block->p_buffer, 0, i );
461         p_sys->i_silence_countdown -= i;
462     }
463
464     p_block->i_dts = p_block->i_pts =
465         date_Increment( &p_sys->pts, p_sys->fmt.audio.i_frame_length * i );
466
467     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
468
469     es_out_Send( p_demux->out, p_sys->p_es, p_block );
470
471     return 1;
472 }
473
474 /*****************************************************************************
475  * Close: frees unused data
476  *****************************************************************************/
477 static void Close ( vlc_object_t * p_this )
478 {
479     demux_sys_t *p_sys  = ((demux_t *)p_this)->p_sys;
480
481     free( p_sys );
482 }
483
484 /*****************************************************************************
485  * Control:
486  *****************************************************************************/
487 static int Control( demux_t *p_demux, int i_query, va_list args )
488 {
489     demux_sys_t *p_sys  = p_demux->p_sys;
490
491     return demux2_vaControlHelper( p_demux->s, p_sys->i_block_start,
492                                    p_sys->i_block_end,
493                                    p_sys->fmt.i_bitrate,
494                                    p_sys->fmt.audio.i_blockalign,
495                                    i_query, args );
496 }