]> git.sesse.net Git - vlc/blob - modules/demux/voc.c
Handle VOC files with multiple blocks properly
[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 # via.ecp.fr>
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     int32_t         i_block_size;
62
63     date_t          pts;
64 };
65
66 static const char ct_header[] = "Creative Voice File\x1a";
67
68 /*****************************************************************************
69  * Open: check file and initializes structures
70  *****************************************************************************/
71 static int Open( vlc_object_t * p_this )
72 {
73     demux_t     *p_demux = (demux_t*)p_this;
74     demux_sys_t *p_sys;
75     uint8_t     *p_buf;
76     uint16_t    i_data_offset, i_version;
77
78     if( stream_Peek( p_demux->s, &p_buf, 26 ) < 26 )
79         return VLC_EGENERIC;
80
81     if( memcmp( p_buf, ct_header, 20 ) )
82         return VLC_EGENERIC;
83     p_buf += 20;
84
85     i_data_offset = GetWLE( p_buf );
86     if ( i_data_offset < 26 /* not enough room for full VOC header */ )
87         return VLC_EGENERIC;
88     p_buf += 2;
89
90     i_version = GetWLE( p_buf );
91     if( ( i_version != 0x10A ) && ( i_version != 0x114 ) )
92         return VLC_EGENERIC; /* unknown VOC version */
93     p_buf += 2;
94
95     if( GetWLE( p_buf ) != (uint16_t)(0x1234 + ~i_version) )
96         return VLC_EGENERIC;
97
98     /* We have a valid VOC header */
99     msg_Dbg( p_demux, "CT Voice file v%d.%d", i_version >> 8,
100              i_version & 0xff );
101
102     /* skip VOC header */
103     if( stream_Read( p_demux->s, NULL, i_data_offset ) < i_data_offset )
104         return VLC_EGENERIC;
105
106     p_demux->pf_demux   = Demux;
107     p_demux->pf_control = Control;
108     p_demux->p_sys      = p_sys = malloc( sizeof( demux_sys_t ) );
109
110     if( p_sys == NULL )
111         return VLC_ENOMEM;
112
113     p_sys->i_block_offset = p_sys->i_block_size = 0;
114     p_sys->p_es = NULL;
115
116     date_Init( &p_sys->pts, 1, 1 );
117     date_Set( &p_sys->pts, 1 );
118
119     return VLC_SUCCESS;
120 }
121
122
123 static int fmtcmp( es_format_t *ofmt, es_format_t *nfmt )
124 {
125     return (ofmt->audio.i_bitspersample != nfmt->audio.i_bitspersample)
126         || (ofmt->audio.i_rate != nfmt->audio.i_rate)
127         || (ofmt->audio.i_channels != nfmt->audio.i_channels);
128 }
129
130
131 static int ReadBlockHeader( demux_t *p_demux )
132 {
133     es_format_t     new_fmt;
134     uint8_t buf[8];
135     int32_t i_block_size;
136     demux_sys_t *p_sys = p_demux->p_sys;
137
138     if( stream_Read( p_demux->s, buf, 4 ) < 4 )
139         return VLC_EGENERIC; /* EOF */
140
141     i_block_size = GetDWLE( buf ) >> 8;
142     msg_Dbg( p_demux, "new block: type: %u, size: %u",
143              (unsigned)*buf, i_block_size );
144
145     es_format_Init( &new_fmt, AUDIO_ES, 0 );
146
147     switch( *buf )
148     {
149         /*case 0: -- not possible caught earlier with stream_Read */
150         case 1:
151             if( i_block_size < 2 )
152                 return VLC_EGENERIC;
153             i_block_size -= 2;
154
155             if( stream_Read( p_demux->s, buf, 2 ) < 2 )
156                 return VLC_EGENERIC;
157
158             new_fmt.audio.i_rate = 1000000L / (256L - buf[0]);
159             if( buf[1] )
160             {
161                 msg_Err( p_demux, "Unsupported compression" );
162                 return VLC_EGENERIC;
163             }
164
165             new_fmt.i_codec = VLC_FOURCC('u','8',' ',' ');
166             new_fmt.audio.i_bytes_per_frame = 1;
167             new_fmt.audio.i_frame_length = 1;
168             new_fmt.audio.i_channels = 1;
169             new_fmt.audio.i_blockalign = 1;
170             new_fmt.audio.i_bitspersample = 8;
171             new_fmt.i_bitrate = p_sys->fmt.audio.i_rate * 8;
172             break;
173
174         /* FIXME: support block types 2, 3, 6, 7, 8 properly */
175         case 2:
176         case 3:
177         case 6:
178         case 7:
179         case 8:
180
181         /* non-audio block types can be skipped */
182         case 4:
183         case 5:
184             if( stream_Read( p_demux->s, NULL, i_block_size ) < i_block_size )
185                 return VLC_EGENERIC;
186             i_block_size = 0;
187             break;
188
189         case 9:
190             if( i_block_size < 12 )
191                 return VLC_EGENERIC;
192             i_block_size -= 12;
193
194             if( ( stream_Read( p_demux->s, buf, 8 ) < 8 )
195              || ( stream_Read( p_demux->s, NULL, 4 ) < 4 ) )
196                 return VLC_EGENERIC;
197
198             new_fmt.audio.i_rate = GetDWLE( buf );
199             new_fmt.audio.i_bitspersample = buf[4];
200             new_fmt.audio.i_channels = buf[5];
201
202             switch( GetWLE( &buf[6] ) ) /* format */
203             {
204                 case 0x0000: /* PCM */
205                     switch( new_fmt.audio.i_bitspersample )
206                     {
207                         case 8:
208                             new_fmt.i_codec = VLC_FOURCC('u','8',' ',' ');
209                             break;
210
211                         case 16:
212                             new_fmt.i_codec = VLC_FOURCC('u','1','6','l');
213                             break;
214
215                         default:
216                             msg_Err( p_demux, "Unsupported bit res.: %u bits",
217                                      new_fmt.audio.i_bitspersample );
218                             return VLC_EGENERIC;
219                     }
220                     break;
221
222                 case 0x0004: /* signed */
223                     switch( new_fmt.audio.i_bitspersample )
224                     {
225                         case 8:
226                             new_fmt.i_codec = VLC_FOURCC('s','8',' ',' ');
227                             break;
228
229                         case 16:
230                             new_fmt.i_codec = VLC_FOURCC('s','1','6','l');
231                             break;
232
233                         default:
234                             msg_Err( p_demux, "Unsupported bit res.: %u bits",
235                                      new_fmt.audio.i_bitspersample );
236                             return VLC_EGENERIC;
237                     }
238                     break;
239
240                 default: 
241                     msg_Err( p_demux, "Unsupported compression" );
242                     return VLC_EGENERIC;
243             }
244
245             new_fmt.audio.i_bytes_per_frame = new_fmt.audio.i_channels
246                 * (new_fmt.audio.i_bitspersample / 8);
247             new_fmt.audio.i_frame_length = 1;
248             new_fmt.audio.i_blockalign = p_sys->fmt.audio.i_bytes_per_frame;
249             new_fmt.i_bitrate = 8 * new_fmt.audio.i_rate
250                                      * new_fmt.audio.i_bytes_per_frame;
251             break;
252
253         default:
254             msg_Dbg( p_demux, "Unsupported block type %u", (unsigned)*buf);
255             return VLC_EGENERIC;
256     }
257
258     p_sys->i_block_size = i_block_size;
259     p_sys->i_block_offset = stream_Tell( p_demux->s );
260
261     if( i_block_size )
262     {
263         /* we've read a block with data in it - update decoder */
264         msg_Dbg( p_demux, "fourcc: %4.4s, channels: %d, "
265                  "freq: %d Hz, bitrate: %dKo/s, blockalign: %d, "
266                  "bits/samples: %d", (char *)&new_fmt.i_codec,
267                  new_fmt.audio.i_channels, new_fmt.audio.i_rate,
268                  new_fmt.i_bitrate / 8192, new_fmt.audio.i_blockalign,
269                  new_fmt.audio.i_bitspersample );
270
271         if( ( p_sys->p_es != NULL ) && fmtcmp( &p_sys->fmt, &new_fmt ) )
272         {
273             msg_Dbg( p_demux, "codec change needed" );
274             es_out_Del( p_demux->out, p_sys->p_es );
275             p_sys->p_es = NULL;
276         }
277
278         if( p_sys->p_es == NULL )
279         {
280             memcpy( &p_sys->fmt, &new_fmt, sizeof( p_sys->fmt ) );
281             date_Change( &p_sys->pts, p_sys->fmt.audio.i_rate, 1 );
282             p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
283         }
284     }
285
286     return VLC_SUCCESS;
287 }
288
289 /*****************************************************************************
290  * Demux: read packet and send them to decoders
291  *****************************************************************************
292  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
293  *****************************************************************************/
294 static int Demux( demux_t *p_demux )
295 {
296     demux_sys_t *p_sys = p_demux->p_sys;
297     block_t     *p_block;
298     int64_t     i_offset;
299
300     i_offset = stream_Tell( p_demux->s );
301
302     while( i_offset >= p_sys->i_block_offset + p_sys->i_block_size )
303         if( ReadBlockHeader( p_demux ) != VLC_SUCCESS )
304             return 0;
305
306     p_block = stream_Block( p_demux->s, p_sys->fmt.audio.i_bytes_per_frame );
307     if( p_block == NULL )
308     {
309         msg_Warn( p_demux, "cannot read data" );
310         return 0;
311     }
312
313     p_block->i_dts = p_block->i_pts =
314         date_Increment( &p_sys->pts, p_sys->fmt.audio.i_frame_length );
315
316     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
317
318     es_out_Send( p_demux->out, p_sys->p_es, p_block );
319
320     return 1;
321 }
322
323 /*****************************************************************************
324  * Close: frees unused data
325  *****************************************************************************/
326 static void Close ( vlc_object_t * p_this )
327 {
328     demux_sys_t *p_sys  = ((demux_t *)p_this)->p_sys;
329
330     free( p_sys );
331 }
332
333 /*****************************************************************************
334  * Control:
335  *****************************************************************************/
336 static int Control( demux_t *p_demux, int i_query, va_list args )
337 {
338     demux_sys_t *p_sys  = p_demux->p_sys;
339
340     return demux2_vaControlHelper( p_demux->s, p_sys->i_block_offset,
341                                    p_sys->i_block_offset + p_sys->i_block_size,
342                                    p_sys->fmt.i_bitrate,
343                                    p_sys->fmt.audio.i_blockalign,
344                                    i_query, args );
345 }