]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/wv.c
Export metadata in the generic format. Deprecate old conversion API.
[ffmpeg] / libavformat / wv.c
index d46f90d78b44621133d185c69f76137c94a52719..03b864bdaf4ab086a4ffda92a01d1026046e429d 100644 (file)
@@ -21,6 +21,8 @@
 
 #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
@@ -99,9 +101,31 @@ static int wv_read_block_header(AVFormatContext *ctx, ByteIOContext *pb)
     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;
@@ -115,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;
     }
@@ -130,22 +154,31 @@ 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)
         return -1;
-    st->codec->codec_type = CODEC_TYPE_AUDIO;
+    st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
     st->codec->codec_id = CODEC_ID_WAVPACK;
     st->codec->channels = wc->chan;
     st->codec->sample_rate = wc->rate;
     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;
+    st->start_time = 0;
+    st->duration = wc->samples;
+
+    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;
 }