FFmpeg
westwood_audenc.c
Go to the documentation of this file.
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 "mux.h"
35 #include <stdint.h>
36 
37 #define AUD_CHUNK_SIGNATURE 0x0000DEAF
38 
39 typedef struct AUDMuxContext {
41  int size;
43 
45 {
46  AVStream *st = ctx->streams[0];
47  AVIOContext *pb = ctx->pb;
48 
49  /* Stream must be seekable to correctly write the file. */
50  if (!(pb->seekable & AVIO_SEEKABLE_NORMAL)) {
51  av_log(ctx->streams[0], AV_LOG_ERROR, "Cannot write Westwood AUD to"
52  " non-seekable stream.\n");
53  return AVERROR(EINVAL);
54  }
55 
57  av_log(st, AV_LOG_ERROR, "%s codec not supported for Westwood AUD.\n",
59  return AVERROR(EINVAL);
60  }
61 
62  if (ctx->nb_streams != 1) {
63  av_log(st, AV_LOG_ERROR, "AUD files have exactly one stream\n");
64  return AVERROR(EINVAL);
65  }
66 
67  return 0;
68 }
69 
71 {
72  AVStream *st = ctx->streams[0];
73  AVIOContext *pb = ctx->pb;
75  unsigned char flags = 0;
76 
77  a->uncomp_size = 0;
78  a->size = 0;
79 
80  /* Flag if we have stereo data. */
81  if (st->codecpar->ch_layout.nb_channels == 2)
82  flags |= 1;
83 
84  /* This flags that the file contains 16 bit samples rather than 8 bit
85  since the encoder only encodes 16 bit samples this should be set. */
87  flags |= 2;
88 
89  avio_wl16(pb, st->codecpar->sample_rate);
90  /* We don't know the file size yet, so just zero 8 bytes */
91  ffio_fill(pb, 0, 8);
92  avio_w8(pb, flags);
93  /* 99 indicates the ADPCM format. Other formats not supported. */
94  avio_w8(pb, 99);
95 
96  return 0;
97 }
98 
100 {
101  AVIOContext *pb = ctx->pb;
103 
104  if (pkt->size > UINT16_MAX / 4)
105  return AVERROR_INVALIDDATA;
106  /* Assumes ADPCM since this muxer doesn't support SND1 or PCM format. */
107  avio_wl16(pb, pkt->size);
108  avio_wl16(pb, pkt->size * 4);
110  avio_write(pb, pkt->data, pkt->size);
111  a->size += pkt->size + 8;
112  a->uncomp_size += pkt->size * 4;
113 
114  return 0;
115 }
116 
118 {
119  AVIOContext *pb = ctx->pb;
121 
122  avio_seek(pb, 2, SEEK_SET);
123  avio_wl32(pb, a->size);
124  avio_wl32(pb, a->uncomp_size);
125 
126  return 0;
127 }
128 
130  .p.name = "wsaud",
131  .p.long_name = NULL_IF_CONFIG_SMALL("Westwood Studios audio"),
132  .p.extensions = "aud",
133  .priv_data_size = sizeof(AUDMuxContext),
134  .p.audio_codec = AV_CODEC_ID_ADPCM_IMA_WS,
135  .p.video_codec = AV_CODEC_ID_NONE,
136  .init = wsaud_write_init,
137  .write_header = wsaud_write_header,
138  .write_packet = wsaud_write_packet,
139  .write_trailer = wsaud_write_trailer,
140 };
AVOutputFormat::name
const char * name
Definition: avformat.h:508
AUDMuxContext
Definition: westwood_audenc.c:39
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVFormatContext::streams
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1172
AVPacket::data
uint8_t * data
Definition: packet.h:374
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:311
FFOutputFormat::p
AVOutputFormat p
The public AVOutputFormat.
Definition: mux.h:34
wsaud_write_header
static int wsaud_write_header(AVFormatContext *ctx)
Definition: westwood_audenc.c:70
ff_wsaud_muxer
const FFOutputFormat ff_wsaud_muxer
Definition: westwood_audenc.c:129
avio_wl16
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:458
av_get_bits_per_sample
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:583
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
wsaud_write_init
static int wsaud_write_init(AVFormatContext *ctx)
Definition: westwood_audenc.c:44
ctx
AVFormatContext * ctx
Definition: movenc.c:48
AUDMuxContext::uncomp_size
int uncomp_size
Definition: westwood_audenc.c:40
AUDMuxContext::size
int size
Definition: westwood_audenc.c:41
AVFormatContext
Format I/O context.
Definition: avformat.h:1104
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:861
wsaud_write_packet
static int wsaud_write_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: westwood_audenc.c:99
AV_CODEC_ID_ADPCM_IMA_WS
@ AV_CODEC_ID_ADPCM_IMA_WS
Definition: codec_id.h:369
AVFormatContext::pb
AVIOContext * pb
I/O context.
Definition: avformat.h:1146
FFOutputFormat
Definition: mux.h:30
avio_w8
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:200
ffio_fill
void ffio_fill(AVIOContext *s, int b, int64_t count)
Definition: aviobuf.c:208
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:213
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:178
AVFormatContext::nb_streams
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1160
AVIOContext
Bytestream IO Context.
Definition: avio.h:166
AVPacket::size
int size
Definition: packet.h:375
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:115
AVIOContext::seekable
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:267
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:222
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
avio_wl32
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:378
avcodec_get_name
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
Definition: utils.c:442
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:50
avio_internal.h
wsaud_write_trailer
static int wsaud_write_trailer(AVFormatContext *ctx)
Definition: westwood_audenc.c:117
AVStream
Stream structure.
Definition: avformat.h:838
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:252
avformat.h
AVIO_SEEKABLE_NORMAL
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:41
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:62
AVPacket
This structure stores compressed data.
Definition: packet.h:351
AUD_CHUNK_SIGNATURE
#define AUD_CHUNK_SIGNATURE
Definition: westwood_audenc.c:37
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1132
mux.h