]> git.sesse.net Git - vlc/blob - modules/demux/voc.c
Remove most stray semi-colons in module descriptions
[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_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_demux.h>
35 #include <vlc_aout.h>
36
37 #include <vlc_codecs.h>
38
39 /*****************************************************************************
40  * Module descriptor
41  *****************************************************************************/
42 static int  Open ( vlc_object_t * );
43 static void Close( vlc_object_t * );
44
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 ()
52
53 /*****************************************************************************
54  * Local prototypes
55  *****************************************************************************/
56 static int Demux  ( demux_t * );
57 static int Control( demux_t *, int i_query, va_list args );
58
59 struct demux_sys_t
60 {
61     es_format_t     fmt;
62     es_out_id_t     *p_es;
63
64     int64_t         i_block_start;
65     int64_t         i_block_end;
66
67     int64_t         i_loop_offset;
68     unsigned        i_loop_count;
69     unsigned        i_silence_countdown;
70
71     date_t          pts;
72 };
73
74 static const char ct_header[] = "Creative Voice File\x1a";
75
76 /*****************************************************************************
77  * Open: check file and initializes structures
78  *****************************************************************************/
79 static int Open( vlc_object_t * p_this )
80 {
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;
85
86     if( stream_Peek( p_demux->s, &p_buf, 26 ) < 26 )
87         return VLC_EGENERIC;
88
89     if( memcmp( p_buf, ct_header, 20 ) )
90         return VLC_EGENERIC;
91     p_buf += 20;
92
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;
97
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;
102
103     if( GetWLE( p_buf ) != (uint16_t)(0x1234 + ~i_version) )
104         return VLC_EGENERIC;
105
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 );
109
110     /* skip VOC header */
111     if( stream_Read( p_demux->s, NULL, i_data_offset ) < i_data_offset )
112         return VLC_EGENERIC;
113
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;
119
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;
123
124     date_Init( &p_sys->pts, 1, 1 );
125     date_Set( &p_sys->pts, 1 );
126
127     es_format_Init( &p_sys->fmt, AUDIO_ES, 0 );
128
129     return VLC_SUCCESS;
130 }
131
132
133 static int fmtcmp( es_format_t *ofmt, es_format_t *nfmt )
134 {
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);
138 }
139
140
141 /*
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)
145  */
146 static unsigned int fix_voc_sr( unsigned int sr )
147 {
148     switch( sr )
149     {
150         /*case 8000:
151             return 8000;*/
152         case 11111:
153             return 11025;
154
155         case 22222:
156             return 22050;
157
158         case 44444:
159             return 44100;
160     }
161     return sr;
162 }
163
164 static int ReadBlockHeader( demux_t *p_demux )
165 {
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;
170
171     if( stream_Read( p_demux->s, buf, 4 ) < 4 )
172         return VLC_EGENERIC; /* EOF */
173
174     i_block_size = GetDWLE( buf ) >> 8;
175     msg_Dbg( p_demux, "new block: type: %u, size: %u",
176              (unsigned)*buf, i_block_size );
177
178     es_format_Init( &new_fmt, AUDIO_ES, 0 );
179
180     switch( *buf )
181     {
182         case 0: /* not possible : caught with earlier stream_Read */
183             goto corrupt;
184
185         case 1:
186             if( i_block_size < 2 )
187                 goto corrupt;
188             i_block_size -= 2;
189
190             if( stream_Read( p_demux->s, buf, 2 ) < 2 )
191                 goto corrupt;
192
193             if( buf[1] )
194             {
195                 msg_Err( p_demux, "unsupported compression" );
196                 return VLC_EGENERIC;
197             }
198
199             new_fmt.i_codec = VLC_FOURCC('u','8',' ',' ');
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;
208
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! */
212
213             memcpy( &new_fmt, &p_sys->fmt, sizeof( new_fmt ) );
214             break;
215
216         case 3: /* silence block */
217             if( ( i_block_size != 3 )
218              || ( stream_Read( p_demux->s, buf, 3 ) < 3 ) )
219                 goto corrupt;
220
221             i_block_size = 0;
222             p_sys->i_silence_countdown = GetWLE( buf );
223
224             new_fmt.i_codec = VLC_FOURCC('u','8',' ',' ');
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;
233
234         case 6: /* repeat block */
235             if( ( i_block_size != 2 )
236              || ( stream_Read( p_demux->s, buf, 2 ) < 2 ) )
237                 goto corrupt;
238
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;
243
244         case 7: /* repeat end block */
245             if( i_block_size != 0 )
246                 goto corrupt;
247
248             if( p_sys->i_loop_count > 0 )
249             {
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--;
254             }
255             break;
256
257         case 8:
258             /*
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
264              */
265             if( ( i_block_size != 4 )
266              || ( stream_Read( p_demux->s, buf, 4 ) < 4 ) )
267                 goto corrupt;
268
269             if( buf[2] )
270             {
271                 msg_Err( p_demux, "unsupported compression" );
272                 return VLC_EGENERIC;
273             }
274
275             new_fmt.i_codec = VLC_FOURCC('u','8',' ',' ');
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;
284
285             /* read subsequent block 1 */
286             if( stream_Read( p_demux->s, buf, 4 ) < 4 )
287                 return VLC_EGENERIC; /* EOF */
288  
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;
295
296             if( stream_Read( p_demux->s, buf, 2 ) < 2 )
297                 goto corrupt;
298
299             if( buf[1] )
300             {
301                 msg_Err( p_demux, "unsupported compression" );
302                 return VLC_EGENERIC;
303             }
304
305             break;
306
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;
311
312             if( ( stream_Read( p_demux->s, buf, 8 ) < 8 )
313              || ( stream_Read( p_demux->s, NULL, 4 ) < 4 ) )
314                 goto corrupt;
315
316             new_fmt.audio.i_rate = GetDWLE( buf );
317             new_fmt.audio.i_bitspersample = buf[4];
318             new_fmt.audio.i_channels = buf[5];
319
320             switch( GetWLE( &buf[6] ) ) /* format */
321             {
322                 case 0x0000: /* PCM */
323                     switch( new_fmt.audio.i_bitspersample )
324                     {
325                         case 8:
326                             new_fmt.i_codec = VLC_FOURCC('u','8',' ',' ');
327                             break;
328
329                         case 16:
330                             new_fmt.i_codec = VLC_FOURCC('u','1','6','l');
331                             break;
332
333                         default:
334                             msg_Err( p_demux, "unsupported bit res.: %u bits",
335                                      new_fmt.audio.i_bitspersample );
336                             return VLC_EGENERIC;
337                     }
338                     break;
339
340                 case 0x0004: /* signed */
341                     switch( new_fmt.audio.i_bitspersample )
342                     {
343                         case 8:
344                             new_fmt.i_codec = VLC_FOURCC('s','8',' ',' ');
345                             break;
346
347                         case 16:
348                             new_fmt.i_codec = VLC_FOURCC('s','1','6','l');
349                             break;
350
351                         default:
352                             msg_Err( p_demux, "unsupported bit res.: %u bits",
353                                      new_fmt.audio.i_bitspersample );
354                             return VLC_EGENERIC;
355                     }
356                     break;
357
358                 default:
359                     msg_Err( p_demux, "unsupported compression" );
360                     return VLC_EGENERIC;
361             }
362
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;
370
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;
380     }
381
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;
384
385     if( i_block_size || p_sys->i_silence_countdown )
386     {
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 );
394
395         if( ( p_sys->p_es != NULL ) && fmtcmp( &p_sys->fmt, &new_fmt ) )
396         {
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;
400         }
401
402         if( p_sys->p_es == NULL )
403         {
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 );
407         }
408     }
409
410     return VLC_SUCCESS;
411
412 corrupt:
413     msg_Err( p_demux, "corrupted file - halting demux" );
414     return VLC_EGENERIC;
415 }
416
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 )
424 {
425     demux_sys_t *p_sys = p_demux->p_sys;
426     block_t     *p_block;
427     int64_t     i_offset, i;
428
429     i_offset = stream_Tell( p_demux->s );
430
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;
435
436     if( p_sys->i_silence_countdown == 0 )
437     {
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;
442
443         p_block = stream_Block( p_demux->s,
444                                 p_sys->fmt.audio.i_bytes_per_frame * i );
445         if( p_block == NULL )
446         {
447             msg_Warn( p_demux, "cannot read data" );
448             return 0;
449         }
450     }
451     else
452     {   /* emulates silence from the stream */
453         i = p_sys->i_silence_countdown;
454         if( i > SAMPLES_BUFFER )
455             i = SAMPLES_BUFFER;
456
457         p_block = block_New( p_demux, i );
458         if( p_block == NULL )
459             return VLC_ENOMEM;
460
461         memset( p_block->p_buffer, 0, i );
462         p_sys->i_silence_countdown -= i;
463     }
464
465     p_block->i_dts = p_block->i_pts =
466         date_Increment( &p_sys->pts, p_sys->fmt.audio.i_frame_length * i );
467
468     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
469
470     es_out_Send( p_demux->out, p_sys->p_es, p_block );
471
472     return 1;
473 }
474
475 /*****************************************************************************
476  * Close: frees unused data
477  *****************************************************************************/
478 static void Close ( vlc_object_t * p_this )
479 {
480     demux_sys_t *p_sys  = ((demux_t *)p_this)->p_sys;
481
482     free( p_sys );
483 }
484
485 /*****************************************************************************
486  * Control:
487  *****************************************************************************/
488 static int Control( demux_t *p_demux, int i_query, va_list args )
489 {
490     demux_sys_t *p_sys  = p_demux->p_sys;
491
492     return demux_vaControlHelper( p_demux->s, p_sys->i_block_start,
493                                    p_sys->i_block_end,
494                                    p_sys->fmt.i_bitrate,
495                                    p_sys->fmt.audio.i_blockalign,
496                                    i_query, args );
497 }