[PATCH 1/6] Refactor the 'fmt ' tag search and parsing

Tomas Härdin tomas.hardin
Thu Feb 17 15:49:35 CET 2011


Moving the search and parsing of the 'fmt ' info the main loop of wav_read_header() allows tags that precede it to be parsed.
Creating wav_parse_fmt_tag() makes wav_read_header() easier to read.
---
 libavformat/wav.c |   51 ++++++++++++++++++++++++++++++++++++---------------
 1 files changed, 36 insertions(+), 15 deletions(-)

diff --git a/libavformat/wav.c b/libavformat/wav.c
index d6329e3..768da56 100644
--- a/libavformat/wav.c
+++ b/libavformat/wav.c
@@ -185,6 +185,23 @@ static int wav_probe(AVProbeData *p)
     return 0;
 }
 
+static int wav_parse_fmt_tag(AVFormatContext *s, int64_t size, AVStream **st)
+{
+    AVIOContext *pb = s->pb;
+
+    /* parse fmt header */
+    *st = av_new_stream(s, 0);
+    if (!*st)
+        return AVERROR(ENOMEM);
+
+    ff_get_wav_header(pb, (*st)->codec, size);
+    (*st)->need_parsing = AVSTREAM_PARSE_FULL;
+
+    av_set_pts_info(*st, 64, 1, (*st)->codec->sample_rate);
+
+    return 0;
+}
+
 /* wav input */
 static int wav_read_header(AVFormatContext *s,
                            AVFormatParameters *ap)
@@ -196,6 +213,8 @@ static int wav_read_header(AVFormatContext *s,
     AVIOContext *pb = s->pb;
     AVStream *st;
     WAVContext *wav = s->priv_data;
+    int ret, got_fmt = 0;
+    int64_t next_tag_ofs;
 
     /* check RIFF header */
     tag = avio_rl32(pb);
@@ -220,30 +239,32 @@ static int wav_read_header(AVFormatContext *s,
         avio_seek(pb, size - 16, SEEK_CUR); /* skip rest of ds64 chunk */
     }
 
-    /* parse fmt header */
-    size = find_tag(pb, MKTAG('f', 'm', 't', ' '));
-    if (size < 0)
-        return -1;
-    st = av_new_stream(s, 0);
-    if (!st)
-        return AVERROR(ENOMEM);
-
-    ff_get_wav_header(pb, st->codec, size);
-    st->need_parsing = AVSTREAM_PARSE_FULL;
-
-    av_set_pts_info(st, 64, 1, st->codec->sample_rate);
-
     for (;;) {
         if (url_feof(pb))
             return -1;
         size = next_tag(pb, &tag);
+        next_tag_ofs = url_ftell(pb) + size;
+
+        if (tag == MKTAG('f', 'm', 't', ' ')) {
+            /* only parse the first 'fmt ' tag found */
+            if (!got_fmt && (ret = wav_parse_fmt_tag(s, size, &st) < 0))
+                return ret;
+            else if (got_fmt)
+                av_log(s, AV_LOG_WARNING, "found more than one 'fmt ' tag\n");
+
+            got_fmt = 1;
+        } else
         if (tag == MKTAG('d', 'a', 't', 'a')){
+            if (!got_fmt) {
+                av_log(s, AV_LOG_ERROR, "found no 'fmt ' tag before the 'data' tag\n");
+                return AVERROR_INVALIDDATA;
+            }
+
             break;
         }else if (tag == MKTAG('f','a','c','t') && !sample_count){
             sample_count = avio_rl32(pb);
-            size -= 4;
         }
-        avio_seek(pb, size, SEEK_CUR);
+        avio_seek(pb, next_tag_ofs, SEEK_SET);
     }
     if (rf64)
         size = data_size;
-- 
1.7.1


--------------080207040505040500040804--



More information about the ffmpeg-devel mailing list