/src/vlc/modules/demux/aiff.c
Line | Count | Source (jump to first uncovered line) |
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 | 104 | vlc_module_begin () |
51 | 52 | set_subcategory( SUBCAT_INPUT_DEMUX ) |
52 | 52 | set_description( N_("AIFF demuxer" ) ) |
53 | 52 | set_capability( "demux", 10 ) |
54 | 104 | set_callbacks( Open, Close ) |
55 | 52 | add_shortcut( "aiff" ) |
56 | 52 | add_file_extension("aiff") |
57 | 52 | 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 | | bool b_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 | 0 | { |
93 | 0 | unsigned int i_mantissa = GetDWBE( &p[2] ); |
94 | 0 | int i_exp = 30 - p[1]; |
95 | 0 | unsigned int i_last = 0; |
96 | |
|
97 | 0 | while( i_exp-- > 0 ) |
98 | 0 | { |
99 | 0 | i_last = i_mantissa; |
100 | 0 | i_mantissa >>= 1; |
101 | 0 | } |
102 | 0 | if( i_last&0x01 ) |
103 | 0 | { |
104 | 0 | i_mantissa++; |
105 | 0 | } |
106 | 0 | return i_mantissa; |
107 | 0 | } |
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 | 0 | { |
114 | 0 | demux_sys_t *p_sys = p_demux->p_sys; |
115 | 0 | const uint8_t *p_peek; |
116 | |
|
117 | 0 | ssize_t ret = vlc_stream_Peek( p_demux->s, &p_peek, i_chunk_size ); |
118 | 0 | if( ret == -1 || (size_t) ret != i_chunk_size ) |
119 | 0 | return VLC_EGENERIC; |
120 | | |
121 | 0 | static const struct { |
122 | 0 | const char psz_name[4]; |
123 | 0 | int i_meta; |
124 | 0 | } p_dsc[4] = { |
125 | 0 | { "NAME", vlc_meta_Title }, |
126 | 0 | { "AUTH", vlc_meta_Artist }, |
127 | 0 | { "(c) ", vlc_meta_Copyright }, |
128 | 0 | { "ANNO", vlc_meta_Description } |
129 | 0 | }; |
130 | |
|
131 | 0 | for( size_t i = 0; i < ARRAY_SIZE( p_dsc ); i++ ) |
132 | 0 | { |
133 | 0 | if( memcmp(p_peek, p_dsc[i].psz_name, 4) ) |
134 | 0 | continue; |
135 | | |
136 | 0 | char *psz_value = malloc( i_data_size + 1 ); |
137 | |
|
138 | 0 | if( unlikely(psz_value == NULL) ) |
139 | 0 | return VLC_ENOMEM; |
140 | | |
141 | 0 | if( !p_sys->p_meta ) |
142 | 0 | { |
143 | 0 | p_sys->p_meta = vlc_meta_New(); |
144 | 0 | if( unlikely(p_sys->p_meta == NULL) ) |
145 | 0 | { |
146 | 0 | free( psz_value ); |
147 | 0 | return VLC_ENOMEM; |
148 | 0 | } |
149 | 0 | } |
150 | | |
151 | 0 | memcpy( psz_value, (char*)&p_peek[8], i_data_size ); |
152 | 0 | psz_value[i_data_size] = '\0'; |
153 | 0 | vlc_meta_Set( p_sys->p_meta, p_dsc[i].i_meta, psz_value ); |
154 | 0 | free( psz_value ); |
155 | |
|
156 | 0 | return VLC_SUCCESS; |
157 | 0 | } |
158 | | |
159 | 0 | return VLC_EGENERIC; |
160 | 0 | } |
161 | | |
162 | | /***************************************************************************** |
163 | | * Open |
164 | | *****************************************************************************/ |
165 | | static int Open( vlc_object_t *p_this ) |
166 | 0 | { |
167 | 0 | demux_t *p_demux = (demux_t*)p_this; |
168 | 0 | demux_sys_t *p_sys; |
169 | |
|
170 | 0 | const uint8_t *p_peek; |
171 | |
|
172 | 0 | if( vlc_stream_Peek( p_demux->s, &p_peek, 12 ) < 12 ) |
173 | 0 | return VLC_EGENERIC; |
174 | 0 | if( memcmp( p_peek, "FORM", 4 ) || memcmp( &p_peek[8], "AIFF", 4 ) ) |
175 | 0 | return VLC_EGENERIC; |
176 | | |
177 | | /* skip aiff header */ |
178 | 0 | if( vlc_stream_Read( p_demux->s, NULL, 12 ) != 12 ) |
179 | 0 | return VLC_EGENERIC; |
180 | | |
181 | 0 | p_sys = vlc_obj_calloc( p_this, 1, sizeof( *p_sys ) ); |
182 | |
|
183 | 0 | if( unlikely(p_sys == NULL) ) |
184 | 0 | return VLC_ENOMEM; |
185 | | |
186 | 0 | p_demux->p_sys = p_sys; |
187 | |
|
188 | 0 | p_sys->i_time = 0; |
189 | 0 | p_sys->i_ssnd_pos = -1; |
190 | 0 | p_sys->b_reorder = false; |
191 | 0 | es_format_Init( &p_sys->fmt, AUDIO_ES, VLC_FOURCC( 't', 'w', 'o', 's' ) ); |
192 | |
|
193 | 0 | const uint32_t *pi_channels_in = NULL; |
194 | |
|
195 | 0 | for( ;; ) |
196 | 0 | { |
197 | 0 | if( vlc_stream_Peek( p_demux->s, &p_peek, 8 ) < 8 ) |
198 | 0 | break; |
199 | | |
200 | 0 | uint32_t i_data_size = GetDWBE( &p_peek[4] ); |
201 | 0 | uint64_t i_chunk_size = UINT64_C( 8 ) + i_data_size + ( i_data_size & 1 ); |
202 | |
|
203 | 0 | msg_Dbg( p_demux, "chunk fcc=%4.4s size=%" PRIu64 " data_size=%" PRIu32, |
204 | 0 | p_peek, i_chunk_size, i_data_size ); |
205 | |
|
206 | 0 | if( !memcmp( p_peek, "COMM", 4 ) ) |
207 | 0 | { |
208 | 0 | if( vlc_stream_Peek( p_demux->s, &p_peek, 18+8 ) < 18+8 ) |
209 | 0 | return VLC_EGENERIC; |
210 | | |
211 | 0 | p_sys->fmt.audio.i_channels = GetWBE( &p_peek[8] ); |
212 | 0 | p_sys->fmt.audio.i_bitspersample = GetWBE( &p_peek[14] ); |
213 | 0 | p_sys->fmt.audio.i_rate = GetF80BE( &p_peek[16] ); |
214 | |
|
215 | 0 | msg_Dbg( p_demux, "COMM: channels=%d samples_frames=%d bits=%d rate=%d", |
216 | 0 | GetWBE( &p_peek[8] ), GetDWBE( &p_peek[10] ), GetWBE( &p_peek[14] ), |
217 | 0 | GetF80BE( &p_peek[16] ) ); |
218 | 0 | } |
219 | 0 | else if( !memcmp( p_peek, "SSND", 4 ) ) |
220 | 0 | { |
221 | 0 | if( vlc_stream_Peek( p_demux->s, &p_peek, 8+8 ) < 8+8 ) |
222 | 0 | return VLC_EGENERIC; |
223 | | |
224 | 0 | p_sys->i_ssnd_pos = vlc_stream_Tell( p_demux->s ); |
225 | 0 | p_sys->i_ssnd_size = i_data_size; |
226 | 0 | p_sys->i_ssnd_offset = GetDWBE( &p_peek[8] ); |
227 | 0 | p_sys->i_ssnd_blocksize = GetDWBE( &p_peek[12] ); |
228 | |
|
229 | 0 | msg_Dbg( p_demux, "SSND: (offset=%d blocksize=%d)", |
230 | 0 | p_sys->i_ssnd_offset, p_sys->i_ssnd_blocksize ); |
231 | 0 | } |
232 | 0 | else if( !memcmp( p_peek, "CHAN", 4 ) && i_chunk_size > 8 + 12 ) |
233 | 0 | { |
234 | 0 | ssize_t ret = vlc_stream_Peek( p_demux->s, &p_peek, i_chunk_size ); |
235 | 0 | if( ret == -1 || (size_t) ret != i_chunk_size ) |
236 | 0 | return VLC_EGENERIC; |
237 | | |
238 | 0 | struct CoreAudio_layout_s layout; |
239 | 0 | layout.i_channels_layout_tag = GetDWBE( &p_peek[8] ); |
240 | 0 | layout.i_channels_bitmap = GetDWBE( &p_peek[8+4] ); |
241 | 0 | layout.i_channels_description_count = GetDWBE( &p_peek[8+8] ); |
242 | | /* TODO: handle CoreAudio_layout_s.p_descriptions */ |
243 | |
|
244 | 0 | if ( CoreAudio_Layout_to_vlc( &layout, |
245 | 0 | &p_sys->fmt.audio.i_physical_channels, |
246 | 0 | &p_sys->fmt.audio.i_channels, |
247 | 0 | &pi_channels_in ) != VLC_SUCCESS ) |
248 | 0 | msg_Warn( p_demux, "discarding chan mapping" ); |
249 | 0 | } |
250 | 0 | else |
251 | 0 | { |
252 | 0 | int i_ret = ReadTextChunk( p_demux, i_chunk_size, i_data_size ); |
253 | |
|
254 | 0 | if( unlikely(i_ret == VLC_ENOMEM) ) |
255 | 0 | return VLC_ENOMEM; |
256 | 0 | } |
257 | | |
258 | | /* consume chunk data */ |
259 | 0 | for( ssize_t i_req; i_chunk_size; i_chunk_size -= i_req ) |
260 | 0 | { |
261 | 0 | #if SSIZE_MAX < UINT64_MAX |
262 | 0 | i_req = __MIN( SSIZE_MAX, i_chunk_size ); |
263 | | #else |
264 | | i_req = i_chunk_size; |
265 | | #endif |
266 | 0 | if( vlc_stream_Read( p_demux->s, NULL, i_req ) != i_req ) |
267 | 0 | { |
268 | 0 | msg_Warn( p_demux, "incomplete file" ); |
269 | 0 | return VLC_EGENERIC; |
270 | 0 | } |
271 | 0 | } |
272 | 0 | } |
273 | | |
274 | 0 | if( pi_channels_in != NULL ) |
275 | 0 | { |
276 | 0 | p_sys->b_reorder = |
277 | 0 | aout_CheckChannelReorder( pi_channels_in, NULL, |
278 | 0 | p_sys->fmt.audio.i_physical_channels, |
279 | 0 | p_sys->pi_chan_table ) > 0; |
280 | 0 | p_sys->audio_fourcc = |
281 | 0 | vlc_fourcc_GetCodecAudio( p_sys->fmt.i_codec, |
282 | 0 | p_sys->fmt.audio.i_bitspersample ); |
283 | 0 | if( p_sys->audio_fourcc == 0 ) |
284 | 0 | p_sys->b_reorder = false; |
285 | 0 | } |
286 | |
|
287 | 0 | p_sys->i_ssnd_start = p_sys->i_ssnd_pos + 16 + p_sys->i_ssnd_offset; |
288 | 0 | p_sys->i_ssnd_end = p_sys->i_ssnd_start + p_sys->i_ssnd_size; |
289 | |
|
290 | 0 | p_sys->i_ssnd_fsize = p_sys->fmt.audio.i_channels * |
291 | 0 | ((p_sys->fmt.audio.i_bitspersample + 7) / 8); |
292 | |
|
293 | 0 | if( p_sys->i_ssnd_fsize <= 0 || p_sys->fmt.audio.i_rate == 0 || p_sys->i_ssnd_pos < 12 ) |
294 | 0 | { |
295 | 0 | msg_Err( p_demux, "invalid audio parameters" ); |
296 | 0 | return VLC_EGENERIC; |
297 | 0 | } |
298 | | |
299 | 0 | if( p_sys->i_ssnd_size <= 0 ) |
300 | 0 | { |
301 | | /* unknown */ |
302 | 0 | p_sys->i_ssnd_end = 0; |
303 | 0 | } |
304 | | |
305 | | /* seek into SSND chunk */ |
306 | 0 | if( vlc_stream_Seek( p_demux->s, p_sys->i_ssnd_start ) ) |
307 | 0 | { |
308 | 0 | msg_Err( p_demux, "cannot seek to data chunk" ); |
309 | 0 | return VLC_EGENERIC; |
310 | 0 | } |
311 | | |
312 | | /* */ |
313 | 0 | p_sys->fmt.i_id = 0; |
314 | 0 | p_sys->es = es_out_Add( p_demux->out, &p_sys->fmt ); |
315 | 0 | if( unlikely(p_sys->es == NULL) ) |
316 | 0 | return VLC_ENOMEM; |
317 | | |
318 | 0 | p_demux->pf_demux = Demux; |
319 | 0 | p_demux->pf_control = Control; |
320 | |
|
321 | 0 | return VLC_SUCCESS; |
322 | 0 | } |
323 | | |
324 | | /***************************************************************************** |
325 | | * Demux: |
326 | | *****************************************************************************/ |
327 | | static int Demux( demux_t *p_demux ) |
328 | 0 | { |
329 | 0 | demux_sys_t *p_sys = p_demux->p_sys; |
330 | 0 | int64_t i_tell = vlc_stream_Tell( p_demux->s ); |
331 | |
|
332 | 0 | block_t *p_block; |
333 | 0 | int i_read; |
334 | |
|
335 | 0 | if( p_sys->i_ssnd_end > 0 && i_tell >= p_sys->i_ssnd_end ) |
336 | 0 | { |
337 | | /* EOF */ |
338 | 0 | return VLC_DEMUXER_EOF; |
339 | 0 | } |
340 | | |
341 | | /* Set PCR */ |
342 | 0 | es_out_SetPCR( p_demux->out, VLC_TICK_0 + p_sys->i_time); |
343 | | |
344 | | /* we will read 100ms at once */ |
345 | 0 | i_read = p_sys->i_ssnd_fsize * ( p_sys->fmt.audio.i_rate / 10 ); |
346 | 0 | if( p_sys->i_ssnd_end > 0 && p_sys->i_ssnd_end - i_tell < i_read ) |
347 | 0 | { |
348 | 0 | i_read = p_sys->i_ssnd_end - i_tell; |
349 | 0 | } |
350 | 0 | if( ( p_block = vlc_stream_Block( p_demux->s, i_read ) ) == NULL ) |
351 | 0 | { |
352 | 0 | return VLC_DEMUXER_EOF; |
353 | 0 | } |
354 | | |
355 | 0 | p_block->i_dts = |
356 | 0 | p_block->i_pts = VLC_TICK_0 + p_sys->i_time; |
357 | |
|
358 | 0 | p_sys->i_time += vlc_tick_from_samples(p_block->i_buffer, |
359 | 0 | p_sys->i_ssnd_fsize) / |
360 | 0 | p_sys->fmt.audio.i_rate; |
361 | |
|
362 | 0 | if( p_sys->b_reorder ) |
363 | 0 | aout_ChannelReorder( p_block->p_buffer, p_block->i_buffer, |
364 | 0 | p_sys->fmt.audio.i_channels, p_sys->pi_chan_table, |
365 | 0 | p_sys->audio_fourcc ); |
366 | | /* */ |
367 | 0 | es_out_Send( p_demux->out, p_sys->es, p_block ); |
368 | 0 | return VLC_DEMUXER_SUCCESS; |
369 | 0 | } |
370 | | |
371 | | /***************************************************************************** |
372 | | * Close: frees unused data |
373 | | *****************************************************************************/ |
374 | | static void Close( vlc_object_t * p_this ) |
375 | 0 | { |
376 | 0 | demux_t *p_demux = (demux_t*)p_this; |
377 | 0 | demux_sys_t *p_sys = p_demux->p_sys; |
378 | |
|
379 | 0 | if( p_sys->p_meta ) |
380 | 0 | vlc_meta_Delete( p_sys->p_meta ); |
381 | 0 | } |
382 | | |
383 | | /***************************************************************************** |
384 | | * Control: |
385 | | *****************************************************************************/ |
386 | | static int Control( demux_t *p_demux, int i_query, va_list args ) |
387 | 0 | { |
388 | 0 | demux_sys_t *p_sys = p_demux->p_sys; |
389 | 0 | double f, *pf; |
390 | |
|
391 | 0 | switch( i_query ) |
392 | 0 | { |
393 | 0 | case DEMUX_CAN_SEEK: |
394 | 0 | return vlc_stream_vaControl( p_demux->s, i_query, args ); |
395 | | |
396 | 0 | case DEMUX_GET_POSITION: |
397 | 0 | { |
398 | 0 | int64_t i_start = p_sys->i_ssnd_start; |
399 | 0 | int64_t i_end = p_sys->i_ssnd_end > 0 ? p_sys->i_ssnd_end : stream_Size( p_demux->s ); |
400 | 0 | int64_t i_tell = vlc_stream_Tell( p_demux->s ); |
401 | |
|
402 | 0 | pf = va_arg( args, double * ); |
403 | |
|
404 | 0 | if( i_start < i_end ) |
405 | 0 | { |
406 | 0 | *pf = (double)(i_tell - i_start)/(double)(i_end - i_start); |
407 | 0 | return VLC_SUCCESS; |
408 | 0 | } |
409 | 0 | return VLC_EGENERIC; |
410 | 0 | } |
411 | | |
412 | 0 | case DEMUX_SET_POSITION: |
413 | 0 | { |
414 | 0 | int64_t i_start = p_sys->i_ssnd_start; |
415 | 0 | int64_t i_end = p_sys->i_ssnd_end > 0 ? p_sys->i_ssnd_end : stream_Size( p_demux->s ); |
416 | |
|
417 | 0 | f = va_arg( args, double ); |
418 | |
|
419 | 0 | if( i_start < i_end ) |
420 | 0 | { |
421 | 0 | int i_frame = (f * ( i_end - i_start )) / p_sys->i_ssnd_fsize; |
422 | 0 | int64_t i_new = i_start + i_frame * p_sys->i_ssnd_fsize; |
423 | |
|
424 | 0 | if( vlc_stream_Seek( p_demux->s, i_new ) ) |
425 | 0 | { |
426 | 0 | return VLC_EGENERIC; |
427 | 0 | } |
428 | 0 | p_sys->i_time = vlc_tick_from_samples( i_frame, p_sys->fmt.audio.i_rate ); |
429 | 0 | return VLC_SUCCESS; |
430 | 0 | } |
431 | 0 | return VLC_EGENERIC; |
432 | 0 | } |
433 | | |
434 | 0 | case DEMUX_GET_TIME: |
435 | 0 | *va_arg( args, vlc_tick_t * ) = p_sys->i_time; |
436 | 0 | return VLC_SUCCESS; |
437 | | |
438 | 0 | case DEMUX_GET_LENGTH: |
439 | 0 | { |
440 | 0 | int64_t i_end = p_sys->i_ssnd_end > 0 ? p_sys->i_ssnd_end : stream_Size( p_demux->s ); |
441 | |
|
442 | 0 | if( p_sys->i_ssnd_start < i_end ) |
443 | 0 | { |
444 | 0 | *va_arg( args, vlc_tick_t * ) = |
445 | 0 | vlc_tick_from_samples( i_end - p_sys->i_ssnd_start, p_sys->i_ssnd_fsize) / p_sys->fmt.audio.i_rate; |
446 | 0 | return VLC_SUCCESS; |
447 | 0 | } |
448 | 0 | return VLC_EGENERIC; |
449 | 0 | } |
450 | 0 | case DEMUX_GET_META: |
451 | 0 | { |
452 | 0 | vlc_meta_t *p_meta = va_arg( args, vlc_meta_t * ); |
453 | 0 | if( !p_sys->p_meta ) |
454 | 0 | return VLC_EGENERIC; |
455 | 0 | vlc_meta_Merge( p_meta, p_sys->p_meta ); |
456 | 0 | return VLC_SUCCESS; |
457 | 0 | } |
458 | | |
459 | 0 | case DEMUX_SET_TIME: |
460 | 0 | case DEMUX_GET_FPS: |
461 | 0 | return VLC_EGENERIC; |
462 | | |
463 | 0 | case DEMUX_CAN_PAUSE: |
464 | 0 | case DEMUX_SET_PAUSE_STATE: |
465 | 0 | case DEMUX_CAN_CONTROL_PACE: |
466 | 0 | case DEMUX_GET_PTS_DELAY: |
467 | 0 | return demux_vaControlHelper( p_demux->s, 0, -1, 0, 1, i_query, args ); |
468 | | |
469 | 0 | default: |
470 | 0 | return VLC_EGENERIC; |
471 | 0 | } |
472 | 0 | } |