]> git.sesse.net Git - vlc/blob - modules/demux/voc.c
Copyright fixes
[vlc] / modules / demux / voc.c
1 /*****************************************************************************
2  * voc.c : Creative Voice File (.VOC) demux module for vlc
3  *****************************************************************************
4  * Copyright (C) 2005 VideoLAN (Centrale Réseaux) and its contributors
5  * $Id$
6  *
7  * Authors: Remi 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., 59 Temple Place - Suite 330, Boston, MA  02111, 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
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_FOURCC('u','8',' ',' ');
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_FOURCC('u','8',' ',' ');
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         /* FIXME: support block type 8 properly */
255         case 8:
256             msg_Err( p_demux, "Unimplemented block type 8" );
257             return VLC_EGENERIC;
258
259         case 9:
260             if( i_block_size < 12 )
261                 goto corrupt;
262             i_block_size -= 12;
263
264             if( ( stream_Read( p_demux->s, buf, 8 ) < 8 )
265              || ( stream_Read( p_demux->s, NULL, 4 ) < 4 ) )
266                 goto corrupt;
267
268             new_fmt.audio.i_rate = GetDWLE( buf );
269             new_fmt.audio.i_bitspersample = buf[4];
270             new_fmt.audio.i_channels = buf[5];
271
272             switch( GetWLE( &buf[6] ) ) /* format */
273             {
274                 case 0x0000: /* PCM */
275                     switch( new_fmt.audio.i_bitspersample )
276                     {
277                         case 8:
278                             new_fmt.i_codec = VLC_FOURCC('u','8',' ',' ');
279                             break;
280
281                         case 16:
282                             new_fmt.i_codec = VLC_FOURCC('u','1','6','l');
283                             break;
284
285                         default:
286                             msg_Err( p_demux, "Unsupported bit res.: %u bits",
287                                      new_fmt.audio.i_bitspersample );
288                             return VLC_EGENERIC;
289                     }
290                     break;
291
292                 case 0x0004: /* signed */
293                     switch( new_fmt.audio.i_bitspersample )
294                     {
295                         case 8:
296                             new_fmt.i_codec = VLC_FOURCC('s','8',' ',' ');
297                             break;
298
299                         case 16:
300                             new_fmt.i_codec = VLC_FOURCC('s','1','6','l');
301                             break;
302
303                         default:
304                             msg_Err( p_demux, "Unsupported bit res.: %u bits",
305                                      new_fmt.audio.i_bitspersample );
306                             return VLC_EGENERIC;
307                     }
308                     break;
309
310                 default: 
311                     msg_Err( p_demux, "Unsupported compression" );
312                     return VLC_EGENERIC;
313             }
314
315             new_fmt.audio.i_bytes_per_frame = new_fmt.audio.i_channels
316                 * (new_fmt.audio.i_bitspersample / 8);
317             new_fmt.audio.i_frame_length = 1;
318             new_fmt.audio.i_blockalign = new_fmt.audio.i_bytes_per_frame;
319             new_fmt.i_bitrate = 8 * new_fmt.audio.i_rate
320                                      * new_fmt.audio.i_bytes_per_frame;
321             break;
322
323         default:
324             msg_Dbg( p_demux, "Unknown block type %u - skipping block",
325                      (unsigned)*buf);
326         case 4: /* blocks of non-audio types can be skipped */
327         case 5:
328             if( stream_Read( p_demux->s, NULL, i_block_size ) < i_block_size )
329                 goto corrupt;
330             i_block_size = 0;
331             break;
332     }
333
334     p_sys->i_block_start = stream_Tell( p_demux->s );
335     p_sys->i_block_end = p_sys->i_block_start + i_block_size;
336
337     if( i_block_size || p_sys->i_silence_countdown )
338     {
339         /* we've read a block with data in it - update decoder */
340         msg_Dbg( p_demux, "fourcc: %4.4s, channels: %d, "
341                  "freq: %d Hz, bitrate: %dKo/s, blockalign: %d, "
342                  "bits/samples: %d", (char *)&new_fmt.i_codec,
343                  new_fmt.audio.i_channels, new_fmt.audio.i_rate,
344                  new_fmt.i_bitrate / 8192, new_fmt.audio.i_blockalign,
345                  new_fmt.audio.i_bitspersample );
346
347         if( ( p_sys->p_es != NULL ) && fmtcmp( &p_sys->fmt, &new_fmt ) )
348         {
349             msg_Dbg( p_demux, "codec change needed" );
350             es_out_Del( p_demux->out, p_sys->p_es );
351             p_sys->p_es = NULL;
352         }
353
354         if( p_sys->p_es == NULL )
355         {
356             memcpy( &p_sys->fmt, &new_fmt, sizeof( p_sys->fmt ) );
357             date_Change( &p_sys->pts, p_sys->fmt.audio.i_rate, 1 );
358             p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
359         }
360     }
361
362     return VLC_SUCCESS;
363
364 corrupt:
365     msg_Err( p_demux, "corrupted file - halting demux" );
366     return VLC_EGENERIC;
367 }
368
369 /*****************************************************************************
370  * Demux: read packet and send them to decoders
371  *****************************************************************************
372  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
373  *****************************************************************************/
374 #define SAMPLES_BUFFER 1000
375 static int Demux( demux_t *p_demux )
376 {
377     demux_sys_t *p_sys = p_demux->p_sys;
378     block_t     *p_block;
379     int64_t     i_offset, i;
380
381     i_offset = stream_Tell( p_demux->s );
382
383     while( ( i_offset >= p_sys->i_block_end )
384          && ( p_sys->i_silence_countdown == 0 ) )
385         if( ReadBlockHeader( p_demux ) != VLC_SUCCESS )
386             return 0;
387
388     if( p_sys->i_silence_countdown == 0 )
389     {
390         i = ( p_sys->i_block_end - i_offset )
391             / p_sys->fmt.audio.i_bytes_per_frame;
392         if( i > SAMPLES_BUFFER )
393             i = SAMPLES_BUFFER;
394
395         p_block = stream_Block( p_demux->s,
396                                 p_sys->fmt.audio.i_bytes_per_frame * i );
397         if( p_block == NULL )
398         {
399             msg_Warn( p_demux, "cannot read data" );
400             return 0;
401         }
402     }
403     else
404     {   /* emulates silence from the stream */
405         i = p_sys->i_silence_countdown;
406         if( i > SAMPLES_BUFFER )
407             i = SAMPLES_BUFFER;
408
409         p_block = block_New( p_demux, i );
410         if( p_block == NULL )
411             return VLC_ENOMEM;
412
413         memset( p_block->p_buffer, 0, i );
414         p_sys->i_silence_countdown -= i;
415     }
416
417     p_block->i_dts = p_block->i_pts =
418         date_Increment( &p_sys->pts, p_sys->fmt.audio.i_frame_length * i );
419
420     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
421
422     es_out_Send( p_demux->out, p_sys->p_es, p_block );
423
424     return 1;
425 }
426
427 /*****************************************************************************
428  * Close: frees unused data
429  *****************************************************************************/
430 static void Close ( vlc_object_t * p_this )
431 {
432     demux_sys_t *p_sys  = ((demux_t *)p_this)->p_sys;
433
434     free( p_sys );
435 }
436
437 /*****************************************************************************
438  * Control:
439  *****************************************************************************/
440 static int Control( demux_t *p_demux, int i_query, va_list args )
441 {
442     demux_sys_t *p_sys  = p_demux->p_sys;
443
444     return demux2_vaControlHelper( p_demux->s, p_sys->i_block_start,
445                                    p_sys->i_block_end,
446                                    p_sys->fmt.i_bitrate,
447                                    p_sys->fmt.audio.i_blockalign,
448                                    i_query, args );
449 }