#define m4af_realloc(memory,size) realloc(memory, size)
#define m4af_free(memory) free(memory)
+#define m4af_max(a,b) ((a)<(b)?(b):(a))
#define M4AF_ATOM_WILD 0xffffffff
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;
uint32_t bufferSizeDB;
uint32_t maxBitrate;
uint32_t avgBitrate;
+ int is_vbr;
m4af_sample_entry_t *sample_table;
uint32_t num_samples;
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
*/
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;
}
*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)
{
return ctx->last_error;
}
+void m4af_set_vbr_mode(m4af_ctx_t *ctx, uint32_t track_idx, int is_vbr)
+{
+ m4af_track_t *track = &ctx->track[track_idx];
+ track->is_vbr = is_vbr;
+}
+
void m4af_set_priming(m4af_ctx_t *ctx, uint32_t track_idx,
uint32_t encoder_delay, uint32_t padding)
{
}
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);
m4af_set_pos(ctx, pos);
m4af_write32(ctx, current_pos - pos);
m4af_set_pos(ctx, current_pos);
+ return current_pos - pos;
}
static
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 */
, 2);
m4af_write24(ctx, track->bufferSizeDB);
m4af_write32(ctx, track->maxBitrate);
- m4af_write32(ctx, track->avgBitrate);
+ m4af_write32(ctx, track->is_vbr ? 0: track->avgBitrate);
/* DecoderSpecificInfo */
m4af_write_descriptor(ctx, 5, track->decSpecificInfoSize);
m4af_write(ctx, track->decSpecificInfo, track->decSpecificInfoSize);
"\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);
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\032" /* size */
+ "\0\0\0\026" /* size: 22 */
"sgpd" /* type */
- "\001" /* version */
+ "\0" /* version */
"\0\0\0" /* flags */
"roll" /* grouping_type */
- "\0\0\0\002" /* default_length: 2 */
"\0\0\0\001" /* entry_count: 1 */
"\377\377" /* payload_data: -1 */
- , 26);
+ , 22);
}
static
/* 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);
}
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);
}
static
-void m4af_write_moov_box(m4af_ctx_t *ctx)
+uint32_t m4af_write_moov_box(m4af_ctx_t *ctx)
{
unsigned i;
int64_t pos = m4af_tell(ctx);
m4af_write_trak_box(ctx, i);
if (ctx->num_tags)
m4af_write_udta_box(ctx);
- m4af_update_box_size(ctx, pos);
+ return m4af_update_box_size(ctx, pos);
}
static
m4af_set_pos(ctx, ctx->mdat_pos + ctx->mdat_size);
}
-int m4af_finalize(m4af_ctx_t *ctx)
+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)
+{
+ int64_t begin, end;
+ char *buf;
+
+ buf = malloc(1024*1024*2);
+
+ end = ctx->mdat_pos + ctx->mdat_size;
+ 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);
+ }
+ 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)
{
unsigned i;
m4af_track_t *track;
+ uint32_t moov_size;
for (i = 0; i < ctx->num_tracks; ++i) {
track = ctx->track + i;
(track->encoder_delay || track->padding))
m4af_set_iTunSMPB(ctx);
m4af_finalize_mdat(ctx);
- m4af_write_moov_box(ctx);
+ moov_size = m4af_write_moov_box(ctx);
+ if (optimize) {
+ int64_t pos;
+ 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);
+ m4af_write_free_box(ctx, ctx->mdat_pos - pos - 24);
+ }
return ctx->last_error;
}