demux: libmp4: fix reading last iinf entry v0
[vlc.git] / modules / demux / caf.c
blobcc312a5a306a824e17713975000b9741736e0ea3
1 /*****************************************************************************
2 * caf.c: Core Audio File Format demuxer
3 *****************************************************************************
4 * Copyright (C) 2013 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Matthias Keiser <matthias@tristan-inc.com>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31 #include <math.h>
32 #include <limits.h>
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_demux.h>
36 #include <vlc_codecs.h>
38 /* TODO:
40 * - handle channel layout
41 * - 64 bit float LPCM is broken (sound has artifacts with little endian, only silence plays for big endian).
44 /*****************************************************************************
45 * Module descriptor
46 *****************************************************************************/
47 static int Open ( vlc_object_t * );
48 static void Close ( vlc_object_t * );
50 vlc_module_begin ()
51 set_category( CAT_INPUT )
52 set_subcategory( SUBCAT_INPUT_DEMUX )
53 set_description( N_( "CAF demuxer" ))
54 set_capability( "demux", 140 )
55 set_callbacks( Open, Close )
56 add_shortcut( "caf" )
57 vlc_module_end ()
59 /*****************************************************************************
60 * Local prototypes
61 *****************************************************************************/
62 static int Demux ( demux_t * );
63 static int Control( demux_t *, int i_query, va_list args );
65 typedef struct frame_span_t
67 uint64_t i_frames;
68 uint64_t i_samples;
69 uint64_t i_bytes;
70 uint64_t i_desc_bytes;
71 } frame_span_t;
73 typedef struct packet_table_t
75 uint64_t i_num_packets;
76 uint64_t i_num_valid_frames;
77 uint32_t i_num_priming_frames;
78 uint32_t i_num_remainder_frames;
79 uint64_t i_descriptions_start;
80 } packet_table_t;
82 typedef struct
84 es_format_t fmt;
85 es_out_id_t *es;
86 unsigned i_max_frames;
88 uint64_t i_data_offset;
89 uint64_t i_data_size;
91 frame_span_t position;
92 packet_table_t packet_table;
93 } demux_sys_t;
96 We use this value to indicate that the data section extends until the end of the file.
98 static const uint64_t kCHUNK_SIZE_EOF = UINT64_C( 0xffffffffffffffff );
100 /*****************************************************************************
101 * Various Utility Functions
102 *****************************************************************************/
104 /* ParseVarLenInteger parses a var length integer as found in the packet descriptions
105 (and in the aac magic cookie). In theorie a var length integer could be bigger than
106 an uint64_t, but I think it's unlikely to ever be a problem... */
108 static int ParseVarLenInteger( const uint8_t *p_buff, size_t i_buff_len, uint64_t *pi_value_out, uint32_t *i_len_out )
110 *i_len_out = 0;
112 uint64_t i_value = 0;
113 bool finished = false;
115 for( uint32_t i = 0; i < i_buff_len; i++ )
117 if( (( i_value >> 32 ) << 7 ) > UINT32_MAX )
119 return VLC_EGENERIC; /* overflow */
121 uint8_t i_byte = p_buff[i];
122 i_value = ( i_value << 7 ) | ( i_byte & 0x7f );
124 ( *i_len_out )++;
126 if( !( i_byte & 0x80 ))
128 finished = true;
129 break;
133 if( !finished )
135 return VLC_EGENERIC;
138 *pi_value_out = i_value;
140 return VLC_SUCCESS;
143 /* Utility function that reads a big endian double from the input buffer. */
145 static inline double GetDBLBE( const uint8_t *p )
147 union
149 uint64_t uint64;
150 double dbl;
151 } u_64;
153 u_64.uint64 = GetQWBE( p );
154 return u_64.dbl;
157 /* Utility function that reads a big endian signed 32 bit integer into an unsigned 32 bit variable.
158 If the read value is negative, this function returns an error.
161 static inline int ReadBEInt32ToUInt32( const uint8_t *p, uint32_t *i_out )
163 uint32_t i_value = GetDWBE( p );
165 if( i_value > INT32_MAX ) return VLC_EGENERIC;
167 *i_out = i_value;
168 return VLC_SUCCESS;
171 /* Utility function that reads a big endian signed 64 bit integer into an unsigned 64 bit variable.
172 If the read value is negative, this function returns an error.
175 static inline int ReadBEInt64ToUInt64( const uint8_t *p, uint64_t *i_out )
177 uint64_t i_value = GetQWBE( p );
179 if( i_value > INT64_MAX ) return VLC_EGENERIC;
181 *i_out = i_value;
182 return VLC_SUCCESS;
185 static inline bool NeedsPacketTable( demux_sys_t *p_sys )
187 return ( !p_sys->fmt.audio.i_bytes_per_frame || !p_sys->fmt.audio.i_frame_length );
190 static uint64_t TotalNumFrames( demux_t *p_demux )
192 demux_sys_t *p_sys = p_demux->p_sys;
194 if( !NeedsPacketTable( p_sys ))
196 uint64_t i_data_size;
198 if( p_sys->i_data_size != kCHUNK_SIZE_EOF)
200 i_data_size = p_sys->i_data_size;
202 else
204 int64_t i_stream_size = stream_Size( p_demux->s );
205 if(i_stream_size >= 0 && (uint64_t)i_stream_size >= p_sys->i_data_offset)
206 i_data_size = i_stream_size - p_sys->i_data_offset;
207 else
208 i_data_size = 0;
211 return i_data_size / p_sys->fmt.audio.i_bytes_per_frame;
213 else
215 return p_sys->packet_table.i_num_packets;
219 static uint64_t TotalNumSamples( demux_t *p_demux )
221 demux_sys_t *p_sys = p_demux->p_sys;
223 if( !NeedsPacketTable( p_sys ))
225 return TotalNumFrames( p_demux ) * p_sys->fmt.audio.i_frame_length;
227 else
229 return p_sys->packet_table.i_num_valid_frames + p_sys->packet_table.i_num_priming_frames +
230 p_sys->packet_table.i_num_remainder_frames;
234 static inline vlc_fourcc_t ReadFOURCC( const uint8_t *p )
236 return VLC_FOURCC( p[0], p[1], p[2], p[3] );
239 /*****************************************************************************
240 * FrameSpan Functions
241 *****************************************************************************
242 * A FrameSpan structure contains the relationship between a number of
243 frames, the number of samples they contain, the amount of data they
244 use, and the length of their packet descriptions (if any).
245 *****************************************************************************/
247 /* FrameSpanAddSpan adds span2 to span1 */
249 static inline void FrameSpanAddSpan( frame_span_t *span1, frame_span_t *span2 )
251 span1->i_frames += span2->i_frames;
252 span1->i_samples += span2->i_samples;
253 span1->i_bytes += span2->i_bytes;
254 span1->i_desc_bytes += span2->i_desc_bytes;
257 /* Adds the frame that is described at i_desc_offset to span */
259 static int FrameSpanAddDescription( demux_t *p_demux, uint64_t i_desc_offset, frame_span_t *span )
261 demux_sys_t *p_sys = p_demux->p_sys;
263 /* Avoid seeking + peeking for simple case (PCM) */
264 if( p_sys->fmt.audio.i_bytes_per_frame && p_sys->fmt.audio.i_frame_length )
266 span->i_bytes += p_sys->fmt.audio.i_bytes_per_frame;
267 span->i_samples += p_sys->fmt.audio.i_frame_length;
268 span->i_frames++;
269 return VLC_SUCCESS;
272 uint32_t i_desc_size = 0;
274 if( vlc_stream_Seek( p_demux->s, p_sys->packet_table.i_descriptions_start + i_desc_offset ))
276 msg_Err( p_demux, "Couldn't seek packet description." );
277 return VLC_EGENERIC;
280 const uint8_t *p_peek;
281 int i_peek_len = vlc_stream_Peek( p_demux->s, &p_peek, 2 * 10 );
282 /* Peeking the maximum number of bytes that two 64 bit numbers could use
283 * (( 64 + 6 ) / 7 = 10). */
284 if( i_peek_len < 0 )
285 i_peek_len = 0;
287 if( p_sys->fmt.audio.i_bytes_per_frame )
289 span->i_bytes += p_sys->fmt.audio.i_bytes_per_frame;
291 else
293 uint64_t i_size;
294 uint32_t i_this_int;
295 if( ParseVarLenInteger( p_peek, i_peek_len, &i_size, &i_this_int ))
297 return VLC_EGENERIC;
300 i_desc_size += i_this_int;
301 span->i_bytes += i_size;
304 if( p_sys->fmt.audio.i_frame_length )
306 span->i_samples += p_sys->fmt.audio.i_frame_length;
308 else
310 if( i_desc_size >= (unsigned)i_peek_len )
312 return VLC_EGENERIC;
315 uint64_t i_num_samples;
316 uint32_t i_this_int;
317 if( ParseVarLenInteger( p_peek + i_desc_size, i_peek_len - i_desc_size, &i_num_samples, &i_this_int ))
319 return VLC_EGENERIC;
322 i_desc_size += i_this_int;
323 span->i_samples += i_num_samples;
325 span->i_desc_bytes += i_desc_size;
326 span->i_frames++;
328 return VLC_SUCCESS;
331 /* FrameSpanGetTime returns the time span represented by the frame span. */
333 static inline vlc_tick_t FrameSpanGetTime( frame_span_t *span, uint32_t i_sample_rate )
335 if( !i_sample_rate )
336 return VLC_TICK_INVALID;
338 return vlc_tick_from_samples( span->i_samples, i_sample_rate) + VLC_TICK_0;
341 /* SetSpanWithSample returns the span from the beginning of the file up to and
342 including the specified sample. This works at frame granularity, so the
343 returned span may not represent the exact target sample.
344 Since we might have to lineraly search the packet descriptions, this is a
345 potentially slow operation! */
347 static int SetSpanWithSample( demux_t *p_demux, frame_span_t *p_span, uint64_t i_target_sample )
349 demux_sys_t *p_sys = p_demux->p_sys;
351 uint64_t i_num_frames = TotalNumFrames( p_demux );
353 if( !NeedsPacketTable( p_sys ))
355 uint64_t i_frame = i_target_sample / p_sys->fmt.audio.i_frame_length;
356 uint64_t i_remaining = i_target_sample - i_frame * p_sys->fmt.audio.i_frame_length;
357 if( i_remaining > ( p_sys->fmt.audio.i_frame_length / 2 ))
358 i_frame++;
360 if( i_frame > i_num_frames )
361 i_frame = i_num_frames;
363 p_span->i_frames = i_frame;
364 p_span->i_samples = i_frame * p_sys->fmt.audio.i_frame_length;
365 p_span->i_bytes = i_frame * p_sys->fmt.audio.i_bytes_per_frame;
366 p_span->i_desc_bytes = 0;
368 else
370 *p_span = (frame_span_t){0};
371 frame_span_t prev_span;
373 while( p_span->i_samples < i_target_sample && p_span->i_frames < i_num_frames )
375 prev_span = *p_span;
377 if( FrameSpanAddDescription( p_demux, p_span->i_desc_bytes, p_span ))
378 return VLC_EGENERIC;
380 if( p_span->i_samples >= i_target_sample )
382 uint64_t i_this_samples = p_span->i_samples - prev_span.i_samples;
384 if( i_target_sample - prev_span.i_samples < i_this_samples / 2 )
385 *p_span = prev_span;
387 break;
392 return VLC_SUCCESS;
395 /*****************************************************************************
396 * CAF Parsing Functions
397 *****************************************************************************/
399 /* The NextChunk function returns the four char code and the (sanitized) size at the current file position.
400 Upon return the file position points to the start of the chunk payload.
401 Note that the ReadXXXChunk functions expect the chunk to size to be sanitized
402 (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).
405 static int NextChunk( demux_t *p_demux, vlc_fourcc_t *p_fcc, uint64_t *pi_size )
407 uint8_t p_read[12];
409 if( vlc_stream_Read( p_demux->s, p_read, 12 ) < 12 )
410 return VLC_EGENERIC;
412 *p_fcc = ReadFOURCC( p_read );
413 uint64_t i_size = GetQWBE( p_read + 4 );
415 /* We accept no negativ sizes for chunks, except -1 for the data chunk. */
417 if( i_size > INT64_MAX )
419 if( *p_fcc == VLC_FOURCC( 'd', 'a', 't', 'a' ) && i_size == UINT64_C( -1 ))
420 i_size = kCHUNK_SIZE_EOF;
421 else
422 return VLC_EGENERIC;
425 *pi_size = i_size;
427 return VLC_SUCCESS;
430 static int ReadDescChunk( demux_t *p_demux )
432 demux_sys_t *p_sys = p_demux->p_sys;
434 const uint8_t *p_peek;
436 if ( vlc_stream_Peek( p_demux->s, &p_peek, 8 + 6 * 4 ) < ( 8 + 6 * 4 ))
438 return VLC_EGENERIC;
441 vlc_fourcc_t i_fmt = ReadFOURCC( p_peek + 8 );
442 uint32_t i_fmt_flags = GetDWBE( p_peek + 12 );
444 uint32_t i_bits_per_channel = GetDWBE( p_peek + 28 );
445 uint32_t i_bytes_per_packet = GetDWBE( p_peek + 16 );
446 uint32_t i_frames_per_packet = GetDWBE( p_peek + 20 );
447 uint32_t i_channels_per_frame = GetDWBE( p_peek + 24 );
449 if( i_fmt == VLC_CODEC_DVD_LPCM )
451 if( !i_frames_per_packet || !i_channels_per_frame )
453 msg_Err( p_demux, "Absurd LPCM parameters (frames_per_packet: %u, channels_per_frame: %u).", i_frames_per_packet, i_channels_per_frame );
454 return VLC_EGENERIC;
457 uint32_t i_unpacked_bits_per_sample = ( i_bytes_per_packet / i_frames_per_packet / i_channels_per_frame ) * 8;
459 bool b_is_float = !!( i_fmt_flags & ( 1 << 0 ) );
460 bool b_is_be = !( i_fmt_flags & ( 1 << 1 ) );
462 vlc_fourcc_t i_basic_codec = 0;
464 if( !b_is_float )
466 i_basic_codec = b_is_be ? VLC_FOURCC( 't', 'w', 'o', 's' ) : VLC_FOURCC( 's', 'o', 'w', 't' );
467 es_format_Init( &p_sys->fmt, AUDIO_ES, vlc_fourcc_GetCodecAudio( i_basic_codec, i_unpacked_bits_per_sample ));
469 else
471 if( i_bits_per_channel == 32 )
472 i_basic_codec = b_is_be ? VLC_CODEC_F32B : VLC_CODEC_F32L;
473 else if( i_bits_per_channel == 64 )
474 i_basic_codec = b_is_be ? VLC_CODEC_F64B : VLC_CODEC_F64L;
476 if( i_basic_codec )
477 es_format_Init( &p_sys->fmt, AUDIO_ES, vlc_fourcc_GetCodecAudio( i_basic_codec, i_bits_per_channel ));
480 else if( i_fmt == VLC_FOURCC( 'a', 'a', 'c', ' ' ))
482 const uint32_t kMP4Audio_AAC_LC_ObjectType = 2;
484 if( i_fmt_flags != kMP4Audio_AAC_LC_ObjectType )
486 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 );
489 es_format_Init( &p_sys->fmt, AUDIO_ES, vlc_fourcc_GetCodecAudio( VLC_CODEC_MP4A, 0 ));
492 else
494 es_format_Init( &p_sys->fmt, AUDIO_ES, vlc_fourcc_GetCodecAudio( i_fmt, 0 ));
497 if( !p_sys->fmt.i_codec )
499 msg_Err( p_demux, "could not determine codec" );
500 return VLC_EGENERIC;
503 double d_rate = round( GetDBLBE( p_peek ));
505 if( d_rate <= 0 || d_rate > UINT_MAX )
506 return VLC_EGENERIC;
508 p_sys->fmt.audio.i_rate = (unsigned int)lround( d_rate );
509 p_sys->fmt.audio.i_channels = i_channels_per_frame;
510 p_sys->fmt.audio.i_bytes_per_frame = i_bytes_per_packet; /* "mBytesPerPacket" in Apple parlance */
511 p_sys->fmt.audio.i_frame_length = i_frames_per_packet; /* "mFramesPerPacket" in Apple parlance */
512 p_sys->fmt.audio.i_bitspersample = i_bits_per_channel; /* mBitsPerChannel */
513 p_sys->fmt.audio.i_blockalign = i_bytes_per_packet;
514 p_sys->fmt.i_bitrate = i_bits_per_channel * p_sys->fmt.audio.i_rate * i_channels_per_frame;
516 if( p_sys->fmt.i_codec == VLC_CODEC_OPUS )
518 p_sys->i_max_frames = 1;
520 else p_sys->i_max_frames = UINT_MAX;
522 return VLC_SUCCESS;
525 /* This is lifted from cafdec.c in libavformat (function read_kuki_chunk). Appearantly the
526 alac library expects the cookie to be of length 36, but current alac files
527 have a cookie length of 24.
529 static int ProcessALACCookie( demux_t *p_demux, const uint8_t *p, uint64_t i_size )
531 demux_sys_t *p_sys = p_demux->p_sys;
533 const unsigned int kALAC_NEW_KUKI_SIZE = 24;
534 const unsigned int kALAC_LIB_REQ_KUKI_SIZE = 36;
535 int i_extra;
537 if( i_size == kALAC_NEW_KUKI_SIZE || i_size == kALAC_LIB_REQ_KUKI_SIZE )
539 i_extra = kALAC_LIB_REQ_KUKI_SIZE;
541 else
543 msg_Warn( p_demux, "Unknown alac magic cookie. Passing it on to the decoder as is and hoping for the best." );
544 i_extra = ( int )i_size;
547 p_sys->fmt.i_extra = i_extra;
548 p_sys->fmt.p_extra = malloc( i_extra );
550 if( !p_sys->fmt.p_extra )
551 return VLC_ENOMEM;
553 uint8_t *p_extra = ( uint8_t * )p_sys->fmt.p_extra;
555 if( i_size == kALAC_NEW_KUKI_SIZE )
557 SetDWBE( p_extra, 36 );
558 memcpy( p_extra + 4, "alac", 4 );
559 SetDWBE( p_extra + 8, 0 );
560 memcpy( p_extra + 12, p, 24 );
562 else
564 memcpy( p_sys->fmt.p_extra, p, i_size );
567 return VLC_SUCCESS;
570 /* Helper functions for AAC cookie processing. */
572 static inline bool AACCookieGetTag( uint8_t *p_tag, const uint8_t *p, uint64_t *p_offset, uint64_t i_size )
574 if( *p_offset + 1 > i_size )
575 return false;
577 *p_tag = *( p + *p_offset );
578 *p_offset += 1;
580 return true;
583 static inline bool AACCookieTagLen( uint64_t *p_tag_len, const uint8_t *p, uint64_t *p_offset, uint64_t i_size )
585 uint32_t i_int_size;
587 if( ParseVarLenInteger( p + *p_offset, i_size - *p_offset, p_tag_len, &i_int_size ))
588 return false;
590 *p_offset += i_int_size;
592 return true;
595 static inline bool AACCookieChkLen( int64_t i_length, uint64_t i_size, uint64_t i_offset )
597 return ( i_offset + i_length <= i_size );
600 /* This is lifted from libmp4.c in the mp4 demuxer module (function MP4_ReadBox_esds). The aac
601 library only want a subset of the magic cookie ("decoder specific info"), so we have to parse it.
603 static int ProcessAACCookie( demux_t *p_demux, const uint8_t *p, uint64_t i_size )
605 const uint8_t kAAC_ES_DESCR_TAG = 3;
606 const uint8_t kAAC_DEC_CONFIG_DESCR_TAG = 4;
607 const uint8_t kAAC_DEC_SPEC_INFO_TAG = 5;
609 demux_sys_t *p_sys = p_demux->p_sys;
611 uint64_t i_offset = 0;
612 uint64_t i_kuki_size = 0;
613 uint64_t i_tag_len;
614 uint8_t i_tag;
616 if( !AACCookieGetTag( &i_tag, p, &i_offset, i_size )) goto aac_kuki_finish;
618 if( i_tag == kAAC_ES_DESCR_TAG )
621 if( !AACCookieTagLen( &i_tag_len, p, &i_offset, i_size )) goto aac_kuki_finish;
623 if( !AACCookieChkLen( 3, i_size, i_offset )) goto aac_kuki_finish;
624 i_offset += 2; /* don't care (ES ID) */
625 uint8_t i_flags = *( p + i_offset++ );
627 if( i_flags&0x80 )
629 if( !AACCookieChkLen( 2, i_size, i_offset )) goto aac_kuki_finish;
630 i_offset += 2; /* don't care (dependance) */
632 if( i_flags&0x40 )
634 if( !AACCookieChkLen( 1, i_size, i_offset )) goto aac_kuki_finish;
635 uint8_t i_url_len = *( p + i_offset++ );
636 i_offset += i_url_len; /* don't care (url) */
638 if( i_flags&0x20 )
640 if( !AACCookieChkLen( 2, i_size, i_offset )) goto aac_kuki_finish;
641 i_offset += 2; /* don't care (OCR) */
644 if( !AACCookieGetTag( &i_tag, p, &i_offset, i_size )) goto aac_kuki_finish;
647 if( i_tag != kAAC_DEC_CONFIG_DESCR_TAG )
648 goto aac_kuki_finish;
650 if( !AACCookieTagLen( &i_tag_len, p, &i_offset, i_size )) goto aac_kuki_finish;
652 if( !AACCookieChkLen( 1 + 1 + 3 + 4 + 4, i_size, i_offset )) goto aac_kuki_finish;
653 i_offset += ( 1 + 1 + 3 + 4 + 4 ); /* don't care */
655 if( !AACCookieGetTag( &i_tag, p, &i_offset, i_size )) goto aac_kuki_finish;
657 if( i_tag != kAAC_DEC_SPEC_INFO_TAG ) /* this is the one we need */
658 goto aac_kuki_finish;
660 if( !AACCookieTagLen( &i_tag_len, p, &i_offset, i_size )) goto aac_kuki_finish;
662 if( i_offset + i_tag_len > i_size )
663 goto aac_kuki_finish;
665 i_kuki_size = i_tag_len;
667 aac_kuki_finish:
669 if( !i_kuki_size )
671 msg_Warn( p_demux, "Error parsing aac cookie. Passing it on to the decoder as is and hoping for the best." );
672 i_kuki_size = i_size;
673 i_offset = 0;
676 p_sys->fmt.i_extra = (int)i_kuki_size;
677 p_sys->fmt.p_extra = malloc( i_kuki_size );
679 if( !p_sys->fmt.p_extra )
681 return VLC_ENOMEM;
684 memcpy( p_sys->fmt.p_extra, p + i_offset, i_kuki_size );
686 return VLC_SUCCESS;
689 static int ReadKukiChunk( demux_t *p_demux, uint64_t i_size )
691 demux_sys_t *p_sys = p_demux->p_sys;
692 const uint8_t *p_peek;
694 /* vlc_stream_Peek can't handle sizes bigger than INT32_MAX, and also p_sys->fmt.i_extra is of type 'int'*/
695 if( i_size > INT32_MAX )
697 msg_Err( p_demux, "Magic Cookie chunk too big" );
698 return VLC_EGENERIC;
701 if( (unsigned int)vlc_stream_Peek( p_demux->s, &p_peek, (int)i_size ) < i_size )
703 msg_Err( p_demux, "Couldn't peek extra data" );
704 return VLC_EGENERIC;
707 if( p_sys->fmt.i_codec == VLC_CODEC_ALAC )
709 int error = ProcessALACCookie( p_demux, p_peek, i_size );
710 if( error ) return error;
712 else if( p_sys->fmt.i_codec == VLC_CODEC_MP4A )
714 int error = ProcessAACCookie( p_demux, p_peek, i_size );
715 if( error ) return error;
717 else
719 p_sys->fmt.i_extra = (int)i_size;
720 p_sys->fmt.p_extra = malloc( i_size );
722 if( !p_sys->fmt.p_extra )
724 return VLC_ENOMEM;
726 memcpy( p_sys->fmt.p_extra, p_peek, p_sys->fmt.i_extra );
729 return VLC_SUCCESS;
732 static int ReadDataChunk( demux_t *p_demux, uint64_t i_size )
734 if( i_size < 4 )
735 return VLC_EGENERIC;
737 demux_sys_t *p_sys = p_demux->p_sys;
739 p_sys->i_data_offset = vlc_stream_Tell( p_demux->s ) + 4; /* skip edit count */
740 p_sys->i_data_size = i_size == kCHUNK_SIZE_EOF ? kCHUNK_SIZE_EOF : ( i_size - 4 );
742 return VLC_SUCCESS;
745 static int ReadPaktChunk( demux_t *p_demux )
747 demux_sys_t *p_sys = p_demux->p_sys;
749 const uint8_t *p_peek;
751 if ( vlc_stream_Peek( p_demux->s, &p_peek, 8 + 8 + 4 + 4 ) < ( 8 + 8 + 4 + 4 ))
753 msg_Err( p_demux, "Couldn't peek packet descriptions" );
754 return VLC_EGENERIC;
757 if( ReadBEInt64ToUInt64( p_peek, &p_sys->packet_table.i_num_packets ))
759 msg_Err( p_demux, "Invalid packet table: i_num_packets is negative.");
760 return VLC_EGENERIC;
762 if( ReadBEInt64ToUInt64( p_peek + 8, &p_sys->packet_table.i_num_valid_frames ))
764 msg_Err( p_demux, "Invalid packet table: i_num_valid_frames is negative.");
765 return VLC_EGENERIC;
767 if( ReadBEInt32ToUInt32( p_peek + 16, &p_sys->packet_table.i_num_priming_frames ))
769 msg_Err( p_demux, "Invalid packet table: i_num_priming_frames is negative.");
770 return VLC_EGENERIC;
772 if( ReadBEInt32ToUInt32( p_peek + 20, &p_sys->packet_table.i_num_remainder_frames ))
774 msg_Err( p_demux, "Invalid packet table: i_num_remainder_frames is negative.");
775 return VLC_EGENERIC;
778 p_sys->packet_table.i_descriptions_start = vlc_stream_Tell( p_demux->s ) + 24;
780 return VLC_SUCCESS;
783 /*****************************************************************************
784 * Open
785 *****************************************************************************/
786 static int Open( vlc_object_t *p_this )
788 int i_error = VLC_SUCCESS;
790 demux_t *p_demux = (demux_t*)p_this;
791 demux_sys_t *p_sys;
793 const uint8_t *p_peek;
795 if( vlc_stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
796 return VLC_EGENERIC;
798 /* Is it a caf file? */
799 if( memcmp( p_peek, "caff", 4 ))
800 return VLC_EGENERIC;
802 /* check file version (we only handle version 1) */
803 uint16_t i_version = GetWBE( p_peek + 4 );
804 if( i_version != 1 )
806 msg_Dbg( p_demux, "Unknown caf file version %d.", i_version );
807 return VLC_EGENERIC;
810 /* check file flags (must be 0) */
811 uint16_t i_flags = GetWBE( p_peek + 6 );
812 if( i_flags != 0 )
814 msg_Dbg( p_demux, "Unknown caf file flags %d.", i_flags );
815 return VLC_EGENERIC;
818 if( vlc_stream_Read( p_demux->s, NULL, 8 ) < 8 )
819 return VLC_EGENERIC; /* This would be very strange since we justed peeked at these bytes. */
821 p_demux->p_sys = calloc( 1, sizeof( demux_sys_t ));
822 if( !p_demux->p_sys ) return VLC_ENOMEM;
824 /* From this point on, we have to free p_sys if we return an error (e.g. "goto caf_open_end") */
826 p_sys = p_demux->p_sys;
827 es_format_Init( &p_sys->fmt, AUDIO_ES, 0 );
829 vlc_fourcc_t i_fcc;
830 uint64_t i_size;
831 uint64_t i_idx = 0;
833 while( NextChunk( p_demux, &i_fcc, &i_size ) == VLC_SUCCESS )
835 bool b_handled = true;
837 switch ( i_fcc )
839 case VLC_FOURCC( 'd', 'e', 's', 'c' ):
841 if( i_idx != 0 )
843 msg_Err( p_demux, "The audio description chunk must be the first chunk in a caf file." );
844 i_error = VLC_EGENERIC;
845 goto caf_open_end;
848 i_error = ReadDescChunk( p_demux );
849 break;
851 case VLC_FOURCC( 'd', 'a', 't', 'a' ):
853 i_error = ReadDataChunk( p_demux, i_size );
854 break;
856 case VLC_FOURCC( 'p', 'a', 'k', 't' ):
858 i_error = ReadPaktChunk( p_demux );
859 break;
861 case VLC_FOURCC( 'k', 'u', 'k', 'i' ):
863 i_error = ReadKukiChunk( p_demux, i_size );
864 break;
866 default:
868 b_handled = false;
869 break;
872 if( i_error )
873 goto caf_open_end;
875 if( b_handled )
876 msg_Dbg( p_demux, "Found '%4.4s' chunk.", ( char * )&i_fcc );
877 else
878 msg_Dbg( p_demux, "Ignoring '%4.4s' chunk.", ( char * )&i_fcc );
880 if( i_size == kCHUNK_SIZE_EOF )
881 break;
883 if( vlc_stream_Seek( p_demux->s, vlc_stream_Tell( p_demux->s ) + i_size ) != VLC_SUCCESS )
884 break;
886 i_idx++;
889 if ( !p_sys->i_data_offset || p_sys->fmt.i_cat != AUDIO_ES ||
890 ( NeedsPacketTable( p_sys ) && !p_sys->packet_table.i_descriptions_start ))
892 msg_Err( p_demux, "Did not find all necessary chunks." );
893 i_error = VLC_EGENERIC;
894 goto caf_open_end;
897 p_sys->es = es_out_Add( p_demux->out, &p_sys->fmt );
899 if( !p_sys->es )
901 msg_Err( p_demux, "Could not add elementary stream." );
902 i_error = VLC_EGENERIC;
903 goto caf_open_end;
906 p_demux->pf_control = Control;
907 p_demux->pf_demux = Demux;
908 return VLC_SUCCESS;
910 caf_open_end:
911 es_format_Clean( &p_sys->fmt );
912 free( p_sys );
913 return i_error;
916 /*****************************************************************************
917 * Demux:
918 *****************************************************************************/
919 static int Demux( demux_t *p_demux )
921 demux_sys_t *p_sys = p_demux->p_sys;
922 block_t *p_block;
924 if( p_sys->i_data_size != kCHUNK_SIZE_EOF && p_sys->position.i_bytes >= p_sys->i_data_size )
926 /* EOF */
927 return VLC_DEMUXER_EOF;
930 frame_span_t advance = (frame_span_t){0};
932 /* we will read 50ms at once */
933 uint64_t i_req_samples = __MAX( p_sys->fmt.audio.i_rate / 20, 1 );
935 if( !NeedsPacketTable( p_sys )) /* PCM/IMA4 */
937 int64_t i_req_frames = ( i_req_samples + ( p_sys->fmt.audio.i_frame_length - 1 )) / p_sys->fmt.audio.i_frame_length;
939 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 )
941 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;
944 advance.i_frames = i_req_frames;
945 advance.i_samples = i_req_frames * p_sys->fmt.audio.i_frame_length;
946 advance.i_bytes = p_sys->fmt.audio.i_bytes_per_frame * advance.i_frames;
948 else /* use packet table */
950 uint64_t i_max_frames;
951 if( p_sys->packet_table.i_num_packets > p_sys->position.i_frames )
952 i_max_frames = p_sys->packet_table.i_num_packets - p_sys->position.i_frames;
953 else
954 i_max_frames = 1; /* will be rejected on FrameSpanAddDescription below */
956 if( i_max_frames > p_sys->i_max_frames )
957 i_max_frames = p_sys->i_max_frames;
961 if( FrameSpanAddDescription( p_demux, p_sys->position.i_desc_bytes + advance.i_desc_bytes, &advance ))
962 break;
964 while ( i_req_samples > advance.i_samples && advance.i_frames < i_max_frames );
967 if( !advance.i_frames )
969 msg_Err( p_demux, "Unexpected end of file" );
970 return VLC_DEMUXER_EGENERIC;
973 if( vlc_stream_Seek( p_demux->s, p_sys->i_data_offset + p_sys->position.i_bytes ))
975 if( p_sys->i_data_size == kCHUNK_SIZE_EOF)
976 return VLC_DEMUXER_EOF;
978 msg_Err( p_demux, "cannot seek data" );
979 return VLC_DEMUXER_EGENERIC;
982 p_block = vlc_stream_Block( p_demux->s, (int)advance.i_bytes );
983 if( p_block == NULL )
985 msg_Err( p_demux, "cannot read data" );
986 return VLC_DEMUXER_EGENERIC;
989 p_block->i_dts =
990 p_block->i_pts = FrameSpanGetTime( &p_sys->position, p_sys->fmt.audio.i_rate );
992 FrameSpanAddSpan( &p_sys->position, &advance );
994 es_out_SetPCR( p_demux->out, p_block->i_pts );
995 es_out_Send( p_demux->out, p_sys->es, p_block );
997 return VLC_DEMUXER_SUCCESS;
1000 /*****************************************************************************
1001 * Control:
1002 *****************************************************************************/
1003 static int Control( demux_t *p_demux, int i_query, va_list args )
1005 int64_t i_sample;
1006 double f, *pf;
1007 frame_span_t position;
1009 demux_sys_t *p_sys = p_demux->p_sys;
1010 uint64_t i_num_samples = TotalNumSamples( p_demux );
1012 switch( i_query )
1014 case DEMUX_CAN_SEEK:
1015 *va_arg( args, bool * ) = true;
1016 return VLC_SUCCESS;
1018 case DEMUX_GET_LENGTH:
1019 *va_arg( args, vlc_tick_t * ) =
1020 vlc_tick_from_samples( i_num_samples, p_sys->fmt.audio.i_rate );
1021 return VLC_SUCCESS;
1023 case DEMUX_GET_TIME:
1024 *va_arg( args, vlc_tick_t * ) =
1025 vlc_tick_from_samples( p_sys->position.i_samples, p_sys->fmt.audio.i_rate );
1026 return VLC_SUCCESS;
1028 case DEMUX_GET_POSITION:
1029 pf = va_arg( args, double * );
1030 *pf = i_num_samples ? (double)p_sys->position.i_samples / (double)i_num_samples : 0.0;
1031 return VLC_SUCCESS;
1033 case DEMUX_SET_POSITION:
1034 f = va_arg( args, double );
1035 i_sample = f * i_num_samples;
1036 if( SetSpanWithSample( p_demux, &position, i_sample ))
1037 return VLC_EGENERIC;
1038 p_sys->position = position;
1039 return VLC_SUCCESS;
1041 case DEMUX_SET_TIME:
1042 i_sample =
1043 samples_from_vlc_tick( va_arg( args, vlc_tick_t ), p_sys->fmt.audio.i_rate );
1044 if( SetSpanWithSample( p_demux, &position, i_sample ))
1045 return VLC_EGENERIC;
1046 p_sys->position = position;
1047 return VLC_SUCCESS;
1049 case DEMUX_GET_META:
1050 return vlc_stream_Control( p_demux->s, STREAM_GET_META, args );
1052 case DEMUX_CAN_PAUSE:
1053 case DEMUX_SET_PAUSE_STATE:
1054 case DEMUX_CAN_CONTROL_PACE:
1055 case DEMUX_GET_PTS_DELAY:
1056 return demux_vaControlHelper( p_demux->s, p_sys->i_data_offset,
1057 p_sys->i_data_size, 0, 1, i_query, args );
1059 default:
1060 return VLC_EGENERIC;
1063 return VLC_EGENERIC;
1066 /*****************************************************************************
1067 * Close
1068 *****************************************************************************/
1069 static void Close( vlc_object_t *p_this )
1071 demux_t *p_demux = (demux_t*)p_this;
1072 demux_sys_t *p_sys = p_demux->p_sys;
1074 es_format_Clean( &p_sys->fmt );
1075 free( p_sys );