From f75c9c7b3e90df13b91f3de94c6374c58e4c3d52 Mon Sep 17 00:00:00 2001 From: Marius Gavrilescu Date: Fri, 17 Jul 2015 20:18:31 +0300 Subject: [PATCH] Imported Upstream version 0.6.2 --- src/m4af.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++------ src/main.c | 13 ++++++++-- version.h | 2 +- 3 files changed, 77 insertions(+), 11 deletions(-) diff --git a/src/m4af.c b/src/m4af.c index f3a4852..1c51c00 100644 --- a/src/m4af.c +++ b/src/m4af.c @@ -103,12 +103,37 @@ typedef struct m4af_box_parser_t { int (*handler)(m4af_ctx_t *ctx, uint32_t name, uint64_t size); } m4af_box_parser_t; +static +int m4af_write_null_cb(void *cookie, const void *data, uint32_t size) +{ + int64_t *pos = cookie; + *pos += size; + return 0; +} +static +int m4af_seek_null_cb(void *cookie, int64_t off, int whence) +{ + int64_t *pos = cookie; + *pos = off; /* XXX: we use only SEEK_SET */ + return 0; +} +static +int64_t m4af_tell_null_cb(void *cookie) +{ + return *((int64_t*)cookie); +} + +static m4af_io_callbacks_t m4af_null_io_callbacks = { + 0, m4af_write_null_cb, m4af_seek_null_cb, m4af_tell_null_cb +}; + static int64_t m4af_timestamp(void) { return (int64_t)(time(0)) + (((1970 - 1904) * 365) + 17) * 24 * 60 * 60; } + /* * http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 */ @@ -664,7 +689,7 @@ void m4af_write_stco_box(m4af_ctx_t *ctx, uint32_t track_idx) m4af_track_t *track = &ctx->track[track_idx]; uint32_t i; m4af_chunk_entry_t *index = track->chunk_table; - int is_co64 = (ctx->mdat_pos + ctx->mdat_size > UINT32_MAX); + int is_co64 = index[track->num_chunks - 1].offset > 0xffffffff; int64_t pos = m4af_tell(ctx); m4af_write32(ctx, 0); /* size */ @@ -1012,7 +1037,7 @@ void m4af_write_hdlr_box(m4af_ctx_t *ctx, uint32_t track_idx, const char *type) /* reserved[0] */ m4af_write(ctx, !strcmp(type, "mdir") ? "appl" : "\0\0\0\0", 4); /* reserved[1], reserved[2], name */ - m4af_write(ctx, reserved_and_name, (pos & 1) ? 9 : 10); + m4af_write(ctx, reserved_and_name, 9); m4af_update_box_size(ctx, pos); } @@ -1317,28 +1342,59 @@ void m4af_finalize_mdat(m4af_ctx_t *ctx) m4af_set_pos(ctx, ctx->mdat_pos + ctx->mdat_size); } +static +uint64_t m4af_patch_moov(m4af_ctx_t *ctx, uint32_t moov_size, uint32_t offset) +{ + int64_t pos = 0; + uint32_t moov_size2; + int i, j; + m4af_io_callbacks_t io_reserve = ctx->io; + void *io_cookie_reserve = ctx->io_cookie; + + for (i = 0; i < ctx->num_tracks; ++i) + for (j = 0; j < ctx->track[i].num_chunks; ++j) + ctx->track[i].chunk_table[j].offset += offset; + + ctx->io = m4af_null_io_callbacks; + ctx->io_cookie = &pos; + moov_size2 = m4af_write_moov_box(ctx); + + if (moov_size2 != moov_size) { + /* stco -> co64 switching */ + for (i = 0; i < ctx->num_tracks; ++i) + for (j = 0; j < ctx->track[i].num_chunks; ++j) + ctx->track[i].chunk_table[j].offset += moov_size2 - moov_size; + moov_size2 = m4af_write_moov_box(ctx); + } + ctx->io = io_reserve; + ctx->io_cookie = io_cookie_reserve; + return moov_size2; +} + static void m4af_shift_mdat_pos(m4af_ctx_t *ctx, uint32_t offset) { unsigned i, j; int64_t begin, end; - char buf[8192]; + char *buf; + + buf = malloc(1024*1024*2); end = ctx->mdat_pos + ctx->mdat_size; - for (; (begin = m4af_max(ctx->mdat_pos, end - 8192)) < end; end = begin) { + for (; (begin = m4af_max(ctx->mdat_pos, end - 1024*1024*2)) < end; + end = begin) { m4af_set_pos(ctx, begin); ctx->io.read(ctx->io_cookie, buf, end - begin); m4af_set_pos(ctx, begin + offset); m4af_write(ctx, buf, end - begin); } - for (i = 0; i < ctx->num_tracks; ++i) - for (j = 0; j < ctx->track[i].num_chunks; ++j) - ctx->track[i].chunk_table[j].offset += offset; ctx->mdat_pos += offset; m4af_set_pos(ctx, ctx->mdat_pos - 16); m4af_write_free_box(ctx, 0); m4af_write(ctx, "\0\0\0\0mdat", 8); m4af_finalize_mdat(ctx); + + free(buf); } int m4af_finalize(m4af_ctx_t *ctx, int optimize) @@ -1367,7 +1423,8 @@ int m4af_finalize(m4af_ctx_t *ctx, int optimize) moov_size = m4af_write_moov_box(ctx); if (optimize) { int64_t pos; - m4af_shift_mdat_pos(ctx, moov_size + 1024); + uint32_t moov_size2 = m4af_patch_moov(ctx, moov_size, moov_size + 1024); + m4af_shift_mdat_pos(ctx, moov_size2 + 1024); m4af_set_pos(ctx, 32); m4af_write_moov_box(ctx); pos = m4af_tell(ctx); diff --git a/src/main.c b/src/main.c index f4ecd75..6ffb7b1 100644 --- a/src/main.c +++ b/src/main.c @@ -563,11 +563,20 @@ int encode(aacenc_param_ex_t *params, pcm_reader_t *reader, ++encoded; if (encoded == 1 || encoded == 3) continue; - obp = &obuf[flip]; - if (write_sample(params->output_fp, m4af, obp) < 0) + + if (write_sample(params->output_fp, m4af, &obuf[flip]) < 0) goto END; ++frames_written; } while (remaining > 0); + /* + * When interrupted, we haven't pulled out last extrapolated frames + * from the reader. Therefore, we have to write the final outcome. + */ + if (g_interrupted) { + if (write_sample(params->output_fp, m4af, &obp[flip^1]) < 0) + goto END; + ++frames_written; + } } DONE: if (!params->silent) diff --git a/version.h b/version.h index c766f56..151b33e 100644 --- a/version.h +++ b/version.h @@ -1,4 +1,4 @@ #ifndef VERSION_H #define VERSION_H -const char *fdkaac_version = "0.6.1"; +const char *fdkaac_version = "0.6.2"; #endif -- 2.30.2