2 * ALAC (Apple Lossless Audio Codec) decoder
3 * Copyright (c) 2005 David Hammerton
6 * This is the quicktime container demuxer.
8 * http://crazney.net/programs/itunes/alac.html
10 * Permission is hereby granted, free of charge, to any person
11 * obtaining a copy of this software and associated documentation
12 * files (the "Software"), to deal in the Software without
13 * restriction, including without limitation the rights to use,
14 * copy, modify, merge, publish, distribute, sublicense, and/or
15 * sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be
19 * included in all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 * OTHER DEALINGS IN THE SOFTWARE.
42 #define DEBUGF qtmovie->stream->ci->debugf
55 static void read_chunk_ftyp(qtmovie_t
*qtmovie
, size_t chunk_len
)
59 size_t size_remaining
= chunk_len
- 8;
61 type
= stream_read_uint32(qtmovie
->stream
);
63 if ((type
!= MAKEFOURCC('M','4','A',' ')) &&
64 (type
!= MAKEFOURCC('m','4','a',' ')) &&
65 (type
!= MAKEFOURCC('M','4','B',' ')) &&
66 (type
!= MAKEFOURCC('m','p','4','2')) &&
67 (type
!= MAKEFOURCC('3','g','p','6')) &&
68 (type
!= MAKEFOURCC('q','t',' ',' ')) &&
69 (type
!= MAKEFOURCC('i','s','o','m')))
71 DEBUGF("not M4A file\n");
74 minor_ver
= stream_read_uint32(qtmovie
->stream
);
77 /* compatible brands */
78 while (size_remaining
)
81 /*fourcc_t cbrand =*/ stream_read_uint32(qtmovie
->stream
);
86 static uint32_t mp4ff_read_mp4_descr_length(stream_t
* stream
)
94 b
= stream_read_uint8(stream
);
96 length
= (length
<< 7) | (b
& 0x7F);
97 } while ((b
& 0x80) && numBytes
< 4);
102 /* The following function is based on mp4ff */
103 static bool read_chunk_esds(qtmovie_t
*qtmovie
, size_t chunk_len
)
112 /* version and flags */
113 temp
=stream_read_uint32(qtmovie
->stream
);
115 /* get and verify ES_DescrTag */
116 tag
= stream_read_uint8(qtmovie
->stream
);
120 if (mp4ff_read_mp4_descr_length(qtmovie
->stream
) < 5 + 15)
125 stream_skip(qtmovie
->stream
,3);
128 stream_skip(qtmovie
->stream
,2);
131 /* get and verify DecoderConfigDescrTab */
132 if (stream_read_uint8(qtmovie
->stream
) != 0x04)
138 temp
= mp4ff_read_mp4_descr_length(qtmovie
->stream
);
139 if (temp
< 13) return false;
141 audioType
= stream_read_uint8(qtmovie
->stream
);
142 temp
=stream_read_int32(qtmovie
->stream
);//0x15000414 ????
143 maxBitrate
= stream_read_int32(qtmovie
->stream
);
144 avgBitrate
= stream_read_int32(qtmovie
->stream
);
145 DEBUGF("audioType=%d, maxBitrate=%ld, avgBitrate=%ld\n",audioType
,
146 (long)maxBitrate
,(long)avgBitrate
);
148 /* get and verify DecSpecificInfoTag */
149 if (stream_read_uint8(qtmovie
->stream
) != 0x05)
155 qtmovie
->res
->codecdata_len
= mp4ff_read_mp4_descr_length(qtmovie
->stream
);
156 if (qtmovie
->res
->codecdata_len
> MAX_CODECDATA_SIZE
)
158 DEBUGF("codecdata too large (%d) in esds\n",
159 (int)qtmovie
->res
->codecdata_len
);
163 stream_read(qtmovie
->stream
, qtmovie
->res
->codecdata_len
, qtmovie
->res
->codecdata
);
165 /* will skip the remainder of the atom */
169 static bool read_chunk_stsd(qtmovie_t
*qtmovie
, size_t chunk_len
)
174 size_t size_remaining
= chunk_len
- 8;
175 bool got_codec_data
= false;
178 stream_read_uint8(qtmovie
->stream
);
181 stream_read_uint8(qtmovie
->stream
);
182 stream_read_uint8(qtmovie
->stream
);
183 stream_read_uint8(qtmovie
->stream
);
186 numentries
= stream_read_uint32(qtmovie
->stream
);
189 /* if (numentries != 1)
191 DEBUGF("only expecting one entry in sample description atom!\n");
195 for (i
= 0; i
< numentries
; i
++)
199 uint32_t entry_remaining
;
201 entry_size
= stream_read_uint32(qtmovie
->stream
);
202 qtmovie
->res
->format
= stream_read_uint32(qtmovie
->stream
);
203 DEBUGF("format: %c%c%c%c\n",SPLITFOURCC(qtmovie
->res
->format
));
204 entry_remaining
= entry_size
;
205 entry_remaining
-= 8;
209 /* reserved + data reference index + sound version + reserved */
210 stream_skip(qtmovie
->stream
, 6 + 2 + 2 + 6);
211 entry_remaining
-= 6 + 2 + 2 + 6;
213 qtmovie
->res
->num_channels
= stream_read_uint16(qtmovie
->stream
);
214 qtmovie
->res
->sound_sample_size
= stream_read_uint16(qtmovie
->stream
);
215 entry_remaining
-= 4;
218 stream_skip(qtmovie
->stream
, 2);
219 qtmovie
->res
->sound_sample_rate
= stream_read_uint32(qtmovie
->stream
);
221 stream_skip(qtmovie
->stream
, 2);
222 entry_remaining
-= 8;
224 /* remaining is codec data */
226 if ((qtmovie
->res
->format
==MAKEFOURCC('a','l','a','c'))) {
227 if (qtmovie
->stream
->ci
->id3
->codectype
!=AFMT_MP4_ALAC
) {
231 /* 12 = audio format atom, 8 = padding */
232 qtmovie
->res
->codecdata_len
= entry_remaining
+ 12 + 8;
233 if (qtmovie
->res
->codecdata_len
> MAX_CODECDATA_SIZE
)
235 DEBUGF("codecdata too large (%d) in stsd\n",
236 (int)qtmovie
->res
->codecdata_len
);
240 memset(qtmovie
->res
->codecdata
, 0, qtmovie
->res
->codecdata_len
);
241 /* audio format atom */
243 /* The ALAC decoder skips these bytes, so there is no need to store them,
244 and this code isn't endian/alignment safe */
245 ((unsigned int*)qtmovie
->res
->codecdata
)[0] = 0x0c000000;
246 ((unsigned int*)qtmovie
->res
->codecdata
)[1] = MAKEFOURCC('a','m','r','f');
247 ((unsigned int*)qtmovie
->res
->codecdata
)[2] = MAKEFOURCC('c','a','l','a');
250 stream_read(qtmovie
->stream
,
252 ((char*)qtmovie
->res
->codecdata
) + 12);
253 entry_remaining
-= entry_remaining
;
254 got_codec_data
= true;
257 stream_skip(qtmovie
->stream
, entry_remaining
);
259 } else if (qtmovie
->res
->format
==MAKEFOURCC('m','p','4','a')) {
260 if (qtmovie
->stream
->ci
->id3
->codectype
!=AFMT_MP4_AAC
&&
261 qtmovie
->stream
->ci
->id3
->codectype
!=AFMT_MP4_AAC_HE
) {
265 size_t sub_chunk_len
;
266 fourcc_t sub_chunk_id
;
268 sub_chunk_len
= stream_read_uint32(qtmovie
->stream
);
269 if (sub_chunk_len
<= 1 || sub_chunk_len
> entry_remaining
)
271 DEBUGF("strange size (%lu) for chunk inside mp4a\n",
272 (unsigned long)sub_chunk_len
);
276 sub_chunk_id
= stream_read_uint32(qtmovie
->stream
);
278 if (sub_chunk_id
!= MAKEFOURCC('e','s','d','s'))
280 DEBUGF("Expected esds chunk inside mp4a, found %c%c%c%c\n",SPLITFOURCC(sub_chunk_id
));
284 j
=qtmovie
->stream
->ci
->curpos
+sub_chunk_len
-8;
285 if (read_chunk_esds(qtmovie
,sub_chunk_len
)) {
286 if (j
!=qtmovie
->stream
->ci
->curpos
) {
287 DEBUGF("curpos=%ld, j=%d - Skipping %ld bytes\n",qtmovie
->stream
->ci
->curpos
,j
,j
-qtmovie
->stream
->ci
->curpos
);
288 stream_skip(qtmovie
->stream
,j
-qtmovie
->stream
->ci
->curpos
);
290 got_codec_data
= true;
291 entry_remaining
-=sub_chunk_len
;
293 DEBUGF("Error reading esds\n");
297 DEBUGF("entry_remaining=%ld\n",(long)entry_remaining
);
298 stream_skip(qtmovie
->stream
,entry_remaining
);
300 } else if (qtmovie
->res
->format
==MAKEFOURCC('f','r','e','e')) {
301 /* Skip "filler" atom */
302 stream_skip(qtmovie
->stream
,entry_remaining
);
304 DEBUGF("expecting 'alac', 'mp4a' or 'free' data format, got %c%c%c%c\n",
305 SPLITFOURCC(qtmovie
->res
->format
));
309 return got_codec_data
;
312 static bool read_chunk_stts(qtmovie_t
*qtmovie
, size_t chunk_len
)
316 size_t size_remaining
= chunk_len
- 8;
319 stream_read_uint8(qtmovie
->stream
);
322 stream_read_uint8(qtmovie
->stream
);
323 stream_read_uint8(qtmovie
->stream
);
324 stream_read_uint8(qtmovie
->stream
);
327 numentries
= stream_read_uint32(qtmovie
->stream
);
330 qtmovie
->res
->num_time_to_samples
= numentries
;
331 qtmovie
->res
->time_to_sample
= malloc(numentries
* sizeof(*qtmovie
->res
->time_to_sample
));
333 if (!qtmovie
->res
->time_to_sample
)
335 DEBUGF("stts too large\n");
339 for (i
= 0; i
< numentries
; i
++)
341 qtmovie
->res
->time_to_sample
[i
].sample_count
= stream_read_uint32(qtmovie
->stream
);
342 qtmovie
->res
->time_to_sample
[i
].sample_duration
= stream_read_uint32(qtmovie
->stream
);
348 DEBUGF("ehm, size remianing?\n");
349 stream_skip(qtmovie
->stream
, size_remaining
);
355 static bool read_chunk_stsz(qtmovie_t
*qtmovie
, size_t chunk_len
)
357 size_t size_remaining
= chunk_len
- 8;
360 stream_read_uint8(qtmovie
->stream
);
363 stream_read_uint8(qtmovie
->stream
);
364 stream_read_uint8(qtmovie
->stream
);
365 stream_read_uint8(qtmovie
->stream
);
368 /* default sample size */
369 if (stream_read_uint32(qtmovie
->stream
) != 0)
371 DEBUGF("i was expecting variable samples sizes\n");
372 stream_read_uint32(qtmovie
->stream
);
378 qtmovie
->res
->num_sample_byte_sizes
= stream_read_uint32(qtmovie
->stream
);
383 stream_skip(qtmovie
->stream
, size_remaining
);
389 static bool read_chunk_stsc(qtmovie_t
*qtmovie
, size_t chunk_len
)
393 size_t size_remaining
= chunk_len
- 8;
395 /* version + flags */
396 stream_read_uint32(qtmovie
->stream
);
399 numentries
= stream_read_uint32(qtmovie
->stream
);
402 qtmovie
->res
->num_sample_to_chunks
= numentries
;
403 qtmovie
->res
->sample_to_chunk
= malloc(numentries
* sizeof(sample_to_chunk_t
));
405 if (!qtmovie
->res
->sample_to_chunk
)
407 DEBUGF("stsc too large\n");
411 for (i
= 0; i
< numentries
; i
++)
413 qtmovie
->res
->sample_to_chunk
[i
].first_chunk
=
414 stream_read_uint32(qtmovie
->stream
);
415 qtmovie
->res
->sample_to_chunk
[i
].num_samples
=
416 stream_read_uint32(qtmovie
->stream
);
417 stream_read_uint32(qtmovie
->stream
);
418 size_remaining
-= 12;
423 DEBUGF("ehm, size remianing?\n");
424 stream_skip(qtmovie
->stream
, size_remaining
);
430 static bool read_chunk_stco(qtmovie_t
*qtmovie
, size_t chunk_len
)
432 uint32_t i
, k
, old_i
;
440 size_t size_remaining
= chunk_len
- 8;
442 /* version + flags */
443 stream_read_uint32(qtmovie
->stream
);
446 numentries
= stream_read_uint32(qtmovie
->stream
);
449 qtmovie
->res
->num_lookup_table
= numentries
;
450 qtmovie
->res
->lookup_table
= malloc(numentries
* sizeof(*qtmovie
->res
->lookup_table
));
452 if (!qtmovie
->res
->lookup_table
)
454 DEBUGF("stco too large to allocate lookup_table[]\n");
458 /* read first offset */
459 offset
= stream_read_uint32(qtmovie
->stream
);
462 /* Build up lookup table. The lookup table contains the sample index and
463 * byte position in the file for each chunk. This table is used to seek
464 * and resume (see m4a_seek() and m4a_seek_raw() in libm4a/m4a.c) and
465 * to skip empty chunks (see m4a_check_sample_offset() in codecs/aac.c and
467 * The seek/resume precision is lower than using sample_byte_size[] and
468 * depends on numentries. Typically the resolution is ~1/10 of all frames
469 * which equals about 1/4-1/2 seconds. The loss of seek precision is
470 * accepted to be able to avoid allocation of the large sample_byte_size[]
471 * table. This reduces the memory consumption by a factor of 2 or even
476 old_first
= qtmovie
->res
->sample_to_chunk
[0].first_chunk
;
477 old_frame
= qtmovie
->res
->sample_to_chunk
[0].num_samples
;
478 new_first
= qtmovie
->res
->sample_to_chunk
[1].first_chunk
;
479 for (k
= 1; k
< numentries
; ++k
)
481 for (; i
< qtmovie
->res
->num_sample_to_chunks
; ++i
)
485 /* Only access sample_to_chunk[] if new data is required. */
486 old_first
= qtmovie
->res
->sample_to_chunk
[i
-1].first_chunk
;
487 old_frame
= qtmovie
->res
->sample_to_chunk
[i
-1].num_samples
;
488 new_first
= qtmovie
->res
->sample_to_chunk
[i
].first_chunk
;
495 frame
+= (new_first
- old_first
) * old_frame
;
497 frame
+= (k
- old_first
) * old_frame
;
499 qtmovie
->res
->lookup_table
[idx
].sample
= frame
;
500 qtmovie
->res
->lookup_table
[idx
].offset
= offset
;
503 frame
-= (k
- old_first
) * old_frame
;
505 offset
= stream_read_uint32(qtmovie
->stream
);
511 DEBUGF("ehm, size remianing?\n");
512 stream_skip(qtmovie
->stream
, size_remaining
);
518 static bool read_chunk_stbl(qtmovie_t
*qtmovie
, size_t chunk_len
)
520 size_t size_remaining
= chunk_len
- 8;
522 while (size_remaining
)
524 size_t sub_chunk_len
;
525 fourcc_t sub_chunk_id
;
527 sub_chunk_len
= stream_read_uint32(qtmovie
->stream
);
528 if (sub_chunk_len
<= 1 || sub_chunk_len
> size_remaining
)
530 DEBUGF("strange size (%lu) for chunk inside stbl\n",
531 (unsigned long)sub_chunk_len
);
535 sub_chunk_id
= stream_read_uint32(qtmovie
->stream
);
537 switch (sub_chunk_id
)
539 case MAKEFOURCC('s','t','s','d'):
540 if (!read_chunk_stsd(qtmovie
, sub_chunk_len
)) {
544 case MAKEFOURCC('s','t','t','s'):
545 if (!read_chunk_stts(qtmovie
, sub_chunk_len
))
550 case MAKEFOURCC('s','t','s','z'):
551 if (!read_chunk_stsz(qtmovie
, sub_chunk_len
))
556 case MAKEFOURCC('s','t','s','c'):
557 if (!read_chunk_stsc(qtmovie
, sub_chunk_len
))
562 case MAKEFOURCC('s','t','c','o'):
563 if (!read_chunk_stco(qtmovie
, sub_chunk_len
))
569 /*DEBUGF("(stbl) unknown chunk id: %c%c%c%c\n",
570 SPLITFOURCC(sub_chunk_id));*/
571 stream_skip(qtmovie
->stream
, sub_chunk_len
- 8);
574 size_remaining
-= sub_chunk_len
;
579 static bool read_chunk_minf(qtmovie_t
*qtmovie
, size_t chunk_len
)
581 size_t size_remaining
= chunk_len
- 8;
584 /* Check for smhd, only kind of minf we care about */
586 if ((i
= stream_read_uint32(qtmovie
->stream
)) != 16)
588 DEBUGF("unexpected size in media info: %ld\n", (long)i
);
589 stream_skip(qtmovie
->stream
, size_remaining
-4);
593 if (stream_read_uint32(qtmovie
->stream
) != MAKEFOURCC('s','m','h','d'))
595 DEBUGF("not a sound header! can't handle this.\n");
599 /* now skip the rest of the atom */
600 stream_skip(qtmovie
->stream
, 16 - 8);
601 size_remaining
-= 16;
603 while (size_remaining
)
605 size_t sub_chunk_len
;
606 fourcc_t sub_chunk_id
;
608 sub_chunk_len
= stream_read_uint32(qtmovie
->stream
);
610 if (sub_chunk_len
<= 1 || sub_chunk_len
> size_remaining
)
612 DEBUGF("strange size (%lu) for chunk inside minf\n",
613 (unsigned long)sub_chunk_len
);
617 sub_chunk_id
= stream_read_uint32(qtmovie
->stream
);
619 switch (sub_chunk_id
)
621 case MAKEFOURCC('s','t','b','l'):
622 if (!read_chunk_stbl(qtmovie
, sub_chunk_len
)) {
627 /*DEBUGF("(minf) unknown chunk id: %c%c%c%c\n",
628 SPLITFOURCC(sub_chunk_id));*/
629 stream_skip(qtmovie
->stream
, sub_chunk_len
- 8);
633 size_remaining
-= sub_chunk_len
;
638 static bool read_chunk_mdia(qtmovie_t
*qtmovie
, size_t chunk_len
)
640 size_t size_remaining
= chunk_len
- 8;
642 while (size_remaining
)
644 size_t sub_chunk_len
;
645 fourcc_t sub_chunk_id
;
647 sub_chunk_len
= stream_read_uint32(qtmovie
->stream
);
648 if (sub_chunk_len
<= 1 || sub_chunk_len
> size_remaining
)
650 DEBUGF("strange size (%lu) for chunk inside mdia\n",
651 (unsigned long)sub_chunk_len
);
655 sub_chunk_id
= stream_read_uint32(qtmovie
->stream
);
657 switch (sub_chunk_id
)
659 case MAKEFOURCC('m','i','n','f'):
660 if (!read_chunk_minf(qtmovie
, sub_chunk_len
)) {
665 /*DEBUGF("(mdia) unknown chunk id: %c%c%c%c\n",
666 SPLITFOURCC(sub_chunk_id));*/
667 stream_skip(qtmovie
->stream
, sub_chunk_len
- 8);
671 size_remaining
-= sub_chunk_len
;
676 /* 'trak' - a movie track - contains other atoms */
677 static bool read_chunk_trak(qtmovie_t
*qtmovie
, size_t chunk_len
)
679 size_t size_remaining
= chunk_len
- 8;
681 while (size_remaining
)
683 size_t sub_chunk_len
;
684 fourcc_t sub_chunk_id
;
686 sub_chunk_len
= stream_read_uint32(qtmovie
->stream
);
687 if (sub_chunk_len
<= 1 || sub_chunk_len
> size_remaining
)
689 DEBUGF("strange size (%lu) for chunk inside trak\n",
690 (unsigned long)sub_chunk_len
);
694 sub_chunk_id
= stream_read_uint32(qtmovie
->stream
);
696 switch (sub_chunk_id
)
698 case MAKEFOURCC('m','d','i','a'):
699 if (!read_chunk_mdia(qtmovie
, sub_chunk_len
)) {
704 /*DEBUGF("(trak) unknown chunk id: %c%c%c%c\n",
705 SPLITFOURCC(sub_chunk_id));*/
706 stream_skip(qtmovie
->stream
, sub_chunk_len
- 8);
710 size_remaining
-= sub_chunk_len
;
715 /* 'moov' movie atom - contains other atoms */
716 static bool read_chunk_moov(qtmovie_t
*qtmovie
, size_t chunk_len
)
718 size_t size_remaining
= chunk_len
- 8;
720 while (size_remaining
)
722 size_t sub_chunk_len
;
723 fourcc_t sub_chunk_id
;
725 sub_chunk_len
= stream_read_uint32(qtmovie
->stream
);
726 if (sub_chunk_len
<= 1 || sub_chunk_len
> size_remaining
)
728 DEBUGF("strange size (%lu) for chunk inside moov\n",
729 (unsigned long)sub_chunk_len
);
733 sub_chunk_id
= stream_read_uint32(qtmovie
->stream
);
735 switch (sub_chunk_id
)
737 case MAKEFOURCC('t','r','a','k'):
738 if (!read_chunk_trak(qtmovie
, sub_chunk_len
)) {
743 /*DEBUGF("(moov) unknown chunk id: %c%c%c%c\n",
744 SPLITFOURCC(sub_chunk_id));*/
745 stream_skip(qtmovie
->stream
, sub_chunk_len
- 8);
749 size_remaining
-= sub_chunk_len
;
754 static void read_chunk_mdat(qtmovie_t
*qtmovie
, size_t chunk_len
)
756 size_t size_remaining
= chunk_len
- 8;
758 qtmovie
->res
->mdat_len
= size_remaining
;
761 int qtmovie_read(stream_t
*file
, demux_res_t
*demux_res
)
765 /* construct the stream */
766 qtmovie
.stream
= file
;
767 qtmovie
.res
= demux_res
;
769 /* read the chunks */
775 chunk_len
= stream_read_uint32(qtmovie
.stream
);
776 if (stream_eof(qtmovie
.stream
))
778 if(qtmovie
.res
->mdat_offset
== 0 || qtmovie
.res
->format
== 0)
780 stream_seek(qtmovie
.stream
, qtmovie
.res
->mdat_offset
);
786 //DEBUGF("need 64bit support\n");
789 chunk_id
= stream_read_uint32(qtmovie
.stream
);
791 //DEBUGF("Found a chunk %c%c%c%c, length=%d\n",SPLITFOURCC(chunk_id),chunk_len);
794 case MAKEFOURCC('f','t','y','p'):
795 read_chunk_ftyp(&qtmovie
, chunk_len
);
797 case MAKEFOURCC('m','o','o','v'):
798 if (!read_chunk_moov(&qtmovie
, chunk_len
)) {
802 case MAKEFOURCC('m','d','a','t'):
803 /* There can be empty mdats before the real one. If so, skip them */
806 read_chunk_mdat(&qtmovie
, chunk_len
);
807 qtmovie
.res
->mdat_offset
=stream_tell(qtmovie
.stream
);
808 /* If we've already seen the format, assume there's nothing
809 interesting after the mdat chunk (the file is "streamable").
810 This avoids having to seek, which might cause rebuffering. */
811 if(qtmovie
.res
->format
> 0)
813 stream_skip(qtmovie
.stream
, chunk_len
- 8);
816 /* these following atoms can be skipped !!!! */
817 case MAKEFOURCC('f','r','e','e'):
818 stream_skip(qtmovie
.stream
, chunk_len
- 8);
821 //DEBUGF("(top) unknown chunk id: %c%c%c%c\n",SPLITFOURCC(chunk_id));