]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/wv.c
10l: WavPack demuxer forgot to seek back to initial position after block
[ffmpeg] / libavformat / wv.c
index 953955412d773e37c7b2a71a3f70982f4ee7b1f8..9809b37a15c41aff74b057575fa8efab8ed0c6f7 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * WavPack demuxer
- * Copyright (c) 2006 Konstantin Shishkov.
+ * Copyright (c) 2006 Konstantin Shishkov
  *
  * This file is part of FFmpeg.
  *
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include "libavutil/bswap.h"
+#include "libavutil/intreadwrite.h"
 #include "avformat.h"
+#include "apetag.h"
+#include "id3v1.h"
 
 // specs say that maximum block size is 1Mb
 #define WV_BLOCK_LIMIT 1047576
@@ -96,21 +98,34 @@ static int wv_read_block_header(AVFormatContext *ctx, ByteIOContext *pb)
     get_buffer(pb, wc->extra, WV_EXTRA_SIZE);
     wc->flags = AV_RL32(wc->extra + 4);
     //parse flags
-    if(wc->flags & WV_FLOAT){
-        av_log(ctx, AV_LOG_ERROR, "Floating point data is not supported\n");
-        return -1;
-    }
-    if(wc->flags & WV_HYBRID){
-        av_log(ctx, AV_LOG_ERROR, "Hybrid coding mode is not supported\n");
-        return -1;
-    }
-
     bpp = ((wc->flags & 3) + 1) << 3;
     chan = 1 + !(wc->flags & WV_MONO);
     rate = wv_rates[(wc->flags >> 23) & 0xF];
-    if(rate == -1){
-        av_log(ctx, AV_LOG_ERROR, "Unknown sampling rate\n");
-        return -1;
+    if(rate == -1 && !wc->block_parsed){
+        int64_t block_end = url_ftell(pb) + wc->blksize - 24;
+        if(url_is_streamed(pb)){
+            av_log(ctx, AV_LOG_ERROR, "Cannot determine custom sampling rate\n");
+            return -1;
+        }
+        while(url_ftell(pb) < block_end){
+            int id, size;
+            id = get_byte(pb);
+            size = (id & 0x80) ? get_le24(pb) : get_byte(pb);
+            size <<= 1;
+            if(id&0x40)
+                size--;
+            if((id&0x3F) == 0x27){
+                rate = get_le24(pb);
+                break;
+            }else{
+                url_fskip(pb, size);
+            }
+        }
+        if(rate == -1){
+            av_log(ctx, AV_LOG_ERROR, "Cannot determine custom sampling rate\n");
+            return -1;
+        }
+        url_fseek(pb, block_end - wc->blksize + 24, SEEK_SET);
     }
     if(!wc->bpp) wc->bpp = bpp;
     if(!wc->chan) wc->chan = chan;
@@ -124,7 +139,7 @@ static int wv_read_block_header(AVFormatContext *ctx, ByteIOContext *pb)
         av_log(ctx, AV_LOG_ERROR, "Channels differ, this block: %i, header block: %i\n", chan, wc->chan);
         return -1;
     }
-    if(wc->flags && rate != wc->rate){
+    if(wc->flags && rate != -1 && rate != wc->rate){
         av_log(ctx, AV_LOG_ERROR, "Sampling rate differ, this block: %i, header block: %i\n", rate, wc->rate);
         return -1;
     }
@@ -139,10 +154,10 @@ static int wv_read_header(AVFormatContext *s,
     WVContext *wc = s->priv_data;
     AVStream *st;
 
+    wc->block_parsed = 0;
     if(wv_read_block_header(s, pb) < 0)
         return -1;
 
-    wc->block_parsed = 0;
     /* now we are ready: build format streams */
     st = av_new_stream(s, 0);
     if (!st)
@@ -151,10 +166,19 @@ static int wv_read_header(AVFormatContext *s,
     st->codec->codec_id = CODEC_ID_WAVPACK;
     st->codec->channels = wc->chan;
     st->codec->sample_rate = wc->rate;
-    st->codec->bits_per_sample = wc->bpp;
+    st->codec->bits_per_coded_sample = wc->bpp;
     av_set_pts_info(st, 64, 1, wc->rate);
     s->start_time = 0;
     s->duration = (int64_t)wc->samples * AV_TIME_BASE / st->codec->sample_rate;
+
+    if(!url_is_streamed(s->pb)) {
+        int64_t cur = url_ftell(s->pb);
+        ff_ape_parse_tag(s);
+        if(!av_metadata_get(s->metadata, "", NULL, AV_METADATA_IGNORE_SUFFIX))
+            ff_id3v1_read(s);
+        url_fseek(s->pb, cur, SEEK_SET);
+    }
+
     return 0;
 }
 
@@ -187,11 +211,6 @@ static int wv_read_packet(AVFormatContext *s,
     return 0;
 }
 
-static int wv_read_close(AVFormatContext *s)
-{
-    return 0;
-}
-
 static int wv_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
 {
     AVStream *st = s->streams[stream_index];
@@ -231,6 +250,6 @@ AVInputFormat wv_demuxer = {
     wv_probe,
     wv_read_header,
     wv_read_packet,
-    wv_read_close,
+    NULL,
     wv_read_seek,
 };