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