/src/vlc/modules/demux/aiff.c
Line | Count | Source |
1 | | /***************************************************************************** |
2 | | * aiff.c: Audio Interchange File Format demuxer |
3 | | ***************************************************************************** |
4 | | * Copyright (C) 2004-2007 VLC authors and VideoLAN |
5 | | * |
6 | | * Authors: Laurent Aimar <fenrir@via.ecp.fr> |
7 | | * |
8 | | * This program is free software; you can redistribute it and/or modify it |
9 | | * under the terms of the GNU Lesser General Public License as published by |
10 | | * the Free Software Foundation; either version 2.1 of the License, or |
11 | | * (at your option) any later version. |
12 | | * |
13 | | * This program is distributed in the hope that it will be useful, |
14 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | * GNU Lesser General Public License for more details. |
17 | | * |
18 | | * You should have received a copy of the GNU Lesser General Public License |
19 | | * along with this program; if not, write to the Free Software Foundation, |
20 | | * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. |
21 | | *****************************************************************************/ |
22 | | |
23 | | /***************************************************************************** |
24 | | * Preamble |
25 | | *****************************************************************************/ |
26 | | |
27 | | #ifdef HAVE_CONFIG_H |
28 | | # include "config.h" |
29 | | #endif |
30 | | |
31 | | #include <vlc_common.h> |
32 | | #include <vlc_plugin.h> |
33 | | #include <vlc_demux.h> |
34 | | #include <vlc_aout.h> |
35 | | #include <vlc_meta.h> |
36 | | #include <limits.h> |
37 | | |
38 | | #include "mp4/coreaudio.h" |
39 | | |
40 | | /* TODO: |
41 | | * - ... |
42 | | */ |
43 | | |
44 | | /***************************************************************************** |
45 | | * Module descriptor |
46 | | *****************************************************************************/ |
47 | | static int Open ( vlc_object_t * ); |
48 | | static void Close ( vlc_object_t * ); |
49 | | |
50 | 168 | vlc_module_begin () |
51 | 84 | set_subcategory( SUBCAT_INPUT_DEMUX ) |
52 | 84 | set_description( N_("AIFF demuxer" ) ) |
53 | 84 | set_capability( "demux", 10 ) |
54 | 168 | set_callbacks( Open, Close ) |
55 | 84 | add_shortcut( "aiff" ) |
56 | 84 | add_file_extension("aiff") |
57 | 84 | vlc_module_end () |
58 | | |
59 | | /***************************************************************************** |
60 | | * Local prototypes |
61 | | *****************************************************************************/ |
62 | | |
63 | | typedef struct |
64 | | { |
65 | | es_format_t fmt; |
66 | | es_out_id_t *es; |
67 | | |
68 | | int64_t i_ssnd_pos; |
69 | | int64_t i_ssnd_size; |
70 | | int i_ssnd_offset; |
71 | | int i_ssnd_blocksize; |
72 | | |
73 | | /* real data start */ |
74 | | int64_t i_ssnd_start; |
75 | | int64_t i_ssnd_end; |
76 | | |
77 | | int i_ssnd_fsize; |
78 | | |
79 | | vlc_tick_t i_time; |
80 | | |
81 | | uint8_t i_chans_to_reorder; |
82 | | uint8_t pi_chan_table[AOUT_CHAN_MAX]; |
83 | | vlc_meta_t *p_meta; |
84 | | vlc_fourcc_t audio_fourcc; |
85 | | } demux_sys_t; |
86 | | |
87 | | static int Demux ( demux_t *p_demux ); |
88 | | static int Control( demux_t *p_demux, int i_query, va_list args ); |
89 | | |
90 | | /* GetF80BE: read a 80 bits float in big endian */ |
91 | | static unsigned int GetF80BE( const uint8_t p[10] ) |
92 | 2.20k | { |
93 | 2.20k | unsigned int i_mantissa = GetDWBE( &p[2] ); |
94 | 2.20k | int i_exp = 30 - p[1]; |
95 | 2.20k | unsigned int i_last = 0; |
96 | | |
97 | 26.3k | while( i_exp-- > 0 ) |
98 | 24.1k | { |
99 | 24.1k | i_last = i_mantissa; |
100 | 24.1k | i_mantissa >>= 1; |
101 | 24.1k | } |
102 | 2.20k | if( i_last&0x01 ) |
103 | 444 | { |
104 | 444 | i_mantissa++; |
105 | 444 | } |
106 | 2.20k | return i_mantissa; |
107 | 2.20k | } |
108 | | |
109 | | /***************************************************************************** |
110 | | * ReadTextChunk: reads aiff text chunks - name, author, copyright, annotation |
111 | | *****************************************************************************/ |
112 | | static int ReadTextChunk( demux_t *p_demux, uint64_t i_chunk_size, uint32_t i_data_size ) |
113 | 345k | { |
114 | 345k | demux_sys_t *p_sys = p_demux->p_sys; |
115 | 345k | const uint8_t *p_peek; |
116 | | |
117 | | #if UINT32_MAX >= SIZE_MAX |
118 | | if ( i_data_size >= SIZE_MAX ) |
119 | | return VLC_ENOMEM; |
120 | | #endif |
121 | | |
122 | 345k | ssize_t ret = vlc_stream_Peek( p_demux->s, &p_peek, i_chunk_size ); |
123 | 345k | if( ret == -1 || (size_t) ret != i_chunk_size ) |
124 | 25 | return VLC_EGENERIC; |
125 | | |
126 | 345k | static const struct { |
127 | 345k | const char chunk_id[4]; |
128 | 345k | int i_meta; |
129 | 345k | } p_dsc[4] = { |
130 | 345k | { {'N', 'A', 'M', 'E'}, vlc_meta_Title }, |
131 | 345k | { {'A', 'U', 'T', 'H'}, vlc_meta_Artist }, |
132 | 345k | { {'(', 'c', ')', ' '}, vlc_meta_Copyright }, |
133 | 345k | { {'A', 'N', 'N', 'O'}, vlc_meta_Description } |
134 | 345k | }; |
135 | | |
136 | 1.72M | for( size_t i = 0; i < ARRAY_SIZE( p_dsc ); i++ ) |
137 | 1.37M | { |
138 | 1.37M | if( memcmp(p_peek, p_dsc[i].chunk_id, 4) ) |
139 | 1.37M | continue; |
140 | | |
141 | 861 | char *psz_value = malloc( i_data_size + 1 ); |
142 | | |
143 | 861 | if( unlikely(psz_value == NULL) ) |
144 | 0 | return VLC_ENOMEM; |
145 | | |
146 | 861 | if( !p_sys->p_meta ) |
147 | 8 | { |
148 | 8 | p_sys->p_meta = vlc_meta_New(); |
149 | 8 | if( unlikely(p_sys->p_meta == NULL) ) |
150 | 0 | { |
151 | 0 | free( psz_value ); |
152 | 0 | return VLC_ENOMEM; |
153 | 0 | } |
154 | 8 | } |
155 | | |
156 | 861 | memcpy( psz_value, (char*)&p_peek[8], i_data_size ); |
157 | 861 | psz_value[i_data_size] = '\0'; |
158 | 861 | vlc_meta_Set( p_sys->p_meta, p_dsc[i].i_meta, psz_value ); |
159 | 861 | free( psz_value ); |
160 | | |
161 | 861 | return VLC_SUCCESS; |
162 | 861 | } |
163 | | |
164 | 344k | return VLC_EGENERIC; |
165 | 345k | } |
166 | | |
167 | | /***************************************************************************** |
168 | | * Open |
169 | | *****************************************************************************/ |
170 | | static int Open( vlc_object_t *p_this ) |
171 | 607 | { |
172 | 607 | demux_t *p_demux = (demux_t*)p_this; |
173 | 607 | demux_sys_t *p_sys; |
174 | | |
175 | 607 | const uint8_t *p_peek; |
176 | | |
177 | 607 | if( vlc_stream_Peek( p_demux->s, &p_peek, 12 ) < 12 ) |
178 | 0 | return VLC_EGENERIC; |
179 | 607 | if( memcmp( p_peek, "FORM", 4 ) || memcmp( &p_peek[8], "AIFF", 4 ) ) |
180 | 422 | return VLC_EGENERIC; |
181 | | |
182 | | /* skip aiff header */ |
183 | 185 | if( vlc_stream_Read( p_demux->s, NULL, 12 ) != 12 ) |
184 | 0 | return VLC_EGENERIC; |
185 | | |
186 | 185 | p_sys = vlc_obj_calloc( p_this, 1, sizeof( *p_sys ) ); |
187 | | |
188 | 185 | if( unlikely(p_sys == NULL) ) |
189 | 0 | return VLC_ENOMEM; |
190 | | |
191 | 185 | p_demux->p_sys = p_sys; |
192 | | |
193 | 185 | p_sys->i_time = 0; |
194 | 185 | p_sys->i_ssnd_pos = -1; |
195 | 185 | p_sys->i_chans_to_reorder = 0; |
196 | 185 | es_format_Init( &p_sys->fmt, AUDIO_ES, VLC_FOURCC( 't', 'w', 'o', 's' ) ); |
197 | | |
198 | 185 | const uint32_t *pi_channels_in = NULL; |
199 | | |
200 | 185 | for( ;; ) |
201 | 347k | { |
202 | 347k | if( vlc_stream_Peek( p_demux->s, &p_peek, 8 ) < 8 ) |
203 | 139 | break; |
204 | | |
205 | 347k | uint32_t i_data_size = GetDWBE( &p_peek[4] ); |
206 | 347k | uint64_t i_chunk_size = UINT64_C( 8 ) + i_data_size + ( i_data_size & 1 ); |
207 | | |
208 | 347k | msg_Dbg( p_demux, "chunk fcc=%4.4s size=%" PRIu64 " data_size=%" PRIu32, |
209 | 347k | p_peek, i_chunk_size, i_data_size ); |
210 | | |
211 | 347k | if( !memcmp( p_peek, "COMM", 4 ) ) |
212 | 1.10k | { |
213 | 1.10k | if( vlc_stream_Peek( p_demux->s, &p_peek, 18+8 ) < 18+8 ) |
214 | 3 | goto error; |
215 | | |
216 | 1.10k | p_sys->fmt.audio.i_channels = GetWBE( &p_peek[8] ); |
217 | 1.10k | p_sys->fmt.audio.i_bitspersample = GetWBE( &p_peek[14] ); |
218 | 1.10k | p_sys->fmt.audio.i_rate = GetF80BE( &p_peek[16] ); |
219 | | |
220 | 1.10k | msg_Dbg( p_demux, "COMM: channels=%d samples_frames=%d bits=%d rate=%d", |
221 | 1.10k | GetWBE( &p_peek[8] ), GetDWBE( &p_peek[10] ), GetWBE( &p_peek[14] ), |
222 | 1.10k | GetF80BE( &p_peek[16] ) ); |
223 | 1.10k | } |
224 | 346k | else if( !memcmp( p_peek, "SSND", 4 ) ) |
225 | 511 | { |
226 | 511 | if( vlc_stream_Peek( p_demux->s, &p_peek, 8+8 ) < 8+8 ) |
227 | 0 | goto error; |
228 | | |
229 | 511 | p_sys->i_ssnd_pos = vlc_stream_Tell( p_demux->s ); |
230 | 511 | p_sys->i_ssnd_size = i_data_size; |
231 | 511 | p_sys->i_ssnd_offset = GetDWBE( &p_peek[8] ); |
232 | 511 | p_sys->i_ssnd_blocksize = GetDWBE( &p_peek[12] ); |
233 | | |
234 | 511 | msg_Dbg( p_demux, "SSND: (offset=%d blocksize=%d)", |
235 | 511 | p_sys->i_ssnd_offset, p_sys->i_ssnd_blocksize ); |
236 | 511 | } |
237 | 345k | else if( !memcmp( p_peek, "CHAN", 4 ) && i_chunk_size > 8 + 12 ) |
238 | 721 | { |
239 | 721 | ssize_t ret = vlc_stream_Peek( p_demux->s, &p_peek, i_chunk_size ); |
240 | 721 | if( ret == -1 || (size_t) ret != i_chunk_size ) |
241 | 15 | goto error; |
242 | | |
243 | 706 | struct CoreAudio_layout_s layout; |
244 | 706 | layout.i_channels_layout_tag = GetDWBE( &p_peek[8] ); |
245 | 706 | layout.i_channels_bitmap = GetDWBE( &p_peek[8+4] ); |
246 | 706 | layout.i_channels_description_count = GetDWBE( &p_peek[8+8] ); |
247 | | /* TODO: handle CoreAudio_layout_s.p_descriptions */ |
248 | | |
249 | 706 | if ( CoreAudio_Layout_to_vlc( &layout, |
250 | 706 | &p_sys->fmt.audio.i_physical_channels, |
251 | 706 | &p_sys->fmt.audio.i_channels, |
252 | 706 | &pi_channels_in ) != VLC_SUCCESS ) |
253 | 706 | msg_Warn( p_demux, "discarding chan mapping" ); |
254 | 706 | } |
255 | 345k | else |
256 | 345k | { |
257 | 345k | int i_ret = ReadTextChunk( p_demux, i_chunk_size, i_data_size ); |
258 | | |
259 | 345k | if( unlikely(i_ret == VLC_ENOMEM) ) |
260 | 0 | goto error; |
261 | 345k | } |
262 | | |
263 | | /* consume chunk data */ |
264 | 694k | for( ssize_t i_req; i_chunk_size; i_chunk_size -= i_req ) |
265 | 347k | { |
266 | 347k | #if SSIZE_MAX < UINT64_MAX |
267 | 347k | i_req = __MIN( SSIZE_MAX, i_chunk_size ); |
268 | | #else |
269 | | i_req = i_chunk_size; |
270 | | #endif |
271 | 347k | if( vlc_stream_Read( p_demux->s, NULL, i_req ) != i_req ) |
272 | 28 | { |
273 | 28 | msg_Warn( p_demux, "incomplete file" ); |
274 | 28 | goto error; |
275 | 28 | } |
276 | 347k | } |
277 | 347k | } |
278 | | |
279 | 139 | if( pi_channels_in != NULL ) |
280 | 40 | { |
281 | 40 | p_sys->i_chans_to_reorder = |
282 | 40 | aout_CheckChannelReorder( pi_channels_in, NULL, |
283 | 40 | p_sys->fmt.audio.i_physical_channels, |
284 | 40 | p_sys->pi_chan_table ) > 0; |
285 | 40 | p_sys->audio_fourcc = |
286 | 40 | vlc_fourcc_GetCodecAudio( p_sys->fmt.i_codec, |
287 | 40 | p_sys->fmt.audio.i_bitspersample ); |
288 | 40 | if( p_sys->audio_fourcc == 0 ) |
289 | 0 | p_sys->i_chans_to_reorder = 0; |
290 | 40 | } |
291 | | |
292 | 139 | p_sys->i_ssnd_start = p_sys->i_ssnd_pos + 16 + p_sys->i_ssnd_offset; |
293 | 139 | p_sys->i_ssnd_end = p_sys->i_ssnd_start + p_sys->i_ssnd_size; |
294 | | |
295 | 139 | p_sys->i_ssnd_fsize = p_sys->fmt.audio.i_channels * |
296 | 139 | ((p_sys->fmt.audio.i_bitspersample + 7) / 8); |
297 | | |
298 | 139 | if( p_sys->i_ssnd_fsize <= 0 || p_sys->fmt.audio.i_rate == 0 || p_sys->i_ssnd_pos < 12 ) |
299 | 12 | { |
300 | 12 | msg_Err( p_demux, "invalid audio parameters" ); |
301 | 12 | goto error; |
302 | 12 | } |
303 | | |
304 | 127 | if( p_sys->i_ssnd_size <= 0 ) |
305 | 105 | { |
306 | | /* unknown */ |
307 | 105 | p_sys->i_ssnd_end = 0; |
308 | 105 | } |
309 | | |
310 | | /* seek into SSND chunk */ |
311 | 127 | if( vlc_stream_Seek( p_demux->s, p_sys->i_ssnd_start ) ) |
312 | 0 | { |
313 | 0 | msg_Err( p_demux, "cannot seek to data chunk" ); |
314 | 0 | goto error; |
315 | 0 | } |
316 | | |
317 | | /* */ |
318 | 127 | p_sys->fmt.i_id = 0; |
319 | 127 | p_sys->es = es_out_Add( p_demux->out, &p_sys->fmt ); |
320 | 127 | if( unlikely(p_sys->es == NULL) ) |
321 | 0 | goto error; |
322 | | |
323 | 127 | p_demux->pf_demux = Demux; |
324 | 127 | p_demux->pf_control = Control; |
325 | | |
326 | 127 | return VLC_SUCCESS; |
327 | | |
328 | 58 | error: |
329 | 58 | Close( p_this ); |
330 | 58 | return VLC_EGENERIC; |
331 | 127 | } |
332 | | |
333 | | /***************************************************************************** |
334 | | * Demux: |
335 | | *****************************************************************************/ |
336 | | static int Demux( demux_t *p_demux ) |
337 | 794k | { |
338 | 794k | demux_sys_t *p_sys = p_demux->p_sys; |
339 | 794k | int64_t i_tell = vlc_stream_Tell( p_demux->s ); |
340 | | |
341 | 794k | block_t *p_block; |
342 | 794k | int i_read; |
343 | | |
344 | 794k | if( p_sys->i_ssnd_end > 0 && i_tell >= p_sys->i_ssnd_end ) |
345 | 2 | { |
346 | | /* EOF */ |
347 | 2 | return VLC_DEMUXER_EOF; |
348 | 2 | } |
349 | | |
350 | | /* Set PCR */ |
351 | 794k | es_out_SetPCR( p_demux->out, VLC_TICK_0 + p_sys->i_time); |
352 | | |
353 | | /* we will read 100ms at once */ |
354 | 794k | i_read = p_sys->i_ssnd_fsize * ( p_sys->fmt.audio.i_rate / 10 ); |
355 | 794k | if( p_sys->i_ssnd_end > 0 && p_sys->i_ssnd_end - i_tell < i_read ) |
356 | 23 | { |
357 | 23 | i_read = p_sys->i_ssnd_end - i_tell; |
358 | 23 | } |
359 | 794k | if( ( p_block = vlc_stream_Block( p_demux->s, i_read ) ) == NULL ) |
360 | 125 | { |
361 | 125 | return VLC_DEMUXER_EOF; |
362 | 125 | } |
363 | | |
364 | 794k | p_block->i_dts = |
365 | 794k | p_block->i_pts = VLC_TICK_0 + p_sys->i_time; |
366 | | |
367 | 794k | p_sys->i_time += vlc_tick_from_samples(p_block->i_buffer, |
368 | 794k | p_sys->i_ssnd_fsize) / |
369 | 794k | p_sys->fmt.audio.i_rate; |
370 | | |
371 | 794k | if( p_sys->i_chans_to_reorder ) |
372 | 8.18k | aout_ChannelReorder( p_block->p_buffer, p_block->i_buffer, |
373 | 8.18k | p_sys->i_chans_to_reorder, p_sys->pi_chan_table, |
374 | 8.18k | p_sys->audio_fourcc ); |
375 | | /* */ |
376 | 794k | es_out_Send( p_demux->out, p_sys->es, p_block ); |
377 | 794k | return VLC_DEMUXER_SUCCESS; |
378 | 794k | } |
379 | | |
380 | | /***************************************************************************** |
381 | | * Close: frees unused data |
382 | | *****************************************************************************/ |
383 | | static void Close( vlc_object_t * p_this ) |
384 | 185 | { |
385 | 185 | demux_t *p_demux = (demux_t*)p_this; |
386 | 185 | demux_sys_t *p_sys = p_demux->p_sys; |
387 | | |
388 | 185 | if( p_sys->p_meta ) |
389 | 8 | vlc_meta_Delete( p_sys->p_meta ); |
390 | 185 | } |
391 | | |
392 | | /***************************************************************************** |
393 | | * Control: |
394 | | *****************************************************************************/ |
395 | | static int Control( demux_t *p_demux, int i_query, va_list args ) |
396 | 0 | { |
397 | 0 | demux_sys_t *p_sys = p_demux->p_sys; |
398 | 0 | double f, *pf; |
399 | |
|
400 | 0 | switch( i_query ) |
401 | 0 | { |
402 | 0 | case DEMUX_CAN_SEEK: |
403 | 0 | return vlc_stream_vaControl( p_demux->s, i_query, args ); |
404 | | |
405 | 0 | case DEMUX_GET_POSITION: |
406 | 0 | { |
407 | 0 | int64_t i_start = p_sys->i_ssnd_start; |
408 | 0 | int64_t i_end = p_sys->i_ssnd_end > 0 ? p_sys->i_ssnd_end : stream_Size( p_demux->s ); |
409 | 0 | int64_t i_tell = vlc_stream_Tell( p_demux->s ); |
410 | |
|
411 | 0 | pf = va_arg( args, double * ); |
412 | |
|
413 | 0 | if( i_start < i_end ) |
414 | 0 | { |
415 | 0 | *pf = (double)(i_tell - i_start)/(double)(i_end - i_start); |
416 | 0 | return VLC_SUCCESS; |
417 | 0 | } |
418 | 0 | return VLC_EGENERIC; |
419 | 0 | } |
420 | | |
421 | 0 | case DEMUX_SET_POSITION: |
422 | 0 | { |
423 | 0 | int64_t i_start = p_sys->i_ssnd_start; |
424 | 0 | int64_t i_end = p_sys->i_ssnd_end > 0 ? p_sys->i_ssnd_end : stream_Size( p_demux->s ); |
425 | |
|
426 | 0 | f = va_arg( args, double ); |
427 | |
|
428 | 0 | if( i_start < i_end ) |
429 | 0 | { |
430 | 0 | int i_frame = (f * ( i_end - i_start )) / p_sys->i_ssnd_fsize; |
431 | 0 | int64_t i_new = i_start + i_frame * p_sys->i_ssnd_fsize; |
432 | |
|
433 | 0 | if( vlc_stream_Seek( p_demux->s, i_new ) ) |
434 | 0 | { |
435 | 0 | return VLC_EGENERIC; |
436 | 0 | } |
437 | 0 | p_sys->i_time = vlc_tick_from_samples( i_frame, p_sys->fmt.audio.i_rate ); |
438 | 0 | return VLC_SUCCESS; |
439 | 0 | } |
440 | 0 | return VLC_EGENERIC; |
441 | 0 | } |
442 | | |
443 | 0 | case DEMUX_GET_TIME: |
444 | 0 | *va_arg( args, vlc_tick_t * ) = p_sys->i_time; |
445 | 0 | return VLC_SUCCESS; |
446 | | |
447 | 0 | case DEMUX_GET_LENGTH: |
448 | 0 | { |
449 | 0 | int64_t i_end = p_sys->i_ssnd_end > 0 ? p_sys->i_ssnd_end : stream_Size( p_demux->s ); |
450 | |
|
451 | 0 | if( p_sys->i_ssnd_start < i_end ) |
452 | 0 | { |
453 | 0 | *va_arg( args, vlc_tick_t * ) = |
454 | 0 | vlc_tick_from_samples( i_end - p_sys->i_ssnd_start, p_sys->i_ssnd_fsize) / p_sys->fmt.audio.i_rate; |
455 | 0 | return VLC_SUCCESS; |
456 | 0 | } |
457 | 0 | return VLC_EGENERIC; |
458 | 0 | } |
459 | 0 | case DEMUX_GET_META: |
460 | 0 | { |
461 | 0 | vlc_meta_t *p_meta = va_arg( args, vlc_meta_t * ); |
462 | 0 | if( !p_sys->p_meta ) |
463 | 0 | return VLC_EGENERIC; |
464 | 0 | vlc_meta_Merge( p_meta, p_sys->p_meta ); |
465 | 0 | return VLC_SUCCESS; |
466 | 0 | } |
467 | | |
468 | 0 | case DEMUX_SET_TIME: |
469 | 0 | case DEMUX_GET_FPS: |
470 | 0 | return VLC_EGENERIC; |
471 | | |
472 | 0 | case DEMUX_CAN_PAUSE: |
473 | 0 | case DEMUX_SET_PAUSE_STATE: |
474 | 0 | case DEMUX_CAN_CONTROL_PACE: |
475 | 0 | case DEMUX_GET_PTS_DELAY: |
476 | 0 | return demux_vaControlHelper( p_demux->s, 0, -1, 0, 1, i_query, args ); |
477 | | |
478 | 0 | default: |
479 | 0 | return VLC_EGENERIC; |
480 | 0 | } |
481 | 0 | } |