• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Aidan Richmond
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20 
21 /**
22  * @file
23  * Westwood Studios AUD file muxer
24  * by Aidan Richmond (aidan.is@hotmail.co.uk)
25  *
26  * This muxer supports IMA ADPCM packed in westwoods format.
27  *
28  * @see http://xhp.xwis.net/documents/aud3.txt
29  */
30 
31 #include "avformat.h"
32 #include "avio_internal.h"
33 #include "internal.h"
34 #include <stdint.h>
35 
36 #define AUD_CHUNK_SIGNATURE 0x0000DEAF
37 
38 typedef struct AUDMuxContext {
39     int uncomp_size;
40     int size;
41 } AUDMuxContext;
42 
wsaud_write_init(AVFormatContext * ctx)43 static int wsaud_write_init(AVFormatContext *ctx)
44 {
45     AVStream     *st = ctx->streams[0];
46     AVIOContext  *pb = ctx->pb;
47 
48     /* Stream must be seekable to correctly write the file. */
49     if (!(pb->seekable & AVIO_SEEKABLE_NORMAL)) {
50         av_log(ctx->streams[0], AV_LOG_ERROR, "Cannot write Westwood AUD to"
51                " non-seekable stream.\n");
52         return AVERROR(EINVAL);
53     }
54 
55     if (st->codecpar->codec_id != AV_CODEC_ID_ADPCM_IMA_WS) {
56         av_log(st, AV_LOG_ERROR, "%s codec not supported for Westwood AUD.\n",
57                avcodec_get_name(st->codecpar->codec_id));
58         return AVERROR(EINVAL);
59     }
60 
61     if (ctx->nb_streams != 1) {
62         av_log(st, AV_LOG_ERROR, "AUD files have exactly one stream\n");
63         return AVERROR(EINVAL);
64     }
65 
66     return 0;
67 }
68 
wsaud_write_header(AVFormatContext * ctx)69 static int wsaud_write_header(AVFormatContext *ctx)
70 {
71     AVStream     *st = ctx->streams[0];
72     AVIOContext  *pb = ctx->pb;
73     AUDMuxContext *a = ctx->priv_data;
74     unsigned char flags = 0;
75 
76     a->uncomp_size = 0;
77     a->size = 0;
78 
79     /* Flag if we have stereo data. */
80     if (st->codecpar->ch_layout.nb_channels == 2)
81         flags |= 1;
82 
83     /* This flags that the file contains 16 bit samples rather than 8 bit
84        since the encoder only encodes 16 bit samples this should be set. */
85     if (av_get_bits_per_sample(st->codecpar->codec_id) == 4)
86         flags |= 2;
87 
88     avio_wl16(pb, st->codecpar->sample_rate);
89     /* We don't know the file size yet, so just zero 8 bytes */
90     ffio_fill(pb, 0, 8);
91     avio_w8(pb, flags);
92     /* 99 indicates the ADPCM format. Other formats not supported. */
93     avio_w8(pb, 99);
94 
95     return 0;
96 }
97 
wsaud_write_packet(AVFormatContext * ctx,AVPacket * pkt)98 static int wsaud_write_packet(AVFormatContext *ctx, AVPacket *pkt)
99 {
100     AVIOContext  *pb = ctx->pb;
101     AUDMuxContext *a = ctx->priv_data;
102 
103     if (pkt->size > UINT16_MAX / 4)
104         return AVERROR_INVALIDDATA;
105     /* Assumes ADPCM since this muxer doesn't support SND1 or PCM format. */
106     avio_wl16(pb, pkt->size);
107     avio_wl16(pb, pkt->size * 4);
108     avio_wl32(pb, AUD_CHUNK_SIGNATURE);
109     avio_write(pb, pkt->data, pkt->size);
110     a->size += pkt->size + 8;
111     a->uncomp_size += pkt->size * 4;
112 
113     return 0;
114 }
115 
wsaud_write_trailer(AVFormatContext * ctx)116 static int wsaud_write_trailer(AVFormatContext *ctx)
117 {
118     AVIOContext  *pb = ctx->pb;
119     AUDMuxContext *a = ctx->priv_data;
120 
121     avio_seek(pb, 2, SEEK_SET);
122     avio_wl32(pb, a->size);
123     avio_wl32(pb, a->uncomp_size);
124 
125     return 0;
126 }
127 
128 const AVOutputFormat ff_wsaud_muxer = {
129     .name              = "wsaud",
130     .long_name         = NULL_IF_CONFIG_SMALL("Westwood Studios audio"),
131     .extensions        = "aud",
132     .priv_data_size    = sizeof(AUDMuxContext),
133     .audio_codec       = AV_CODEC_ID_ADPCM_IMA_WS,
134     .video_codec       = AV_CODEC_ID_NONE,
135     .init              = wsaud_write_init,
136     .write_header      = wsaud_write_header,
137     .write_packet      = wsaud_write_packet,
138     .write_trailer     = wsaud_write_trailer,
139 };
140