]> git.sesse.net Git - vlc/blob - modules/demux/voc.c
Fix my email address
[vlc] / modules / demux / voc.c
1 /*****************************************************************************
2  * voc.c : Creative Voice File (.VOC) demux module for vlc
3  *****************************************************************************
4  * Copyright (C) 2005 VideoLAN
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_offset;
61
62     int64_t         i_loop_offset;
63     unsigned        i_loop_count;
64     unsigned        i_silence_countdown;
65
66     int32_t         i_block_size;    
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     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_offset = p_sys->i_block_size =
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 static int ReadBlockHeader( demux_t *p_demux )
139 {
140     es_format_t     new_fmt;
141     uint8_t buf[8];
142     int32_t i_block_size;
143     demux_sys_t *p_sys = p_demux->p_sys;
144
145     if( stream_Read( p_demux->s, buf, 4 ) < 4 )
146         return VLC_EGENERIC; /* EOF */
147
148     i_block_size = GetDWLE( buf ) >> 8;
149     msg_Dbg( p_demux, "new block: type: %u, size: %u",
150              (unsigned)*buf, i_block_size );
151
152     es_format_Init( &new_fmt, AUDIO_ES, 0 );
153
154     switch( *buf )
155     {
156         case 0: /* not possible : caught with earlier stream_Read */
157             goto corrupt;
158
159         case 1:
160             if( i_block_size < 2 )
161                 goto corrupt;
162             i_block_size -= 2;
163
164             if( stream_Read( p_demux->s, buf, 2 ) < 2 )
165                 goto corrupt;
166
167             new_fmt.audio.i_rate = 1000000L / (256L - buf[0]);
168             if( buf[1] )
169             {
170                 msg_Err( p_demux, "Unsupported compression" );
171                 return VLC_EGENERIC;
172             }
173
174             new_fmt.i_codec = VLC_FOURCC('u','8',' ',' ');
175             new_fmt.audio.i_bytes_per_frame = 1;
176             new_fmt.audio.i_frame_length = 1;
177             new_fmt.audio.i_channels = 1;
178             new_fmt.audio.i_blockalign = 1;
179             new_fmt.audio.i_bitspersample = 8;
180             new_fmt.i_bitrate = p_sys->fmt.audio.i_rate * 8;
181             break;
182
183         case 2: /* data block with same format as the previous one */
184             if( p_sys->p_es == NULL )
185                 goto corrupt; /* no previous block! */
186
187             memcpy( &new_fmt, &p_sys->fmt, sizeof( new_fmt ) );
188             break;
189
190         case 3: /* silence block */
191             if( ( i_block_size != 3 )
192              || ( stream_Read( p_demux->s, buf, 3 ) < 3 ) )
193                 goto corrupt;
194
195             i_block_size = 0;
196             p_sys->i_silence_countdown = GetWLE( buf );
197
198             new_fmt.audio.i_rate = 1000000L / (256L - buf[2]);
199             new_fmt.i_codec = VLC_FOURCC('u','8',' ',' ');
200             new_fmt.audio.i_bytes_per_frame = 1;
201             new_fmt.audio.i_frame_length = 1;
202             new_fmt.audio.i_channels = 1;
203             new_fmt.audio.i_blockalign = 1;
204             new_fmt.audio.i_bitspersample = 8;
205             new_fmt.i_bitrate = p_sys->fmt.audio.i_rate * 8;
206             break;
207
208         case 6: /* repeat block */
209             if( ( i_block_size != 2 )
210              || ( stream_Read( p_demux->s, buf, 2 ) < 2 ) )
211                 goto corrupt;
212
213             i_block_size = 0;
214             p_sys->i_loop_count = GetWLE( buf );
215             p_sys->i_loop_offset = stream_Tell( p_demux->s );
216             break;
217
218         case 7: /* repeat end block */
219             if( i_block_size != 0 )
220                 goto corrupt;
221
222             if( p_sys->i_loop_count > 0 )
223             {
224                 if( stream_Seek( p_demux->s, p_sys->i_loop_offset ) )
225                     msg_Warn( p_demux, "cannot loop: seek failed" );
226                 else
227                     p_sys->i_loop_count--;
228             }
229             break;
230
231         /* FIXME: support block type 8 properly */
232         case 8:
233             msg_Err( p_demux, "Unimplemented block type 8" );
234             return VLC_EGENERIC;
235
236         case 9:
237             if( i_block_size < 12 )
238                 goto corrupt;
239             i_block_size -= 12;
240
241             if( ( stream_Read( p_demux->s, buf, 8 ) < 8 )
242              || ( stream_Read( p_demux->s, NULL, 4 ) < 4 ) )
243                 goto corrupt;
244
245             new_fmt.audio.i_rate = GetDWLE( buf );
246             new_fmt.audio.i_bitspersample = buf[4];
247             new_fmt.audio.i_channels = buf[5];
248
249             switch( GetWLE( &buf[6] ) ) /* format */
250             {
251                 case 0x0000: /* PCM */
252                     switch( new_fmt.audio.i_bitspersample )
253                     {
254                         case 8:
255                             new_fmt.i_codec = VLC_FOURCC('u','8',' ',' ');
256                             break;
257
258                         case 16:
259                             new_fmt.i_codec = VLC_FOURCC('u','1','6','l');
260                             break;
261
262                         default:
263                             msg_Err( p_demux, "Unsupported bit res.: %u bits",
264                                      new_fmt.audio.i_bitspersample );
265                             return VLC_EGENERIC;
266                     }
267                     break;
268
269                 case 0x0004: /* signed */
270                     switch( new_fmt.audio.i_bitspersample )
271                     {
272                         case 8:
273                             new_fmt.i_codec = VLC_FOURCC('s','8',' ',' ');
274                             break;
275
276                         case 16:
277                             new_fmt.i_codec = VLC_FOURCC('s','1','6','l');
278                             break;
279
280                         default:
281                             msg_Err( p_demux, "Unsupported bit res.: %u bits",
282                                      new_fmt.audio.i_bitspersample );
283                             return VLC_EGENERIC;
284                     }
285                     break;
286
287                 default: 
288                     msg_Err( p_demux, "Unsupported compression" );
289                     return VLC_EGENERIC;
290             }
291
292             new_fmt.audio.i_bytes_per_frame = new_fmt.audio.i_channels
293                 * (new_fmt.audio.i_bitspersample / 8);
294             new_fmt.audio.i_frame_length = 1;
295             new_fmt.audio.i_blockalign = p_sys->fmt.audio.i_bytes_per_frame;
296             new_fmt.i_bitrate = 8 * new_fmt.audio.i_rate
297                                      * new_fmt.audio.i_bytes_per_frame;
298             break;
299
300         default:
301             msg_Dbg( p_demux, "Unknown block type %u - skipping block",
302                      (unsigned)*buf);
303         case 4: /* blocks of non-audio types can be skipped */
304         case 5:
305             if( stream_Read( p_demux->s, NULL, i_block_size ) < i_block_size )
306                 goto corrupt;
307             i_block_size = 0;
308             break;
309     }
310
311     p_sys->i_block_size = i_block_size;
312     p_sys->i_block_offset = stream_Tell( p_demux->s );
313
314     if( i_block_size || p_sys->i_silence_countdown )
315     {
316         /* we've read a block with data in it - update decoder */
317         msg_Dbg( p_demux, "fourcc: %4.4s, channels: %d, "
318                  "freq: %d Hz, bitrate: %dKo/s, blockalign: %d, "
319                  "bits/samples: %d", (char *)&new_fmt.i_codec,
320                  new_fmt.audio.i_channels, new_fmt.audio.i_rate,
321                  new_fmt.i_bitrate / 8192, new_fmt.audio.i_blockalign,
322                  new_fmt.audio.i_bitspersample );
323
324         if( ( p_sys->p_es != NULL ) && fmtcmp( &p_sys->fmt, &new_fmt ) )
325         {
326             msg_Dbg( p_demux, "codec change needed" );
327             es_out_Del( p_demux->out, p_sys->p_es );
328             p_sys->p_es = NULL;
329         }
330
331         if( p_sys->p_es == NULL )
332         {
333             memcpy( &p_sys->fmt, &new_fmt, sizeof( p_sys->fmt ) );
334             date_Change( &p_sys->pts, p_sys->fmt.audio.i_rate, 1 );
335             p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
336         }
337     }
338
339     return VLC_SUCCESS;
340
341 corrupt:
342     msg_Err( p_demux, "corrupted file - halting demux" );
343     return VLC_EGENERIC;
344 }
345
346 /*****************************************************************************
347  * Demux: read packet and send them to decoders
348  *****************************************************************************
349  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
350  *****************************************************************************/
351 static int Demux( demux_t *p_demux )
352 {
353     demux_sys_t *p_sys = p_demux->p_sys;
354     block_t     *p_block;
355     int64_t     i_offset;
356
357     if( p_sys->i_silence_countdown == 0 )
358     {
359         i_offset = stream_Tell( p_demux->s );
360
361         while( ( i_offset >= p_sys->i_block_offset + p_sys->i_block_size )
362              && ( p_sys->i_silence_countdown == 0 ) )
363             if( ReadBlockHeader( p_demux ) != VLC_SUCCESS )
364                 return 0;
365
366         p_block = stream_Block( p_demux->s,
367                                 p_sys->fmt.audio.i_bytes_per_frame );
368         if( p_block == NULL )
369         {
370             msg_Warn( p_demux, "cannot read data" );
371             return 0;
372         }
373     }
374     else
375     {   /* emulates silence from the stream */
376         p_block = block_New( p_demux, 1 );
377         if( p_block == NULL )
378             return VLC_ENOMEM;
379
380         p_block->p_buffer[0] = 0;
381         p_sys->i_silence_countdown--;
382     }
383
384     p_block->i_dts = p_block->i_pts =
385         date_Increment( &p_sys->pts, p_sys->fmt.audio.i_frame_length );
386
387     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
388
389     es_out_Send( p_demux->out, p_sys->p_es, p_block );
390
391     return 1;
392 }
393
394 /*****************************************************************************
395  * Close: frees unused data
396  *****************************************************************************/
397 static void Close ( vlc_object_t * p_this )
398 {
399     demux_sys_t *p_sys  = ((demux_t *)p_this)->p_sys;
400
401     free( p_sys );
402 }
403
404 /*****************************************************************************
405  * Control:
406  *****************************************************************************/
407 static int Control( demux_t *p_demux, int i_query, va_list args )
408 {
409     demux_sys_t *p_sys  = p_demux->p_sys;
410
411     return demux2_vaControlHelper( p_demux->s, p_sys->i_block_offset,
412                                    p_sys->i_block_offset + p_sys->i_block_size,
413                                    p_sys->fmt.i_bitrate,
414                                    p_sys->fmt.audio.i_blockalign,
415                                    i_query, args );
416 }