]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/mov.c
flv/swf do not have a big endian codec id, they only support
[ffmpeg] / libavformat / mov.c
index 2b09c78a8e2d3b84bf8d626d139024488e91ba49..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 {
@@ -108,6 +103,7 @@ typedef struct MOVStreamContext {
     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 {
@@ -117,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) */
@@ -233,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 */
@@ -317,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 */
@@ -601,7 +590,10 @@ 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 && st->codec->codec_tag != MKTAG('j', 'p', 'e', 'g')) {
+        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 */
@@ -624,6 +616,11 @@ 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,
@@ -733,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));
@@ -754,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;
             }
@@ -783,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));
@@ -1220,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;
@@ -1318,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;
@@ -1364,47 +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)
                 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;
@@ -1416,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++;
-                    }
                 }
             }
+            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:
@@ -1491,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;
 }
 
@@ -1530,19 +1528,14 @@ static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
                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;