]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blobdiff - libavformat/mov.c
parse 'stps' atom to set keyframe, partial sync sample for mpeg-2 open gop
[frescor/ffmpeg.git] / libavformat / mov.c
index 175c2c24828d09bc20b73648819c5b8390852862..cf12409055db3afb1144dff9540ba69693f55e9d 100644 (file)
@@ -384,6 +384,7 @@ static const AVCodecTag mp4_audio_types[] = {
     { CODEC_ID_MP3ON4, AOT_L1   }, /* layer 1 */
     { CODEC_ID_MP3ON4, AOT_L2   }, /* layer 2 */
     { CODEC_ID_MP3ON4, AOT_L3   }, /* layer 3 */
+    { CODEC_ID_MP4ALS, AOT_ALS  }, /* MPEG-4 ALS */
     { CODEC_ID_NONE,   AOT_NULL },
 };
 
@@ -1101,6 +1102,35 @@ static int mov_read_stsc(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
     return 0;
 }
 
+static int mov_read_stps(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
+{
+    AVStream *st;
+    MOVStreamContext *sc;
+    unsigned i, entries;
+
+    if (c->fc->nb_streams < 1)
+        return 0;
+    st = c->fc->streams[c->fc->nb_streams-1];
+    sc = st->priv_data;
+
+    get_be32(pb); // version + flags
+
+    entries = get_be32(pb);
+    if (entries >= UINT_MAX / sizeof(*sc->stps_data))
+        return -1;
+    sc->stps_data = av_malloc(entries * sizeof(*sc->stps_data));
+    if (!sc->stps_data)
+        return AVERROR(ENOMEM);
+    sc->stps_count = entries;
+
+    for (i = 0; i < entries; i++) {
+        sc->stps_data[i] = get_be32(pb);
+        //dprintf(c->fc, "stps %d\n", sc->stps_data[i]);
+    }
+
+    return 0;
+}
+
 static int mov_read_stss(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
 {
     AVStream *st = c->fc->streams[c->fc->nb_streams-1];
@@ -1235,6 +1265,31 @@ static int mov_read_stts(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
     return 0;
 }
 
+static int mov_read_cslg(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
+{
+    AVStream *st;
+    MOVStreamContext *sc;
+
+    if (c->fc->nb_streams < 1)
+        return 0;
+    st = c->fc->streams[c->fc->nb_streams-1];
+    sc = st->priv_data;
+
+    get_be32(pb); // version + flags
+
+    sc->dts_shift = get_be32(pb);
+    dprintf(c->fc, "dts shift %d\n", sc->dts_shift);
+
+    sc->time_rate= av_gcd(sc->time_rate, FFABS(sc->dts_shift));
+
+    get_be32(pb); // least dts to pts delta
+    get_be32(pb); // greatest dts to pts delta
+    get_be32(pb); // pts start
+    get_be32(pb); // pts end
+
+    return 0;
+}
+
 static int mov_read_ctts(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
 {
     AVStream *st = c->fc->streams[c->fc->nb_streams-1];
@@ -1258,10 +1313,6 @@ static int mov_read_ctts(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
         int count    =get_be32(pb);
         int duration =get_be32(pb);
 
-        if (duration < 0) {
-            sc->wrong_dts = 1;
-            st->codec->has_b_frames = 1;
-        }
         sc->ctts_data[i].count   = count;
         sc->ctts_data[i].duration= duration;
 
@@ -1278,6 +1329,7 @@ static void mov_build_index(MOVContext *mov, AVStream *st)
     unsigned int stts_index = 0;
     unsigned int stsc_index = 0;
     unsigned int stss_index = 0;
+    unsigned int stps_index = 0;
     unsigned int i, j;
 
     /* adjust first dts according to edit list */
@@ -1298,10 +1350,13 @@ static void mov_build_index(MOVContext *mov, AVStream *st)
           sc->stts_count == 1 && sc->stts_data[0].duration == 1)) {
         unsigned int current_sample = 0;
         unsigned int stts_sample = 0;
-        unsigned int keyframe, sample_size;
+        unsigned int sample_size;
         unsigned int distance = 0;
         int key_off = sc->keyframes && sc->keyframes[0] == 1;
 
+        sc->dts_shift /= sc->time_rate;
+        current_dts -= sc->dts_shift;
+
         st->nb_frames = sc->sample_count;
         for (i = 0; i < sc->chunk_count; i++) {
             current_offset = sc->chunk_offsets[i];
@@ -1309,16 +1364,23 @@ static void mov_build_index(MOVContext *mov, AVStream *st)
                 i + 1 == sc->stsc_data[stsc_index + 1].first)
                 stsc_index++;
             for (j = 0; j < sc->stsc_data[stsc_index].count; j++) {
+                int keyframe = 0;
                 if (current_sample >= sc->sample_count) {
                     av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
                     return;
                 }
-                keyframe = !sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index];
-                if (keyframe) {
-                    distance = 0;
+
+                if (!sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index]) {
+                    keyframe = 1;
                     if (stss_index + 1 < sc->keyframe_count)
                         stss_index++;
+                } else if (sc->stps_count && current_sample+key_off == sc->stps_data[stps_index]) {
+                    keyframe = 1;
+                    if (stps_index + 1 < sc->stps_count)
+                        stps_index++;
                 }
+                if (keyframe)
+                    distance = 0;
                 sample_size = sc->sample_size > 0 ? sc->sample_size : sc->sample_sizes[current_sample];
                 if(sc->pseudo_stream_id == -1 ||
                    sc->stsc_data[stsc_index].id - 1 == sc->pseudo_stream_id) {
@@ -1467,6 +1529,7 @@ static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
     av_freep(&sc->sample_sizes);
     av_freep(&sc->keyframes);
     av_freep(&sc->stts_data);
+    av_freep(&sc->stps_data);
 
     return 0;
 }
@@ -1808,6 +1871,7 @@ static int mov_read_elst(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
 static const MOVParseTableEntry mov_default_parse_table[] = {
 { MKTAG('a','v','s','s'), mov_read_extradata },
 { MKTAG('c','o','6','4'), mov_read_stco },
+{ MKTAG('c','s','l','g'), mov_read_cslg },
 { MKTAG('c','t','t','s'), mov_read_ctts }, /* composition time to sample */
 { MKTAG('d','i','n','f'), mov_read_default },
 { MKTAG('d','r','e','f'), mov_read_dref },
@@ -1835,6 +1899,7 @@ static const MOVParseTableEntry mov_default_parse_table[] = {
 { MKTAG('p','a','s','p'), mov_read_pasp },
 { MKTAG('s','t','b','l'), mov_read_default },
 { MKTAG('s','t','c','o'), mov_read_stco },
+{ MKTAG('s','t','p','s'), mov_read_stps },
 { MKTAG('s','t','s','c'), mov_read_stsc },
 { MKTAG('s','t','s','d'), mov_read_stsd }, /* sample description */
 { MKTAG('s','t','s','s'), mov_read_stss }, /* sync sample */
@@ -1987,7 +2052,7 @@ static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
     pkt->dts = sample->timestamp;
     if (sc->ctts_data) {
         assert(sc->ctts_data[sc->ctts_index].duration % sc->time_rate == 0);
-        pkt->pts = pkt->dts + sc->ctts_data[sc->ctts_index].duration / sc->time_rate;
+        pkt->pts = pkt->dts + sc->dts_shift + sc->ctts_data[sc->ctts_index].duration / sc->time_rate;
         /* update ctts context */
         sc->ctts_sample++;
         if (sc->ctts_index < sc->ctts_count &&