]> git.sesse.net Git - ffmpeg/commitdiff
avformat/westwoodaudenc: Adds muxer for Westwood AUD format.
authorAidan Richmond <aidan.is@hotmail.co.uk>
Sun, 25 Apr 2021 20:00:01 +0000 (21:00 +0100)
committerZane van Iperen <zane@zanevaniperen.com>
Mon, 26 Apr 2021 09:56:33 +0000 (19:56 +1000)
Format is still used by modders of these old games.

Reviewed-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Signed-off-by: Aidan Richmond <aidan.is@hotmail.co.uk>
Signed-off-by: Zane van Iperen <zane@zanevaniperen.com>
Changelog
libavformat/Makefile
libavformat/allformats.c
libavformat/version.h
libavformat/westwood_audenc.c [new file with mode: 0644]

index 320a18bcd6e7dd41df9a76aa5965f3ee2bc5f986..ad950354d06de908019be6365de797d1021f30f9 100644 (file)
--- a/Changelog
+++ b/Changelog
@@ -3,6 +3,7 @@ releases are sorted from youngest to oldest.
 
 version <next>:
 - ADPCM IMA Westwood encoder
+- Westwood AUD muxer
 
 
 version 4.4:
index bc1ddfa81c98447c820d2a5c0639a2e3fe3d279d..85b5d8e7ebfd66077c3e2c2212bb4e9620c3b1c4 100644 (file)
@@ -581,6 +581,7 @@ OBJS-$(CONFIG_WEBP_MUXER)                += webpenc.o
 OBJS-$(CONFIG_WEBVTT_DEMUXER)            += webvttdec.o subtitles.o
 OBJS-$(CONFIG_WEBVTT_MUXER)              += webvttenc.o
 OBJS-$(CONFIG_WSAUD_DEMUXER)             += westwood_aud.o
+OBJS-$(CONFIG_WSAUD_MUXER)               += westwood_audenc.o
 OBJS-$(CONFIG_WSD_DEMUXER)               += wsddec.o rawdec.o
 OBJS-$(CONFIG_WSVQA_DEMUXER)             += westwood_vqa.o
 OBJS-$(CONFIG_WTV_DEMUXER)               += wtvdec.o wtv_common.o \
index fa093c7ac290c156274248601e039ad4b66c2aef..fe70a1e9a22c59d1c12bc33a5fd75a5ee2ff3e1f 100644 (file)
@@ -478,6 +478,7 @@ extern AVOutputFormat ff_webp_muxer;
 extern AVInputFormat  ff_webvtt_demuxer;
 extern AVOutputFormat ff_webvtt_muxer;
 extern AVInputFormat  ff_wsaud_demuxer;
+extern AVOutputFormat ff_wsaud_muxer;
 extern AVInputFormat  ff_wsd_demuxer;
 extern AVInputFormat  ff_wsvqa_demuxer;
 extern AVInputFormat  ff_wtv_demuxer;
index 66068d4d56c031213a9dafe310b1ae46f5b95db1..c77d500192245411557d08db308c09f0ea571e07 100644 (file)
@@ -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  78
+#define LIBAVFORMAT_VERSION_MINOR  79
 #define LIBAVFORMAT_VERSION_MICRO 100
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
diff --git a/libavformat/westwood_audenc.c b/libavformat/westwood_audenc.c
new file mode 100644 (file)
index 0000000..4ec905b
--- /dev/null
@@ -0,0 +1,141 @@
+/*
+ * Copyright (c) 2021 Aidan Richmond
+ *
+ * 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
+ */
+
+/**
+ * @file
+ * Westwood Studios AUD file muxer
+ * by Aidan Richmond (aidan.is@hotmail.co.uk)
+ *
+ * This muxer supports IMA ADPCM packed in westwoods format.
+ *
+ * @see http://xhp.xwis.net/documents/aud3.txt
+ */
+
+#include "libavutil/avassert.h"
+
+#include "avformat.h"
+#include "avio_internal.h"
+#include "internal.h"
+#include <stdint.h>
+
+#define AUD_CHUNK_SIGNATURE 0x0000DEAF
+
+typedef struct AUDMuxContext {
+    int uncomp_size;
+    int size;
+} AUDMuxContext;
+
+static int wsaud_write_init(AVFormatContext *ctx)
+{
+    AVStream     *st = ctx->streams[0];
+    AVIOContext  *pb = ctx->pb;
+
+    /* Stream must be seekable to correctly write the file. */
+    if (!(pb->seekable & AVIO_SEEKABLE_NORMAL)) {
+        av_log(ctx->streams[0], AV_LOG_ERROR, "Cannot write Westwood AUD to"
+               " non-seekable stream.\n");
+        return AVERROR(EINVAL);
+    }
+
+    if (st->codecpar->codec_id != AV_CODEC_ID_ADPCM_IMA_WS) {
+        av_log(st, AV_LOG_ERROR, "%s codec not supported for Westwood AUD.\n",
+               avcodec_get_name(st->codecpar->codec_id));
+        return AVERROR(EINVAL);
+    }
+
+    if (ctx->nb_streams != 1) {
+        av_log(st, AV_LOG_ERROR, "AUD files have exactly one stream\n");
+        return AVERROR(EINVAL);
+    }
+
+    return 0;
+}
+
+static int wsaud_write_header(AVFormatContext *ctx)
+{
+    AVStream     *st = ctx->streams[0];
+    AVIOContext  *pb = ctx->pb;
+    AUDMuxContext *a = ctx->priv_data;
+    int ret;
+    unsigned char flags = 0;
+
+    a->uncomp_size = 0;
+    a->size = 0;
+
+    /* Flag if we have stereo data. */
+    if (st->codecpar->channels == 2)
+        flags |= 1;
+
+    /* This flags that the file contains 16 bit samples rather than 8 bit
+       since the encoder only encodes 16 bit samples this should be set. */
+    if (av_get_bits_per_sample(st->codecpar->codec_id) == 4)
+        flags |= 2;
+
+    avio_wl16(pb, st->codecpar->sample_rate);
+    /* We don't know the file size yet, so just zero 8 bytes */
+    ffio_fill(pb, 0, 8);
+    avio_w8(pb, flags);
+    /* 99 indicates the ADPCM format. Other formats not supported. */
+    avio_w8(pb, 99);
+
+    return 0;
+}
+
+static int wsaud_write_packet(AVFormatContext *ctx, AVPacket *pkt)
+{
+    AVIOContext  *pb = ctx->pb;
+    AUDMuxContext *a = ctx->priv_data;
+
+    av_assert1(pkt->size < UINT16_MAX && (pkt->size * 4) < UINT16_MAX);
+    /* Assumes ADPCM since this muxer doesn't support SND1 or PCM format. */
+    avio_wl16(pb, pkt->size);
+    avio_wl16(pb, pkt->size * 4);
+    avio_wl32(pb, AUD_CHUNK_SIGNATURE);
+    avio_write(pb, pkt->data, pkt->size);
+    a->size += pkt->size + 8;
+    a->uncomp_size += pkt->size * 4;
+
+    return 0;
+}
+
+static int wsaud_write_trailer(AVFormatContext *ctx)
+{
+    AVIOContext  *pb = ctx->pb;
+    AUDMuxContext *a = ctx->priv_data;
+
+    avio_seek(pb, 2, SEEK_SET);
+    avio_wl32(pb, a->size);
+    avio_wl32(pb, a->uncomp_size);
+
+    return 0;
+}
+
+AVOutputFormat ff_wsaud_muxer = {
+    .name              = "wsaud",
+    .long_name         = NULL_IF_CONFIG_SMALL("Westwood Studios audio"),
+    .extensions        = "aud",
+    .priv_data_size    = sizeof(AUDMuxContext),
+    .audio_codec       = AV_CODEC_ID_ADPCM_IMA_WS,
+    .video_codec       = AV_CODEC_ID_NONE,
+    .init              = wsaud_write_init,
+    .write_header      = wsaud_write_header,
+    .write_packet      = wsaud_write_packet,
+    .write_trailer     = wsaud_write_trailer,
+};