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.
40 #if defined(DEBUG) || defined(SIMULATOR)
44 #define DEBUGF qtmovie->stream->ci->debugf
57 static void read_chunk_ftyp(qtmovie_t
*qtmovie
, size_t chunk_len
)
61 size_t size_remaining
= chunk_len
- 8;
63 type
= stream_read_uint32(qtmovie
->stream
);
65 if ((type
!= MAKEFOURCC('M','4','A',' ')) &&
66 (type
!= MAKEFOURCC('M','4','B',' ')) &&
67 (type
!= MAKEFOURCC('m','p','4','2')) &&
68 (type
!= MAKEFOURCC('3','g','p','6')) &&
69 (type
!= MAKEFOURCC('q','t',' ',' ')))
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;
177 stream_read_uint8(qtmovie
->stream
);
180 stream_read_uint8(qtmovie
->stream
);
181 stream_read_uint8(qtmovie
->stream
);
182 stream_read_uint8(qtmovie
->stream
);
185 numentries
= stream_read_uint32(qtmovie
->stream
);
190 DEBUGF("only expecting one entry in sample description atom!\n");
194 for (i
= 0; i
< numentries
; i
++)
198 uint32_t entry_remaining
;
200 entry_size
= stream_read_uint32(qtmovie
->stream
);
201 qtmovie
->res
->format
= stream_read_uint32(qtmovie
->stream
);
202 DEBUGF("format: %c%c%c%c\n",SPLITFOURCC(qtmovie
->res
->format
));
203 entry_remaining
= entry_size
;
204 entry_remaining
-= 8;
208 /* reserved + data reference index + sound version + reserved */
209 stream_skip(qtmovie
->stream
, 6 + 2 + 2 + 6);
210 entry_remaining
-= 6 + 2 + 2 + 6;
212 qtmovie
->res
->num_channels
= stream_read_uint16(qtmovie
->stream
);
213 qtmovie
->res
->sound_sample_size
= stream_read_uint16(qtmovie
->stream
);
214 entry_remaining
-= 4;
217 stream_skip(qtmovie
->stream
, 2);
218 qtmovie
->res
->sound_sample_rate
= stream_read_uint32(qtmovie
->stream
);
220 stream_skip(qtmovie
->stream
, 2);
221 entry_remaining
-= 8;
223 /* remaining is codec data */
225 if ((qtmovie
->res
->format
==MAKEFOURCC('a','l','a','c'))) {
226 if (qtmovie
->stream
->ci
->id3
->codectype
!=AFMT_MP4_ALAC
) {
230 /* 12 = audio format atom, 8 = padding */
231 qtmovie
->res
->codecdata_len
= entry_remaining
+ 12 + 8;
232 if (qtmovie
->res
->codecdata_len
> MAX_CODECDATA_SIZE
)
234 DEBUGF("codecdata too large (%d) in stsd\n",
235 (int)qtmovie
->res
->codecdata_len
);
238 memset(qtmovie
->res
->codecdata
, 0, qtmovie
->res
->codecdata_len
);
239 /* audio format atom */
241 /* The ALAC decoder skips these bytes, so there is no need to store them,
242 and this code isn't endian/alignment safe */
243 ((unsigned int*)qtmovie
->res
->codecdata
)[0] = 0x0c000000;
244 ((unsigned int*)qtmovie
->res
->codecdata
)[1] = MAKEFOURCC('a','m','r','f');
245 ((unsigned int*)qtmovie
->res
->codecdata
)[2] = MAKEFOURCC('c','a','l','a');
248 stream_read(qtmovie
->stream
,
250 ((char*)qtmovie
->res
->codecdata
) + 12);
251 entry_remaining
-= entry_remaining
;
254 stream_skip(qtmovie
->stream
, entry_remaining
);
256 } else if (qtmovie
->res
->format
==MAKEFOURCC('m','p','4','a')) {
257 if (qtmovie
->stream
->ci
->id3
->codectype
!=AFMT_MP4_AAC
) {
261 size_t sub_chunk_len
;
262 fourcc_t sub_chunk_id
;
264 sub_chunk_len
= stream_read_uint32(qtmovie
->stream
);
265 if (sub_chunk_len
<= 1 || sub_chunk_len
> entry_remaining
)
267 DEBUGF("strange size (%lu) for chunk inside mp4a\n",
268 (unsigned long)sub_chunk_len
);
272 sub_chunk_id
= stream_read_uint32(qtmovie
->stream
);
274 if (sub_chunk_id
!= MAKEFOURCC('e','s','d','s'))
276 DEBUGF("Expected esds chunk inside mp4a, found %c%c%c%c\n",SPLITFOURCC(sub_chunk_id
));
280 j
=qtmovie
->stream
->ci
->curpos
+sub_chunk_len
-8;
281 if (read_chunk_esds(qtmovie
,sub_chunk_len
)) {
282 if (j
!=qtmovie
->stream
->ci
->curpos
) {
283 DEBUGF("curpos=%ld, j=%d - Skipping %ld bytes\n",qtmovie
->stream
->ci
->curpos
,j
,j
-qtmovie
->stream
->ci
->curpos
);
284 stream_skip(qtmovie
->stream
,j
-qtmovie
->stream
->ci
->curpos
);
286 entry_remaining
-=sub_chunk_len
;
288 DEBUGF("Error reading esds\n");
292 DEBUGF("entry_remaining=%ld\n",(long)entry_remaining
);
293 stream_skip(qtmovie
->stream
,entry_remaining
);
296 DEBUGF("expecting 'alac' or 'mp4a' data format, got %c%c%c%c\n",
297 SPLITFOURCC(qtmovie
->res
->format
));
304 static bool read_chunk_stts(qtmovie_t
*qtmovie
, size_t chunk_len
)
308 size_t size_remaining
= chunk_len
- 8;
311 stream_read_uint8(qtmovie
->stream
);
314 stream_read_uint8(qtmovie
->stream
);
315 stream_read_uint8(qtmovie
->stream
);
316 stream_read_uint8(qtmovie
->stream
);
319 numentries
= stream_read_uint32(qtmovie
->stream
);
322 qtmovie
->res
->num_time_to_samples
= numentries
;
323 qtmovie
->res
->time_to_sample
= malloc(numentries
* sizeof(*qtmovie
->res
->time_to_sample
));
325 if (!qtmovie
->res
->time_to_sample
)
327 DEBUGF("stts too large\n");
331 for (i
= 0; i
< numentries
; i
++)
333 qtmovie
->res
->time_to_sample
[i
].sample_count
= stream_read_uint32(qtmovie
->stream
);
334 qtmovie
->res
->time_to_sample
[i
].sample_duration
= stream_read_uint32(qtmovie
->stream
);
340 DEBUGF("ehm, size remianing?\n");
341 stream_skip(qtmovie
->stream
, size_remaining
);
347 static bool read_chunk_stsz(qtmovie_t
*qtmovie
, size_t chunk_len
)
351 size_t size_remaining
= chunk_len
- 8;
354 stream_read_uint8(qtmovie
->stream
);
357 stream_read_uint8(qtmovie
->stream
);
358 stream_read_uint8(qtmovie
->stream
);
359 stream_read_uint8(qtmovie
->stream
);
362 /* default sample size */
363 if (stream_read_uint32(qtmovie
->stream
) != 0)
365 DEBUGF("i was expecting variable samples sizes\n");
366 stream_read_uint32(qtmovie
->stream
);
372 numentries
= stream_read_uint32(qtmovie
->stream
);
375 qtmovie
->res
->num_sample_byte_sizes
= numentries
;
376 qtmovie
->res
->sample_byte_size
= malloc(numentries
* sizeof(*qtmovie
->res
->sample_byte_size
));
378 if (!qtmovie
->res
->sample_byte_size
)
380 DEBUGF("stsz too large\n");
384 for (i
= 0; i
< numentries
; i
++)
386 uint32_t v
= stream_read_uint32(qtmovie
->stream
);
390 DEBUGF("stsz[%d] > 65 kB (%ld)\n", i
, (long)v
);
394 qtmovie
->res
->sample_byte_size
[i
] = v
;
400 DEBUGF("ehm, size remianing?\n");
401 stream_skip(qtmovie
->stream
, size_remaining
);
407 static bool read_chunk_stsc(qtmovie_t
*qtmovie
, size_t chunk_len
)
411 size_t size_remaining
= chunk_len
- 8;
413 /* version + flags */
414 stream_read_uint32(qtmovie
->stream
);
417 numentries
= stream_read_uint32(qtmovie
->stream
);
420 qtmovie
->res
->num_sample_to_chunks
= numentries
;
421 qtmovie
->res
->sample_to_chunk
= malloc(numentries
*
422 sizeof(*qtmovie
->res
->sample_to_chunk
));
424 if (!qtmovie
->res
->sample_to_chunk
)
426 DEBUGF("stsc too large\n");
430 for (i
= 0; i
< numentries
; i
++)
432 qtmovie
->res
->sample_to_chunk
[i
].first_chunk
=
433 stream_read_uint32(qtmovie
->stream
);
434 qtmovie
->res
->sample_to_chunk
[i
].num_samples
=
435 stream_read_uint32(qtmovie
->stream
);
436 stream_read_uint32(qtmovie
->stream
);
437 size_remaining
-= 12;
442 DEBUGF("ehm, size remianing?\n");
443 stream_skip(qtmovie
->stream
, size_remaining
);
449 static bool read_chunk_stco(qtmovie_t
*qtmovie
, size_t chunk_len
)
453 size_t size_remaining
= chunk_len
- 8;
455 /* version + flags */
456 stream_read_uint32(qtmovie
->stream
);
459 numentries
= stream_read_uint32(qtmovie
->stream
);
462 qtmovie
->res
->num_chunk_offsets
= numentries
;
463 qtmovie
->res
->chunk_offset
= malloc(numentries
*
464 sizeof(*qtmovie
->res
->chunk_offset
));
466 if (!qtmovie
->res
->chunk_offset
)
468 DEBUGF("stco too large\n");
472 for (i
= 0; i
< numentries
; i
++)
474 qtmovie
->res
->chunk_offset
[i
] = stream_read_uint32(qtmovie
->stream
);
480 DEBUGF("ehm, size remianing?\n");
481 stream_skip(qtmovie
->stream
, size_remaining
);
487 static bool read_chunk_stbl(qtmovie_t
*qtmovie
, size_t chunk_len
)
489 size_t size_remaining
= chunk_len
- 8;
491 while (size_remaining
)
493 size_t sub_chunk_len
;
494 fourcc_t sub_chunk_id
;
496 sub_chunk_len
= stream_read_uint32(qtmovie
->stream
);
497 if (sub_chunk_len
<= 1 || sub_chunk_len
> size_remaining
)
499 DEBUGF("strange size (%lu) for chunk inside stbl\n",
500 (unsigned long)sub_chunk_len
);
504 sub_chunk_id
= stream_read_uint32(qtmovie
->stream
);
506 switch (sub_chunk_id
)
508 case MAKEFOURCC('s','t','s','d'):
509 if (!read_chunk_stsd(qtmovie
, sub_chunk_len
)) {
513 case MAKEFOURCC('s','t','t','s'):
514 if (!read_chunk_stts(qtmovie
, sub_chunk_len
))
519 case MAKEFOURCC('s','t','s','z'):
520 if (!read_chunk_stsz(qtmovie
, sub_chunk_len
))
525 case MAKEFOURCC('s','t','s','c'):
526 if (!read_chunk_stsc(qtmovie
, sub_chunk_len
))
531 case MAKEFOURCC('s','t','c','o'):
532 if (!read_chunk_stco(qtmovie
, sub_chunk_len
))
538 /*DEBUGF("(stbl) unknown chunk id: %c%c%c%c\n",
539 SPLITFOURCC(sub_chunk_id));*/
540 stream_skip(qtmovie
->stream
, sub_chunk_len
- 8);
543 size_remaining
-= sub_chunk_len
;
548 static bool read_chunk_minf(qtmovie_t
*qtmovie
, size_t chunk_len
)
550 size_t size_remaining
= chunk_len
- 8;
553 /* Check for smhd, only kind of minf we care about */
555 if ((i
= stream_read_uint32(qtmovie
->stream
)) != 16)
557 DEBUGF("unexpected size in media info: %ld\n", (long)i
);
558 stream_skip(qtmovie
->stream
, size_remaining
-4);
562 if (stream_read_uint32(qtmovie
->stream
) != MAKEFOURCC('s','m','h','d'))
564 DEBUGF("not a sound header! can't handle this.\n");
568 /* now skip the rest of the atom */
569 stream_skip(qtmovie
->stream
, 16 - 8);
570 size_remaining
-= 16;
572 while (size_remaining
)
574 size_t sub_chunk_len
;
575 fourcc_t sub_chunk_id
;
577 sub_chunk_len
= stream_read_uint32(qtmovie
->stream
);
579 if (sub_chunk_len
<= 1 || sub_chunk_len
> size_remaining
)
581 DEBUGF("strange size (%lu) for chunk inside minf\n",
582 (unsigned long)sub_chunk_len
);
586 sub_chunk_id
= stream_read_uint32(qtmovie
->stream
);
588 switch (sub_chunk_id
)
590 case MAKEFOURCC('s','t','b','l'):
591 if (!read_chunk_stbl(qtmovie
, sub_chunk_len
)) {
596 /*DEBUGF("(minf) unknown chunk id: %c%c%c%c\n",
597 SPLITFOURCC(sub_chunk_id));*/
598 stream_skip(qtmovie
->stream
, sub_chunk_len
- 8);
602 size_remaining
-= sub_chunk_len
;
607 static bool read_chunk_mdia(qtmovie_t
*qtmovie
, size_t chunk_len
)
609 size_t size_remaining
= chunk_len
- 8;
611 while (size_remaining
)
613 size_t sub_chunk_len
;
614 fourcc_t sub_chunk_id
;
616 sub_chunk_len
= stream_read_uint32(qtmovie
->stream
);
617 if (sub_chunk_len
<= 1 || sub_chunk_len
> size_remaining
)
619 DEBUGF("strange size (%lu) for chunk inside mdia\n",
620 (unsigned long)sub_chunk_len
);
624 sub_chunk_id
= stream_read_uint32(qtmovie
->stream
);
626 switch (sub_chunk_id
)
628 case MAKEFOURCC('m','i','n','f'):
629 if (!read_chunk_minf(qtmovie
, sub_chunk_len
)) {
634 /*DEBUGF("(mdia) unknown chunk id: %c%c%c%c\n",
635 SPLITFOURCC(sub_chunk_id));*/
636 stream_skip(qtmovie
->stream
, sub_chunk_len
- 8);
640 size_remaining
-= sub_chunk_len
;
645 /* 'trak' - a movie track - contains other atoms */
646 static bool read_chunk_trak(qtmovie_t
*qtmovie
, size_t chunk_len
)
648 size_t size_remaining
= chunk_len
- 8;
650 while (size_remaining
)
652 size_t sub_chunk_len
;
653 fourcc_t sub_chunk_id
;
655 sub_chunk_len
= stream_read_uint32(qtmovie
->stream
);
656 if (sub_chunk_len
<= 1 || sub_chunk_len
> size_remaining
)
658 DEBUGF("strange size (%lu) for chunk inside trak\n",
659 (unsigned long)sub_chunk_len
);
663 sub_chunk_id
= stream_read_uint32(qtmovie
->stream
);
665 switch (sub_chunk_id
)
667 case MAKEFOURCC('m','d','i','a'):
668 if (!read_chunk_mdia(qtmovie
, sub_chunk_len
)) {
673 /*DEBUGF("(trak) unknown chunk id: %c%c%c%c\n",
674 SPLITFOURCC(sub_chunk_id));*/
675 stream_skip(qtmovie
->stream
, sub_chunk_len
- 8);
679 size_remaining
-= sub_chunk_len
;
684 /* 'moov' movie atom - contains other atoms */
685 static bool read_chunk_moov(qtmovie_t
*qtmovie
, size_t chunk_len
)
687 size_t size_remaining
= chunk_len
- 8;
689 while (size_remaining
)
691 size_t sub_chunk_len
;
692 fourcc_t sub_chunk_id
;
694 sub_chunk_len
= stream_read_uint32(qtmovie
->stream
);
695 if (sub_chunk_len
<= 1 || sub_chunk_len
> size_remaining
)
697 DEBUGF("strange size (%lu) for chunk inside moov\n",
698 (unsigned long)sub_chunk_len
);
702 sub_chunk_id
= stream_read_uint32(qtmovie
->stream
);
704 switch (sub_chunk_id
)
706 case MAKEFOURCC('t','r','a','k'):
707 if (!read_chunk_trak(qtmovie
, sub_chunk_len
)) {
712 /*DEBUGF("(moov) unknown chunk id: %c%c%c%c\n",
713 SPLITFOURCC(sub_chunk_id));*/
714 stream_skip(qtmovie
->stream
, sub_chunk_len
- 8);
718 size_remaining
-= sub_chunk_len
;
723 static void read_chunk_mdat(qtmovie_t
*qtmovie
, size_t chunk_len
)
725 size_t size_remaining
= chunk_len
- 8;
727 qtmovie
->res
->mdat_len
= size_remaining
;
730 int qtmovie_read(stream_t
*file
, demux_res_t
*demux_res
)
734 /* construct the stream */
735 qtmovie
.stream
= file
;
736 qtmovie
.res
= demux_res
;
738 /* read the chunks */
744 chunk_len
= stream_read_uint32(qtmovie
.stream
);
745 if (stream_eof(qtmovie
.stream
))
752 //DEBUGF("need 64bit support\n");
755 chunk_id
= stream_read_uint32(qtmovie
.stream
);
757 //DEBUGF("Found a chunk %c%c%c%c, length=%d\n",SPLITFOURCC(chunk_id),chunk_len);
760 case MAKEFOURCC('f','t','y','p'):
761 read_chunk_ftyp(&qtmovie
, chunk_len
);
763 case MAKEFOURCC('m','o','o','v'):
764 if (!read_chunk_moov(&qtmovie
, chunk_len
)) {
768 /* once we hit mdat we stop reading and return.
769 * this is on the assumption that there is no furhter interesting
770 * stuff in the stream. if there is stuff will fail (:()).
771 * But we need the read pointer to be at the mdat stuff
772 * for the decoder. And we don't want to rely on fseek/ftell,
773 * as they may not always be avilable */
774 case MAKEFOURCC('m','d','a','t'):
775 read_chunk_mdat(&qtmovie
, chunk_len
);
776 /* Keep track of start of stream in file - used for seeking */
777 qtmovie
.res
->mdat_offset
=stream_tell(qtmovie
.stream
);
778 /* There can be empty mdats before the real one. If so, skip them */
779 if (qtmovie
.res
->mdat_len
> 0) {
784 /* these following atoms can be skipped !!!! */
785 case MAKEFOURCC('f','r','e','e'):
786 stream_skip(qtmovie
.stream
, chunk_len
- 8);
789 //DEBUGF("(top) unknown chunk id: %c%c%c%c\n",SPLITFOURCC(chunk_id));