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
)
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
475 old_frame
= qtmovie
->res
->sample_to_chunk
[0].num_samples
;
476 old_first
= qtmovie
->res
->sample_to_chunk
[0].first_chunk
;
477 for (k
= 1; k
< numentries
; ++k
)
479 for (; i
< qtmovie
->res
->num_sample_to_chunks
; ++i
)
481 old_frame
= qtmovie
->res
->sample_to_chunk
[i
-1].num_samples
;
482 old_first
= qtmovie
->res
->sample_to_chunk
[i
-1].first_chunk
;
483 new_first
= qtmovie
->res
->sample_to_chunk
[i
].first_chunk
;
485 if (qtmovie
->res
->sample_to_chunk
[i
].first_chunk
> k
)
488 frame
+= (new_first
- old_first
) * old_frame
;
490 frame
+= (k
- old_first
) * old_frame
;
492 qtmovie
->res
->lookup_table
[idx
].sample
= frame
;
493 qtmovie
->res
->lookup_table
[idx
].offset
= offset
;
496 frame
-= (k
- old_first
) * old_frame
;
498 offset
= stream_read_uint32(qtmovie
->stream
);
504 DEBUGF("ehm, size remianing?\n");
505 stream_skip(qtmovie
->stream
, size_remaining
);
511 static bool read_chunk_stbl(qtmovie_t
*qtmovie
, size_t chunk_len
)
513 size_t size_remaining
= chunk_len
- 8;
515 while (size_remaining
)
517 size_t sub_chunk_len
;
518 fourcc_t sub_chunk_id
;
520 sub_chunk_len
= stream_read_uint32(qtmovie
->stream
);
521 if (sub_chunk_len
<= 1 || sub_chunk_len
> size_remaining
)
523 DEBUGF("strange size (%lu) for chunk inside stbl\n",
524 (unsigned long)sub_chunk_len
);
528 sub_chunk_id
= stream_read_uint32(qtmovie
->stream
);
530 switch (sub_chunk_id
)
532 case MAKEFOURCC('s','t','s','d'):
533 if (!read_chunk_stsd(qtmovie
, sub_chunk_len
)) {
537 case MAKEFOURCC('s','t','t','s'):
538 if (!read_chunk_stts(qtmovie
, sub_chunk_len
))
543 case MAKEFOURCC('s','t','s','z'):
544 if (!read_chunk_stsz(qtmovie
, sub_chunk_len
))
549 case MAKEFOURCC('s','t','s','c'):
550 if (!read_chunk_stsc(qtmovie
, sub_chunk_len
))
555 case MAKEFOURCC('s','t','c','o'):
556 if (!read_chunk_stco(qtmovie
, sub_chunk_len
))
562 /*DEBUGF("(stbl) unknown chunk id: %c%c%c%c\n",
563 SPLITFOURCC(sub_chunk_id));*/
564 stream_skip(qtmovie
->stream
, sub_chunk_len
- 8);
567 size_remaining
-= sub_chunk_len
;
572 static bool read_chunk_minf(qtmovie_t
*qtmovie
, size_t chunk_len
)
574 size_t size_remaining
= chunk_len
- 8;
577 /* Check for smhd, only kind of minf we care about */
579 if ((i
= stream_read_uint32(qtmovie
->stream
)) != 16)
581 DEBUGF("unexpected size in media info: %ld\n", (long)i
);
582 stream_skip(qtmovie
->stream
, size_remaining
-4);
586 if (stream_read_uint32(qtmovie
->stream
) != MAKEFOURCC('s','m','h','d'))
588 DEBUGF("not a sound header! can't handle this.\n");
592 /* now skip the rest of the atom */
593 stream_skip(qtmovie
->stream
, 16 - 8);
594 size_remaining
-= 16;
596 while (size_remaining
)
598 size_t sub_chunk_len
;
599 fourcc_t sub_chunk_id
;
601 sub_chunk_len
= stream_read_uint32(qtmovie
->stream
);
603 if (sub_chunk_len
<= 1 || sub_chunk_len
> size_remaining
)
605 DEBUGF("strange size (%lu) for chunk inside minf\n",
606 (unsigned long)sub_chunk_len
);
610 sub_chunk_id
= stream_read_uint32(qtmovie
->stream
);
612 switch (sub_chunk_id
)
614 case MAKEFOURCC('s','t','b','l'):
615 if (!read_chunk_stbl(qtmovie
, sub_chunk_len
)) {
620 /*DEBUGF("(minf) unknown chunk id: %c%c%c%c\n",
621 SPLITFOURCC(sub_chunk_id));*/
622 stream_skip(qtmovie
->stream
, sub_chunk_len
- 8);
626 size_remaining
-= sub_chunk_len
;
631 static bool read_chunk_mdia(qtmovie_t
*qtmovie
, size_t chunk_len
)
633 size_t size_remaining
= chunk_len
- 8;
635 while (size_remaining
)
637 size_t sub_chunk_len
;
638 fourcc_t sub_chunk_id
;
640 sub_chunk_len
= stream_read_uint32(qtmovie
->stream
);
641 if (sub_chunk_len
<= 1 || sub_chunk_len
> size_remaining
)
643 DEBUGF("strange size (%lu) for chunk inside mdia\n",
644 (unsigned long)sub_chunk_len
);
648 sub_chunk_id
= stream_read_uint32(qtmovie
->stream
);
650 switch (sub_chunk_id
)
652 case MAKEFOURCC('m','i','n','f'):
653 if (!read_chunk_minf(qtmovie
, sub_chunk_len
)) {
658 /*DEBUGF("(mdia) unknown chunk id: %c%c%c%c\n",
659 SPLITFOURCC(sub_chunk_id));*/
660 stream_skip(qtmovie
->stream
, sub_chunk_len
- 8);
664 size_remaining
-= sub_chunk_len
;
669 /* 'trak' - a movie track - contains other atoms */
670 static bool read_chunk_trak(qtmovie_t
*qtmovie
, size_t chunk_len
)
672 size_t size_remaining
= chunk_len
- 8;
674 while (size_remaining
)
676 size_t sub_chunk_len
;
677 fourcc_t sub_chunk_id
;
679 sub_chunk_len
= stream_read_uint32(qtmovie
->stream
);
680 if (sub_chunk_len
<= 1 || sub_chunk_len
> size_remaining
)
682 DEBUGF("strange size (%lu) for chunk inside trak\n",
683 (unsigned long)sub_chunk_len
);
687 sub_chunk_id
= stream_read_uint32(qtmovie
->stream
);
689 switch (sub_chunk_id
)
691 case MAKEFOURCC('m','d','i','a'):
692 if (!read_chunk_mdia(qtmovie
, sub_chunk_len
)) {
697 /*DEBUGF("(trak) unknown chunk id: %c%c%c%c\n",
698 SPLITFOURCC(sub_chunk_id));*/
699 stream_skip(qtmovie
->stream
, sub_chunk_len
- 8);
703 size_remaining
-= sub_chunk_len
;
708 /* 'moov' movie atom - contains other atoms */
709 static bool read_chunk_moov(qtmovie_t
*qtmovie
, size_t chunk_len
)
711 size_t size_remaining
= chunk_len
- 8;
713 while (size_remaining
)
715 size_t sub_chunk_len
;
716 fourcc_t sub_chunk_id
;
718 sub_chunk_len
= stream_read_uint32(qtmovie
->stream
);
719 if (sub_chunk_len
<= 1 || sub_chunk_len
> size_remaining
)
721 DEBUGF("strange size (%lu) for chunk inside moov\n",
722 (unsigned long)sub_chunk_len
);
726 sub_chunk_id
= stream_read_uint32(qtmovie
->stream
);
728 switch (sub_chunk_id
)
730 case MAKEFOURCC('t','r','a','k'):
731 if (!read_chunk_trak(qtmovie
, sub_chunk_len
)) {
736 /*DEBUGF("(moov) unknown chunk id: %c%c%c%c\n",
737 SPLITFOURCC(sub_chunk_id));*/
738 stream_skip(qtmovie
->stream
, sub_chunk_len
- 8);
742 size_remaining
-= sub_chunk_len
;
747 static void read_chunk_mdat(qtmovie_t
*qtmovie
, size_t chunk_len
)
749 size_t size_remaining
= chunk_len
- 8;
751 qtmovie
->res
->mdat_len
= size_remaining
;
754 int qtmovie_read(stream_t
*file
, demux_res_t
*demux_res
)
758 /* construct the stream */
759 qtmovie
.stream
= file
;
760 qtmovie
.res
= demux_res
;
762 /* read the chunks */
768 chunk_len
= stream_read_uint32(qtmovie
.stream
);
769 if (stream_eof(qtmovie
.stream
))
771 if(qtmovie
.res
->mdat_offset
== 0 || qtmovie
.res
->format
== 0)
773 stream_seek(qtmovie
.stream
, qtmovie
.res
->mdat_offset
);
779 //DEBUGF("need 64bit support\n");
782 chunk_id
= stream_read_uint32(qtmovie
.stream
);
784 //DEBUGF("Found a chunk %c%c%c%c, length=%d\n",SPLITFOURCC(chunk_id),chunk_len);
787 case MAKEFOURCC('f','t','y','p'):
788 read_chunk_ftyp(&qtmovie
, chunk_len
);
790 case MAKEFOURCC('m','o','o','v'):
791 if (!read_chunk_moov(&qtmovie
, chunk_len
)) {
795 case MAKEFOURCC('m','d','a','t'):
796 /* There can be empty mdats before the real one. If so, skip them */
799 read_chunk_mdat(&qtmovie
, chunk_len
);
800 qtmovie
.res
->mdat_offset
=stream_tell(qtmovie
.stream
);
801 /* If we've already seen the format, assume there's nothing
802 interesting after the mdat chunk (the file is "streamable").
803 This avoids having to seek, which might cause rebuffering. */
804 if(qtmovie
.res
->format
> 0)
806 stream_skip(qtmovie
.stream
, chunk_len
- 8);
809 /* these following atoms can be skipped !!!! */
810 case MAKEFOURCC('f','r','e','e'):
811 stream_skip(qtmovie
.stream
, chunk_len
- 8);
814 //DEBUGF("(top) unknown chunk id: %c%c%c%c\n",SPLITFOURCC(chunk_id));