[PATCH 4/6] Keep parsing wav until EOF if the input is seekable and we know the size of the data tag

Tomas Härdin tomas.hardin
Thu Mar 3 16:38:32 CET 2011


---
 libavformat/wav.c |   39 +++++++++++++++++++++++++++------------
 1 files changed, 27 insertions(+), 12 deletions(-)

diff --git a/libavformat/wav.c b/libavformat/wav.c
index dca5db0..7c6d001 100644
--- a/libavformat/wav.c
+++ b/libavformat/wav.c
@@ -214,7 +214,7 @@ static int wav_read_header(AVFormatContext *s,
     AVStream *st;
     WAVContext *wav = s->priv_data;
     int ret, got_fmt = 0;
-    int64_t next_tag_ofs;
+    int64_t next_tag_ofs, data_ofs = -1;
 
     /* check RIFF header */
     tag = avio_rl32(pb);
@@ -245,11 +245,18 @@ static int wav_read_header(AVFormatContext *s,
     }
 
     for (;;) {
-        if (url_feof(pb))
-            return -1;
         size = next_tag(pb, &tag);
         next_tag_ofs = url_ftell(pb) + size;
 
+        if (url_feof(pb)) {
+            if (data_ofs < 0) {
+                av_log(s, AV_LOG_ERROR, "no 'data' tag found\n");
+                return AVERROR_INVALIDDATA;
+            }
+
+            break;
+        }
+
         switch (tag) {
         case MKTAG('f', 'm', 't', ' '):
             if ((ret = wav_parse_fmt_tag(s, size, &st) < 0))
@@ -263,7 +270,21 @@ static int wav_read_header(AVFormatContext *s,
                 return AVERROR_INVALIDDATA;
             }
 
+            if (rf64) {
+                next_tag_ofs = wav->data_end = url_ftell(pb) + data_size;
+            } else {
+                data_size = size;
+                wav->data_end = size ? next_tag_ofs : INT64_MAX;
+            }
+
+            /* don't look for footer metadata if we can't seek or if we don't
+             * know where the data tag ends
+             */
+            if (pb->is_streamed || (!rf64 && !size))
             goto break_loop;
+
+            data_ofs = url_ftell(pb);
+            break;
         case MKTAG('f','a','c','t'):
             if(!sample_count)
             sample_count = avio_rl32(pb);
@@ -272,17 +293,11 @@ static int wav_read_header(AVFormatContext *s,
         avio_seek(pb, next_tag_ofs, SEEK_SET);
     }
 break_loop:
-    if (rf64)
-        size = data_size;
-    if (size < 0)
-        return -1;
-    if (!size) {
-        wav->data_end = INT64_MAX;
-    } else
-        wav->data_end= url_ftell(pb) + size;
+    if (data_ofs >= 0)
+        avio_seek(pb, data_ofs, SEEK_SET);
 
     if (!sample_count && st->codec->channels && av_get_bits_per_sample(st->codec->codec_id))
-        sample_count = (size<<3) / (st->codec->channels * (uint64_t)av_get_bits_per_sample(st->codec->codec_id));
+        sample_count = (data_size<<3) / (st->codec->channels * (uint64_t)av_get_bits_per_sample(st->codec->codec_id));
     if (sample_count)
         st->duration = sample_count;
     return 0;
-- 
1.7.1


--------------040805030609080409010803--



More information about the ffmpeg-devel mailing list