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