From patchwork Sun Apr 25 20:00:01 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aidan Richmond X-Patchwork-Id: 27370 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:5014:0:0:0:0:0 with SMTP id e20csp3382969iob; Sun, 25 Apr 2021 13:00:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxROIgb9u4OfWn3aDK2wk+BYgqvoEzssBc+VUISbfN68WguQBc5l+DDVH5MBsXnNIIwOJu1 X-Received: by 2002:a17:906:3f88:: with SMTP id b8mr15443021ejj.36.1619380840875; Sun, 25 Apr 2021 13:00:40 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id n23si4932088eje.216.2021.04.25.13.00.40; Sun, 25 Apr 2021 13:00:40 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; arc=fail (body hash mismatch); spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=hotmail.co.uk Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 015E26880FA; Sun, 25 Apr 2021 23:00:30 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from EUR03-AM5-obe.outbound.protection.outlook.com (mail-oln040092070094.outbound.protection.outlook.com [40.92.70.94]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id DF5D868807C for ; Sun, 25 Apr 2021 23:00:24 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=iJi+oJaZ59CZsfKdU5X9ctBNNIFDtjk8TwBgvTt9WA2EK+9TqmhwPOGeBVdyqI/80z2VI+m5AyDLZhMyrOuK0h2qTGzaM+vI9Hh83UkI4I5tYyMcAzf1D4TY0fwfkldO1QzVhzAhoFXoQz1VsbPIDOJiMPC6WN4sFpneFaumItT7PzNqc99l6rFSUqvIvm/7XZPyXiFGWO1+8Xp3ZpeYeKGwDwKy1g2k07KqphCYanVoS32TFBNackU6EqM/IJxhxSScLqnqxrxTR0HBntPDdW0nzMqRekHMe6h4ZRsDwBQuPiajhVF6fJa3AG0zZTsyWaWa0o0cyGpYmCiWSP6nsQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=HpaxScU1J+Yia3RAnb1XviJ3eirB4DRwyg+AWq9oPSI=; b=OhZy/VYK6CcEngIkAUMsUD1EILDlV/FWlf0vJYWpZe+FArHEZb1V5xEwDx0Bqdb0KqaLw8YqpSlTtaNJCvNOyLwa7e6jfN7CSS7eZMUB+y9SpiaBAmu0zmib7APx7m10WNa52US9krj8iNP+3yHus3z8WstopiO/AjiZ6Naxl+3ugRXUQzFzjYqsLGOELZyY/zFuAXBWKVNjCs8y7dtqDRxgil4j1o0oGchTvGm9oey2zxdD3jfXrtGRY4R8Cq7fxYjiV7buC+0UZxX9UIIsX1cd6koJQNM3ypfKCSWg9hpl+RLxUS8Dznw15G9wIRT/5P+2ziniCtptjEtKMjE9yQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none Received: from VE1EUR03FT005.eop-EUR03.prod.protection.outlook.com (2a01:111:e400:7e09::4d) by VE1EUR03HT178.eop-EUR03.prod.protection.outlook.com (2a01:111:e400:7e09::396) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4065.21; Sun, 25 Apr 2021 20:00:23 +0000 Received: from AM9PR09MB5170.eurprd09.prod.outlook.com (2a01:111:e400:7e09::40) by VE1EUR03FT005.mail.protection.outlook.com (2a01:111:e400:7e09::172) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4065.21 via Frontend Transport; Sun, 25 Apr 2021 20:00:23 +0000 X-IncomingTopHeaderMarker: OriginalChecksum:D2FEEE6D828C9868D2C4BD0A10E3A5EB7061073210D95AC2608D56C5BBFC8119; UpperCasedChecksum:F982DB47D74DD19B22786A393A330295946421AE9DC2BCB37C5010A4F7916E8C; SizeAsReceived:7426; Count:46 Received: from AM9PR09MB5170.eurprd09.prod.outlook.com ([fe80::4545:9b8c:a216:3436]) by AM9PR09MB5170.eurprd09.prod.outlook.com ([fe80::4545:9b8c:a216:3436%3]) with mapi id 15.20.4065.026; Sun, 25 Apr 2021 20:00:23 +0000 From: Aidan Richmond To: ffmpeg-devel@ffmpeg.org Date: Sun, 25 Apr 2021 21:00:01 +0100 Message-ID: X-Mailer: git-send-email 2.16.1.windows.1 In-Reply-To: <20210425200002.16708-1-aidan.is@hotmail.co.uk> References: <20210425200002.16708-1-aidan.is@hotmail.co.uk> X-TMN: [eyuKF6+rvQ96G5dq45ckVgW/HgXUHi2h] X-ClientProxiedBy: LO4P123CA0411.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:189::20) To AM9PR09MB5170.eurprd09.prod.outlook.com (2603:10a6:20b:301::8) X-Microsoft-Original-Message-ID: <20210425200002.16708-2-aidan.is@hotmail.co.uk> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from localhost.localdomain (86.28.139.128) by LO4P123CA0411.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:189::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256) id 15.20.4065.24 via Frontend Transport; Sun, 25 Apr 2021 20:00:23 +0000 X-MS-PublicTrafficType: Email X-IncomingHeaderCount: 46 X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-Correlation-Id: 12b2f570-de11-4608-0cc8-08d90824c302 X-MS-Exchange-SLBlob-MailProps: pt8ZUcDWjMVOUy0JiOhI44kpuqFG4v58ctUVGL+Xi3FxRjua+PBbKH/9lvUj9CQojbQLUOVU7bPiaNu1ugUZc5Xamc+m+2mVhhJ4nxKQQPKDLe6mo4sxM2sBXswFpzEPbgDMITe+w9VIv08N9V1g+8ouCcHJIYxgAUKhcN8uyOt83qCLGYy5hOlKvpZeROqCCeNly5B0xhs33CE8oAJrcDx8pTPItyuAWRU8693H2lgzu3ZseOqvAQcxcRwrshNVOseg3toBXS+HziABIGAEiHG5gOipzf7PWSrXtuSKHuHA6CfgjhmL18dRB/5U6uOa/ESmTAv62SxUfXmNBtuWO7TFXZmoTbntHt0cIdXA8dmIGez0wbhGSwOETe7Ze1ljvS4CNm+2T7dH/HYyqRw65g/cQyvXbs6nW0GxeDmL4i5DT4FYDH1xk0Z45ptttcXJtBKjybpfO2g1PVAHGuiW9ViVJMm4gLfWvMSsoIO0COR+EGnQGf/goo0pbBDLTrWMMqr+IMdcZSf92HV0fOtpQoKrgazNbIb+r30DhoFuK6wh75t258b+2+qOb6POsmt2RqnfEcOVSnMOsNwChz8hA3jnQRpj8cU5QOQ5ADY6UQm/wT9pbe+dUPhMBpCien+1bWARxQAqR+s4K7jQ9NfgwC0z1toan1ELTHqQgk69y5Swobk7W1Zk+NfbD1TcvA6VWaa4R1i51/x5FtDWMz+Y47eE40lG0BihNP0+kptCiu1umViGZlbAaonQoCQ3qlBhDHoxPcI0K9i98EvbQFB7MAikA+sPCa6FH3LBuUp+oEM5fU+C9dWfO+eNph//oUtLFDj0Z3zQEsU6wHTRytgvQbzT/t3Nu5MLbEd7EvVcLPKrF6+lHSZo8EONdGA0BUNd6IPO4y5NtMrW0ITpZ/Y3zjbH2UARqgqCnYUN3nJIPQbQMPSTtiV1sCsXIUbYTFxP X-MS-TrafficTypeDiagnostic: VE1EUR03HT178: X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: /hewVVeQM+IXjKxY1zwauXPmULKauUz+CypTy0K/K/sjViAks5Rz0fpsuXpzvvTmQOVh6D6TtdILvfap6h0ke8XYB+oxbpjK1jpShN/Zs9OCqEUHAMdhcZ8hsusrnKqObYqvgAOb1urowSQrCw2ymjIz/r1h8dtbY+hOrI0j6c+Th4TyVTlESfuzH3FW2qxM0Lm1+S/hlEQv0fbSLhd79CM6TlNwzb/mH4kv/F/UvNA5uq+ll4aZHfOYP9NqDwPFA5sBIBgpM+ssNauCenDLkW5q0BG97BZcAekxalcTts1Ire37pDm3UGOQELpeiXBq8K/Eut0Co5mvvCuhuqtmVJcDG1huPgSyft8I33Y8InvosLzMo8Z7t6PcPTpIVlSSxIpgrDsOlOOlPxUvFD/B7WmwLIL6U8IJnWzVl/gPSVQ= X-MS-Exchange-AntiSpam-MessageData: JIEuAyTqYn40WdmhiX/noniQKnz+pnZOAAXjxpjXa0NQonVR3uWguZg6z1mobCjrwVYXl2YojqtBII0LD/uh7CEw5KISPqzduEYiYOeLcwQd8nP6LMtHErz0gezdyVln3GcoWhXvopdzrx6s2t9+LA== X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 12b2f570-de11-4608-0cc8-08d90824c302 X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Apr 2021 20:00:23.4924 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-AuthSource: VE1EUR03FT005.eop-EUR03.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: Internet X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: VE1EUR03HT178 Subject: [FFmpeg-devel] [PATCH v5 2/3] avformat/westwoodaudenc: Adds muxer for Westwood AUD format. X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Aidan Richmond Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: Wc5SQTJb/m8B Format is still used by modders of these old games. Signed-off-by: Aidan Richmond --- libavformat/Makefile | 1 + libavformat/allformats.c | 1 + libavformat/westwood_audenc.c | 141 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 143 insertions(+) create mode 100644 libavformat/westwood_audenc.c diff --git a/libavformat/Makefile b/libavformat/Makefile index bc1ddfa81c..85b5d8e7eb 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -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 \ diff --git a/libavformat/allformats.c b/libavformat/allformats.c index fa093c7ac2..fe70a1e9a2 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -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; diff --git a/libavformat/westwood_audenc.c b/libavformat/westwood_audenc.c new file mode 100644 index 0000000000..b95a3a5d72 --- /dev/null +++ b/libavformat/westwood_audenc.c @@ -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 + +#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" + " none 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, +};