]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/movenc.c
attempt to demux clip wrapped content
[ffmpeg] / libavformat / movenc.c
index 0111777cd8b8fd9b2a9a7d5dba709c0bd7693b53..de9b4a5f2593fb179a594c1da4a42db0063f9f55 100644 (file)
@@ -18,9 +18,9 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 #include "avformat.h"
-#include "avi.h"
+#include "riff.h"
 #include "avio.h"
-#include "mov.h"
+#include "isom.h"
 
 #undef NDEBUG
 #include <assert.h>
@@ -47,12 +47,12 @@ typedef struct MOVIentry {
 typedef struct MOVIndex {
     int         mode;
     int         entry;
-    int         ents_allocated;
     long        timescale;
     long        time;
     int64_t     trackDuration;
     long        sampleCount;
     long        sampleDuration;
+    long        sampleSize;
     int         hasKeyframes;
     int         hasBframes;
     int         language;
@@ -62,7 +62,7 @@ typedef struct MOVIndex {
 
     int         vosLen;
     uint8_t     *vosData;
-    MOVIentry** cluster;
+    MOVIentry   *cluster;
 } MOVTrack;
 
 typedef struct MOVContext {
@@ -101,12 +101,10 @@ static int mov_write_stco_tag(ByteIOContext *pb, MOVTrack* track)
     put_be32(pb, 0); /* version & flags */
     put_be32(pb, track->entry); /* entry count */
     for (i=0; i<track->entry; i++) {
-        int cl = i / MOV_INDEX_CLUSTER_SIZE;
-        int id = i % MOV_INDEX_CLUSTER_SIZE;
         if(mode64 == 1)
-            put_be64(pb, track->cluster[cl][id].pos);
+            put_be64(pb, track->cluster[i].pos);
         else
-            put_be32(pb, track->cluster[cl][id].pos);
+            put_be32(pb, track->cluster[i].pos);
     }
     return updateSize (pb, pos);
 }
@@ -123,17 +121,15 @@ static int mov_write_stsz_tag(ByteIOContext *pb, MOVTrack* track)
     put_be32(pb, 0); /* version & flags */
 
     for (i=0; i<track->entry; i++) {
-        int cl = i / MOV_INDEX_CLUSTER_SIZE;
-        int id = i % MOV_INDEX_CLUSTER_SIZE;
-        tst = track->cluster[cl][id].size/track->cluster[cl][id].entries;
+        tst = track->cluster[i].size/track->cluster[i].entries;
         if(oldtst != -1 && tst != oldtst) {
             equalChunks = 0;
         }
         oldtst = tst;
-        entries += track->cluster[cl][id].entries;
+        entries += track->cluster[i].entries;
     }
     if (equalChunks) {
-        int sSize = track->cluster[0][0].size/track->cluster[0][0].entries;
+        int sSize = track->cluster[0].size/track->cluster[0].entries;
         put_be32(pb, sSize); // sample size
         put_be32(pb, entries); // sample count
     }
@@ -141,11 +137,9 @@ static int mov_write_stsz_tag(ByteIOContext *pb, MOVTrack* track)
         put_be32(pb, 0); // sample size
         put_be32(pb, entries); // sample count
         for (i=0; i<track->entry; i++) {
-            int cl = i / MOV_INDEX_CLUSTER_SIZE;
-            int id = i % MOV_INDEX_CLUSTER_SIZE;
-            for ( j=0; j<track->cluster[cl][id].entries; j++) {
-                put_be32(pb, track->cluster[cl][id].size /
-                         track->cluster[cl][id].entries);
+            for ( j=0; j<track->cluster[i].entries; j++) {
+                put_be32(pb, track->cluster[i].size /
+                         track->cluster[i].entries);
             }
         }
     }
@@ -165,14 +159,12 @@ static int mov_write_stsc_tag(ByteIOContext *pb, MOVTrack* track)
     entryPos = url_ftell(pb);
     put_be32(pb, track->entry); // entry count
     for (i=0; i<track->entry; i++) {
-        int cl = i / MOV_INDEX_CLUSTER_SIZE;
-        int id = i % MOV_INDEX_CLUSTER_SIZE;
-        if(oldval != track->cluster[cl][id].samplesInChunk)
+        if(oldval != track->cluster[i].samplesInChunk)
         {
             put_be32(pb, i+1); // first chunk
-            put_be32(pb, track->cluster[cl][id].samplesInChunk); // samples per chunk
+            put_be32(pb, track->cluster[i].samplesInChunk); // samples per chunk
             put_be32(pb, 0x1); // sample description index
-            oldval = track->cluster[cl][id].samplesInChunk;
+            oldval = track->cluster[i].samplesInChunk;
             index++;
         }
     }
@@ -196,9 +188,7 @@ static int mov_write_stss_tag(ByteIOContext *pb, MOVTrack* track)
     entryPos = url_ftell(pb);
     put_be32(pb, track->entry); // entry count
     for (i=0; i<track->entry; i++) {
-        int cl = i / MOV_INDEX_CLUSTER_SIZE;
-        int id = i % MOV_INDEX_CLUSTER_SIZE;
-        if(track->cluster[cl][id].key_frame == 1) {
+        if(track->cluster[i].key_frame == 1) {
             put_be32(pb, i+1);
             index++;
         }
@@ -210,29 +200,17 @@ static int mov_write_stss_tag(ByteIOContext *pb, MOVTrack* track)
     return updateSize (pb, pos);
 }
 
-static int mov_write_damr_tag(ByteIOContext *pb)
+static int mov_write_amr_tag(ByteIOContext *pb, MOVTrack *track)
 {
     put_be32(pb, 0x11); /* size */
-    put_tag(pb, "damr");
+    if (track->mode == MODE_MOV) put_tag(pb, "samr");
+    else                         put_tag(pb, "damr");
     put_tag(pb, "FFMP");
-    put_byte(pb, 0);
+    put_byte(pb, 0); /* decoder version */
 
-    //put_be16(pb, 0x80); /* Mode set (all modes for AMR_NB) */
-    //put_be16(pb, 0xa); /* Mode change period (no restriction) */
-    put_be16(pb, 0x81ff); /* Mode set (all modes for AMR_NB) */
-    put_be16(pb, 1); /* Mode change period (no restriction) */
-    return 0x11;
-}
-
-static int mov_write_samr_tag(ByteIOContext *pb)
-{
-    put_be32(pb, 0x11); /* size */
-    put_tag(pb, "samr");
-    put_tag(pb, "FFMP");
-    put_byte(pb, 1);
-
-    put_be16(pb, 0x80); /* Mode set (all modes for AMR_NB) */
-    put_be16(pb, 0x5); /* Mode change period (no restriction) */
+    put_be16(pb, 0x81FF); /* Mode set (all modes for AMR_NB) */
+    put_byte(pb, 0x00); /* Mode change period (no restriction) */
+    put_byte(pb, 0x01); /* Frames per sample */
     return 0x11;
 }
 
@@ -323,12 +301,16 @@ static int mov_write_wave_tag(ByteIOContext *pb, MOVTrack* track)
     put_le32(pb, track->tag);
 
     if (track->enc->codec_id == CODEC_ID_AAC) {
+        /* useless atom needed by mplayer, ipod, not needed by quicktime */
+        put_be32(pb, 12); /* size */
+        put_tag(pb, "mp4a");
+        put_be32(pb, 0);
         mov_write_esds_tag(pb, track);
     } else if (track->enc->codec_id == CODEC_ID_PCM_S24LE ||
                track->enc->codec_id == CODEC_ID_PCM_S32LE) {
         mov_write_enda_tag(pb);
     } else if (track->enc->codec_id == CODEC_ID_AMR_NB) {
-        mov_write_samr_tag(pb);
+        mov_write_amr_tag(pb, track);
     }
 
     put_be32(pb, 8);     /* size */
@@ -359,8 +341,11 @@ static const CodecTag codec_movaudio_tags[] = {
 static int mov_write_audio_tag(ByteIOContext *pb, MOVTrack* track)
 {
     offset_t pos = url_ftell(pb);
+    int vbr=  track->enc->codec_id == CODEC_ID_AAC ||
+              track->enc->codec_id == CODEC_ID_MP3 ||
+              track->enc->codec_id == CODEC_ID_AMR_NB;
     int version = track->mode == MODE_MOV &&
-        (track->enc->codec_id == CODEC_ID_AAC ||
+        (vbr ||
          track->enc->codec_id == CODEC_ID_PCM_S32LE ||
          track->enc->codec_id == CODEC_ID_PCM_S24LE);
 
@@ -380,9 +365,7 @@ static int mov_write_audio_tag(ByteIOContext *pb, MOVTrack* track)
                  to be a good way to get number of bits of audio */
     put_be16(pb, 0x10); /* Reserved */
 
-    if(track->enc->codec_id == CODEC_ID_AAC ||
-       track->enc->codec_id == CODEC_ID_MP3 ||
-       track->enc->codec_id == CODEC_ID_AMR_NB) {
+    if(vbr) {
         put_be16(pb, 0xfffe); /* compression ID (vbr)*/
     } else {
         put_be16(pb, 0); /* compression ID (= 0) */
@@ -391,12 +374,13 @@ static int mov_write_audio_tag(ByteIOContext *pb, MOVTrack* track)
     put_be16(pb, track->timescale); /* Time scale */
     put_be16(pb, 0); /* Reserved */
 
-    if(version == 1) {
-        /* SoundDescription V1 extended info */
-        put_be32(pb, track->enc->frame_size); /* Samples per packet  */
-        put_be32(pb, track->sampleDuration); /* Bytes per frame */
-        put_be32(pb, 8); /* Bytes per sample */
-        put_be32(pb, 2);
+    if(version == 1) { /* SoundDescription V1 extended info */
+        if (vbr)
+            track->sampleSize = 0;
+        put_be32(pb, track->enc->frame_size); /* Samples per packet */
+        put_be32(pb, track->sampleSize / track->enc->channels); /* Bytes per packet */
+        put_be32(pb, track->sampleSize); /* Bytes per frame */
+        put_be32(pb, 2); /* Bytes per sample */
     }
 
     if(track->mode == MODE_MOV &&
@@ -408,7 +392,7 @@ static int mov_write_audio_tag(ByteIOContext *pb, MOVTrack* track)
     else if(track->enc->codec_id == CODEC_ID_AAC)
         mov_write_esds_tag(pb, track);
     else if(track->enc->codec_id == CODEC_ID_AMR_NB)
-        mov_write_damr_tag(pb);
+        mov_write_amr_tag(pb, track);
 
     return updateSize (pb, pos);
 }
@@ -684,15 +668,13 @@ static int mov_write_ctts_tag(ByteIOContext *pb, MOVTrack* track)
 
     ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries)); /* worst case */
     ctts_entries[0].count = 1;
-    ctts_entries[0].duration = track->cluster[0][0].cts;
+    ctts_entries[0].duration = track->cluster[0].cts;
     for (i=1; i<track->entry; i++) {
-        int cl = i / MOV_INDEX_CLUSTER_SIZE;
-        int id = i % MOV_INDEX_CLUSTER_SIZE;
-        if (track->cluster[cl][id].cts == ctts_entries[entries].duration) {
+        if (track->cluster[i].cts == ctts_entries[entries].duration) {
             ctts_entries[entries].count++; /* compress */
         } else {
             entries++;
-            ctts_entries[entries].duration = track->cluster[cl][id].cts;
+            ctts_entries[entries].duration = track->cluster[i].cts;
             ctts_entries[entries].count = 1;
         }
     }
@@ -787,7 +769,7 @@ static int mov_write_vmhd_tag(ByteIOContext *pb)
 
 static int mov_write_hdlr_tag(ByteIOContext *pb, MOVTrack* track)
 {
-    char *descr, *hdlr, *hdlr_type;
+    const char *descr, *hdlr, *hdlr_type;
     offset_t pos = url_ftell(pb);
 
     if (!track) { /* no media --> data handler */
@@ -997,7 +979,7 @@ static int mov_write_mvhd_tag(ByteIOContext *pb, MOVContext *mov)
     int64_t maxTrackLenTemp, maxTrackLen = 0;
     int version;
 
-    for (i=0; i<MAX_STREAMS; i++) {
+    for (i=0; i<mov->nb_streams; i++) {
         if(mov->tracks[i].entry > 0) {
             maxTrackLenTemp = av_rescale_rnd(mov->tracks[i].trackDuration, globalTimescale, mov->tracks[i].timescale, AV_ROUND_UP);
             if(maxTrackLen < maxTrackLenTemp)
@@ -1084,7 +1066,7 @@ static int mov_write_string_data_tag(ByteIOContext *pb, const char *data, int lo
     }
 }
 
-static int mov_write_string_tag(ByteIOContext *pb, char *name, char *value, int long_style){
+static int mov_write_string_tag(ByteIOContext *pb, const char *name, const char *value, int long_style){
     int size = 0;
     if ( value && value[0] ) {
         offset_t pos = url_ftell(pb);
@@ -1185,8 +1167,9 @@ static int mov_write_udta_tag(ByteIOContext *pb, MOVContext* mov,
     /* iTunes meta data */
     mov_write_meta_tag(pb, mov, s);
 
+  if(mov->mode == MODE_MOV){ // the title field breaks gtkpod with mp4 and my suspicion is that stuff isnt valid in mp4
     /* Requirements */
-    for (i=0; i<MAX_STREAMS; i++) {
+    for (i=0; i<mov->nb_streams; i++) {
         if(mov->tracks[i].entry <= 0) continue;
         if (mov->tracks[i].enc->codec_id == CODEC_ID_AAC ||
             mov->tracks[i].enc->codec_id == CODEC_ID_MPEG4) {
@@ -1203,6 +1186,7 @@ static int mov_write_udta_tag(ByteIOContext *pb, MOVContext* mov,
         mov_write_string_tag(pb, "\251enc", LIBAVFORMAT_IDENT, 0);
     mov_write_string_tag(pb, "\251des", s->comment       , 0);
     mov_write_string_tag(pb, "\251gen", s->genre         , 0);
+  }
 
     return updateSize(pb, pos);
 }
@@ -1222,9 +1206,9 @@ static size_t ascii_to_wc (ByteIOContext *pb, char *b, size_t n)
     return 2*n;
 }
 
-static uint16_t language_code (char *str)
+static uint16_t language_code (const char *str)
 {
-    return ((((str[0]-'a') & 0x1F)<<10) + (((str[1]-'a') & 0x1F)<<5) + ((str[2]-'a') & 0x1F));
+    return ((((str[0]-0x60) & 0x1F)<<10) + (((str[1]-0x60) & 0x1F)<<5) + ((str[2]-0x60) & 0x1F));
 }
 
 static int mov_write_uuidusmt_tag (ByteIOContext *pb, AVFormatContext *s)
@@ -1245,18 +1229,45 @@ static int mov_write_uuidusmt_tag (ByteIOContext *pb, AVFormatContext *s)
 
         put_be32(pb, 0); /* size placeholder*/
         put_tag(pb, "MTDT");
-        put_be16(pb, 1);
+        put_be16(pb, 4);
         size += 10;
 
+        // ?
+        put_be16(pb, 0x0C);                 /* size */
+        put_be32(pb, 0x0B);                 /* type */
+        put_be16(pb, language_code("und")); /* language */
+        put_be16(pb, 0x0);                  /* ? */
+        put_be16(pb, 0x021C);               /* data */
+        size += 12;
+
+        // Encoder
+        len = strlen(LIBAVCODEC_IDENT)+1;
+        put_be16(pb, len*2+10);             /* size */
+        put_be32(pb, 0x04);                 /* type */
+        put_be16(pb, language_code("eng")); /* language */
+        put_be16(pb, 0x01);                 /* ? */
+        ascii_to_wc(pb, LIBAVCODEC_IDENT, len);
+        size += len*2+10;
+
         // Title
         len = strlen(s->title)+1;
         put_be16(pb, len*2+10);             /* size */
         put_be32(pb, 0x01);                 /* type */
-        put_be16(pb, language_code("und")); /* language */
+        put_be16(pb, language_code("eng")); /* language */
         put_be16(pb, 0x01);                 /* ? */
         ascii_to_wc (pb, s->title, len);
         size += len*2+10;
 
+        // Date
+//        snprintf(dt,32,"%04d/%02d/%02d %02d:%02d:%02d",t_st->tm_year+1900,t_st->tm_mon+1,t_st->tm_mday,t_st->tm_hour,t_st->tm_min,t_st->tm_sec);
+        len = strlen("2006/04/01 11:11:11")+1;
+        put_be16(pb, len*2+10);    /* size */
+        put_be32(pb, 0x03);        /* type */
+        put_be16(pb, language_code("und")); /* language */
+        put_be16(pb, 0x01);        /* ? */
+        ascii_to_wc (pb, "2006/04/01 11:11:11", len);
+        size += len*2+10;
+
         // size
         curpos = url_ftell(pb);
         url_fseek(pb, pos, SEEK_SET);
@@ -1278,7 +1289,7 @@ static int mov_write_moov_tag(ByteIOContext *pb, MOVContext *mov,
     put_tag(pb, "moov");
     mov->timescale = globalTimescale;
 
-    for (i=0; i<MAX_STREAMS; i++) {
+    for (i=0; i<mov->nb_streams; i++) {
         if(mov->tracks[i].entry <= 0) continue;
 
         if(mov->tracks[i].enc->codec_type == CODEC_TYPE_VIDEO) {
@@ -1297,7 +1308,7 @@ static int mov_write_moov_tag(ByteIOContext *pb, MOVContext *mov,
 
     mov_write_mvhd_tag(pb, mov);
     //mov_write_iods_tag(pb, mov);
-    for (i=0; i<MAX_STREAMS; i++) {
+    for (i=0; i<mov->nb_streams; i++) {
         if(mov->tracks[i].entry > 0) {
             mov_write_trak_tag(pb, &(mov->tracks[i]));
         }
@@ -1311,7 +1322,7 @@ static int mov_write_moov_tag(ByteIOContext *pb, MOVContext *mov,
     return updateSize(pb, pos);
 }
 
-int mov_write_mdat_tag(ByteIOContext *pb, MOVContext* mov)
+static int mov_write_mdat_tag(ByteIOContext *pb, MOVContext* mov)
 {
     put_be32(pb, 8);    // placeholder for extended size field (64 bit)
     put_tag(pb, "wide");
@@ -1452,6 +1463,7 @@ static int mov_write_header(AVFormatContext *s)
         }else if(st->codec->codec_type == CODEC_TYPE_AUDIO){
             track->tag = mov_find_audio_codec_tag(s, track);
             av_set_pts_info(st, 64, 1, st->codec->sample_rate);
+            track->sampleSize = (av_get_bits_per_sample(st->codec->codec_id) >> 3) * st->codec->channels;
         }
         track->language = ff_mov_iso639_to_lang(st->language, mov->mode != MODE_MOV);
         track->mode = mov->mode;
@@ -1459,6 +1471,7 @@ static int mov_write_header(AVFormatContext *s)
 
     mov_write_mdat_tag(pb, mov);
     mov->time = s->timestamp + 0x7C25B080; //1970 based -> 1904 based
+    mov->nb_streams = s->nb_streams;
 
     put_flush_packet(pb);
 
@@ -1471,49 +1484,30 @@ static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
     ByteIOContext *pb = &s->pb;
     MOVTrack *trk = &mov->tracks[pkt->stream_index];
     AVCodecContext *enc = trk->enc;
-    int cl, id;
     unsigned int samplesInChunk = 0;
     int size= pkt->size;
 
     if (url_is_streamed(&s->pb)) return 0; /* Can't handle that */
     if (!size) return 0; /* Discard 0 sized packets */
 
-    if (enc->codec_type == CODEC_TYPE_AUDIO) {
-        switch (enc->codec_id) {
-        case CODEC_ID_AMR_NB:
-            { /* We must find out how many AMR blocks there are in one packet */
-                static uint16_t packed_size[16] =
-                    {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 0};
-                int len = 0;
-
-                while (len < size && samplesInChunk < 100) {
-                    len += packed_size[(pkt->data[len] >> 3) & 0x0F];
-                    samplesInChunk++;
-                }
-            }
-            break;
-        case CODEC_ID_PCM_MULAW:
-        case CODEC_ID_PCM_ALAW:
-            samplesInChunk = size/enc->channels;
-            break;
-        case CODEC_ID_PCM_S16BE:
-        case CODEC_ID_PCM_S16LE:
-            samplesInChunk = size/(2*enc->channels);
-            break;
-        case CODEC_ID_PCM_S24BE:
-        case CODEC_ID_PCM_S24LE:
-            samplesInChunk = size/(3*enc->channels);
-            break;
-        case CODEC_ID_PCM_S32BE:
-        case CODEC_ID_PCM_S32LE:
-            samplesInChunk = size/(4*enc->channels);
-            break;
-        default:
-            samplesInChunk = 1;
+    if (enc->codec_id == CODEC_ID_AMR_NB) {
+        /* We must find out how many AMR blocks there are in one packet */
+        static uint16_t packed_size[16] =
+            {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 0};
+        int len = 0;
+
+        while (len < size && samplesInChunk < 100) {
+            len += packed_size[(pkt->data[len] >> 3) & 0x0F];
+            samplesInChunk++;
         }
-    } else {
+        if(samplesInChunk > 1){
+            av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, inplement a AVParser for it\n");
+            return -1;
+        }
+    } else if (trk->sampleSize)
+        samplesInChunk = size/trk->sampleSize;
+    else
         samplesInChunk = 1;
-    }
 
     /* copy extradata if it exists */
     if (trk->vosLen == 0 && enc->extradata_size > 0) {
@@ -1530,29 +1524,22 @@ static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
         size = pkt->size;
     }
 
-    cl = trk->entry / MOV_INDEX_CLUSTER_SIZE;
-    id = trk->entry % MOV_INDEX_CLUSTER_SIZE;
-
-    if (trk->ents_allocated <= trk->entry) {
-        trk->cluster = av_realloc(trk->cluster, (cl+1)*sizeof(void*));
+    if (!(trk->entry % MOV_INDEX_CLUSTER_SIZE)) {
+        trk->cluster = av_realloc(trk->cluster, (trk->entry + MOV_INDEX_CLUSTER_SIZE) * sizeof(*trk->cluster));
         if (!trk->cluster)
             return -1;
-        trk->cluster[cl] = av_malloc(MOV_INDEX_CLUSTER_SIZE*sizeof(MOVIentry));
-        if (!trk->cluster[cl])
-            return -1;
-        trk->ents_allocated += MOV_INDEX_CLUSTER_SIZE;
     }
 
-    trk->cluster[cl][id].pos = url_ftell(pb);
-    trk->cluster[cl][id].samplesInChunk = samplesInChunk;
-    trk->cluster[cl][id].size = size;
-    trk->cluster[cl][id].entries = samplesInChunk;
+    trk->cluster[trk->entry].pos = url_ftell(pb);
+    trk->cluster[trk->entry].samplesInChunk = samplesInChunk;
+    trk->cluster[trk->entry].size = size;
+    trk->cluster[trk->entry].entries = samplesInChunk;
     if(enc->codec_type == CODEC_TYPE_VIDEO) {
         if (pkt->dts != pkt->pts)
             trk->hasBframes = 1;
-        trk->cluster[cl][id].cts = pkt->pts - pkt->dts;
-        trk->cluster[cl][id].key_frame = !!(pkt->flags & PKT_FLAG_KEY);
-        if(trk->cluster[cl][id].key_frame)
+        trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
+        trk->cluster[trk->entry].key_frame = !!(pkt->flags & PKT_FLAG_KEY);
+        if(trk->cluster[trk->entry].key_frame)
             trk->hasKeyframes++;
     }
     trk->entry++;
@@ -1570,7 +1557,7 @@ static int mov_write_trailer(AVFormatContext *s)
     MOVContext *mov = s->priv_data;
     ByteIOContext *pb = &s->pb;
     int res = 0;
-    int i, j;
+    int i;
 
     offset_t moov_pos = url_ftell(pb);
 
@@ -1589,15 +1576,11 @@ static int mov_write_trailer(AVFormatContext *s)
 
     mov_write_moov_tag(pb, mov, s);
 
-    for (i=0; i<MAX_STREAMS; i++) {
-        for (j=0; j<mov->tracks[i].ents_allocated/MOV_INDEX_CLUSTER_SIZE; j++) {
-            av_free(mov->tracks[i].cluster[j]);
-        }
-        av_free(mov->tracks[i].cluster);
+    for (i=0; i<mov->nb_streams; i++) {
+        av_freep(&mov->tracks[i].cluster);
+
         if( mov->tracks[i].vosLen ) av_free( mov->tracks[i].vosData );
 
-        mov->tracks[i].cluster = NULL;
-        mov->tracks[i].ents_allocated = mov->tracks[i].entry = 0;
     }
 
     put_flush_packet(pb);
@@ -1605,7 +1588,8 @@ static int mov_write_trailer(AVFormatContext *s)
     return res;
 }
 
-static AVOutputFormat mov_oformat = {
+#ifdef CONFIG_MOV_MUXER
+AVOutputFormat mov_muxer = {
     "mov",
     "mov format",
     NULL,
@@ -1618,8 +1602,9 @@ static AVOutputFormat mov_oformat = {
     mov_write_trailer,
     .flags = AVFMT_GLOBALHEADER,
 };
-
-static AVOutputFormat _3gp_oformat = {
+#endif
+#ifdef CONFIG_TGP_MUXER
+AVOutputFormat tgp_muxer = {
     "3gp",
     "3gp format",
     NULL,
@@ -1632,8 +1617,9 @@ static AVOutputFormat _3gp_oformat = {
     mov_write_trailer,
     .flags = AVFMT_GLOBALHEADER,
 };
-
-static AVOutputFormat mp4_oformat = {
+#endif
+#ifdef CONFIG_MP4_MUXER
+AVOutputFormat mp4_muxer = {
     "mp4",
     "mp4 format",
     "application/mp4",
@@ -1646,8 +1632,9 @@ static AVOutputFormat mp4_oformat = {
     mov_write_trailer,
     .flags = AVFMT_GLOBALHEADER,
 };
-
-static AVOutputFormat psp_oformat = {
+#endif
+#ifdef CONFIG_PSP_MUXER
+AVOutputFormat psp_muxer = {
     "psp",
     "psp mp4 format",
     NULL,
@@ -1660,8 +1647,9 @@ static AVOutputFormat psp_oformat = {
     mov_write_trailer,
     .flags = AVFMT_GLOBALHEADER,
 };
-
-static AVOutputFormat _3g2_oformat = {
+#endif
+#ifdef CONFIG_TG2_MUXER
+AVOutputFormat tg2_muxer = {
     "3g2",
     "3gp2 format",
     NULL,
@@ -1674,13 +1662,4 @@ static AVOutputFormat _3g2_oformat = {
     mov_write_trailer,
     .flags = AVFMT_GLOBALHEADER,
 };
-
-int movenc_init(void)
-{
-    av_register_output_format(&mov_oformat);
-    av_register_output_format(&_3gp_oformat);
-    av_register_output_format(&mp4_oformat);
-    av_register_output_format(&psp_oformat);
-    av_register_output_format(&_3g2_oformat);
-    return 0;
-}
+#endif