[FFmpeg-devel] [PATCH] make stream-switching work with MOV demuxer

Reimar Döffinger Reimar.Doeffinger
Tue Jun 23 21:19:38 CEST 2009


On Tue, Jun 23, 2009 at 11:59:21AM -0700, Baptiste Coudurier wrote:
> I think discard variable can be avoided by checking
> s->streams[sc->ffindex]->discard.
> 
> We could even declare AVStream *st = s->streams[sc->ffindex] when sample
> is found and factore because it is already done when computing duration.
> 
> We could even get rid of sc->ffindex by choosing AVStream in the loop
> and use ->priv_data, but well that can be done separately.

I think it is uglier, but here it is.
-------------- next part --------------
Index: libavformat/mov.c
===================================================================
--- libavformat/mov.c	(revision 19256)
+++ libavformat/mov.c	(working copy)
@@ -1981,15 +1981,17 @@
 {
     MOVContext *mov = s->priv_data;
     MOVStreamContext *sc = 0;
-    AVIndexEntry *sample = 0;
+    AVStream *st = NULL;
+    AVIndexEntry *sample;
     int64_t best_dts = INT64_MAX;
     int i, ret;
  retry:
+    sample = NULL;
     for (i = 0; i < s->nb_streams; i++) {
-        AVStream *st = s->streams[i];
-        MOVStreamContext *msc = st->priv_data;
-        if (st->discard != AVDISCARD_ALL && msc->pb && msc->current_sample < st->nb_index_entries) {
-            AVIndexEntry *current_sample = &st->index_entries[msc->current_sample];
+        AVStream *avst = s->streams[i];
+        MOVStreamContext *msc = avst->priv_data;
+        if (msc->pb && msc->current_sample < avst->nb_index_entries) {
+            AVIndexEntry *current_sample = &avst->index_entries[msc->current_sample];
             int64_t dts = av_rescale(current_sample->timestamp, 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) ||
@@ -2000,6 +2002,7 @@
                 sample = current_sample;
                 best_dts = dts;
                 sc = msc;
+                st = avst;
             }
         }
     }
@@ -2014,6 +2017,7 @@
     }
     /* must be done just before reading, to avoid infinite loop on sample */
     sc->current_sample++;
+    if (st->discard != AVDISCARD_ALL) {
     if (url_fseek(sc->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);
@@ -2031,6 +2035,7 @@
             return -1;
     }
 #endif
+    }
     pkt->stream_index = sc->ffindex;
     pkt->dts = sample->timestamp;
     if (sc->ctts_data) {
@@ -2045,12 +2050,12 @@
         if (sc->wrong_dts)
             pkt->dts = AV_NOPTS_VALUE;
     } else {
-        AVStream *st = s->streams[sc->ffindex];
         int64_t next_dts = (sc->current_sample < st->nb_index_entries) ?
             st->index_entries[sc->current_sample].timestamp : st->duration;
         pkt->duration = next_dts - pkt->dts;
         pkt->pts = pkt->dts;
     }
+    if (st->discard == AVDISCARD_ALL) goto retry;
     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",
@@ -2108,7 +2113,7 @@
 
     for (i = 0; i < s->nb_streams; i++) {
         st = s->streams[i];
-        if (stream_index == i || st->discard == AVDISCARD_ALL)
+        if (stream_index == i)
             continue;
 
         timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);



More information about the ffmpeg-devel mailing list