]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/mov.c
Fix issue357
[ffmpeg] / libavformat / mov.c
index 50474ba0abbdbf8b97461598b8ab41ccf2bd2255..5fd41f5f5693d6b3da6e556e81d462216bcce4db 100644 (file)
@@ -75,11 +75,6 @@ typedef struct {
     int64_t size; /* total size (excluding the size and type fields) */
 } MOV_atom_t;
 
-typedef struct {
-    offset_t offset;
-    int64_t size;
-} MOV_mdat_t;
-
 struct MOVParseTableEntry;
 
 typedef struct MOVStreamContext {
@@ -107,6 +102,8 @@ typedef struct MOVStreamContext {
     unsigned int bytes_per_frame;
     unsigned int samples_per_frame;
     int dv_audio_container;
+    int pseudo_stream_id;
+    int16_t audio_cid; ///< stsd audio compression id
 } MOVStreamContext;
 
 typedef struct MOVContext {
@@ -116,8 +113,6 @@ typedef struct MOVContext {
     int found_moov; /* when both 'moov' and 'mdat' sections has been found */
     int found_mdat; /* we suppose we have enough data to read the file */
     AVPaletteControl palette_control;
-    MOV_mdat_t *mdat_list;
-    int mdat_count;
     DVDemuxContext *dv_demux;
     AVFormatContext *dv_fctx;
     int isom; /* 1 if file is ISO Media (mp4/3gp) */
@@ -160,7 +155,8 @@ static int mov_read_default(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
         }
         total_size += 8;
         a.offset += 8;
-        dprintf(c->fc, "type: %08x  %.4s  sz: %"PRIx64"  %"PRIx64"   %"PRIx64"\n", a.type, (char*)&a.type, a.size, atom.size, total_size);
+        dprintf(c->fc, "type: %08x  %.4s  sz: %"PRIx64"  %"PRIx64"   %"PRIx64"\n",
+                a.type, (char*)&a.type, a.size, atom.size, total_size);
         if (a.size == 1) { /* 64 bit extended size */
             a.size = get_be64(pb) - 8;
             a.offset += 8;
@@ -217,8 +213,10 @@ static int mov_read_hdlr(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
     ctype = get_le32(pb);
     type = get_le32(pb); /* component subtype */
 
-    dprintf(c->fc, "ctype= %c%c%c%c (0x%08x)\n", *((char *)&ctype), ((char *)&ctype)[1], ((char *)&ctype)[2], ((char *)&ctype)[3], (int) ctype);
-    dprintf(c->fc, "stype= %c%c%c%c\n", *((char *)&type), ((char *)&type)[1], ((char *)&type)[2], ((char *)&type)[3]);
+    dprintf(c->fc, "ctype= %c%c%c%c (0x%08x)\n", *((char *)&ctype), ((char *)&ctype)[1],
+            ((char *)&ctype)[2], ((char *)&ctype)[3], (int) ctype);
+    dprintf(c->fc, "stype= %c%c%c%c\n",
+            *((char *)&type), ((char *)&type)[1], ((char *)&type)[2], ((char *)&type)[3]);
     if(!ctype)
         c->isom = 1;
     if(type == MKTAG('v', 'i', 'd', 'e'))
@@ -229,7 +227,6 @@ static int mov_read_hdlr(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
         st->codec->codec_id = CODEC_ID_MP2;
     else if(type == MKTAG('s', 'u', 'b', 'p')) {
         st->codec->codec_type = CODEC_TYPE_SUBTITLE;
-        st->codec->codec_id = CODEC_ID_DVD_SUBTITLE;
     }
     get_be32(pb); /* component  manufacture */
     get_be32(pb); /* component flags */
@@ -313,10 +310,6 @@ static int mov_read_mdat(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
 {
     if(atom.size == 0) /* wrong one (MP4) */
         return 0;
-    c->mdat_list = av_realloc(c->mdat_list, (c->mdat_count + 1) * sizeof(*c->mdat_list));
-    c->mdat_list[c->mdat_count].offset = atom.offset;
-    c->mdat_list[c->mdat_count].size = atom.size;
-    c->mdat_count++;
     c->found_mdat=1;
     if(c->found_moov)
         return 1; /* found both, just go */
@@ -578,7 +571,7 @@ static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
     int color_dec;
     int color_greyscale;
     const uint8_t *color_table;
-    int j;
+    int j, pseudo_stream_id;
     unsigned char r, g, b;
 
     get_byte(pb); /* version */
@@ -586,7 +579,7 @@ static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
 
     entries = get_be32(pb);
 
-    while(entries--) { //Parsing Sample description table
+    for(pseudo_stream_id=0; pseudo_stream_id<entries; pseudo_stream_id++) { //Parsing Sample description table
         enum CodecID id;
         MOV_atom_t a = { 0, 0, 0 };
         offset_t start_pos = url_ftell(pb);
@@ -597,11 +590,17 @@ static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
         get_be16(pb); /* reserved */
         get_be16(pb); /* index */
 
-        if (st->codec->codec_tag) {
-            /* multiple fourcc, just skip for now */
+        if (st->codec->codec_tag &&
+            (c->fc->video_codec_id ? codec_get_id(codec_movvideo_tags, format) != c->fc->video_codec_id
+                                   : st->codec->codec_tag != MKTAG('j', 'p', 'e', 'g'))
+           ){
+            /* multiple fourcc, we skip jpeg, this isnt correct, we should export it as
+               seperate AVStream but this needs a few changes in the mov demuxer, patch
+               welcome */
             url_fskip(pb, size - (url_ftell(pb) - start_pos));
             continue;
         }
+        sc->pseudo_stream_id= pseudo_stream_id;
 
         st->codec->codec_tag = format;
         id = codec_get_id(codec_movaudio_tags, format);
@@ -617,12 +616,16 @@ static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
                 id = codec_get_id(codec_bmp_tags, format);
             if (id > 0)
                 st->codec->codec_type = CODEC_TYPE_VIDEO;
+            else if(st->codec->codec_type == CODEC_TYPE_DATA){
+                id = codec_get_id(ff_codec_movsubtitle_tags, format);
+                if(id > 0)
+                    st->codec->codec_type = CODEC_TYPE_SUBTITLE;
+            }
         }
 
-        dprintf(c->fc, "size=%d 4CC= %c%c%c%c codec_type=%d\n",
-                size,
-                (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff, (format >> 24) & 0xff,
-                st->codec->codec_type);
+        dprintf(c->fc, "size=%d 4CC= %c%c%c%c codec_type=%d\n", size,
+                (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff,
+                (format >> 24) & 0xff, st->codec->codec_type);
 
         if(st->codec->codec_type==CODEC_TYPE_VIDEO) {
             st->codec->codec_id = id;
@@ -727,10 +730,8 @@ static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
             st->codec->channels = get_be16(pb);             /* channel count */
             dprintf(c->fc, "audio channels %d\n", st->codec->channels);
             st->codec->bits_per_sample = get_be16(pb);      /* sample size */
-            /* do we need to force to 16 for AMR ? */
 
-            /* handle specific s8 codec */
-            get_be16(pb); /* compression id = 0*/
+            sc->audio_cid = get_be16(pb);
             get_be16(pb); /* packet size = 0 */
 
             st->codec->sample_rate = ((get_be32(pb) >> 16));
@@ -748,6 +749,19 @@ static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
                 else if (st->codec->bits_per_sample == 24)
                     st->codec->codec_id = CODEC_ID_PCM_S24BE;
                 break;
+            /* set values for old format before stsd version 1 appeared */
+            case CODEC_ID_MACE3:
+                sc->samples_per_frame = 6;
+                sc->bytes_per_frame = 2*st->codec->channels;
+                break;
+            case CODEC_ID_MACE6:
+                sc->samples_per_frame = 6;
+                sc->bytes_per_frame = 1*st->codec->channels;
+                break;
+            case CODEC_ID_ADPCM_IMA_QT:
+                sc->samples_per_frame = 64;
+                sc->bytes_per_frame = 34*st->codec->channels;
+                break;
             default:
                 break;
             }
@@ -777,6 +791,8 @@ static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
                 st->codec->bits_per_sample = bits_per_sample;
                 sc->sample_size = (bits_per_sample >> 3) * st->codec->channels;
             }
+        } else if(st->codec->codec_type==CODEC_TYPE_SUBTITLE){
+            st->codec->codec_id= id;
         } else {
             /* other codec type, just skip (rtp, mp4s, tmcd ...) */
             url_fskip(pb, size - (url_ftell(pb) - start_pos));
@@ -1214,9 +1230,13 @@ static int mov_read_elst(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
     edit_count= sc->edit_count = get_be32(pb);     /* entries */
 
     for(i=0; i<edit_count; i++){
+        int time;
         get_be32(pb); /* Track duration */
-        get_be32(pb); /* Media time */
+        time = get_be32(pb); /* Media time */
         get_be32(pb); /* Media rate */
+        if (time != 0)
+            av_log(c->fc, AV_LOG_WARNING, "edit list not starting at 0, "
+                   "a/v desync might occur, patch welcome\n");
     }
     dprintf(c->fc, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, sc->edit_count);
     return 0;
@@ -1312,9 +1332,10 @@ 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 i, j, k;
+    unsigned int i, j;
 
-    if (sc->sample_sizes || st->codec->codec_type == CODEC_TYPE_VIDEO || sc->dv_audio_container) {
+    if (sc->sample_sizes || st->codec->codec_type == CODEC_TYPE_VIDEO ||
+        sc->audio_cid == -2) {
         unsigned int current_sample = 0;
         unsigned int stts_sample = 0;
         unsigned int keyframe, sample_size;
@@ -1323,7 +1344,8 @@ static void mov_build_index(MOVContext *mov, AVStream *st)
         st->nb_frames = sc->sample_count;
         for (i = 0; i < sc->chunk_count; i++) {
             current_offset = sc->chunk_offsets[i];
-            if (stsc_index + 1 < sc->sample_to_chunk_sz && i + 1 == sc->sample_to_chunk[stsc_index + 1].first)
+            if (stsc_index + 1 < sc->sample_to_chunk_sz &&
+                i + 1 == sc->sample_to_chunk[stsc_index + 1].first)
                 stsc_index++;
             for (j = 0; j < sc->sample_to_chunk[stsc_index].count; j++) {
                 if (current_sample >= sc->sample_count) {
@@ -1337,9 +1359,12 @@ static void mov_build_index(MOVContext *mov, AVStream *st)
                         stss_index++;
                 }
                 sample_size = sc->sample_size > 0 ? sc->sample_size : sc->sample_sizes[current_sample];
-                dprintf(mov->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", size %d, distance %d, keyframe %d\n",
-                        st->index, current_sample, current_offset, current_dts, sample_size, distance, keyframe);
-                av_add_index_entry(st, current_offset, current_dts, sample_size, distance, keyframe ? AVINDEX_KEYFRAME : 0);
+                dprintf(mov->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
+                        "size %d, distance %d, keyframe %d\n", st->index, current_sample,
+                        current_offset, current_dts, sample_size, distance, keyframe);
+                if(sc->sample_to_chunk[stsc_index].id - 1 == sc->pseudo_stream_id)
+                    av_add_index_entry(st, current_offset, current_dts, sample_size, distance,
+                                    keyframe ? AVINDEX_KEYFRAME : 0);
                 current_offset += sample_size;
                 assert(sc->stts_data[stts_index].duration % sc->time_rate == 0);
                 current_dts += sc->stts_data[stts_index].duration / sc->time_rate;
@@ -1354,42 +1379,30 @@ static void mov_build_index(MOVContext *mov, AVStream *st)
         }
     } else { /* read whole chunk */
         unsigned int chunk_samples, chunk_size, chunk_duration;
+        unsigned int frames = 1;
         for (i = 0; i < sc->chunk_count; i++) {
             current_offset = sc->chunk_offsets[i];
-            if (stsc_index + 1 < sc->sample_to_chunk_sz && i + 1 == sc->sample_to_chunk[stsc_index + 1].first)
+            if (stsc_index + 1 < sc->sample_to_chunk_sz &&
+                i + 1 == sc->sample_to_chunk[stsc_index + 1].first)
                 stsc_index++;
             chunk_samples = sc->sample_to_chunk[stsc_index].count;
-            /* get chunk size */
-            if (sc->sample_size > 1 || st->codec->codec_id == CODEC_ID_PCM_U8 || st->codec->codec_id == CODEC_ID_PCM_S8)
-                chunk_size = chunk_samples * sc->sample_size;
-            else if (sc->samples_per_frame > 0 && (chunk_samples * sc->bytes_per_frame % sc->samples_per_frame == 0))
-                chunk_size = chunk_samples * sc->bytes_per_frame / sc->samples_per_frame;
-            else { /* workaround to find nearest next chunk offset */
-                chunk_size = INT_MAX;
-                for (j = 0; j < mov->fc->nb_streams; j++) {
-                    MOVStreamContext *msc = mov->fc->streams[j]->priv_data;
-                    for (k = msc->next_chunk; k < msc->chunk_count; k++) {
-                        if (msc->chunk_offsets[k] > current_offset && msc->chunk_offsets[k] - current_offset < chunk_size) {
-                            chunk_size = msc->chunk_offsets[k] - current_offset;
-                            msc->next_chunk = k;
-                            break;
-                        }
-                    }
-                }
-                /* check for last chunk */
-                if (chunk_size == INT_MAX)
-                    for (j = 0; j < mov->mdat_count; j++) {
-                        dprintf(mov->fc, "mdat %d, offset %"PRIx64", size %"PRId64", current offset %"PRIx64"\n",
-                                j, mov->mdat_list[j].offset, mov->mdat_list[j].size, current_offset);
-                        if (mov->mdat_list[j].offset <= current_offset && mov->mdat_list[j].offset + mov->mdat_list[j].size > current_offset)
-                            chunk_size = mov->mdat_list[j].offset + mov->mdat_list[j].size - current_offset;
-                    }
-                assert(chunk_size != INT_MAX);
-                for (j = 0; j < mov->fc->nb_streams; j++) {
-                    MOVStreamContext *msc = mov->fc->streams[j]->priv_data;
-                    msc->next_chunk = 0;
+            /* get chunk size, beware of alaw/ulaw/mace */
+            if (sc->samples_per_frame > 0 &&
+                (chunk_samples * sc->bytes_per_frame % sc->samples_per_frame == 0)) {
+                if (sc->samples_per_frame < 1024)
+                    chunk_size = chunk_samples * sc->bytes_per_frame / sc->samples_per_frame;
+                else {
+                    chunk_size = sc->bytes_per_frame;
+                    frames = chunk_samples / sc->samples_per_frame;
+                    chunk_samples = sc->samples_per_frame;
                 }
+            } else if (sc->sample_size > 1 || st->codec->bits_per_sample == 8) {
+                chunk_size = chunk_samples * sc->sample_size;
+            } else {
+                av_log(mov->fc, AV_LOG_ERROR, "could not determine chunk size, report problem\n");
+                goto out;
             }
+            for (j = 0; j < frames; j++) {
             av_add_index_entry(st, current_offset, current_dts, chunk_size, 0, AVINDEX_KEYFRAME);
             /* get chunk duration */
             chunk_duration = 0;
@@ -1401,15 +1414,16 @@ static void mov_build_index(MOVContext *mov, AVStream *st)
                 } else {
                     chunk_duration += sc->stts_data[stts_index].duration * chunk_samples;
                     chunk_samples -= sc->stts_data[stts_index].count;
-                    if (stts_index + 1 < sc->stts_count) {
+                    if (stts_index + 1 < sc->stts_count)
                         stts_index++;
-                    }
                 }
             }
-            dprintf(mov->fc, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", size %d, duration %d\n",
-                    st->index, i, current_offset, current_dts, chunk_size, chunk_duration);
+            current_offset += sc->bytes_per_frame;
+            dprintf(mov->fc, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", size %d, "
+                    "duration %d\n", st->index, i, current_offset, current_dts, chunk_size, chunk_duration);
             assert(chunk_duration % sc->time_rate == 0);
             current_dts += chunk_duration / sc->time_rate;
+            }
         }
     }
  out:
@@ -1476,7 +1490,6 @@ static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
         av_freep(&sc->keyframes);
         av_freep(&sc->stts_data);
     }
-    av_freep(&mov->mdat_list);
     return 0;
 }
 
@@ -1493,7 +1506,8 @@ static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
         MOVStreamContext *msc = st->priv_data;
         if (st->discard != AVDISCARD_ALL && msc->current_sample < msc->sample_count) {
             AVIndexEntry *current_sample = &st->index_entries[msc->current_sample];
-            int64_t dts = av_rescale(current_sample->timestamp * (int64_t)msc->time_rate, AV_TIME_BASE, msc->time_scale);
+            int64_t dts = av_rescale(current_sample->timestamp * (int64_t)msc->time_rate,
+                                     AV_TIME_BASE, msc->time_scale);
             dprintf(s, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
             if (!sample || (url_is_streamed(s->pb) && current_sample->pos < sample->pos) ||
                 (!url_is_streamed(s->pb) &&
@@ -1510,22 +1524,18 @@ static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
     /* must be done just before reading, to avoid infinite loop on sample */
     sc->current_sample++;
     if (url_fseek(s->pb, sample->pos, SEEK_SET) != sample->pos) {
-        av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n", sc->ffindex, sample->pos);
+        av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n",
+               sc->ffindex, sample->pos);
         return -1;
     }
+    av_get_packet(s->pb, pkt, sample->size);
 #ifdef CONFIG_DV_DEMUXER
-    if (sc->dv_audio_container) {
-        dv_get_packet(mov->dv_demux, pkt);
-        dprintf(s, "dv audio pkt size %d\n", pkt->size);
-    } else {
-#endif
-        av_get_packet(s->pb, pkt, sample->size);
-#ifdef CONFIG_DV_DEMUXER
-        if (mov->dv_demux) {
-            void *pkt_destruct_func = pkt->destruct;
-            dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size);
-            pkt->destruct = pkt_destruct_func;
-        }
+    if (mov->dv_demux && sc->dv_audio_container) {
+        dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size);
+        av_free(pkt->data);
+        pkt->size = 0;
+        if (dv_get_packet(mov->dv_demux, pkt) < 0)
+            return -1;
     }
 #endif
     pkt->stream_index = sc->ffindex;
@@ -1535,7 +1545,8 @@ static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
         pkt->pts = pkt->dts + sc->ctts_data[sc->sample_to_ctime_index].duration / sc->time_rate;
         /* update ctts context */
         sc->sample_to_ctime_sample++;
-        if (sc->sample_to_ctime_index < sc->ctts_count && sc->ctts_data[sc->sample_to_ctime_index].count == sc->sample_to_ctime_sample) {
+        if (sc->sample_to_ctime_index < sc->ctts_count &&
+            sc->ctts_data[sc->sample_to_ctime_index].count == sc->sample_to_ctime_sample) {
             sc->sample_to_ctime_index++;
             sc->sample_to_ctime_sample = 0;
         }
@@ -1544,7 +1555,8 @@ static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
     }
     pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? PKT_FLAG_KEY : 0;
     pkt->pos = sample->pos;
-    dprintf(s, "stream %d, pts %"PRId64", dts %"PRId64", pos 0x%"PRIx64", duration %d\n", pkt->stream_index, pkt->pts, pkt->dts, pkt->pos, pkt->duration);
+    dprintf(s, "stream %d, pts %"PRId64", dts %"PRId64", pos 0x%"PRIx64", duration %d\n",
+            pkt->stream_index, pkt->pts, pkt->dts, pkt->pos, pkt->duration);
     return 0;
 }