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','B',' ')) &&
65 (type
!= MAKEFOURCC('m','p','4','2')) &&
66 (type
!= MAKEFOURCC('3','g','p','6')) &&
67 (type
!= MAKEFOURCC('q','t',' ',' ')) &&
68 (type
!= MAKEFOURCC('i','s','o','m')))
70 DEBUGF("not M4A file\n");
73 minor_ver
= stream_read_uint32(qtmovie
->stream
);
76 /* compatible brands */
77 while (size_remaining
)
80 /*fourcc_t cbrand =*/ stream_read_uint32(qtmovie
->stream
);
85 static uint32_t mp4ff_read_mp4_descr_length(stream_t
* stream
)
93 b
= stream_read_uint8(stream
);
95 length
= (length
<< 7) | (b
& 0x7F);
96 } while ((b
& 0x80) && numBytes
< 4);
101 /* The following function is based on mp4ff */
102 static bool read_chunk_esds(qtmovie_t
*qtmovie
, size_t chunk_len
)
111 /* version and flags */
112 temp
=stream_read_uint32(qtmovie
->stream
);
114 /* get and verify ES_DescrTag */
115 tag
= stream_read_uint8(qtmovie
->stream
);
119 if (mp4ff_read_mp4_descr_length(qtmovie
->stream
) < 5 + 15)
124 stream_skip(qtmovie
->stream
,3);
127 stream_skip(qtmovie
->stream
,2);
130 /* get and verify DecoderConfigDescrTab */
131 if (stream_read_uint8(qtmovie
->stream
) != 0x04)
137 temp
= mp4ff_read_mp4_descr_length(qtmovie
->stream
);
138 if (temp
< 13) return false;
140 audioType
= stream_read_uint8(qtmovie
->stream
);
141 temp
=stream_read_int32(qtmovie
->stream
);//0x15000414 ????
142 maxBitrate
= stream_read_int32(qtmovie
->stream
);
143 avgBitrate
= stream_read_int32(qtmovie
->stream
);
144 DEBUGF("audioType=%d, maxBitrate=%ld, avgBitrate=%ld\n",audioType
,
145 (long)maxBitrate
,(long)avgBitrate
);
147 /* get and verify DecSpecificInfoTag */
148 if (stream_read_uint8(qtmovie
->stream
) != 0x05)
154 qtmovie
->res
->codecdata_len
= mp4ff_read_mp4_descr_length(qtmovie
->stream
);
155 if (qtmovie
->res
->codecdata_len
> MAX_CODECDATA_SIZE
)
157 DEBUGF("codecdata too large (%d) in esds\n",
158 (int)qtmovie
->res
->codecdata_len
);
162 stream_read(qtmovie
->stream
, qtmovie
->res
->codecdata_len
, qtmovie
->res
->codecdata
);
164 /* will skip the remainder of the atom */
168 static bool read_chunk_stsd(qtmovie_t
*qtmovie
, size_t chunk_len
)
173 size_t size_remaining
= chunk_len
- 8;
176 stream_read_uint8(qtmovie
->stream
);
179 stream_read_uint8(qtmovie
->stream
);
180 stream_read_uint8(qtmovie
->stream
);
181 stream_read_uint8(qtmovie
->stream
);
184 numentries
= stream_read_uint32(qtmovie
->stream
);
189 DEBUGF("only expecting one entry in sample description atom!\n");
193 for (i
= 0; i
< numentries
; i
++)
197 uint32_t entry_remaining
;
199 entry_size
= stream_read_uint32(qtmovie
->stream
);
200 qtmovie
->res
->format
= stream_read_uint32(qtmovie
->stream
);
201 DEBUGF("format: %c%c%c%c\n",SPLITFOURCC(qtmovie
->res
->format
));
202 entry_remaining
= entry_size
;
203 entry_remaining
-= 8;
207 /* reserved + data reference index + sound version + reserved */
208 stream_skip(qtmovie
->stream
, 6 + 2 + 2 + 6);
209 entry_remaining
-= 6 + 2 + 2 + 6;
211 qtmovie
->res
->num_channels
= stream_read_uint16(qtmovie
->stream
);
212 qtmovie
->res
->sound_sample_size
= stream_read_uint16(qtmovie
->stream
);
213 entry_remaining
-= 4;
216 stream_skip(qtmovie
->stream
, 2);
217 qtmovie
->res
->sound_sample_rate
= stream_read_uint32(qtmovie
->stream
);
219 stream_skip(qtmovie
->stream
, 2);
220 entry_remaining
-= 8;
222 /* remaining is codec data */
224 if ((qtmovie
->res
->format
==MAKEFOURCC('a','l','a','c'))) {
225 if (qtmovie
->stream
->ci
->id3
->codectype
!=AFMT_MP4_ALAC
) {
229 /* 12 = audio format atom, 8 = padding */
230 qtmovie
->res
->codecdata_len
= entry_remaining
+ 12 + 8;
231 if (qtmovie
->res
->codecdata_len
> MAX_CODECDATA_SIZE
)
233 DEBUGF("codecdata too large (%d) in stsd\n",
234 (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
))
747 if(qtmovie
.res
->mdat_offset
== 0 || qtmovie
.res
->format
== 0)
749 stream_seek(qtmovie
.stream
, qtmovie
.res
->mdat_offset
);
755 //DEBUGF("need 64bit support\n");
758 chunk_id
= stream_read_uint32(qtmovie
.stream
);
760 //DEBUGF("Found a chunk %c%c%c%c, length=%d\n",SPLITFOURCC(chunk_id),chunk_len);
763 case MAKEFOURCC('f','t','y','p'):
764 read_chunk_ftyp(&qtmovie
, chunk_len
);
766 case MAKEFOURCC('m','o','o','v'):
767 if (!read_chunk_moov(&qtmovie
, chunk_len
)) {
771 case MAKEFOURCC('m','d','a','t'):
772 /* There can be empty mdats before the real one. If so, skip them */
775 read_chunk_mdat(&qtmovie
, chunk_len
);
776 qtmovie
.res
->mdat_offset
=stream_tell(qtmovie
.stream
);
777 /* If we've already seen the format, assume there's nothing
778 interesting after the mdat chunk (the file is "streamable").
779 This avoids having to seek, which might cause rebuffering. */
780 if(qtmovie
.res
->format
> 0)
782 stream_skip(qtmovie
.stream
, chunk_len
- 8);
785 /* these following atoms can be skipped !!!! */
786 case MAKEFOURCC('f','r','e','e'):
787 stream_skip(qtmovie
.stream
, chunk_len
- 8);
790 //DEBUGF("(top) unknown chunk id: %c%c%c%c\n",SPLITFOURCC(chunk_id));