[FFmpeg-devel] [PATCH 3/4] movenc: allow writing of multiple trefs per track

Jean First jeanfirst at gmail.com
Sat Aug 25 11:44:13 CEST 2012


Signed-off-by: Jean First <jeanfirst at gmail.com>
---
 libavformat/movenc.c |   42 +++++++++++++++++++++++++++++++-----------
 libavformat/movenc.h |    3 ++-
 2 files changed, 33 insertions(+), 12 deletions(-)

diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index 1cb1853..aa055ee 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -1665,12 +1665,18 @@ static int mov_write_edts_tag(AVIOContext *pb, MOVTrack *track)
 
 static int mov_write_tref_tag(AVIOContext *pb, MOVTrack *track)
 {
-    avio_wb32(pb, 20);   // size
+    int i;
+    int64_t pos1, pos2;
+    pos1 = avio_tell(pb);
+    avio_wb32(pb, 0); /* size */
     ffio_wfourcc(pb, "tref");
-    avio_wb32(pb, 12);   // size (subatom)
+    pos2 = avio_tell(pb);
+    avio_wb32(pb, 0);  // size (subatom)
     avio_wl32(pb, track->tref_tag);
-    avio_wb32(pb, track->tref_id);
-    return 20;
+    for (i = 0; i < track->tref_count; i++)
+        avio_wb32(pb, track->tref_ids[i]);
+    update_size(pb, pos2);
+    return update_size(pb, pos1);
 }
 
 // goes at the end of each track!  ... Critical for PSP playback ("Incompatible data" without it)
@@ -2205,21 +2211,35 @@ static int mov_write_moov_tag(AVIOContext *pb, MOVMuxContext *mov,
     if (mov->chapter_track)
         for (i=0; i<s->nb_streams; i++) {
             mov->tracks[i].tref_tag = MKTAG('c','h','a','p');
-            mov->tracks[i].tref_id = mov->tracks[mov->chapter_track].track_id;
+            mov->tracks[i].tref_count++;
+            mov->tracks[i].tref_ids = av_realloc(mov->tracks[i].tref_ids,
+                    mov->tracks[i].tref_count * sizeof(*mov->tracks[i].tref_ids));
+            if (!mov->tracks[i].tref_ids)
+                return AVERROR(ENOMEM);
+            mov->tracks[i].tref_ids[mov->tracks[i].tref_count - 1] = mov->tracks[mov->chapter_track].track_id;
         }
     for (i = 0; i < mov->nb_streams; i++) {
         if (mov->tracks[i].tag == MKTAG('r','t','p',' ')) {
             mov->tracks[i].tref_tag = MKTAG('h','i','n','t');
-            mov->tracks[i].tref_id =
-                mov->tracks[mov->tracks[i].src_track].track_id;
+            mov->tracks[i].tref_count++;
+            mov->tracks[i].tref_ids = av_realloc(mov->tracks[i].tref_ids,
+                    mov->tracks[i].tref_count * sizeof(*mov->tracks[i].tref_ids));
+            if (!mov->tracks[i].tref_ids)
+                return AVERROR(ENOMEM);
+            mov->tracks[i].tref_ids[mov->tracks[i].tref_count - 1] = mov->tracks[mov->tracks[i].src_track].track_id;
         }
     }
     for (i = 0; i < mov->nb_streams; i++) {
         if (mov->tracks[i].tag == MKTAG('t','m','c','d')) {
-            int src_trk = mov->tracks[i].src_track;
-            mov->tracks[src_trk].tref_tag = mov->tracks[i].tag;
-            mov->tracks[src_trk].tref_id  = mov->tracks[i].track_id;
-            mov->tracks[i].track_duration = mov->tracks[src_trk].track_duration;
+            MOVTrack *src_track = &mov->tracks[mov->tracks[i].src_track];
+            src_track->tref_tag = mov->tracks[i].tag;
+            mov->tracks[i].track_duration = src_track->track_duration;
+            src_track->tref_count++;
+            src_track->tref_ids = av_realloc(src_track->tref_ids,
+                    src_track->tref_count * sizeof(*src_track->tref_ids));
+            if (!src_track->tref_ids)
+                return AVERROR(ENOMEM);
+            src_track->tref_ids[src_track->tref_count - 1] = mov->tracks[i].track_id;
         }
     }
 
diff --git a/libavformat/movenc.h b/libavformat/movenc.h
index 72427d8..e3cb8e5 100644
--- a/libavformat/movenc.h
+++ b/libavformat/movenc.h
@@ -103,7 +103,8 @@ typedef struct MOVIndex {
     int         audio_vbr;
     int         height; ///< active picture (w/o VBI) height for D-10/IMX
     uint32_t    tref_tag;
-    int         tref_id; ///< trackID of the referenced track
+    uint32_t    *tref_ids; ///< trackID of the referenced track
+    unsigned    tref_count;
     int64_t     start_dts;
 
     int         hint_track;   ///< the track that hints this track, -1 if no hint track is set
-- 
1.7.9.4



More information about the ffmpeg-devel mailing list