[FFmpeg-devel] [PATCH 2/2] avformat: add demuxer for Simon & Schuster Interactive's VAG format

Zane van Iperen zane at zanevaniperen.com
Sat Feb 1 06:12:42 EET 2020


Adds support for the custom VAG container used by some Simon & Schuster
Interactive games such as Real War, and Real War: Rogue States.

Signed-off-by: Zane van Iperen <zane at zanevaniperen.com>
---
 libavformat/Makefile     |   1 +
 libavformat/allformats.c |   1 +
 libavformat/kvag.c       | 149 +++++++++++++++++++++++++++++++++++++++
 libavformat/version.h    |   2 +-
 4 files changed, 152 insertions(+), 1 deletion(-)
 create mode 100644 libavformat/kvag.c

diff --git a/libavformat/Makefile b/libavformat/Makefile
index ba6ea8c4a6..710cc4d088 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -279,6 +279,7 @@ OBJS-$(CONFIG_JACOSUB_DEMUXER)           += jacosubdec.o subtitles.o
 OBJS-$(CONFIG_JACOSUB_MUXER)             += jacosubenc.o rawenc.o
 OBJS-$(CONFIG_JV_DEMUXER)                += jvdec.o
 OBJS-$(CONFIG_KUX_DEMUXER)               += flvdec.o
+OBJS-$(CONFIG_KVAG_DEMUXER)              += kvag.o
 OBJS-$(CONFIG_LATM_MUXER)                += latmenc.o rawenc.o
 OBJS-$(CONFIG_LMLM4_DEMUXER)             += lmlm4.o
 OBJS-$(CONFIG_LOAS_DEMUXER)              += loasdec.o rawdec.o
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index fe74a32e47..3ea4100e85 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -214,6 +214,7 @@ extern AVInputFormat  ff_jacosub_demuxer;
 extern AVOutputFormat ff_jacosub_muxer;
 extern AVInputFormat  ff_jv_demuxer;
 extern AVInputFormat  ff_kux_demuxer;
+extern AVInputFormat  ff_kvag_demuxer;
 extern AVOutputFormat ff_latm_muxer;
 extern AVInputFormat  ff_lmlm4_demuxer;
 extern AVInputFormat  ff_loas_demuxer;
diff --git a/libavformat/kvag.c b/libavformat/kvag.c
new file mode 100644
index 0000000000..fb58701fd7
--- /dev/null
+++ b/libavformat/kvag.c
@@ -0,0 +1,149 @@
+/*
+ * Simon & Schuster Interactive VAG demuxer
+ *
+ * Copyright (C) 2020 Zane van Iperen (zane at zanevaniperen.com)
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+#include "avformat.h"
+#include "internal.h"
+#include "libavutil/intreadwrite.h"
+
+#define KVAG_TAG            MKTAG('K', 'V', 'A', 'G')
+#define KVAG_HEADER_SIZE    14
+
+typedef struct KVAGHeader {
+    uint32_t    magic;
+    uint32_t    data_size;
+    uint32_t    sample_rate;
+    uint32_t    stereo;
+} KVAGHeader;
+
+typedef struct KVAGDemuxContext {
+    KVAGHeader  hdr;
+    uint32_t    samples_per_frame;
+    uint32_t    bytes_read;
+} KVAGDemuxContext;
+
+static int kvag_probe(const AVProbeData *p)
+{
+    if (AV_RL32(p->buf) != KVAG_TAG)
+        return 0;
+
+    return AVPROBE_SCORE_EXTENSION + 1;
+}
+
+static int kvag_read_header(AVFormatContext *s)
+{
+    int64_t ret;
+    AVIOContext *pb = s->pb;
+    AVStream *st;
+    KVAGDemuxContext *kvag = s->priv_data;
+
+    uint8_t buf[KVAG_HEADER_SIZE];
+
+    if (!(st = avformat_new_stream(s, NULL)))
+        return AVERROR(ENOMEM);
+
+    if ((ret = avio_read(pb, buf, KVAG_HEADER_SIZE)) < 0)
+        return ret;
+    else if (ret != KVAG_HEADER_SIZE)
+        return AVERROR(EIO);
+
+    kvag->hdr.magic                     = AV_RL32(buf +  0);
+    kvag->hdr.data_size                 = AV_RL32(buf +  4);
+    kvag->hdr.sample_rate               = AV_RL32(buf +  8);
+    kvag->hdr.stereo                    = AV_RL16(buf + 12);
+
+
+    st->codecpar->codec_type            = AVMEDIA_TYPE_AUDIO;
+    st->codecpar->codec_id              = AV_CODEC_ID_ADPCM_IMA_SSI;
+    st->codecpar->format                = AV_SAMPLE_FMT_S16;
+
+    if (kvag->hdr.stereo) {
+        st->codecpar->channel_layout    = AV_CH_LAYOUT_STEREO;
+        st->codecpar->channels          = 2;
+    } else {
+        st->codecpar->channel_layout    = AV_CH_LAYOUT_MONO;
+        st->codecpar->channels          = 1;
+    }
+
+    st->codecpar->sample_rate           = kvag->hdr.sample_rate;
+    st->codecpar->bits_per_coded_sample = 4;
+    st->codecpar->bits_per_raw_sample   = 16;
+
+    /*
+     * The game doesn't use "frames", it just reads the whole file.
+     * 32 seems like a good number.
+     */
+    st->codecpar->frame_size            = 32;
+    st->codecpar->block_align           = st->codecpar->frame_size;
+    st->codecpar->bit_rate              = st->codecpar->channels *
+                                          st->codecpar->sample_rate *
+                                          st->codecpar->bits_per_coded_sample;
+
+    avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
+    st->start_time          = 0;
+    st->nb_frames           = ROUNDED_DIV(kvag->hdr.data_size, st->codecpar->frame_size);
+
+    kvag->samples_per_frame = st->codecpar->frame_size *
+                              (8 / st->codecpar->bits_per_coded_sample) /
+                              st->codecpar->channels;
+
+    st->duration            = st->nb_frames * kvag->samples_per_frame;
+    return 0;
+}
+
+static int kvag_read_packet(AVFormatContext *s, AVPacket *pkt)
+{
+    int ret;
+    AVStream *st = s->streams[0];
+    KVAGDemuxContext *kvag = s->priv_data;
+
+    if (kvag->bytes_read >= kvag->hdr.data_size) {
+        return AVERROR_EOF;
+    }
+
+    if ((ret = av_get_packet(s->pb, pkt, st->codecpar->frame_size)) < 0) {
+        return ret;
+    } else if (ret < st->codecpar->frame_size) {
+        if (!avio_feof(s->pb)) {
+            return AVERROR_INVALIDDATA;
+        }
+
+        /*
+         * In some files, the actual size is less than the data size. The game
+         * just overreads the buffer. A truncated final "frame" is fine.
+         */
+        pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
+    }
+
+    pkt->stream_index   = st->index;
+    pkt->duration       = kvag->samples_per_frame;
+
+    kvag->bytes_read    += ret;
+    return 0;
+}
+
+AVInputFormat ff_kvag_demuxer = {
+    .name           = "kvag",
+    .long_name      = NULL_IF_CONFIG_SMALL("Simon & Schuster Interactive VAG"),
+    .priv_data_size = sizeof(KVAGDemuxContext),
+    .read_probe     = kvag_probe,
+    .read_header    = kvag_read_header,
+    .read_packet    = kvag_read_packet
+};
diff --git a/libavformat/version.h b/libavformat/version.h
index 15fdb73e3e..be22abc010 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -32,7 +32,7 @@
 // Major bumping may affect Ticket5467, 5421, 5451(compatibility with Chromium)
 // Also please add any ticket numbers that you believe might be affected here
 #define LIBAVFORMAT_VERSION_MAJOR  58
-#define LIBAVFORMAT_VERSION_MINOR  37
+#define LIBAVFORMAT_VERSION_MINOR  38
 #define LIBAVFORMAT_VERSION_MICRO 100
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
-- 
2.17.1




More information about the ffmpeg-devel mailing list