add --moov-before-mdat

This commit is contained in:
nu774 2013-06-14 16:52:18 +09:00
parent f5363b23c3
commit fb2b36350a
4 changed files with 52 additions and 10 deletions

View File

@ -23,6 +23,7 @@
#define m4af_realloc(memory,size) realloc(memory, size) #define m4af_realloc(memory,size) realloc(memory, size)
#define m4af_free(memory) free(memory) #define m4af_free(memory) free(memory)
#define m4af_max(a,b) ((a)<(b)?(b):(a))
#define M4AF_ATOM_WILD 0xffffffff #define M4AF_ATOM_WILD 0xffffffff
@ -596,12 +597,13 @@ int m4af_set_iTunSMPB(m4af_ctx_t *ctx)
} }
static static
void m4af_update_box_size(m4af_ctx_t *ctx, int64_t pos) uint32_t m4af_update_box_size(m4af_ctx_t *ctx, int64_t pos)
{ {
int64_t current_pos = m4af_tell(ctx); int64_t current_pos = m4af_tell(ctx);
m4af_set_pos(ctx, pos); m4af_set_pos(ctx, pos);
m4af_write32(ctx, current_pos - pos); m4af_write32(ctx, current_pos - pos);
m4af_set_pos(ctx, current_pos); m4af_set_pos(ctx, current_pos);
return current_pos - pos;
} }
static static
@ -1273,7 +1275,7 @@ void m4af_write_udta_box(m4af_ctx_t *ctx)
} }
static static
void m4af_write_moov_box(m4af_ctx_t *ctx) uint32_t m4af_write_moov_box(m4af_ctx_t *ctx)
{ {
unsigned i; unsigned i;
int64_t pos = m4af_tell(ctx); int64_t pos = m4af_tell(ctx);
@ -1283,7 +1285,7 @@ void m4af_write_moov_box(m4af_ctx_t *ctx)
m4af_write_trak_box(ctx, i); m4af_write_trak_box(ctx, i);
if (ctx->num_tags) if (ctx->num_tags)
m4af_write_udta_box(ctx); m4af_write_udta_box(ctx);
m4af_update_box_size(ctx, pos); return m4af_update_box_size(ctx, pos);
} }
static static
@ -1301,10 +1303,35 @@ void m4af_finalize_mdat(m4af_ctx_t *ctx)
m4af_set_pos(ctx, ctx->mdat_pos + ctx->mdat_size); m4af_set_pos(ctx, ctx->mdat_pos + ctx->mdat_size);
} }
int m4af_finalize(m4af_ctx_t *ctx) static
void m4af_shift_mdat_pos(m4af_ctx_t *ctx, uint32_t offset)
{
unsigned i, j;
int64_t begin, end;
char buf[8192];
end = ctx->mdat_pos + ctx->mdat_size;
for (; (begin = m4af_max(ctx->mdat_pos, end - 8192)) < 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);
}
int m4af_finalize(m4af_ctx_t *ctx, int optimize)
{ {
unsigned i; unsigned i;
m4af_track_t *track; m4af_track_t *track;
uint32_t moov_size;
for (i = 0; i < ctx->num_tracks; ++i) { for (i = 0; i < ctx->num_tracks; ++i) {
track = ctx->track + i; track = ctx->track + i;
@ -1323,6 +1350,14 @@ int m4af_finalize(m4af_ctx_t *ctx)
(track->encoder_delay || track->padding)) (track->encoder_delay || track->padding))
m4af_set_iTunSMPB(ctx); m4af_set_iTunSMPB(ctx);
m4af_finalize_mdat(ctx); m4af_finalize_mdat(ctx);
m4af_write_moov_box(ctx); moov_size = m4af_write_moov_box(ctx);
if (optimize) {
int64_t pos;
m4af_shift_mdat_pos(ctx, moov_size + 1024);
m4af_set_pos(ctx, 32);
m4af_write_moov_box(ctx);
pos = m4af_tell(ctx);
m4af_write_free_box(ctx, ctx->mdat_pos - pos - 24);
}
return ctx->last_error; return ctx->last_error;
} }

View File

@ -87,7 +87,7 @@ m4af_ctx_t *m4af_create(uint32_t codec, uint32_t timescale,
int m4af_begin_write(m4af_ctx_t *ctx); int m4af_begin_write(m4af_ctx_t *ctx);
int m4af_finalize(m4af_ctx_t *ctx); int m4af_finalize(m4af_ctx_t *ctx, int optimize);
void m4af_teardown(m4af_ctx_t **ctx); void m4af_teardown(m4af_ctx_t **ctx);

View File

@ -154,6 +154,7 @@ PROGNAME " %s\n"
" 2: Both\n" " 2: Both\n"
" --ignorelength Ignore length of WAV header\n" " --ignorelength Ignore length of WAV header\n"
" -S, --silent Don't print progress messages\n" " -S, --silent Don't print progress messages\n"
" --moov-before-mdat Place moov box before mdat box on m4a output\n"
"\n" "\n"
"Options for raw (headerless) input:\n" "Options for raw (headerless) input:\n"
" -R, --raw Treat input as raw (by default WAV is\n" " -R, --raw Treat input as raw (by default WAV is\n"
@ -206,6 +207,7 @@ typedef struct aacenc_param_ex_t {
unsigned gapless_mode; unsigned gapless_mode;
unsigned ignore_length; unsigned ignore_length;
int silent; int silent;
int moov_before_mdat;
int is_raw; int is_raw;
unsigned raw_channels; unsigned raw_channels;
@ -223,6 +225,7 @@ int parse_options(int argc, char **argv, aacenc_param_ex_t *params)
int ch; int ch;
unsigned n; unsigned n;
#define OPT_MOOV_BEFORE_MDAT M4AF_FOURCC('m','o','o','v')
#define OPT_RAW_CHANNELS M4AF_FOURCC('r','c','h','n') #define OPT_RAW_CHANNELS M4AF_FOURCC('r','c','h','n')
#define OPT_RAW_RATE M4AF_FOURCC('r','r','a','t') #define OPT_RAW_RATE M4AF_FOURCC('r','r','a','t')
#define OPT_RAW_FORMAT M4AF_FOURCC('r','f','m','t') #define OPT_RAW_FORMAT M4AF_FOURCC('r','f','m','t')
@ -247,6 +250,7 @@ int parse_options(int argc, char **argv, aacenc_param_ex_t *params)
{ "gapless-mode", required_argument, 0, 'G' }, { "gapless-mode", required_argument, 0, 'G' },
{ "ignorelength", no_argument, 0, 'I' }, { "ignorelength", no_argument, 0, 'I' },
{ "silent", no_argument, 0, 'S' }, { "silent", no_argument, 0, 'S' },
{ "moov-before-mdat", no_argument, 0, OPT_MOOV_BEFORE_MDAT },
{ "raw", no_argument, 0, 'R' }, { "raw", no_argument, 0, 'R' },
{ "raw-channels", required_argument, 0, OPT_RAW_CHANNELS }, { "raw-channels", required_argument, 0, OPT_RAW_CHANNELS },
@ -357,6 +361,9 @@ int parse_options(int argc, char **argv, aacenc_param_ex_t *params)
case 'S': case 'S':
params->silent = 1; params->silent = 1;
break; break;
case OPT_MOOV_BEFORE_MDAT:
params->moov_before_mdat = 1;
break;
case 'R': case 'R':
params->is_raw = 1; params->is_raw = 1;
break; break;
@ -580,7 +587,7 @@ int finalize_m4a(m4af_ctx_t *m4af, const aacenc_param_ex_t *params,
put_tool_tag(m4af, params, encoder); put_tool_tag(m4af, params, encoder);
if (m4af_finalize(m4af) < 0) { if (m4af_finalize(m4af, params->moov_before_mdat) < 0) {
fprintf(stderr, "ERROR: failed to finalize m4a\n"); fprintf(stderr, "ERROR: failed to finalize m4a\n");
return -1; return -1;
} }
@ -647,7 +654,7 @@ int main(int argc, char **argv)
{ {
wav_io_context_t wav_io = { read_callback, seek_callback, tell_callback }; wav_io_context_t wav_io = { read_callback, seek_callback, tell_callback };
m4af_io_callbacks_t m4af_io_callbacks_t
m4af_io = { 0, write_callback, seek_callback, tell_callback }; m4af_io = { read_callback, write_callback, seek_callback, tell_callback };
aacenc_param_ex_t params = { 0 }; aacenc_param_ex_t params = { 0 };
int result = 2; int result = 2;
@ -711,7 +718,7 @@ int main(int argc, char **argv)
params.output_filename = output_filename; params.output_filename = output_filename;
} }
if ((ofp = aacenc_fopen(params.output_filename, "wb")) == 0) { if ((ofp = aacenc_fopen(params.output_filename, "wb+")) == 0) {
aacenc_fprintf(stderr, "ERROR: %s: %s\n", params.output_filename, aacenc_fprintf(stderr, "ERROR: %s: %s\n", params.output_filename,
strerror(errno)); strerror(errno));
goto END; goto END;

View File

@ -1,4 +1,4 @@
#ifndef VERSION_H #ifndef VERSION_H
#define VERSION_H #define VERSION_H
const char *fdkaac_version = "0.2.0"; const char *fdkaac_version = "0.3.0";
#endif #endif