2 * Copyright (c) 2021 Aidan Richmond
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * Westwood Studios AUD file muxer
24 * by Aidan Richmond (aidan.is@hotmail.co.uk)
26 * This muxer supports IMA ADPCM packed in westwoods format.
28 * @see http://xhp.xwis.net/documents/aud3.txt
31 #include "libavutil/avassert.h"
34 #include "avio_internal.h"
38 #define AUD_CHUNK_SIGNATURE 0x0000DEAF
40 typedef struct AUDMuxContext {
45 static int wsaud_write_init(AVFormatContext *ctx)
47 AVStream *st = ctx->streams[0];
48 AVIOContext *pb = ctx->pb;
50 /* Stream must be seekable to correctly write the file. */
51 if (!(pb->seekable & AVIO_SEEKABLE_NORMAL)) {
52 av_log(ctx->streams[0], AV_LOG_ERROR, "Cannot write Westwood AUD to"
53 " non-seekable stream.\n");
54 return AVERROR(EINVAL);
57 if (st->codecpar->codec_id != AV_CODEC_ID_ADPCM_IMA_WS) {
58 av_log(st, AV_LOG_ERROR, "%s codec not supported for Westwood AUD.\n",
59 avcodec_get_name(st->codecpar->codec_id));
60 return AVERROR(EINVAL);
63 if (ctx->nb_streams != 1) {
64 av_log(st, AV_LOG_ERROR, "AUD files have exactly one stream\n");
65 return AVERROR(EINVAL);
71 static int wsaud_write_header(AVFormatContext *ctx)
73 AVStream *st = ctx->streams[0];
74 AVIOContext *pb = ctx->pb;
75 AUDMuxContext *a = ctx->priv_data;
76 unsigned char flags = 0;
81 /* Flag if we have stereo data. */
82 if (st->codecpar->channels == 2)
85 /* This flags that the file contains 16 bit samples rather than 8 bit
86 since the encoder only encodes 16 bit samples this should be set. */
87 if (av_get_bits_per_sample(st->codecpar->codec_id) == 4)
90 avio_wl16(pb, st->codecpar->sample_rate);
91 /* We don't know the file size yet, so just zero 8 bytes */
94 /* 99 indicates the ADPCM format. Other formats not supported. */
100 static int wsaud_write_packet(AVFormatContext *ctx, AVPacket *pkt)
102 AVIOContext *pb = ctx->pb;
103 AUDMuxContext *a = ctx->priv_data;
105 if (pkt->size > UINT16_MAX / 4)
106 return AVERROR_INVALIDDATA;
107 /* Assumes ADPCM since this muxer doesn't support SND1 or PCM format. */
108 avio_wl16(pb, pkt->size);
109 avio_wl16(pb, pkt->size * 4);
110 avio_wl32(pb, AUD_CHUNK_SIGNATURE);
111 avio_write(pb, pkt->data, pkt->size);
112 a->size += pkt->size + 8;
113 a->uncomp_size += pkt->size * 4;
118 static int wsaud_write_trailer(AVFormatContext *ctx)
120 AVIOContext *pb = ctx->pb;
121 AUDMuxContext *a = ctx->priv_data;
123 avio_seek(pb, 2, SEEK_SET);
124 avio_wl32(pb, a->size);
125 avio_wl32(pb, a->uncomp_size);
130 AVOutputFormat ff_wsaud_muxer = {
132 .long_name = NULL_IF_CONFIG_SMALL("Westwood Studios audio"),
134 .priv_data_size = sizeof(AUDMuxContext),
135 .audio_codec = AV_CODEC_ID_ADPCM_IMA_WS,
136 .video_codec = AV_CODEC_ID_NONE,
137 .init = wsaud_write_init,
138 .write_header = wsaud_write_header,
139 .write_packet = wsaud_write_packet,
140 .write_trailer = wsaud_write_trailer,