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;
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
);
239 memset(qtmovie
->res
->codecdata
, 0, qtmovie
->res
->codecdata_len
);
240 /* audio format atom */
242 /* The ALAC decoder skips these bytes, so there is no need to store them,
243 and this code isn't endian/alignment safe */
244 ((unsigned int*)qtmovie
->res
->codecdata
)[0] = 0x0c000000;
245 ((unsigned int*)qtmovie
->res
->codecdata
)[1] = MAKEFOURCC('a','m','r','f');
246 ((unsigned int*)qtmovie
->res
->codecdata
)[2] = MAKEFOURCC('c','a','l','a');
249 stream_read(qtmovie
->stream
,
251 ((char*)qtmovie
->res
->codecdata
) + 12);
252 entry_remaining
-= entry_remaining
;
255 stream_skip(qtmovie
->stream
, entry_remaining
);
257 } else if (qtmovie
->res
->format
==MAKEFOURCC('m','p','4','a')) {
258 if (qtmovie
->stream
->ci
->id3
->codectype
!=AFMT_MP4_AAC
) {
262 size_t sub_chunk_len
;
263 fourcc_t sub_chunk_id
;
265 sub_chunk_len
= stream_read_uint32(qtmovie
->stream
);
266 if (sub_chunk_len
<= 1 || sub_chunk_len
> entry_remaining
)
268 DEBUGF("strange size (%lu) for chunk inside mp4a\n",
269 (unsigned long)sub_chunk_len
);
273 sub_chunk_id
= stream_read_uint32(qtmovie
->stream
);
275 if (sub_chunk_id
!= MAKEFOURCC('e','s','d','s'))
277 DEBUGF("Expected esds chunk inside mp4a, found %c%c%c%c\n",SPLITFOURCC(sub_chunk_id
));
281 j
=qtmovie
->stream
->ci
->curpos
+sub_chunk_len
-8;
282 if (read_chunk_esds(qtmovie
,sub_chunk_len
)) {
283 if (j
!=qtmovie
->stream
->ci
->curpos
) {
284 DEBUGF("curpos=%ld, j=%d - Skipping %ld bytes\n",qtmovie
->stream
->ci
->curpos
,j
,j
-qtmovie
->stream
->ci
->curpos
);
285 stream_skip(qtmovie
->stream
,j
-qtmovie
->stream
->ci
->curpos
);
287 entry_remaining
-=sub_chunk_len
;
289 DEBUGF("Error reading esds\n");
293 DEBUGF("entry_remaining=%ld\n",(long)entry_remaining
);
294 stream_skip(qtmovie
->stream
,entry_remaining
);
297 DEBUGF("expecting 'alac' or 'mp4a' data format, got %c%c%c%c\n",
298 SPLITFOURCC(qtmovie
->res
->format
));
305 static bool read_chunk_stts(qtmovie_t
*qtmovie
, size_t chunk_len
)
309 size_t size_remaining
= chunk_len
- 8;
312 stream_read_uint8(qtmovie
->stream
);
315 stream_read_uint8(qtmovie
->stream
);
316 stream_read_uint8(qtmovie
->stream
);
317 stream_read_uint8(qtmovie
->stream
);
320 numentries
= stream_read_uint32(qtmovie
->stream
);
323 qtmovie
->res
->num_time_to_samples
= numentries
;
324 qtmovie
->res
->time_to_sample
= malloc(numentries
* sizeof(*qtmovie
->res
->time_to_sample
));
326 if (!qtmovie
->res
->time_to_sample
)
328 DEBUGF("stts too large\n");
332 for (i
= 0; i
< numentries
; i
++)
334 qtmovie
->res
->time_to_sample
[i
].sample_count
= stream_read_uint32(qtmovie
->stream
);
335 qtmovie
->res
->time_to_sample
[i
].sample_duration
= stream_read_uint32(qtmovie
->stream
);
341 DEBUGF("ehm, size remianing?\n");
342 stream_skip(qtmovie
->stream
, size_remaining
);
348 static bool read_chunk_stsz(qtmovie_t
*qtmovie
, size_t chunk_len
)
352 size_t size_remaining
= chunk_len
- 8;
355 stream_read_uint8(qtmovie
->stream
);
358 stream_read_uint8(qtmovie
->stream
);
359 stream_read_uint8(qtmovie
->stream
);
360 stream_read_uint8(qtmovie
->stream
);
363 /* default sample size */
364 if (stream_read_uint32(qtmovie
->stream
) != 0)
366 DEBUGF("i was expecting variable samples sizes\n");
367 stream_read_uint32(qtmovie
->stream
);
373 numentries
= stream_read_uint32(qtmovie
->stream
);
376 qtmovie
->res
->num_sample_byte_sizes
= numentries
;
377 qtmovie
->res
->sample_byte_size
= malloc(numentries
* sizeof(*qtmovie
->res
->sample_byte_size
));
379 if (!qtmovie
->res
->sample_byte_size
)
381 DEBUGF("stsz too large\n");
385 for (i
= 0; i
< numentries
; i
++)
387 uint32_t v
= stream_read_uint32(qtmovie
->stream
);
391 DEBUGF("stsz[%d] > 65 kB (%ld)\n", i
, (long)v
);
395 qtmovie
->res
->sample_byte_size
[i
] = v
;
401 DEBUGF("ehm, size remianing?\n");
402 stream_skip(qtmovie
->stream
, size_remaining
);
408 static bool read_chunk_stsc(qtmovie_t
*qtmovie
, size_t chunk_len
)
412 size_t size_remaining
= chunk_len
- 8;
414 /* version + flags */
415 stream_read_uint32(qtmovie
->stream
);
418 numentries
= stream_read_uint32(qtmovie
->stream
);
421 qtmovie
->res
->num_sample_to_chunks
= numentries
;
422 qtmovie
->res
->sample_to_chunk
= malloc(numentries
*
423 sizeof(*qtmovie
->res
->sample_to_chunk
));
425 if (!qtmovie
->res
->sample_to_chunk
)
427 DEBUGF("stsc too large\n");
431 for (i
= 0; i
< numentries
; i
++)
433 qtmovie
->res
->sample_to_chunk
[i
].first_chunk
=
434 stream_read_uint32(qtmovie
->stream
);
435 qtmovie
->res
->sample_to_chunk
[i
].num_samples
=
436 stream_read_uint32(qtmovie
->stream
);
437 stream_read_uint32(qtmovie
->stream
);
438 size_remaining
-= 12;
443 DEBUGF("ehm, size remianing?\n");
444 stream_skip(qtmovie
->stream
, size_remaining
);
450 static bool read_chunk_stco(qtmovie_t
*qtmovie
, size_t chunk_len
)
454 size_t size_remaining
= chunk_len
- 8;
456 /* version + flags */
457 stream_read_uint32(qtmovie
->stream
);
460 numentries
= stream_read_uint32(qtmovie
->stream
);
463 qtmovie
->res
->num_chunk_offsets
= numentries
;
464 qtmovie
->res
->chunk_offset
= malloc(numentries
*
465 sizeof(*qtmovie
->res
->chunk_offset
));
467 if (!qtmovie
->res
->chunk_offset
)
469 DEBUGF("stco too large\n");
473 for (i
= 0; i
< numentries
; i
++)
475 qtmovie
->res
->chunk_offset
[i
] = stream_read_uint32(qtmovie
->stream
);
481 DEBUGF("ehm, size remianing?\n");
482 stream_skip(qtmovie
->stream
, size_remaining
);
488 static bool read_chunk_stbl(qtmovie_t
*qtmovie
, size_t chunk_len
)
490 size_t size_remaining
= chunk_len
- 8;
492 while (size_remaining
)
494 size_t sub_chunk_len
;
495 fourcc_t sub_chunk_id
;
497 sub_chunk_len
= stream_read_uint32(qtmovie
->stream
);
498 if (sub_chunk_len
<= 1 || sub_chunk_len
> size_remaining
)
500 DEBUGF("strange size (%lu) for chunk inside stbl\n",
501 (unsigned long)sub_chunk_len
);
505 sub_chunk_id
= stream_read_uint32(qtmovie
->stream
);
507 switch (sub_chunk_id
)
509 case MAKEFOURCC('s','t','s','d'):
510 if (!read_chunk_stsd(qtmovie
, sub_chunk_len
)) {
514 case MAKEFOURCC('s','t','t','s'):
515 if (!read_chunk_stts(qtmovie
, sub_chunk_len
))
520 case MAKEFOURCC('s','t','s','z'):
521 if (!read_chunk_stsz(qtmovie
, sub_chunk_len
))
526 case MAKEFOURCC('s','t','s','c'):
527 if (!read_chunk_stsc(qtmovie
, sub_chunk_len
))
532 case MAKEFOURCC('s','t','c','o'):
533 if (!read_chunk_stco(qtmovie
, sub_chunk_len
))
539 /*DEBUGF("(stbl) unknown chunk id: %c%c%c%c\n",
540 SPLITFOURCC(sub_chunk_id));*/
541 stream_skip(qtmovie
->stream
, sub_chunk_len
- 8);
544 size_remaining
-= sub_chunk_len
;
549 static bool read_chunk_minf(qtmovie_t
*qtmovie
, size_t chunk_len
)
551 size_t size_remaining
= chunk_len
- 8;
554 /* Check for smhd, only kind of minf we care about */
556 if ((i
= stream_read_uint32(qtmovie
->stream
)) != 16)
558 DEBUGF("unexpected size in media info: %ld\n", (long)i
);
559 stream_skip(qtmovie
->stream
, size_remaining
-4);
563 if (stream_read_uint32(qtmovie
->stream
) != MAKEFOURCC('s','m','h','d'))
565 DEBUGF("not a sound header! can't handle this.\n");
569 /* now skip the rest of the atom */
570 stream_skip(qtmovie
->stream
, 16 - 8);
571 size_remaining
-= 16;
573 while (size_remaining
)
575 size_t sub_chunk_len
;
576 fourcc_t sub_chunk_id
;
578 sub_chunk_len
= stream_read_uint32(qtmovie
->stream
);
580 if (sub_chunk_len
<= 1 || sub_chunk_len
> size_remaining
)
582 DEBUGF("strange size (%lu) for chunk inside minf\n",
583 (unsigned long)sub_chunk_len
);
587 sub_chunk_id
= stream_read_uint32(qtmovie
->stream
);
589 switch (sub_chunk_id
)
591 case MAKEFOURCC('s','t','b','l'):
592 if (!read_chunk_stbl(qtmovie
, sub_chunk_len
)) {
597 /*DEBUGF("(minf) unknown chunk id: %c%c%c%c\n",
598 SPLITFOURCC(sub_chunk_id));*/
599 stream_skip(qtmovie
->stream
, sub_chunk_len
- 8);
603 size_remaining
-= sub_chunk_len
;
608 static bool read_chunk_mdia(qtmovie_t
*qtmovie
, size_t chunk_len
)
610 size_t size_remaining
= chunk_len
- 8;
612 while (size_remaining
)
614 size_t sub_chunk_len
;
615 fourcc_t sub_chunk_id
;
617 sub_chunk_len
= stream_read_uint32(qtmovie
->stream
);
618 if (sub_chunk_len
<= 1 || sub_chunk_len
> size_remaining
)
620 DEBUGF("strange size (%lu) for chunk inside mdia\n",
621 (unsigned long)sub_chunk_len
);
625 sub_chunk_id
= stream_read_uint32(qtmovie
->stream
);
627 switch (sub_chunk_id
)
629 case MAKEFOURCC('m','i','n','f'):
630 if (!read_chunk_minf(qtmovie
, sub_chunk_len
)) {
635 /*DEBUGF("(mdia) unknown chunk id: %c%c%c%c\n",
636 SPLITFOURCC(sub_chunk_id));*/
637 stream_skip(qtmovie
->stream
, sub_chunk_len
- 8);
641 size_remaining
-= sub_chunk_len
;
646 /* 'trak' - a movie track - contains other atoms */
647 static bool read_chunk_trak(qtmovie_t
*qtmovie
, size_t chunk_len
)
649 size_t size_remaining
= chunk_len
- 8;
651 while (size_remaining
)
653 size_t sub_chunk_len
;
654 fourcc_t sub_chunk_id
;
656 sub_chunk_len
= stream_read_uint32(qtmovie
->stream
);
657 if (sub_chunk_len
<= 1 || sub_chunk_len
> size_remaining
)
659 DEBUGF("strange size (%lu) for chunk inside trak\n",
660 (unsigned long)sub_chunk_len
);
664 sub_chunk_id
= stream_read_uint32(qtmovie
->stream
);
666 switch (sub_chunk_id
)
668 case MAKEFOURCC('m','d','i','a'):
669 if (!read_chunk_mdia(qtmovie
, sub_chunk_len
)) {
674 /*DEBUGF("(trak) unknown chunk id: %c%c%c%c\n",
675 SPLITFOURCC(sub_chunk_id));*/
676 stream_skip(qtmovie
->stream
, sub_chunk_len
- 8);
680 size_remaining
-= sub_chunk_len
;
685 /* 'moov' movie atom - contains other atoms */
686 static bool read_chunk_moov(qtmovie_t
*qtmovie
, size_t chunk_len
)
688 size_t size_remaining
= chunk_len
- 8;
690 while (size_remaining
)
692 size_t sub_chunk_len
;
693 fourcc_t sub_chunk_id
;
695 sub_chunk_len
= stream_read_uint32(qtmovie
->stream
);
696 if (sub_chunk_len
<= 1 || sub_chunk_len
> size_remaining
)
698 DEBUGF("strange size (%lu) for chunk inside moov\n",
699 (unsigned long)sub_chunk_len
);
703 sub_chunk_id
= stream_read_uint32(qtmovie
->stream
);
705 switch (sub_chunk_id
)
707 case MAKEFOURCC('t','r','a','k'):
708 if (!read_chunk_trak(qtmovie
, sub_chunk_len
)) {
713 /*DEBUGF("(moov) unknown chunk id: %c%c%c%c\n",
714 SPLITFOURCC(sub_chunk_id));*/
715 stream_skip(qtmovie
->stream
, sub_chunk_len
- 8);
719 size_remaining
-= sub_chunk_len
;
724 static void read_chunk_mdat(qtmovie_t
*qtmovie
, size_t chunk_len
)
726 size_t size_remaining
= chunk_len
- 8;
728 qtmovie
->res
->mdat_len
= size_remaining
;
731 int qtmovie_read(stream_t
*file
, demux_res_t
*demux_res
)
735 /* construct the stream */
736 qtmovie
.stream
= file
;
737 qtmovie
.res
= demux_res
;
739 /* read the chunks */
745 chunk_len
= stream_read_uint32(qtmovie
.stream
);
746 if (stream_eof(qtmovie
.stream
))
748 if(qtmovie
.res
->mdat_offset
== 0 || qtmovie
.res
->format
== 0)
750 stream_seek(qtmovie
.stream
, qtmovie
.res
->mdat_offset
);
756 //DEBUGF("need 64bit support\n");
759 chunk_id
= stream_read_uint32(qtmovie
.stream
);
761 //DEBUGF("Found a chunk %c%c%c%c, length=%d\n",SPLITFOURCC(chunk_id),chunk_len);
764 case MAKEFOURCC('f','t','y','p'):
765 read_chunk_ftyp(&qtmovie
, chunk_len
);
767 case MAKEFOURCC('m','o','o','v'):
768 if (!read_chunk_moov(&qtmovie
, chunk_len
)) {
772 case MAKEFOURCC('m','d','a','t'):
773 /* There can be empty mdats before the real one. If so, skip them */
776 read_chunk_mdat(&qtmovie
, chunk_len
);
777 qtmovie
.res
->mdat_offset
=stream_tell(qtmovie
.stream
);
778 /* If we've already seen the format, assume there's nothing
779 interesting after the mdat chunk (the file is "streamable").
780 This avoids having to seek, which might cause rebuffering. */
781 if(qtmovie
.res
->format
> 0)
783 stream_skip(qtmovie
.stream
, chunk_len
- 8);
786 /* these following atoms can be skipped !!!! */
787 case MAKEFOURCC('f','r','e','e'):
788 stream_skip(qtmovie
.stream
, chunk_len
- 8);
791 //DEBUGF("(top) unknown chunk id: %c%c%c%c\n",SPLITFOURCC(chunk_id));