3 * Copyright (c) 2001 Fabrice Bellard
5 * first version by Francois Revol <revol@free.fr>
7 * This file is part of FFmpeg.
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * Reference documents:
26 * http://www.opengroup.org/public/pubs/external/auformat.html
27 * http://www.goice.co.jp/member/mo/formats/au.html
32 #include "avio_internal.h"
34 #include "libavutil/avassert.h"
36 /* if we don't know the size in advance */
37 #define AU_UNKNOWN_SIZE ((uint32_t)(~0))
39 static const AVCodecTag codec_au_tags[] = {
40 { AV_CODEC_ID_PCM_MULAW, 1 },
41 { AV_CODEC_ID_PCM_S8, 2 },
42 { AV_CODEC_ID_PCM_S16BE, 3 },
43 { AV_CODEC_ID_PCM_S24BE, 4 },
44 { AV_CODEC_ID_PCM_S32BE, 5 },
45 { AV_CODEC_ID_PCM_F32BE, 6 },
46 { AV_CODEC_ID_PCM_F64BE, 7 },
47 { AV_CODEC_ID_ADPCM_G726LE, 23 },
48 { AV_CODEC_ID_ADPCM_G722,24 },
49 { AV_CODEC_ID_ADPCM_G726LE, 25 },
50 { AV_CODEC_ID_ADPCM_G726LE, 26 },
51 { AV_CODEC_ID_PCM_ALAW, 27 },
52 { AV_CODEC_ID_ADPCM_G726LE, MKBETAG('7','2','6','2') },
53 { AV_CODEC_ID_NONE, 0 },
58 static int au_probe(const AVProbeData *p)
60 if (p->buf[0] == '.' && p->buf[1] == 's' &&
61 p->buf[2] == 'n' && p->buf[3] == 'd')
62 return AVPROBE_SCORE_MAX;
67 static int au_read_annotation(AVFormatContext *s, int size)
69 static const char keys[][7] = {
76 AVIOContext *pb = s->pb;
77 enum { PARSE_KEY, PARSE_VALUE, PARSE_FINISHED } state = PARSE_KEY;
84 av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED);
91 state = PARSE_FINISHED;
92 } else if (c == '=') {
93 ret = av_bprint_finalize(&bprint, &key);
96 av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED);
99 av_bprint_chars(&bprint, c, 1);
103 if (c == '\0' || c == '\n') {
104 if (av_bprint_finalize(&bprint, &value) != 0) {
105 av_log(s, AV_LOG_ERROR, "Memory error while parsing AU metadata.\n");
107 av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED);
108 for (i = 0; i < FF_ARRAY_ELEMS(keys); i++) {
109 if (av_strcasecmp(keys[i], key) == 0) {
110 av_dict_set(&(s->metadata), keys[i], value, AV_DICT_DONT_STRDUP_VAL);
118 state = (c == '\0') ? PARSE_FINISHED : PARSE_KEY;
120 av_bprint_chars(&bprint, c, 1);
126 /* should never happen */
130 av_bprint_finalize(&bprint, NULL);
135 #define BLOCK_SIZE 1024
137 static int au_read_header(AVFormatContext *s)
139 int size, data_size = 0;
141 AVIOContext *pb = s->pb;
142 unsigned int id, channels, rate;
144 enum AVCodecID codec;
149 if (tag != MKTAG('.', 's', 'n', 'd'))
150 return AVERROR_INVALIDDATA;
151 size = avio_rb32(pb); /* header size */
152 data_size = avio_rb32(pb); /* data size in bytes */
154 if (data_size < 0 && data_size != AU_UNKNOWN_SIZE) {
155 av_log(s, AV_LOG_ERROR, "Invalid negative data size '%d' found\n", data_size);
156 return AVERROR_INVALIDDATA;
160 rate = avio_rb32(pb);
161 channels = avio_rb32(pb);
164 /* parse annotation field to get metadata */
165 ret = au_read_annotation(s, size - 24);
170 codec = ff_codec_get_id(codec_au_tags, id);
172 if (codec == AV_CODEC_ID_NONE) {
173 avpriv_request_sample(s, "unknown or unsupported codec tag: %u", id);
174 return AVERROR_PATCHWELCOME;
177 bps = av_get_bits_per_sample(codec);
178 if (codec == AV_CODEC_ID_ADPCM_G726LE) {
179 if (id == MKBETAG('7','2','6','2')) {
182 const uint8_t bpcss[] = {4, 0, 3, 5};
183 av_assert0(id >= 23 && id < 23 + 4);
185 bps = bpcss[id - 23];
188 avpriv_request_sample(s, "Unknown bits per sample");
189 return AVERROR_PATCHWELCOME;
192 if (channels == 0 || channels >= INT_MAX / (BLOCK_SIZE * bps >> 3)) {
193 av_log(s, AV_LOG_ERROR, "Invalid number of channels %u\n", channels);
194 return AVERROR_INVALIDDATA;
197 if (rate == 0 || rate > INT_MAX) {
198 av_log(s, AV_LOG_ERROR, "Invalid sample rate: %u\n", rate);
199 return AVERROR_INVALIDDATA;
202 st = avformat_new_stream(s, NULL);
204 return AVERROR(ENOMEM);
205 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
206 st->codecpar->codec_tag = id;
207 st->codecpar->codec_id = codec;
208 st->codecpar->channels = channels;
209 st->codecpar->sample_rate = rate;
210 st->codecpar->bits_per_coded_sample = bps;
211 st->codecpar->bit_rate = channels * rate * bps;
212 st->codecpar->block_align = ba ? ba : FFMAX(bps * st->codecpar->channels / 8, 1);
213 if (data_size != AU_UNKNOWN_SIZE)
214 st->duration = (((int64_t)data_size)<<3) / (st->codecpar->channels * (int64_t)bps);
217 avpriv_set_pts_info(st, 64, 1, rate);
222 AVInputFormat ff_au_demuxer = {
224 .long_name = NULL_IF_CONFIG_SMALL("Sun AU"),
225 .read_probe = au_probe,
226 .read_header = au_read_header,
227 .read_packet = ff_pcm_read_packet,
228 .read_seek = ff_pcm_read_seek,
229 .codec_tag = (const AVCodecTag* const []) { codec_au_tags, 0 },
232 #endif /* CONFIG_AU_DEMUXER */
236 typedef struct AUContext {
237 uint32_t header_size;
242 static int au_get_annotations(AVFormatContext *s, AVBPrint *annotations)
244 static const char keys[][7] = {
252 AVDictionary *m = s->metadata;
253 AVDictionaryEntry *t = NULL;
255 for (int i = 0; i < FF_ARRAY_ELEMS(keys); i++) {
256 t = av_dict_get(m, keys[i], NULL, 0);
259 av_bprint_chars(annotations, '\n', 1);
260 av_bprintf(annotations, "%s=%s", keys[i], t->value);
263 /* The specification requires the annotation field to be zero-terminated
264 * and its length to be a multiple of eight, so pad with 0's */
265 av_bprint_chars(annotations, '\0', 8);
266 return av_bprint_is_complete(annotations) ? 0 : AVERROR(ENOMEM);
269 static int au_write_header(AVFormatContext *s)
272 AUContext *au = s->priv_data;
273 AVIOContext *pb = s->pb;
274 AVCodecParameters *par = s->streams[0]->codecpar;
275 AVBPrint annotations;
277 if (s->nb_streams != 1) {
278 av_log(s, AV_LOG_ERROR, "only one stream is supported\n");
279 return AVERROR(EINVAL);
282 par->codec_tag = ff_codec_get_tag(codec_au_tags, par->codec_id);
283 if (!par->codec_tag) {
284 av_log(s, AV_LOG_ERROR, "unsupported codec\n");
285 return AVERROR(EINVAL);
288 av_bprint_init(&annotations, 0, INT_MAX - 24);
289 ret = au_get_annotations(s, &annotations);
292 au->header_size = 24 + annotations.len & ~7;
294 ffio_wfourcc(pb, ".snd"); /* magic number */
295 avio_wb32(pb, au->header_size); /* header size */
296 avio_wb32(pb, AU_UNKNOWN_SIZE); /* data size */
297 avio_wb32(pb, par->codec_tag); /* codec ID */
298 avio_wb32(pb, par->sample_rate);
299 avio_wb32(pb, par->channels);
300 avio_write(pb, annotations.str, annotations.len & ~7);
303 av_bprint_finalize(&annotations, NULL);
308 static int au_write_trailer(AVFormatContext *s)
310 AVIOContext *pb = s->pb;
311 AUContext *au = s->priv_data;
312 int64_t file_size = avio_tell(pb);
314 if ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) && file_size < INT32_MAX) {
315 /* update file size */
316 avio_seek(pb, 8, SEEK_SET);
317 avio_wb32(pb, (uint32_t)(file_size - au->header_size));
318 avio_seek(pb, file_size, SEEK_SET);
324 AVOutputFormat ff_au_muxer = {
326 .long_name = NULL_IF_CONFIG_SMALL("Sun AU"),
327 .mime_type = "audio/basic",
329 .priv_data_size = sizeof(AUContext),
330 .audio_codec = AV_CODEC_ID_PCM_S16BE,
331 .video_codec = AV_CODEC_ID_NONE,
332 .write_header = au_write_header,
333 .write_packet = ff_raw_write_packet,
334 .write_trailer = au_write_trailer,
335 .codec_tag = (const AVCodecTag* const []) { codec_au_tags, 0 },
336 .flags = AVFMT_NOTIMESTAMPS,
339 #endif /* CONFIG_AU_MUXER */