summaryrefslogtreecommitdiff
path: root/apps/codecs/libm4a/demux.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/codecs/libm4a/demux.c')
-rw-r--r--apps/codecs/libm4a/demux.c92
1 files changed, 54 insertions, 38 deletions
diff --git a/apps/codecs/libm4a/demux.c b/apps/codecs/libm4a/demux.c
index e584c37858..1b8deab602 100644
--- a/apps/codecs/libm4a/demux.c
+++ b/apps/codecs/libm4a/demux.c
@@ -354,8 +354,6 @@ static bool read_chunk_stts(qtmovie_t *qtmovie, size_t chunk_len)
354 354
355static bool read_chunk_stsz(qtmovie_t *qtmovie, size_t chunk_len) 355static bool read_chunk_stsz(qtmovie_t *qtmovie, size_t chunk_len)
356{ 356{
357 unsigned int i;
358 uint32_t numentries;
359 size_t size_remaining = chunk_len - 8; 357 size_t size_remaining = chunk_len - 8;
360 358
361 /* version */ 359 /* version */
@@ -377,35 +375,11 @@ static bool read_chunk_stsz(qtmovie_t *qtmovie, size_t chunk_len)
377 } 375 }
378 size_remaining -= 4; 376 size_remaining -= 4;
379 377
380 numentries = stream_read_uint32(qtmovie->stream); 378 qtmovie->res->num_sample_byte_sizes = stream_read_uint32(qtmovie->stream);
381 size_remaining -= 4; 379 size_remaining -= 4;
382 380
383 qtmovie->res->num_sample_byte_sizes = numentries;
384 qtmovie->res->sample_byte_size = malloc(numentries * sizeof(*qtmovie->res->sample_byte_size));
385
386 if (!qtmovie->res->sample_byte_size)
387 {
388 DEBUGF("stsz too large\n");
389 return false;
390 }
391
392 for (i = 0; i < numentries; i++)
393 {
394 uint32_t v = stream_read_uint32(qtmovie->stream);
395
396 if (v > 0x0000ffff)
397 {
398 DEBUGF("stsz[%d] > 65 kB (%ld)\n", i, (long)v);
399 return false;
400 }
401
402 qtmovie->res->sample_byte_size[i] = v;
403 size_remaining -= 4;
404 }
405
406 if (size_remaining) 381 if (size_remaining)
407 { 382 {
408 DEBUGF("ehm, size remianing?\n");
409 stream_skip(qtmovie->stream, size_remaining); 383 stream_skip(qtmovie->stream, size_remaining);
410 } 384 }
411 385
@@ -426,8 +400,7 @@ static bool read_chunk_stsc(qtmovie_t *qtmovie, size_t chunk_len)
426 size_remaining -= 4; 400 size_remaining -= 4;
427 401
428 qtmovie->res->num_sample_to_chunks = numentries; 402 qtmovie->res->num_sample_to_chunks = numentries;
429 qtmovie->res->sample_to_chunk = malloc(numentries * 403 qtmovie->res->sample_to_chunk = malloc(numentries * sizeof(sample_to_chunk_t));
430 sizeof(*qtmovie->res->sample_to_chunk));
431 404
432 if (!qtmovie->res->sample_to_chunk) 405 if (!qtmovie->res->sample_to_chunk)
433 { 406 {
@@ -456,8 +429,14 @@ static bool read_chunk_stsc(qtmovie_t *qtmovie, size_t chunk_len)
456 429
457static bool read_chunk_stco(qtmovie_t *qtmovie, size_t chunk_len) 430static bool read_chunk_stco(qtmovie_t *qtmovie, size_t chunk_len)
458{ 431{
459 unsigned int i; 432 uint32_t i, k;
460 uint32_t numentries; 433 uint32_t numentries;
434 int32_t idx = 0;
435 int32_t frame;
436 int32_t offset;
437 int32_t old_first;
438 int32_t new_first;
439 int32_t old_frame;
461 size_t size_remaining = chunk_len - 8; 440 size_t size_remaining = chunk_len - 8;
462 441
463 /* version + flags */ 442 /* version + flags */
@@ -466,20 +445,57 @@ static bool read_chunk_stco(qtmovie_t *qtmovie, size_t chunk_len)
466 445
467 numentries = stream_read_uint32(qtmovie->stream); 446 numentries = stream_read_uint32(qtmovie->stream);
468 size_remaining -= 4; 447 size_remaining -= 4;
448
449 qtmovie->res->num_lookup_table = numentries;
450 qtmovie->res->lookup_table = malloc(numentries * sizeof(*qtmovie->res->lookup_table));
469 451
470 qtmovie->res->num_chunk_offsets = numentries; 452 if (!qtmovie->res->lookup_table)
471 qtmovie->res->chunk_offset = malloc(numentries *
472 sizeof(*qtmovie->res->chunk_offset));
473
474 if (!qtmovie->res->chunk_offset)
475 { 453 {
476 DEBUGF("stco too large\n"); 454 DEBUGF("stco too large to allocate lookup_table[]\n");
477 return false; 455 return false;
478 } 456 }
479 457
480 for (i = 0; i < numentries; i++) 458 /* read first offset */
459 offset = stream_read_uint32(qtmovie->stream);
460 size_remaining -= 4;
461
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 frame = 0;
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)
481 { 478 {
482 qtmovie->res->chunk_offset[i] = stream_read_uint32(qtmovie->stream); 479 for (; i < qtmovie->res->num_sample_to_chunks; ++i)
480 {
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;
484
485 if (qtmovie->res->sample_to_chunk[i].first_chunk > k)
486 break;
487
488 frame += (new_first - old_first) * old_frame;
489 }
490 frame += (k - old_first) * old_frame;
491
492 qtmovie->res->lookup_table[idx].sample = frame;
493 qtmovie->res->lookup_table[idx].offset = offset;
494 idx++;
495
496 frame -= (k - old_first) * old_frame;
497
498 offset = stream_read_uint32(qtmovie->stream);
483 size_remaining -= 4; 499 size_remaining -= 4;
484 } 500 }
485 501