Minor speed optimization to m4a lookup table build up.
[kugel-rb.git] / apps / codecs / libm4a / demux.c
blobcd53b12fd0322973b6ab5bacfc338a24e6bbb557
1 /*
2 * ALAC (Apple Lossless Audio Codec) decoder
3 * Copyright (c) 2005 David Hammerton
4 * All rights reserved.
6 * This is the quicktime container demuxer.
8 * http://crazney.net/programs/itunes/alac.html
9 *
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.
32 #include <string.h>
33 #include <inttypes.h>
34 #include <stdlib.h>
36 #include "codeclib.h"
38 #include "m4a.h"
40 #undef DEBUGF
41 #if defined(DEBUG)
42 #define DEBUGF qtmovie->stream->ci->debugf
43 #else
44 #define DEBUGF(...)
45 #endif
47 typedef struct
49 stream_t *stream;
50 demux_res_t *res;
51 } qtmovie_t;
54 /* chunk handlers */
55 static void read_chunk_ftyp(qtmovie_t *qtmovie, size_t chunk_len)
57 fourcc_t type;
58 uint32_t minor_ver;
59 size_t size_remaining = chunk_len - 8;
61 type = stream_read_uint32(qtmovie->stream);
62 size_remaining-=4;
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");
72 return;
74 minor_ver = stream_read_uint32(qtmovie->stream);
75 size_remaining-=4;
77 /* compatible brands */
78 while (size_remaining)
80 /* unused */
81 /*fourcc_t cbrand =*/ stream_read_uint32(qtmovie->stream);
82 size_remaining-=4;
86 static uint32_t mp4ff_read_mp4_descr_length(stream_t* stream)
88 uint8_t b;
89 uint8_t numBytes = 0;
90 uint32_t length = 0;
94 b = stream_read_uint8(stream);
95 numBytes++;
96 length = (length << 7) | (b & 0x7F);
97 } while ((b & 0x80) && numBytes < 4);
99 return length;
102 /* The following function is based on mp4ff */
103 static bool read_chunk_esds(qtmovie_t *qtmovie, size_t chunk_len)
105 uint8_t tag;
106 uint32_t temp;
107 int audioType;
108 int32_t maxBitrate;
109 int32_t avgBitrate;
111 (void)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);
117 if (tag == 0x03)
119 /* read length */
120 if (mp4ff_read_mp4_descr_length(qtmovie->stream) < 5 + 15)
122 return false;
124 /* skip 3 bytes */
125 stream_skip(qtmovie->stream,3);
126 } else {
127 /* skip 2 bytes */
128 stream_skip(qtmovie->stream,2);
131 /* get and verify DecoderConfigDescrTab */
132 if (stream_read_uint8(qtmovie->stream) != 0x04)
134 return false;
137 /* read length */
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)
151 return false;
154 /* read length */
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);
160 return false;
163 stream_read(qtmovie->stream, qtmovie->res->codecdata_len, qtmovie->res->codecdata);
165 /* will skip the remainder of the atom */
166 return true;
169 static bool read_chunk_stsd(qtmovie_t *qtmovie, size_t chunk_len)
171 unsigned int i;
172 int j;
173 uint32_t numentries;
174 size_t size_remaining = chunk_len - 8;
175 bool got_codec_data = false;
177 /* version */
178 stream_read_uint8(qtmovie->stream);
179 size_remaining -= 1;
180 /* flags */
181 stream_read_uint8(qtmovie->stream);
182 stream_read_uint8(qtmovie->stream);
183 stream_read_uint8(qtmovie->stream);
184 size_remaining -= 3;
186 numentries = stream_read_uint32(qtmovie->stream);
187 size_remaining -= 4;
189 /* if (numentries != 1)
191 DEBUGF("only expecting one entry in sample description atom!\n");
192 return false;
193 } */
195 for (i = 0; i < numentries; i++)
197 uint32_t entry_size;
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;
207 /* sound info: */
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;
217 /* packet size */
218 stream_skip(qtmovie->stream, 2);
219 qtmovie->res->sound_sample_rate = stream_read_uint32(qtmovie->stream);
220 /* reserved size */
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) {
228 return false;
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);
237 return false;
240 memset(qtmovie->res->codecdata, 0, qtmovie->res->codecdata_len);
241 /* audio format atom */
242 #if 0
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');
248 #endif
250 stream_read(qtmovie->stream,
251 entry_remaining,
252 ((char*)qtmovie->res->codecdata) + 12);
253 entry_remaining -= entry_remaining;
254 got_codec_data = true;
256 if (entry_remaining)
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) {
262 return false;
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);
273 return false;
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));
281 return false;
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;
292 } else {
293 DEBUGF("Error reading esds\n");
294 return false;
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);
303 } else {
304 DEBUGF("expecting 'alac', 'mp4a' or 'free' data format, got %c%c%c%c\n",
305 SPLITFOURCC(qtmovie->res->format));
306 return false;
309 return got_codec_data;
312 static bool read_chunk_stts(qtmovie_t *qtmovie, size_t chunk_len)
314 unsigned int i;
315 uint32_t numentries;
316 size_t size_remaining = chunk_len - 8;
318 /* version */
319 stream_read_uint8(qtmovie->stream);
320 size_remaining -= 1;
321 /* flags */
322 stream_read_uint8(qtmovie->stream);
323 stream_read_uint8(qtmovie->stream);
324 stream_read_uint8(qtmovie->stream);
325 size_remaining -= 3;
327 numentries = stream_read_uint32(qtmovie->stream);
328 size_remaining -= 4;
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");
336 return false;
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);
343 size_remaining -= 8;
346 if (size_remaining)
348 DEBUGF("ehm, size remianing?\n");
349 stream_skip(qtmovie->stream, size_remaining);
352 return true;
355 static bool read_chunk_stsz(qtmovie_t *qtmovie, size_t chunk_len)
357 size_t size_remaining = chunk_len - 8;
359 /* version */
360 stream_read_uint8(qtmovie->stream);
361 size_remaining -= 1;
362 /* flags */
363 stream_read_uint8(qtmovie->stream);
364 stream_read_uint8(qtmovie->stream);
365 stream_read_uint8(qtmovie->stream);
366 size_remaining -= 3;
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);
373 size_remaining -= 4;
374 return true;
376 size_remaining -= 4;
378 qtmovie->res->num_sample_byte_sizes = stream_read_uint32(qtmovie->stream);
379 size_remaining -= 4;
381 if (size_remaining)
383 stream_skip(qtmovie->stream, size_remaining);
386 return true;
389 static bool read_chunk_stsc(qtmovie_t *qtmovie, size_t chunk_len)
391 unsigned int i;
392 uint32_t numentries;
393 size_t size_remaining = chunk_len - 8;
395 /* version + flags */
396 stream_read_uint32(qtmovie->stream);
397 size_remaining -= 4;
399 numentries = stream_read_uint32(qtmovie->stream);
400 size_remaining -= 4;
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");
408 return false;
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;
421 if (size_remaining)
423 DEBUGF("ehm, size remianing?\n");
424 stream_skip(qtmovie->stream, size_remaining);
427 return true;
430 static bool read_chunk_stco(qtmovie_t *qtmovie, size_t chunk_len)
432 uint32_t i, k, old_i;
433 uint32_t numentries;
434 uint32_t idx = 0;
435 uint32_t frame;
436 uint32_t offset;
437 uint32_t old_first;
438 uint32_t new_first;
439 uint32_t old_frame;
440 size_t size_remaining = chunk_len - 8;
442 /* version + flags */
443 stream_read_uint32(qtmovie->stream);
444 size_remaining -= 4;
446 numentries = stream_read_uint32(qtmovie->stream);
447 size_remaining -= 4;
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");
455 return false;
458 /* read first offset */
459 offset = stream_read_uint32(qtmovie->stream);
460 size_remaining -= 4;
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
466 * libm4a/m4a.c).
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
472 * more. */
473 i = 1;
474 old_i = 1;
475 frame = 0;
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)
483 if (i > old_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;
489 old_i = i;
492 if (new_first > k)
493 break;
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;
501 idx++;
503 frame -= (k - old_first) * old_frame;
505 offset = stream_read_uint32(qtmovie->stream);
506 size_remaining -= 4;
509 if (size_remaining)
511 DEBUGF("ehm, size remianing?\n");
512 stream_skip(qtmovie->stream, size_remaining);
515 return true;
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);
532 return false;
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)) {
541 return false;
543 break;
544 case MAKEFOURCC('s','t','t','s'):
545 if (!read_chunk_stts(qtmovie, sub_chunk_len))
547 return false;
549 break;
550 case MAKEFOURCC('s','t','s','z'):
551 if (!read_chunk_stsz(qtmovie, sub_chunk_len))
553 return false;
555 break;
556 case MAKEFOURCC('s','t','s','c'):
557 if (!read_chunk_stsc(qtmovie, sub_chunk_len))
559 return false;
561 break;
562 case MAKEFOURCC('s','t','c','o'):
563 if (!read_chunk_stco(qtmovie, sub_chunk_len))
565 return false;
567 break;
568 default:
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;
576 return true;
579 static bool read_chunk_minf(qtmovie_t *qtmovie, size_t chunk_len)
581 size_t size_remaining = chunk_len - 8;
582 uint32_t i;
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);
590 return true;
593 if (stream_read_uint32(qtmovie->stream) != MAKEFOURCC('s','m','h','d'))
595 DEBUGF("not a sound header! can't handle this.\n");
596 return false;
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);
614 return false;
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)) {
623 return false;
625 break;
626 default:
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);
630 break;
633 size_remaining -= sub_chunk_len;
635 return true;
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);
652 return false;
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)) {
661 return false;
663 break;
664 default:
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);
668 break;
671 size_remaining -= sub_chunk_len;
673 return true;
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);
691 return false;
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)) {
700 return false;
702 break;
703 default:
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);
707 break;
710 size_remaining -= sub_chunk_len;
712 return true;
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);
730 return false;
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)) {
739 return false;
741 break;
742 default:
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);
746 break;
749 size_remaining -= sub_chunk_len;
751 return true;
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)
763 qtmovie_t qtmovie;
765 /* construct the stream */
766 qtmovie.stream = file;
767 qtmovie.res = demux_res;
769 /* read the chunks */
770 while (1)
772 size_t chunk_len;
773 fourcc_t chunk_id;
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)
779 return 0;
780 stream_seek(qtmovie.stream, qtmovie.res->mdat_offset);
781 return 1;
784 if (chunk_len == 1)
786 //DEBUGF("need 64bit support\n");
787 return 0;
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);
792 switch (chunk_id)
794 case MAKEFOURCC('f','t','y','p'):
795 read_chunk_ftyp(&qtmovie, chunk_len);
796 break;
797 case MAKEFOURCC('m','o','o','v'):
798 if (!read_chunk_moov(&qtmovie, chunk_len)) {
799 return 0;
801 break;
802 case MAKEFOURCC('m','d','a','t'):
803 /* There can be empty mdats before the real one. If so, skip them */
804 if (chunk_len == 8)
805 break;
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)
812 return 1;
813 stream_skip(qtmovie.stream, chunk_len - 8);
814 break;
816 /* these following atoms can be skipped !!!! */
817 case MAKEFOURCC('f','r','e','e'):
818 stream_skip(qtmovie.stream, chunk_len - 8);
819 break;
820 default:
821 //DEBUGF("(top) unknown chunk id: %c%c%c%c\n",SPLITFOURCC(chunk_id));
822 return 0;
826 return 0;