set avgBitrate field to zero for 14496-1 compliance
[fdkaac.git] / src / m4af.c
index bbb1e2980b567e091119a43d680040eb19364dc5..11650f352525271d2f9f631e50283f55b3eae5fc 100644 (file)
@@ -23,6 +23,7 @@
 
 #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
 
@@ -79,6 +80,7 @@ struct m4af_ctx_t {
     int64_t modification_time;
     int64_t mdat_pos;
     int64_t mdat_size;
+    int priming_mode;
     int last_error;
 
     m4af_itmf_entry_t *itmf_table;
@@ -148,6 +150,13 @@ int m4af_write(m4af_ctx_t *ctx, const void *data, uint32_t size)
     return rc;
 }
 
+static
+int m4af_write16(m4af_ctx_t *ctx, uint32_t data)
+{
+    data = m4af_htob16(data);
+    return m4af_write(ctx, &data, 2);
+}
+
 static
 int m4af_write32(m4af_ctx_t *ctx, uint32_t data)
 {
@@ -277,6 +286,11 @@ void m4af_set_priming(m4af_ctx_t *ctx, uint32_t track_idx,
     track->padding = padding;
 }
 
+void m4af_set_priming_mode(m4af_ctx_t *ctx, int mode)
+{
+    ctx->priming_mode = mode;
+}
+
 static
 int m4af_add_sample_entry(m4af_ctx_t *ctx, uint32_t track_idx,
                           uint32_t size, uint32_t delta)
@@ -583,12 +597,13 @@ int m4af_set_iTunSMPB(m4af_ctx_t *ctx)
 }
 
 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
@@ -756,7 +771,11 @@ void m4af_write_esds_box(m4af_ctx_t *ctx, uint32_t track_idx)
                , 2);
     m4af_write24(ctx, track->bufferSizeDB);
     m4af_write32(ctx, track->maxBitrate);
+#if 0
     m4af_write32(ctx, track->avgBitrate);
+#else
+    m4af_write32(ctx, 0);
+#endif
     /* DecoderSpecificInfo */
     m4af_write_descriptor(ctx, 5, track->decSpecificInfoSize);
     m4af_write(ctx, track->decSpecificInfo, track->decSpecificInfoSize);
@@ -823,12 +842,50 @@ void m4af_write_stsd_box(m4af_ctx_t *ctx, uint32_t track_idx)
     m4af_update_box_size(ctx, pos);
 }
 
+static
+void m4af_write_sbgp_box(m4af_ctx_t *ctx, uint32_t track_idx)
+{
+    m4af_track_t *track = &ctx->track[track_idx];
+    m4af_write(ctx,
+               "\0\0\0\034"  /* size: 28                */
+               "sbgp"        /* type                    */
+               "\0"          /* version                 */
+               "\0\0\0"      /* flags                   */
+               "roll"        /* grouping_type           */
+               "\0\0\0\001"  /* entry_count: 1          */
+               , 20);
+    m4af_write32(ctx, track->num_samples);
+    m4af_write32(ctx, 1);    /* group_description_index */
+}
+
+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               */
+               "sgpd"        /* type               */
+               "\001"        /* 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);
+}
+
 static
 void m4af_write_stbl_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\0stbl", 8);
     m4af_write_stsd_box(ctx, track_idx);
+    if ((ctx->priming_mode & M4AF_PRIMING_MODE_EDTS) &&
+        (track->encoder_delay || track->padding)) {
+        m4af_write_sbgp_box(ctx, track_idx);
+        m4af_write_sgpd_box(ctx, track_idx);
+    }
     m4af_write_stts_box(ctx, track_idx);
     m4af_write_stsc_box(ctx, track_idx);
     m4af_write_stsz_box(ctx, track_idx);
@@ -962,6 +1019,42 @@ void m4af_write_mdia_box(m4af_ctx_t *ctx, uint32_t track_idx)
     m4af_update_box_size(ctx, pos);
 }
 
+static
+void m4af_write_elst_box(m4af_ctx_t *ctx, uint32_t track_idx)
+{
+    m4af_track_t *track = &ctx->track[track_idx];
+    uint8_t version;
+    int64_t duration = track->duration - track->encoder_delay - track->padding;
+    int64_t pos = m4af_tell(ctx);
+    duration = (double)duration / track->timescale * ctx->timescale + .5;
+    version  = (duration > UINT32_MAX);
+
+    m4af_write(ctx, "\0\0\0\0elst", 8);
+    m4af_write(ctx, &version, 1);
+    m4af_write(ctx, "\0\0\0", 3);  /* flags          */
+    m4af_write32(ctx, 1);          /* entry_count: 1 */
+    if (version) {
+        m4af_write64(ctx, duration);
+        m4af_write64(ctx, track->encoder_delay);
+    } else {
+        m4af_write32(ctx, duration);
+        m4af_write32(ctx, track->encoder_delay);
+    }
+    m4af_write16(ctx, 1);    /* media_rate_integer  */
+    m4af_write16(ctx, 0);    /* media_rate_fraction */
+    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);
+    m4af_update_box_size(ctx, pos);
+}
+
 static
 void m4af_write_tkhd_box(m4af_ctx_t *ctx, uint32_t track_idx)
 {
@@ -1015,9 +1108,13 @@ void m4af_write_tkhd_box(m4af_ctx_t *ctx, uint32_t track_idx)
 static
 void m4af_write_trak_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\0trak", 8);
     m4af_write_tkhd_box(ctx, track_idx);
+    if ((ctx->priming_mode & M4AF_PRIMING_MODE_EDTS) &&
+        (track->encoder_delay || track->padding))
+        m4af_write_edts_box(ctx, track_idx);
     m4af_write_mdia_box(ctx, track_idx);
     m4af_update_box_size(ctx, pos);
 }
@@ -1182,7 +1279,7 @@ void m4af_write_udta_box(m4af_ctx_t *ctx)
 }
 
 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);
@@ -1192,7 +1289,7 @@ void m4af_write_moov_box(m4af_ctx_t *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
@@ -1210,10 +1307,35 @@ void m4af_finalize_mdat(m4af_ctx_t *ctx)
     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;
     m4af_track_t *track;
+    uint32_t moov_size;
 
     for (i = 0; i < ctx->num_tracks; ++i) {
         track = ctx->track + i;
@@ -1227,9 +1349,19 @@ int m4af_finalize(m4af_ctx_t *ctx)
         }
         m4af_flush_chunk(ctx, i);
     }
-    if (ctx->track[0].encoder_delay || ctx->track[0].padding)
+    track = ctx->track;
+    if ((ctx->priming_mode & M4AF_PRIMING_MODE_ITUNSMPB) &&
+        (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;
+        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;
 }
This page took 0.014361 seconds and 4 git commands to generate.