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