]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blobdiff - libavformat/mov.c
Parse 'cslg' atom to retrieve dts shift when 'ctts' duration is negative.
[frescor/ffmpeg.git] / libavformat / mov.c
index 4621fc8a29c4ae46305b89ef0cb22824a32e89a5..3cc9812a4d1ef787c7ae9f3c64e290227e977e1f 100644 (file)
@@ -33,7 +33,7 @@
 #include "isom.h"
 #include "libavcodec/mpeg4audio.h"
 #include "libavcodec/mpegaudiodata.h"
-#include "libavcodec/bitstream.h"
+#include "libavcodec/get_bits.h"
 
 #if CONFIG_ZLIB
 #include <zlib.h>
@@ -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 },
 };
 
@@ -881,9 +882,9 @@ static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
                         color_table = ff_qt_default_palette_256;
 
                     for (j = 0; j < color_count; j++) {
-                        r = color_table[j * 4 + 0];
-                        g = color_table[j * 4 + 1];
-                        b = color_table[j * 4 + 2];
+                        r = color_table[j * 3 + 0];
+                        g = color_table[j * 3 + 1];
+                        b = color_table[j * 3 + 2];
                         st->codec->palctrl->palette[j] =
                             (r << 16) | (g << 8) | (b);
                     }
@@ -997,7 +998,8 @@ static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
             // ttxt stsd contains display flags, justification, background
             // color, fonts, and default styles, so fake an atom to read it
             MOVAtom fake_atom = { .size = size - (url_ftell(pb) - start_pos) };
-            mov_read_glbl(c, pb, fake_atom);
+            if (format != AV_RL32("mp4s")) // mp4s contains a regular esds atom
+                mov_read_glbl(c, pb, fake_atom);
             st->codec->codec_id= id;
             st->codec->width = sc->width;
             st->codec->height = sc->height;
@@ -1033,6 +1035,9 @@ static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
 #endif
     /* no ifdef since parameters are always those */
     case CODEC_ID_QCELP:
+        // force sample rate for qcelp when not stored in mov
+        if (st->codec->codec_tag != MKTAG('Q','c','l','p'))
+            st->codec->sample_rate = 8000;
         st->codec->frame_size= 160;
         st->codec->channels= 1; /* really needed */
         break;
@@ -1166,7 +1171,7 @@ static int mov_read_stsz(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
 
     num_bytes = (entries*field_size+4)>>3;
 
-    buf = av_malloc(num_bytes);
+    buf = av_malloc(num_bytes+FF_INPUT_BUFFER_PADDING_SIZE);
     if (!buf) {
         av_freep(&sc->sample_sizes);
         return AVERROR(ENOMEM);
@@ -1231,6 +1236,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) // will happen with jp2 files
+        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];
@@ -1254,10 +1284,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;
 
@@ -1281,6 +1307,12 @@ static void mov_build_index(MOVContext *mov, AVStream *st)
         int rescaled = sc->time_offset < 0 ? av_rescale(sc->time_offset, sc->time_scale, mov->time_scale) : sc->time_offset;
         assert(sc->time_offset % sc->time_rate == 0);
         current_dts = - (rescaled / sc->time_rate);
+        if (sc->ctts_data && sc->ctts_data[0].duration / sc->stts_data[0].duration > 16) {
+            /* more than 16 frames delay, dts are likely wrong
+               this happens with files created by iMovie */
+            sc->wrong_dts = 1;
+            st->codec->has_b_frames = 1;
+        }
     }
 
     /* only use old uncompressed audio chunk demuxing when stts specifies it */
@@ -1292,6 +1324,9 @@ static void mov_build_index(MOVContext *mov, AVStream *st)
         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];
@@ -1440,6 +1475,9 @@ static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
 #if CONFIG_H263_DECODER
     case CODEC_ID_H263:
 #endif
+#if CONFIG_H264_DECODER
+    case CODEC_ID_H264:
+#endif
 #if CONFIG_MPEG4_DECODER
     case CODEC_ID_MPEG4:
 #endif
@@ -1795,6 +1833,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 },
@@ -1974,7 +2013,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 &&