changeset 5126:789e13163b3b libavformat

Sony Wave64 demuxer patch by Daniel Verkamp, daniel drv nu
author diego
date Mon, 10 Aug 2009 16:27:33 +0000
parents 2a0aabbef960
children e73c5f33463b
files Makefile allformats.c wav.c
diffstat 3 files changed, 121 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Fri Aug 07 23:57:52 2009 +0000
+++ b/Makefile	Mon Aug 10 16:27:33 2009 +0000
@@ -216,6 +216,7 @@
 OBJS-$(CONFIG_VOC_DEMUXER)               += vocdec.o voc.o
 OBJS-$(CONFIG_VOC_MUXER)                 += vocenc.o voc.o
 OBJS-$(CONFIG_VQF_DEMUXER)               += vqf.o
+OBJS-$(CONFIG_W64_DEMUXER)               += wav.o riff.o raw.o
 OBJS-$(CONFIG_WAV_DEMUXER)               += wav.o riff.o raw.o
 OBJS-$(CONFIG_WAV_MUXER)                 += wav.o riff.o
 OBJS-$(CONFIG_WC3_DEMUXER)               += wc3movie.o
--- a/allformats.c	Fri Aug 07 23:57:52 2009 +0000
+++ b/allformats.c	Mon Aug 10 16:27:33 2009 +0000
@@ -193,6 +193,7 @@
     REGISTER_DEMUXER  (VMD, vmd);
     REGISTER_MUXDEMUX (VOC, voc);
     REGISTER_DEMUXER  (VQF, vqf);
+    REGISTER_DEMUXER  (W64, w64);
     REGISTER_MUXDEMUX (WAV, wav);
     REGISTER_DEMUXER  (WC3, wc3);
     REGISTER_DEMUXER  (WSAUD, wsaud);
--- a/wav.c	Fri Aug 07 23:57:52 2009 +0000
+++ b/wav.c	Mon Aug 10 16:27:33 2009 +0000
@@ -2,6 +2,9 @@
  * WAV muxer and demuxer
  * Copyright (c) 2001, 2002 Fabrice Bellard
  *
+ * Sony Wave64 demuxer
+ * Copyright (c) 2009 Daniel Verkamp
+ *
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
@@ -28,6 +31,7 @@
     int64_t minpts;
     int64_t maxpts;
     int last_duration;
+    int w64;
 } WAVContext;
 
 #if CONFIG_WAV_MUXER
@@ -194,12 +198,109 @@
     return 0;
 }
 
+#if CONFIG_W64_DEMUXER
+
+static const uint8_t guid_riff[16] = { 'r', 'i', 'f', 'f',
+    0x2E, 0x91, 0xCF, 0x11, 0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00 };
+
+static const uint8_t guid_wave[16] = { 'w', 'a', 'v', 'e',
+    0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
+
+static const uint8_t guid_fmt [16] = { 'f', 'm', 't', ' ',
+    0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
+
+static const uint8_t guid_data[16] = { 'd', 'a', 't', 'a',
+    0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
+
+static int w64_probe(AVProbeData *p)
+{
+    if (p->buf_size <= 40)
+        return 0;
+    if (!memcmp(p->buf,      guid_riff, 16) &&
+        !memcmp(p->buf + 24, guid_wave, 16)) {
+        return AVPROBE_SCORE_MAX;
+    } else
+        return 0;
+}
+
+/** Find chunk with w64 GUID by skipping over other chunks
+ * @return the size of the found chunk
+ */
+static int64_t find_guid(ByteIOContext *pb, const uint8_t guid1[16])
+{
+    uint8_t guid[16];
+    int64_t size;
+
+    while (!url_feof(pb)) {
+        get_buffer(pb, guid, 16);
+        size = get_le64(pb);
+        if (size <= 24)
+            return -1;
+        if (!memcmp(guid, guid1, 16))
+            return size;
+        url_fskip(pb, FFALIGN(size, INT64_C(8)) - 24);
+    }
+    return -1;
+}
+
+static int w64_read_header(AVFormatContext *s, AVFormatParameters *ap)
+{
+    int64_t size;
+    ByteIOContext *pb = s->pb;
+    WAVContext *wav   = s->priv_data;
+    AVStream *st;
+    uint8_t guid[16];
+
+    get_buffer(pb, guid, 16);
+    if (memcmp(guid, guid_riff, 16))
+        return -1;
+
+    if (get_le64(pb) < 16 + 8 + 16 + 8 + 16 + 8) /* riff + wave + fmt + sizes */
+        return -1;
+
+    get_buffer(pb, guid, 16);
+    if (memcmp(guid, guid_wave, 16)) {
+        av_log(s, AV_LOG_ERROR, "could not find wave guid\n");
+        return -1;
+    }
+
+    size = find_guid(pb, guid_fmt);
+    if (size < 0) {
+        av_log(s, AV_LOG_ERROR, "could not find fmt guid\n");
+        return -1;
+    }
+
+    st = av_new_stream(s, 0);
+    if (!st)
+        return AVERROR(ENOMEM);
+
+    /* subtract chunk header size - normal wav file doesn't count it */
+    ff_get_wav_header(pb, st->codec, size - 24);
+    url_fskip(pb, FFALIGN(size, INT64_C(8)) - size);
+
+    st->need_parsing = AVSTREAM_PARSE_FULL;
+
+    av_set_pts_info(st, 64, 1, st->codec->sample_rate);
+
+    size = find_guid(pb, guid_data);
+    if (size < 0) {
+        av_log(s, AV_LOG_ERROR, "could not find data guid\n");
+        return -1;
+    }
+    wav->data_end = url_ftell(pb) + size - 24;
+    wav->w64 = 1;
+
+    return 0;
+}
+#endif /* CONFIG_W64_DEMUXER */
+
 #define MAX_SIZE 4096
 
 static int wav_read_packet(AVFormatContext *s,
                            AVPacket *pkt)
 {
-    int ret, size, left;
+    int ret, size;
+    int64_t left;
     AVStream *st;
     WAVContext *wav = s->priv_data;
 
@@ -209,6 +310,9 @@
 
     left= wav->data_end - url_ftell(s->pb);
     if(left <= 0){
+        if (CONFIG_W64_DEMUXER && wav->w64) {
+            left = find_guid(s->pb, guid_data) - 24;
+        } else
         left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a'));
         if (left < 0) {
             return AVERROR(EIO);
@@ -282,3 +386,17 @@
     .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
 };
 #endif
+#if CONFIG_W64_DEMUXER
+AVInputFormat w64_demuxer = {
+    "w64",
+    NULL_IF_CONFIG_SMALL("Sony Wave64 format"),
+    sizeof(WAVContext),
+    w64_probe,
+    w64_read_header,
+    wav_read_packet,
+    NULL,
+    wav_read_seek,
+    .flags = AVFMT_GENERIC_INDEX,
+    .codec_tag = (const AVCodecTag* const []){ff_codec_wav_tags, 0},
+};
+#endif