X-Git-Url: http://git.ieval.ro/?a=blobdiff_plain;f=src%2Fm4af.c;h=7f3b180a46fe093bd1cf80b51239d99eb2db4182;hb=9b7e1ca68c3a594f6657ad1d3d151cd74b3e0228;hp=261ce7192969aa1c6374ac70197b530268f84483;hpb=91ef87b610a03a3f4c4729ed9fb8ae3d70c58719;p=fdkaac.git diff --git a/src/m4af.c b/src/m4af.c index 261ce71..7f3b180 100644 --- a/src/m4af.c +++ b/src/m4af.c @@ -39,9 +39,18 @@ typedef struct m4af_chunk_entry_t { uint32_t duration; } m4af_chunk_entry_t; +typedef struct m4af_itmf_entry_t { + uint32_t fcc; + char *name; + uint32_t type_code; + char *data; + uint32_t data_size; +} m4af_itmf_entry_t; + typedef struct m4af_track_t { uint32_t codec; uint32_t timescale; + uint16_t num_channels; int64_t creation_time; int64_t modification_time; int64_t duration; @@ -102,12 +111,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 */ @@ -211,6 +245,7 @@ m4af_ctx_t *m4af_create(uint32_t codec, uint32_t timescale, ctx->track[0].timescale = timescale; ctx->track[0].creation_time = timestamp; ctx->track[0].modification_time = timestamp; + ctx->track[0].num_channels = 2; return ctx; } @@ -254,6 +289,12 @@ void m4af_teardown(m4af_ctx_t **ctxp) *ctxp = 0; } +void m4af_set_num_channels(m4af_ctx_t *ctx, uint32_t track_idx, + uint16_t channels) +{ + ctx->track[track_idx].num_channels = channels; +} + void m4af_set_fixed_frame_duration(m4af_ctx_t *ctx, uint32_t track_idx, uint32_t length) { @@ -656,7 +697,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 */ @@ -816,11 +857,13 @@ void m4af_write_mp4a_box(m4af_ctx_t *ctx, uint32_t track_idx) "\0\001" /* data_reference_index: 1 */ "\0\0\0\0" /* reserved[0] */ "\0\0\0\0" /* reserved[1] */ - "\0\002" /* channelcount: 2 */ + ,16); + m4af_write16(ctx, track->num_channels); + m4af_write(ctx, "\0\020" /* samplesize: 16 */ "\0\0" /* pre_defined */ "\0\0" /* reserved */ - ,24); + ,6); if (track->codec == M4AF_FOURCC('m','p','4','a')) { m4af_write32(ctx, track->timescale << 16); m4af_write_esds_box(ctx, track_idx); @@ -864,7 +907,6 @@ void m4af_write_sbgp_box(m4af_ctx_t *ctx, uint32_t track_idx) static void m4af_write_sgpd_box(m4af_ctx_t *ctx, uint32_t track_idx) { - m4af_track_t *track = &ctx->track[track_idx]; m4af_write(ctx, "\0\0\0\026" /* size: 22 */ "sgpd" /* type */ @@ -1003,7 +1045,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); } @@ -1050,7 +1092,6 @@ void m4af_write_elst_box(m4af_ctx_t *ctx, uint32_t track_idx) static void m4af_write_edts_box(m4af_ctx_t *ctx, uint32_t track_idx) { - m4af_track_t *track = &ctx->track[track_idx]; int64_t pos = m4af_tell(ctx); m4af_write(ctx, "\0\0\0\0edts", 8); m4af_write_elst_box(ctx, track_idx); @@ -1309,28 +1350,58 @@ 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) @@ -1359,7 +1430,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);