]> git.sesse.net Git - vlc/blob - modules/demux/voc.c
Creative Voice File demux
[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: xa.c 10182 2005-03-07 15:04:53Z courmisch $
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 #if 0
61     int64_t         i_data_offset;
62     unsigned int    i_data_size;
63 #endif
64
65     int32_t         i_block_size;
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, buf[8];
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     /* read first block header */
111     /* FIXME: we only support one-block files at the moment */
112     if( stream_Read( p_demux->s, buf, 4 ) < 4 )
113         return VLC_EGENERIC;
114
115     p_demux->pf_demux   = Demux;
116     p_demux->pf_control = Control;
117     p_demux->p_sys      = p_sys = malloc( sizeof( demux_sys_t ) );
118
119     p_sys->i_block_size = GetDWLE( buf ) >> 8;
120     msg_Dbg( p_demux, "block size: %u bytes", p_sys->i_block_size );
121
122     es_format_Init( &p_sys->fmt, AUDIO_ES, 0 );
123
124     switch( *buf )
125     {
126         case 1:
127             if( p_sys->i_block_size < 2 )
128                 goto error;
129             p_sys->i_block_size -= 2;
130
131             if( stream_Read( p_demux->s, buf, 2 ) < 2 )
132                 goto error;
133
134             p_sys->fmt.audio.i_rate = 1000000L / (256L - buf[0]);
135             if( buf[1] )
136             {
137                 msg_Err( p_demux, "Unsupported compression" );
138                 goto error;
139             }
140
141             p_sys->fmt.i_codec = VLC_FOURCC('u','8',' ',' ');
142             p_sys->fmt.audio.i_bytes_per_frame = 1;
143             p_sys->fmt.audio.i_frame_length = 1;
144             p_sys->fmt.audio.i_channels = 1;
145             p_sys->fmt.audio.i_blockalign = 1;
146             p_sys->fmt.audio.i_bitspersample = 8;
147             p_sys->fmt.i_bitrate = p_sys->fmt.audio.i_rate * 8;
148             break;
149
150         case 9:
151             if( p_sys->i_block_size < 12 )
152                 goto error;
153             p_sys->i_block_size -= 12;
154
155             if( ( stream_Read( p_demux->s, buf, 8 ) < 8 )
156              || ( stream_Read( p_demux->s, NULL, 4 ) < 4 ) )
157                 goto error;
158
159             p_sys->fmt.audio.i_rate = GetDWLE( buf );
160             p_sys->fmt.audio.i_bitspersample = buf[4];
161             p_sys->fmt.audio.i_channels = buf[5];
162
163             switch( GetWLE( &buf[6] ) ) /* format */
164             {
165                 case 0x0000: /* PCM */
166                     switch( p_sys->fmt.audio.i_bitspersample )
167                     {
168                         case 8:
169                             p_sys->fmt.i_codec = VLC_FOURCC('u','8',' ',' ');
170                             break;
171
172                         case 16:
173                             p_sys->fmt.i_codec = VLC_FOURCC('u','1','6','l');
174                             break;
175
176                         default:
177                             msg_Err( p_demux, "Unsupported bit res.: %u bits",
178                                      p_sys->fmt.audio.i_bitspersample );
179                             return VLC_EGENERIC;
180                     }
181                     break;
182
183                 case 0x0004: /* signed */
184                     switch( p_sys->fmt.audio.i_bitspersample )
185                     {
186                         case 8:
187                             p_sys->fmt.i_codec = VLC_FOURCC('s','8',' ',' ');
188                             break;
189
190                         case 16:
191                             p_sys->fmt.i_codec = VLC_FOURCC('s','1','6','l');
192                             break;
193
194                         default:
195                             msg_Err( p_demux, "Unsupported bit res.: %u bits",
196                                      p_sys->fmt.audio.i_bitspersample );
197                             return VLC_EGENERIC;
198                     }
199                     break;
200
201                 default: 
202                     msg_Err( p_demux, "Unsupported compression" );
203                     goto error;
204             }
205
206             p_sys->fmt.audio.i_bytes_per_frame = p_sys->fmt.audio.i_channels
207                 * (p_sys->fmt.audio.i_bitspersample / 8);
208             p_sys->fmt.audio.i_frame_length = 1;
209             p_sys->fmt.audio.i_blockalign = p_sys->fmt.audio.i_bytes_per_frame;
210             p_sys->fmt.i_bitrate = 8 * p_sys->fmt.audio.i_rate
211                                      * p_sys->fmt.audio.i_bytes_per_frame;
212             break;
213
214         default:
215             msg_Dbg( p_demux, "Unsupported block type %u", (unsigned)*p_buf);
216             return VLC_EGENERIC;
217     }
218     p_sys->fmt.i_extra = 0;
219     p_sys->fmt.p_extra = NULL;
220
221     msg_Dbg( p_demux, "fourcc: %4.4s, channels: %d, "
222              "freq: %d Hz, bitrate: %dKo/s, blockalign: %d, bits/samples: %d",
223              (char *)&p_sys->fmt.i_codec, p_sys->fmt.audio.i_channels,
224              p_sys->fmt.audio.i_rate, p_sys->fmt.i_bitrate / 8192,
225              p_sys->fmt.audio.i_blockalign, p_sys->fmt.audio.i_bitspersample
226              );
227
228     p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
229
230     date_Init( &p_sys->pts, p_sys->fmt.audio.i_rate, 1 );
231     date_Set( &p_sys->pts, 1 );
232
233     return VLC_SUCCESS;
234
235 error:
236     msg_Err( p_demux, "Unsupported or malformatted file" );
237     free( p_sys );
238     return VLC_EGENERIC;
239 }
240
241 /*****************************************************************************
242  * Demux: read packet and send them to decoders
243  *****************************************************************************
244  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
245  *****************************************************************************/
246 static int Demux( demux_t *p_demux )
247 {
248     demux_sys_t *p_sys = p_demux->p_sys;
249     block_t     *p_block;
250 #if 0
251     int64_t     i_offset;
252
253     i_offset = stream_Tell( p_demux->s );
254
255     if( p_sys->i_data_size > 0 &&
256         i_offset >= p_sys->i_data_offset + p_sys->i_data_size )
257     {
258         /* EOF */
259         return 0;
260     }
261 #endif
262     p_block = stream_Block( p_demux->s, p_sys->fmt.audio.i_bytes_per_frame );
263     if( p_block == NULL )
264     {
265         msg_Warn( p_demux, "cannot read data" );
266         return 0;
267     }
268
269     p_block->i_dts = p_block->i_pts =
270         date_Increment( &p_sys->pts, p_sys->fmt.audio.i_frame_length );
271
272     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
273
274     es_out_Send( p_demux->out, p_sys->p_es, p_block );
275
276     return 1;
277 }
278
279 /*****************************************************************************
280  * Close: frees unused data
281  *****************************************************************************/
282 static void Close ( vlc_object_t * p_this )
283 {
284     demux_sys_t *p_sys  = ((demux_t *)p_this)->p_sys;
285
286     free( p_sys );
287 }
288
289 /*****************************************************************************
290  * Control:
291  *****************************************************************************/
292 static int Control( demux_t *p_demux, int i_query, va_list args )
293 {
294     demux_sys_t *p_sys  = p_demux->p_sys;
295
296     if( i_query == DEMUX_SET_POSITION )
297         return VLC_EGENERIC; /* not implemented yet */
298
299     return demux2_vaControlHelper( p_demux->s, 0, -1, p_sys->fmt.i_bitrate,
300                                    p_sys->fmt.audio.i_blockalign,
301                                    i_query, args );
302 }