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