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