From 9024befc30159e036b1eaeb6a8aaabbe8159f3d0 Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <michaelni@gmx.at>
Date: Fri, 16 Sep 2011 02:41:27 +0200
Subject: [PATCH 2/2] movenc: set alternative group for audio and subtitles

Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
---
 libavformat/movenc.c |   13 +++++++++++--
 libavformat/movenc.h |    2 ++
 2 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index b1dafdb..0a741de 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -1211,7 +1211,7 @@ static int mov_write_tkhd_tag(AVIOContext *pb, MOVTrack *track, AVStream *st)
     (version == 1) ? avio_wb32(pb, 104) : avio_wb32(pb, 92); /* size */
     ffio_wfourcc(pb, "tkhd");
     avio_w8(pb, version);
-    avio_wb24(pb, 0xf); /* flags (track enabled) */
+    avio_wb24(pb, track->secondary ? 0x2 : 0xf); /* flags (first track enabled) */
     if (version == 1) {
         avio_wb64(pb, track->time);
         avio_wb64(pb, track->time);
@@ -1225,7 +1225,7 @@ static int mov_write_tkhd_tag(AVIOContext *pb, MOVTrack *track, AVStream *st)
 
     avio_wb32(pb, 0); /* reserved */
     avio_wb32(pb, 0); /* reserved */
-    avio_wb32(pb, 0x0); /* reserved (Layer & Alternate group) */
+    avio_wb32(pb, track->alternative_group); /* reserved (Layer & Alternate group) */
     /* Volume, only for audio */
     if(track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
         avio_wb16(pb, 0x0100);
@@ -1807,6 +1807,7 @@ static int mov_write_moov_tag(AVIOContext *pb, MOVMuxContext *mov,
 {
     int i;
     int64_t pos = avio_tell(pb);
+    int not_first[AVMEDIA_TYPE_NB]={0};
     avio_wb32(pb, 0); /* size placeholder*/
     ffio_wfourcc(pb, "moov");
 
@@ -1834,6 +1835,14 @@ static int mov_write_moov_tag(AVIOContext *pb, MOVMuxContext *mov,
     //mov_write_iods_tag(pb, mov);
     for (i=0; i<mov->nb_streams; i++) {
         if(mov->tracks[i].entry > 0) {
+            if(i < s->nb_streams){
+                int codec_type= s->streams[i]->codec->codec_type;
+                if(codec_type==AVMEDIA_TYPE_AUDIO || codec_type==AVMEDIA_TYPE_SUBTITLE){
+                    mov->tracks[i].alternative_group= 1 + (codec_type==AVMEDIA_TYPE_SUBTITLE);
+                    mov->tracks[i].secondary= not_first[codec_type];
+                    not_first[codec_type]= 1;
+                }
+            }
             mov_write_trak_tag(pb, &(mov->tracks[i]), i < s->nb_streams ? s->streams[i] : NULL);
         }
     }
diff --git a/libavformat/movenc.h b/libavformat/movenc.h
index 610683f..4676983 100644
--- a/libavformat/movenc.h
+++ b/libavformat/movenc.h
@@ -78,6 +78,8 @@ typedef struct MOVIndex {
 #define MOV_TRACK_STPS         0x0002
     uint32_t    flags;
     int         language;
+    int         alternative_group;
+    int         secondary;
     int         trackID;
     int         tag; ///< stsd fourcc
     AVCodecContext *enc;
-- 
1.7.4.1

