contrib/gpg-error: simplify darwin triplet handling
[vlc.git] / modules / demux / caf.c
blob268c5ae27029012de78e87b02cc078feab6a9ee0
1 /*****************************************************************************
2 * caf.c: Core Audio File Format demuxer
3 *****************************************************************************
4 * Copyright (C) 2013 VLC authors and VideoLAN
6 * Authors: Matthias Keiser <matthias@tristan-inc.com>
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.
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.
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 *****************************************************************************/
23 /*****************************************************************************
24 * Preamble
25 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30 #include <math.h>
31 #include <limits.h>
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_demux.h>
35 #include <vlc_codecs.h>
37 /* TODO:
39 * - handle channel layout
40 * - 64 bit float LPCM is broken (sound has artifacts with little endian, only silence plays for big endian).
43 /*****************************************************************************
44 * Module descriptor
45 *****************************************************************************/
46 static int Open ( vlc_object_t * );
47 static void Close ( vlc_object_t * );
49 vlc_module_begin ()
50 set_category( CAT_INPUT )
51 set_subcategory( SUBCAT_INPUT_DEMUX )
52 set_description( N_( "CAF demuxer" ))
53 set_capability( "demux", 140 )
54 set_callbacks( Open, Close )
55 add_shortcut( "caf" )
56 vlc_module_end ()
58 /*****************************************************************************
59 * Local prototypes
60 *****************************************************************************/
61 static int Demux ( demux_t * );
62 static int Control( demux_t *, int i_query, va_list args );
64 typedef struct frame_span_t
66 uint64_t i_frames;
67 uint64_t i_samples;
68 uint64_t i_bytes;
69 uint64_t i_desc_bytes;
70 } frame_span_t;
72 typedef struct packet_table_t
74 uint64_t i_num_packets;
75 uint64_t i_num_valid_frames;
76 uint32_t i_num_priming_frames;
77 uint32_t i_num_remainder_frames;
78 uint64_t i_descriptions_start;
79 } packet_table_t;
81 typedef struct
83 es_format_t fmt;
84 es_out_id_t *es;
85 unsigned i_max_frames;
87 uint64_t i_data_offset;
88 uint64_t i_data_size;
90 frame_span_t position;
91 packet_table_t packet_table;
92 } demux_sys_t;
95 We use this value to indicate that the data section extends until the end of the file.
97 static const uint64_t kCHUNK_SIZE_EOF = UINT64_C( 0xffffffffffffffff );
99 /*****************************************************************************
100 * Various Utility Functions
101 *****************************************************************************/
103 /* ParseVarLenInteger parses a var length integer as found in the packet descriptions
104 (and in the aac magic cookie). In theorie a var length integer could be bigger than
105 an uint64_t, but I think it's unlikely to ever be a problem... */
107 static int ParseVarLenInteger( const uint8_t *p_buff, size_t i_buff_len, uint64_t *pi_value_out, uint32_t *i_len_out )
109 *i_len_out = 0;
111 uint64_t i_value = 0;
112 bool finished = false;
114 for( uint32_t i = 0; i < i_buff_len; i++ )
116 if( (( i_value >> 32 ) << 7 ) > UINT32_MAX )
118 return VLC_EGENERIC; /* overflow */
120 uint8_t i_byte = p_buff[i];
121 i_value = ( i_value << 7 ) | ( i_byte & 0x7f );
123 ( *i_len_out )++;
125 if( !( i_byte & 0x80 ))
127 finished = true;
128 break;
132 if( !finished )
134 return VLC_EGENERIC;
137 *pi_value_out = i_value;
139 return VLC_SUCCESS;
142 /* Utility function that reads a big endian double from the input buffer. */
144 static inline double GetDBLBE( const uint8_t *p )
146 union
148 uint64_t uint64;
149 double dbl;
150 } u_64;
152 u_64.uint64 = GetQWBE( p );
153 return u_64.dbl;
156 /* Utility function that reads a big endian signed 32 bit integer into an unsigned 32 bit variable.
157 If the read value is negative, this function returns an error.
160 static inline int ReadBEInt32ToUInt32( const uint8_t *p, uint32_t *i_out )
162 uint32_t i_value = GetDWBE( p );
164 if( i_value > INT32_MAX ) return VLC_EGENERIC;
166 *i_out = i_value;
167 return VLC_SUCCESS;
170 /* Utility function that reads a big endian signed 64 bit integer into an unsigned 64 bit variable.
171 If the read value is negative, this function returns an error.
174 static inline int ReadBEInt64ToUInt64( const uint8_t *p, uint64_t *i_out )
176 uint64_t i_value = GetQWBE( p );
178 if( i_value > INT64_MAX ) return VLC_EGENERIC;
180 *i_out = i_value;
181 return VLC_SUCCESS;
184 static inline bool NeedsPacketTable( demux_sys_t *p_sys )
186 return ( !p_sys->fmt.audio.i_bytes_per_frame || !p_sys->fmt.audio.i_frame_length );
189 static uint64_t TotalNumFrames( demux_t *p_demux )
191 demux_sys_t *p_sys = p_demux->p_sys;
193 if( !NeedsPacketTable( p_sys ))
195 uint64_t i_data_size;
197 if( p_sys->i_data_size != kCHUNK_SIZE_EOF)
199 i_data_size = p_sys->i_data_size;
201 else
203 int64_t i_stream_size = stream_Size( p_demux->s );
204 if(i_stream_size >= 0 && (uint64_t)i_stream_size >= p_sys->i_data_offset)
205 i_data_size = i_stream_size - p_sys->i_data_offset;
206 else
207 i_data_size = 0;
210 return i_data_size / p_sys->fmt.audio.i_bytes_per_frame;
212 else
214 return p_sys->packet_table.i_num_packets;
218 static uint64_t TotalNumSamples( demux_t *p_demux )
220 demux_sys_t *p_sys = p_demux->p_sys;
222 if( !NeedsPacketTable( p_sys ))
224 return TotalNumFrames( p_demux ) * p_sys->fmt.audio.i_frame_length;
226 else
228 return p_sys->packet_table.i_num_valid_frames + p_sys->packet_table.i_num_priming_frames +
229 p_sys->packet_table.i_num_remainder_frames;
233 static inline vlc_fourcc_t ReadFOURCC( const uint8_t *p )
235 return VLC_FOURCC( p[0], p[1], p[2], p[3] );
238 /*****************************************************************************
239 * FrameSpan Functions
240 *****************************************************************************
241 * A FrameSpan structure contains the relationship between a number of
242 frames, the number of samples they contain, the amount of data they
243 use, and the length of their packet descriptions (if any).
244 *****************************************************************************/
246 /* FrameSpanAddSpan adds span2 to span1 */
248 static inline void FrameSpanAddSpan( frame_span_t *span1, frame_span_t *span2 )
250 span1->i_frames += span2->i_frames;
251 span1->i_samples += span2->i_samples;
252 span1->i_bytes += span2->i_bytes;
253 span1->i_desc_bytes += span2->i_desc_bytes;
256 /* Adds the frame that is described at i_desc_offset to span */
258 static int FrameSpanAddDescription( demux_t *p_demux, uint64_t i_desc_offset, frame_span_t *span )
260 demux_sys_t *p_sys = p_demux->p_sys;
262 /* Avoid seeking + peeking for simple case (PCM) */
263 if( p_sys->fmt.audio.i_bytes_per_frame && p_sys->fmt.audio.i_frame_length )
265 span->i_bytes += p_sys->fmt.audio.i_bytes_per_frame;
266 span->i_samples += p_sys->fmt.audio.i_frame_length;
267 span->i_frames++;
268 return VLC_SUCCESS;
271 uint32_t i_desc_size = 0;
273 if( vlc_stream_Seek( p_demux->s, p_sys->packet_table.i_descriptions_start + i_desc_offset ))
275 msg_Err( p_demux, "Couldn't seek packet description." );
276 return VLC_EGENERIC;
279 const uint8_t *p_peek;
280 int i_peek_len = vlc_stream_Peek( p_demux->s, &p_peek, 2 * 10 );
281 /* Peeking the maximum number of bytes that two 64 bit numbers could use
282 * (( 64 + 6 ) / 7 = 10). */
283 if( i_peek_len < 0 )
284 i_peek_len = 0;
286 if( p_sys->fmt.audio.i_bytes_per_frame )
288 span->i_bytes += p_sys->fmt.audio.i_bytes_per_frame;
290 else
292 uint64_t i_size;
293 uint32_t i_this_int;
294 if( ParseVarLenInteger( p_peek, i_peek_len, &i_size, &i_this_int ))
296 return VLC_EGENERIC;
299 i_desc_size += i_this_int;
300 span->i_bytes += i_size;
303 if( p_sys->fmt.audio.i_frame_length )
305 span->i_samples += p_sys->fmt.audio.i_frame_length;
307 else
309 if( i_desc_size >= (unsigned)i_peek_len )
311 return VLC_EGENERIC;
314 uint64_t i_num_samples;
315 uint32_t i_this_int;
316 if( ParseVarLenInteger( p_peek + i_desc_size, i_peek_len - i_desc_size, &i_num_samples, &i_this_int ))
318 return VLC_EGENERIC;
321 i_desc_size += i_this_int;
322 span->i_samples += i_num_samples;
324 span->i_desc_bytes += i_desc_size;
325 span->i_frames++;
327 return VLC_SUCCESS;
330 /* FrameSpanGetTime returns the time span represented by the frame span. */
332 static inline vlc_tick_t FrameSpanGetTime( frame_span_t *span, uint32_t i_sample_rate )
334 if( !i_sample_rate )
335 return VLC_TICK_INVALID;
337 return vlc_tick_from_samples( span->i_samples, i_sample_rate) + VLC_TICK_0;
340 /* SetSpanWithSample returns the span from the beginning of the file up to and
341 including the specified sample. This works at frame granularity, so the
342 returned span may not represent the exact target sample.
343 Since we might have to lineraly search the packet descriptions, this is a
344 potentially slow operation! */
346 static int SetSpanWithSample( demux_t *p_demux, frame_span_t *p_span, uint64_t i_target_sample )
348 demux_sys_t *p_sys = p_demux->p_sys;
350 uint64_t i_num_frames = TotalNumFrames( p_demux );
352 if( !NeedsPacketTable( p_sys ))
354 uint64_t i_frame = i_target_sample / p_sys->fmt.audio.i_frame_length;
355 uint64_t i_remaining = i_target_sample - i_frame * p_sys->fmt.audio.i_frame_length;
356 if( i_remaining > ( p_sys->fmt.audio.i_frame_length / 2 ))
357 i_frame++;
359 if( i_frame > i_num_frames )
360 i_frame = i_num_frames;
362 p_span->i_frames = i_frame;
363 p_span->i_samples = i_frame * p_sys->fmt.audio.i_frame_length;
364 p_span->i_bytes = i_frame * p_sys->fmt.audio.i_bytes_per_frame;
365 p_span->i_desc_bytes = 0;
367 else
369 *p_span = (frame_span_t){0};
370 frame_span_t prev_span;
372 while( p_span->i_samples < i_target_sample && p_span->i_frames < i_num_frames )
374 prev_span = *p_span;
376 if( FrameSpanAddDescription( p_demux, p_span->i_desc_bytes, p_span ))
377 return VLC_EGENERIC;
379 if( p_span->i_samples >= i_target_sample )
381 uint64_t i_this_samples = p_span->i_samples - prev_span.i_samples;
383 if( i_target_sample - prev_span.i_samples < i_this_samples / 2 )
384 *p_span = prev_span;
386 break;
391 return VLC_SUCCESS;
394 /*****************************************************************************
395 * CAF Parsing Functions
396 *****************************************************************************/
398 /* The NextChunk function returns the four char code and the (sanitized) size at the current file position.
399 Upon return the file position points to the start of the chunk payload.
400 Note that the ReadXXXChunk functions expect the chunk to size to be sanitized
401 (i.e. they don't check if it is bigger than INT64_MAX, but note the special case of the 'data' chunk, which can be kCHUNK_SIZE_EOF).
404 static int NextChunk( demux_t *p_demux, vlc_fourcc_t *p_fcc, uint64_t *pi_size )
406 uint8_t p_read[12];
408 if( vlc_stream_Read( p_demux->s, p_read, 12 ) < 12 )
409 return VLC_EGENERIC;
411 *p_fcc = ReadFOURCC( p_read );
412 uint64_t i_size = GetQWBE( p_read + 4 );
414 /* We accept no negativ sizes for chunks, except -1 for the data chunk. */
416 if( i_size > INT64_MAX )
418 if( *p_fcc == VLC_FOURCC( 'd', 'a', 't', 'a' ) && i_size == UINT64_C( -1 ))
419 i_size = kCHUNK_SIZE_EOF;
420 else
421 return VLC_EGENERIC;
424 *pi_size = i_size;
426 return VLC_SUCCESS;
429 static int ReadDescChunk( demux_t *p_demux )
431 demux_sys_t *p_sys = p_demux->p_sys;
433 const uint8_t *p_peek;
435 if ( vlc_stream_Peek( p_demux->s, &p_peek, 8 + 6 * 4 ) < ( 8 + 6 * 4 ))
437 return VLC_EGENERIC;
440 vlc_fourcc_t i_fmt = ReadFOURCC( p_peek + 8 );
441 uint32_t i_fmt_flags = GetDWBE( p_peek + 12 );
443 uint32_t i_bits_per_channel = GetDWBE( p_peek + 28 );
444 uint32_t i_bytes_per_packet = GetDWBE( p_peek + 16 );
445 uint32_t i_frames_per_packet = GetDWBE( p_peek + 20 );
446 uint32_t i_channels_per_frame = GetDWBE( p_peek + 24 );
448 if( i_fmt == VLC_CODEC_DVD_LPCM )
450 if( !i_frames_per_packet || !i_channels_per_frame )
452 msg_Err( p_demux, "Absurd LPCM parameters (frames_per_packet: %u, channels_per_frame: %u).", i_frames_per_packet, i_channels_per_frame );
453 return VLC_EGENERIC;
456 uint32_t i_unpacked_bits_per_sample = ( i_bytes_per_packet / i_frames_per_packet / i_channels_per_frame ) * 8;
458 bool b_is_float = !!( i_fmt_flags & ( 1 << 0 ) );
459 bool b_is_be = !( i_fmt_flags & ( 1 << 1 ) );
461 vlc_fourcc_t i_basic_codec = 0;
463 if( !b_is_float )
465 i_basic_codec = b_is_be ? VLC_FOURCC( 't', 'w', 'o', 's' ) : VLC_FOURCC( 's', 'o', 'w', 't' );
466 es_format_Init( &p_sys->fmt, AUDIO_ES, vlc_fourcc_GetCodecAudio( i_basic_codec, i_unpacked_bits_per_sample ));
468 else
470 if( i_bits_per_channel == 32 )
471 i_basic_codec = b_is_be ? VLC_CODEC_F32B : VLC_CODEC_F32L;
472 else if( i_bits_per_channel == 64 )
473 i_basic_codec = b_is_be ? VLC_CODEC_F64B : VLC_CODEC_F64L;
475 if( i_basic_codec )
476 es_format_Init( &p_sys->fmt, AUDIO_ES, vlc_fourcc_GetCodecAudio( i_basic_codec, i_bits_per_channel ));
479 else if( i_fmt == VLC_FOURCC( 'a', 'a', 'c', ' ' ))
481 const uint32_t kMP4Audio_AAC_LC_ObjectType = 2;
483 if( i_fmt_flags != kMP4Audio_AAC_LC_ObjectType )
485 msg_Warn( p_demux, "The only documented format flag for aac is 2 (kMP4Audio_AAC_LC_ObjectType), but is %i. Continuing anyways.", i_fmt_flags );
488 es_format_Init( &p_sys->fmt, AUDIO_ES, vlc_fourcc_GetCodecAudio( VLC_CODEC_MP4A, 0 ));
491 else
493 es_format_Init( &p_sys->fmt, AUDIO_ES, vlc_fourcc_GetCodecAudio( i_fmt, 0 ));
496 if( !p_sys->fmt.i_codec )
498 msg_Err( p_demux, "could not determine codec" );
499 return VLC_EGENERIC;
502 double d_rate = round( GetDBLBE( p_peek ));
504 if( d_rate <= 0 || d_rate > UINT_MAX )
505 return VLC_EGENERIC;
507 p_sys->fmt.audio.i_rate = (unsigned int)lround( d_rate );
508 if( !p_sys->fmt.audio.i_rate )
510 msg_Err( p_demux, "Sample rate must be non-zero" );
511 return VLC_EGENERIC;
513 p_sys->fmt.audio.i_channels = i_channels_per_frame;
514 p_sys->fmt.audio.i_bytes_per_frame = i_bytes_per_packet; /* "mBytesPerPacket" in Apple parlance */
515 p_sys->fmt.audio.i_frame_length = i_frames_per_packet; /* "mFramesPerPacket" in Apple parlance */
516 p_sys->fmt.audio.i_bitspersample = i_bits_per_channel; /* mBitsPerChannel */
517 p_sys->fmt.audio.i_blockalign = i_bytes_per_packet;
518 p_sys->fmt.i_bitrate = i_bits_per_channel * p_sys->fmt.audio.i_rate * i_channels_per_frame;
520 if( p_sys->fmt.i_codec == VLC_CODEC_OPUS )
522 p_sys->i_max_frames = 1;
524 else p_sys->i_max_frames = UINT_MAX;
526 return VLC_SUCCESS;
529 /* This is lifted from cafdec.c in libavformat (function read_kuki_chunk). Appearantly the
530 alac library expects the cookie to be of length 36, but current alac files
531 have a cookie length of 24.
533 static int ProcessALACCookie( demux_t *p_demux, const uint8_t *p, uint64_t i_size )
535 demux_sys_t *p_sys = p_demux->p_sys;
537 const unsigned int kALAC_NEW_KUKI_SIZE = 24;
538 const unsigned int kALAC_LIB_REQ_KUKI_SIZE = 36;
539 int i_extra;
541 if( i_size == kALAC_NEW_KUKI_SIZE || i_size == kALAC_LIB_REQ_KUKI_SIZE )
543 i_extra = kALAC_LIB_REQ_KUKI_SIZE;
545 else
547 msg_Warn( p_demux, "Unknown alac magic cookie. Passing it on to the decoder as is and hoping for the best." );
548 i_extra = ( int )i_size;
551 p_sys->fmt.i_extra = i_extra;
552 p_sys->fmt.p_extra = malloc( i_extra );
554 if( !p_sys->fmt.p_extra )
555 return VLC_ENOMEM;
557 uint8_t *p_extra = ( uint8_t * )p_sys->fmt.p_extra;
559 if( i_size == kALAC_NEW_KUKI_SIZE )
561 SetDWBE( p_extra, 36 );
562 memcpy( p_extra + 4, "alac", 4 );
563 SetDWBE( p_extra + 8, 0 );
564 memcpy( p_extra + 12, p, 24 );
566 else
568 memcpy( p_sys->fmt.p_extra, p, i_size );
571 return VLC_SUCCESS;
574 /* Helper functions for AAC cookie processing. */
576 static inline bool AACCookieGetTag( uint8_t *p_tag, const uint8_t *p, uint64_t *p_offset, uint64_t i_size )
578 if( *p_offset + 1 > i_size )
579 return false;
581 *p_tag = *( p + *p_offset );
582 *p_offset += 1;
584 return true;
587 static inline bool AACCookieTagLen( uint64_t *p_tag_len, const uint8_t *p, uint64_t *p_offset, uint64_t i_size )
589 uint32_t i_int_size;
591 if( ParseVarLenInteger( p + *p_offset, i_size - *p_offset, p_tag_len, &i_int_size ))
592 return false;
594 *p_offset += i_int_size;
596 return true;
599 static inline bool AACCookieChkLen( int64_t i_length, uint64_t i_size, uint64_t i_offset )
601 return ( i_offset + i_length <= i_size );
604 /* This is lifted from libmp4.c in the mp4 demuxer module (function MP4_ReadBox_esds). The aac
605 library only want a subset of the magic cookie ("decoder specific info"), so we have to parse it.
607 static int ProcessAACCookie( demux_t *p_demux, const uint8_t *p, uint64_t i_size )
609 const uint8_t kAAC_ES_DESCR_TAG = 3;
610 const uint8_t kAAC_DEC_CONFIG_DESCR_TAG = 4;
611 const uint8_t kAAC_DEC_SPEC_INFO_TAG = 5;
613 demux_sys_t *p_sys = p_demux->p_sys;
615 uint64_t i_offset = 0;
616 uint64_t i_kuki_size = 0;
617 uint64_t i_tag_len;
618 uint8_t i_tag;
620 if( !AACCookieGetTag( &i_tag, p, &i_offset, i_size )) goto aac_kuki_finish;
622 if( i_tag == kAAC_ES_DESCR_TAG )
625 if( !AACCookieTagLen( &i_tag_len, p, &i_offset, i_size )) goto aac_kuki_finish;
627 if( !AACCookieChkLen( 3, i_size, i_offset )) goto aac_kuki_finish;
628 i_offset += 2; /* don't care (ES ID) */
629 uint8_t i_flags = *( p + i_offset++ );
631 if( i_flags&0x80 )
633 if( !AACCookieChkLen( 2, i_size, i_offset )) goto aac_kuki_finish;
634 i_offset += 2; /* don't care (dependance) */
636 if( i_flags&0x40 )
638 if( !AACCookieChkLen( 1, i_size, i_offset )) goto aac_kuki_finish;
639 uint8_t i_url_len = *( p + i_offset++ );
640 i_offset += i_url_len; /* don't care (url) */
642 if( i_flags&0x20 )
644 if( !AACCookieChkLen( 2, i_size, i_offset )) goto aac_kuki_finish;
645 i_offset += 2; /* don't care (OCR) */
648 if( !AACCookieGetTag( &i_tag, p, &i_offset, i_size )) goto aac_kuki_finish;
651 if( i_tag != kAAC_DEC_CONFIG_DESCR_TAG )
652 goto aac_kuki_finish;
654 if( !AACCookieTagLen( &i_tag_len, p, &i_offset, i_size )) goto aac_kuki_finish;
656 if( !AACCookieChkLen( 1 + 1 + 3 + 4 + 4, i_size, i_offset )) goto aac_kuki_finish;
657 i_offset += ( 1 + 1 + 3 + 4 + 4 ); /* don't care */
659 if( !AACCookieGetTag( &i_tag, p, &i_offset, i_size )) goto aac_kuki_finish;
661 if( i_tag != kAAC_DEC_SPEC_INFO_TAG ) /* this is the one we need */
662 goto aac_kuki_finish;
664 if( !AACCookieTagLen( &i_tag_len, p, &i_offset, i_size )) goto aac_kuki_finish;
666 if( i_offset + i_tag_len > i_size )
667 goto aac_kuki_finish;
669 i_kuki_size = i_tag_len;
671 aac_kuki_finish:
673 if( !i_kuki_size )
675 msg_Warn( p_demux, "Error parsing aac cookie. Passing it on to the decoder as is and hoping for the best." );
676 i_kuki_size = i_size;
677 i_offset = 0;
680 p_sys->fmt.i_extra = (int)i_kuki_size;
681 p_sys->fmt.p_extra = malloc( i_kuki_size );
683 if( !p_sys->fmt.p_extra )
685 return VLC_ENOMEM;
688 memcpy( p_sys->fmt.p_extra, p + i_offset, i_kuki_size );
690 return VLC_SUCCESS;
693 static int ReadKukiChunk( demux_t *p_demux, uint64_t i_size )
695 demux_sys_t *p_sys = p_demux->p_sys;
696 const uint8_t *p_peek;
698 if( i_size > SSIZE_MAX )
700 msg_Err( p_demux, "Magic Cookie chunk too big" );
701 return VLC_EGENERIC;
704 if( vlc_stream_Peek( p_demux->s, &p_peek, i_size ) < (ssize_t)i_size )
706 msg_Err( p_demux, "Couldn't peek extra data" );
707 return VLC_EGENERIC;
710 if( p_sys->fmt.i_codec == VLC_CODEC_ALAC )
712 int error = ProcessALACCookie( p_demux, p_peek, i_size );
713 if( error ) return error;
715 else if( p_sys->fmt.i_codec == VLC_CODEC_MP4A )
717 int error = ProcessAACCookie( p_demux, p_peek, i_size );
718 if( error ) return error;
720 else
722 p_sys->fmt.i_extra = (int)i_size;
723 p_sys->fmt.p_extra = malloc( i_size );
725 if( !p_sys->fmt.p_extra )
727 return VLC_ENOMEM;
729 memcpy( p_sys->fmt.p_extra, p_peek, p_sys->fmt.i_extra );
732 return VLC_SUCCESS;
735 static int ReadDataChunk( demux_t *p_demux, uint64_t i_size )
737 if( i_size < 4 )
738 return VLC_EGENERIC;
740 demux_sys_t *p_sys = p_demux->p_sys;
742 p_sys->i_data_offset = vlc_stream_Tell( p_demux->s ) + 4; /* skip edit count */
743 p_sys->i_data_size = i_size == kCHUNK_SIZE_EOF ? kCHUNK_SIZE_EOF : ( i_size - 4 );
745 return VLC_SUCCESS;
748 static int ReadPaktChunk( demux_t *p_demux )
750 demux_sys_t *p_sys = p_demux->p_sys;
752 const uint8_t *p_peek;
754 if ( vlc_stream_Peek( p_demux->s, &p_peek, 8 + 8 + 4 + 4 ) < ( 8 + 8 + 4 + 4 ))
756 msg_Err( p_demux, "Couldn't peek packet descriptions" );
757 return VLC_EGENERIC;
760 if( ReadBEInt64ToUInt64( p_peek, &p_sys->packet_table.i_num_packets ))
762 msg_Err( p_demux, "Invalid packet table: i_num_packets is negative.");
763 return VLC_EGENERIC;
765 if( ReadBEInt64ToUInt64( p_peek + 8, &p_sys->packet_table.i_num_valid_frames ))
767 msg_Err( p_demux, "Invalid packet table: i_num_valid_frames is negative.");
768 return VLC_EGENERIC;
770 if( ReadBEInt32ToUInt32( p_peek + 16, &p_sys->packet_table.i_num_priming_frames ))
772 msg_Err( p_demux, "Invalid packet table: i_num_priming_frames is negative.");
773 return VLC_EGENERIC;
775 if( ReadBEInt32ToUInt32( p_peek + 20, &p_sys->packet_table.i_num_remainder_frames ))
777 msg_Err( p_demux, "Invalid packet table: i_num_remainder_frames is negative.");
778 return VLC_EGENERIC;
781 p_sys->packet_table.i_descriptions_start = vlc_stream_Tell( p_demux->s ) + 24;
783 return VLC_SUCCESS;
786 /*****************************************************************************
787 * Open
788 *****************************************************************************/
789 static int Open( vlc_object_t *p_this )
791 int i_error = VLC_SUCCESS;
793 demux_t *p_demux = (demux_t*)p_this;
794 demux_sys_t *p_sys;
796 const uint8_t *p_peek;
798 if( vlc_stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
799 return VLC_EGENERIC;
801 /* Is it a caf file? */
802 if( memcmp( p_peek, "caff", 4 ))
803 return VLC_EGENERIC;
805 /* check file version (we only handle version 1) */
806 uint16_t i_version = GetWBE( p_peek + 4 );
807 if( i_version != 1 )
809 msg_Dbg( p_demux, "Unknown caf file version %d.", i_version );
810 return VLC_EGENERIC;
813 /* check file flags (must be 0) */
814 uint16_t i_flags = GetWBE( p_peek + 6 );
815 if( i_flags != 0 )
817 msg_Dbg( p_demux, "Unknown caf file flags %d.", i_flags );
818 return VLC_EGENERIC;
821 if( vlc_stream_Read( p_demux->s, NULL, 8 ) < 8 )
822 return VLC_EGENERIC; /* This would be very strange since we justed peeked at these bytes. */
824 p_demux->p_sys = calloc( 1, sizeof( demux_sys_t ));
825 if( !p_demux->p_sys ) return VLC_ENOMEM;
827 /* From this point on, we have to free p_sys if we return an error (e.g. "goto caf_open_end") */
829 p_sys = p_demux->p_sys;
830 es_format_Init( &p_sys->fmt, AUDIO_ES, 0 );
832 vlc_fourcc_t i_fcc;
833 uint64_t i_size;
834 uint64_t i_idx = 0;
836 while( NextChunk( p_demux, &i_fcc, &i_size ) == VLC_SUCCESS )
838 bool b_handled = true;
840 switch ( i_fcc )
842 case VLC_FOURCC( 'd', 'e', 's', 'c' ):
844 if( i_idx != 0 )
846 msg_Err( p_demux, "The audio description chunk must be the first chunk in a caf file." );
847 i_error = VLC_EGENERIC;
848 goto caf_open_end;
851 i_error = ReadDescChunk( p_demux );
852 break;
854 case VLC_FOURCC( 'd', 'a', 't', 'a' ):
856 i_error = ReadDataChunk( p_demux, i_size );
857 break;
859 case VLC_FOURCC( 'p', 'a', 'k', 't' ):
861 i_error = ReadPaktChunk( p_demux );
862 break;
864 case VLC_FOURCC( 'k', 'u', 'k', 'i' ):
866 i_error = ReadKukiChunk( p_demux, i_size );
867 break;
869 default:
871 b_handled = false;
872 break;
875 if( i_error )
876 goto caf_open_end;
878 if( b_handled )
879 msg_Dbg( p_demux, "Found '%4.4s' chunk.", ( char * )&i_fcc );
880 else
881 msg_Dbg( p_demux, "Ignoring '%4.4s' chunk.", ( char * )&i_fcc );
883 if( i_size == kCHUNK_SIZE_EOF )
884 break;
886 if( vlc_stream_Seek( p_demux->s, vlc_stream_Tell( p_demux->s ) + i_size ) != VLC_SUCCESS )
887 break;
889 i_idx++;
892 if ( !p_sys->i_data_offset || p_sys->fmt.i_cat != AUDIO_ES || !p_sys->fmt.audio.i_rate ||
893 ( NeedsPacketTable( p_sys ) && !p_sys->packet_table.i_descriptions_start ))
895 msg_Err( p_demux, "Did not find all necessary chunks." );
896 i_error = VLC_EGENERIC;
897 goto caf_open_end;
900 p_sys->fmt.i_id = 0;
901 p_sys->es = es_out_Add( p_demux->out, &p_sys->fmt );
903 if( !p_sys->es )
905 msg_Err( p_demux, "Could not add elementary stream." );
906 i_error = VLC_EGENERIC;
907 goto caf_open_end;
910 p_demux->pf_control = Control;
911 p_demux->pf_demux = Demux;
912 return VLC_SUCCESS;
914 caf_open_end:
915 es_format_Clean( &p_sys->fmt );
916 free( p_sys );
917 return i_error;
920 /*****************************************************************************
921 * Demux:
922 *****************************************************************************/
923 static int Demux( demux_t *p_demux )
925 demux_sys_t *p_sys = p_demux->p_sys;
926 block_t *p_block;
928 if( p_sys->i_data_size != kCHUNK_SIZE_EOF && p_sys->position.i_bytes >= p_sys->i_data_size )
930 /* EOF */
931 return VLC_DEMUXER_EOF;
934 frame_span_t advance = (frame_span_t){0};
936 /* we will read 50ms at once */
937 uint64_t i_req_samples = __MAX( p_sys->fmt.audio.i_rate / 20, 1 );
939 if( !NeedsPacketTable( p_sys )) /* PCM/IMA4 */
941 int64_t i_req_frames = ( i_req_samples + ( p_sys->fmt.audio.i_frame_length - 1 )) / p_sys->fmt.audio.i_frame_length;
943 if( p_sys->i_data_size != kCHUNK_SIZE_EOF && ( p_sys->position.i_bytes + i_req_frames * p_sys->fmt.audio.i_bytes_per_frame ) > p_sys->i_data_size )
945 i_req_frames = ( p_sys->i_data_size - p_sys->position.i_frames * p_sys->fmt.audio.i_bytes_per_frame ) / p_sys->fmt.audio.i_bytes_per_frame;
948 advance.i_frames = i_req_frames;
949 advance.i_samples = i_req_frames * p_sys->fmt.audio.i_frame_length;
950 advance.i_bytes = p_sys->fmt.audio.i_bytes_per_frame * advance.i_frames;
952 else /* use packet table */
954 uint64_t i_max_frames;
955 if( p_sys->packet_table.i_num_packets > p_sys->position.i_frames )
956 i_max_frames = p_sys->packet_table.i_num_packets - p_sys->position.i_frames;
957 else
958 i_max_frames = 1; /* will be rejected on FrameSpanAddDescription below */
960 if( i_max_frames > p_sys->i_max_frames )
961 i_max_frames = p_sys->i_max_frames;
965 if( FrameSpanAddDescription( p_demux, p_sys->position.i_desc_bytes + advance.i_desc_bytes, &advance ))
966 break;
968 while ( i_req_samples > advance.i_samples && advance.i_frames < i_max_frames );
971 if( !advance.i_frames )
973 msg_Err( p_demux, "Unexpected end of file" );
974 return VLC_DEMUXER_EGENERIC;
977 if( vlc_stream_Seek( p_demux->s, p_sys->i_data_offset + p_sys->position.i_bytes ))
979 if( p_sys->i_data_size == kCHUNK_SIZE_EOF)
980 return VLC_DEMUXER_EOF;
982 msg_Err( p_demux, "cannot seek data" );
983 return VLC_DEMUXER_EGENERIC;
986 p_block = vlc_stream_Block( p_demux->s, (int)advance.i_bytes );
987 if( p_block == NULL )
989 msg_Err( p_demux, "cannot read data" );
990 return VLC_DEMUXER_EGENERIC;
993 p_block->i_dts =
994 p_block->i_pts = FrameSpanGetTime( &p_sys->position, p_sys->fmt.audio.i_rate );
996 FrameSpanAddSpan( &p_sys->position, &advance );
998 es_out_SetPCR( p_demux->out, p_block->i_pts );
999 es_out_Send( p_demux->out, p_sys->es, p_block );
1001 return VLC_DEMUXER_SUCCESS;
1004 /*****************************************************************************
1005 * Control:
1006 *****************************************************************************/
1007 static int Control( demux_t *p_demux, int i_query, va_list args )
1009 int64_t i_sample;
1010 double f, *pf;
1011 frame_span_t position;
1013 demux_sys_t *p_sys = p_demux->p_sys;
1014 uint64_t i_num_samples = TotalNumSamples( p_demux );
1016 switch( i_query )
1018 case DEMUX_CAN_SEEK:
1019 *va_arg( args, bool * ) = true;
1020 return VLC_SUCCESS;
1022 case DEMUX_GET_LENGTH:
1023 *va_arg( args, vlc_tick_t * ) =
1024 vlc_tick_from_samples( i_num_samples, p_sys->fmt.audio.i_rate );
1025 return VLC_SUCCESS;
1027 case DEMUX_GET_TIME:
1028 *va_arg( args, vlc_tick_t * ) =
1029 vlc_tick_from_samples( p_sys->position.i_samples, p_sys->fmt.audio.i_rate );
1030 return VLC_SUCCESS;
1032 case DEMUX_GET_POSITION:
1033 pf = va_arg( args, double * );
1034 *pf = i_num_samples ? (double)p_sys->position.i_samples / (double)i_num_samples : 0.0;
1035 return VLC_SUCCESS;
1037 case DEMUX_SET_POSITION:
1038 f = va_arg( args, double );
1039 i_sample = f * i_num_samples;
1040 if( SetSpanWithSample( p_demux, &position, i_sample ))
1041 return VLC_EGENERIC;
1042 p_sys->position = position;
1043 return VLC_SUCCESS;
1045 case DEMUX_SET_TIME:
1046 i_sample =
1047 samples_from_vlc_tick( va_arg( args, vlc_tick_t ), p_sys->fmt.audio.i_rate );
1048 if( SetSpanWithSample( p_demux, &position, i_sample ))
1049 return VLC_EGENERIC;
1050 p_sys->position = position;
1051 return VLC_SUCCESS;
1053 case DEMUX_GET_META:
1054 return vlc_stream_Control( p_demux->s, STREAM_GET_META, args );
1056 case DEMUX_CAN_PAUSE:
1057 case DEMUX_SET_PAUSE_STATE:
1058 case DEMUX_CAN_CONTROL_PACE:
1059 case DEMUX_GET_PTS_DELAY:
1060 return demux_vaControlHelper( p_demux->s, p_sys->i_data_offset,
1061 p_sys->i_data_size, 0, 1, i_query, args );
1063 default:
1064 return VLC_EGENERIC;
1067 return VLC_EGENERIC;
1070 /*****************************************************************************
1071 * Close
1072 *****************************************************************************/
1073 static void Close( vlc_object_t *p_this )
1075 demux_t *p_demux = (demux_t*)p_this;
1076 demux_sys_t *p_sys = p_demux->p_sys;
1078 es_format_Clean( &p_sys->fmt );
1079 free( p_sys );