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))
38 /* the specification requires an annotation field of at least eight bytes */
39 #define AU_DEFAULT_HEADER_SIZE (24+8)
41 static const AVCodecTag codec_au_tags[] = {
42 { AV_CODEC_ID_PCM_MULAW, 1 },
43 { AV_CODEC_ID_PCM_S8, 2 },
44 { AV_CODEC_ID_PCM_S16BE, 3 },
45 { AV_CODEC_ID_PCM_S24BE, 4 },
46 { AV_CODEC_ID_PCM_S32BE, 5 },
47 { AV_CODEC_ID_PCM_F32BE, 6 },
48 { AV_CODEC_ID_PCM_F64BE, 7 },
49 { AV_CODEC_ID_ADPCM_G726LE, 23 },
50 { AV_CODEC_ID_ADPCM_G722,24 },
51 { AV_CODEC_ID_ADPCM_G726LE, 25 },
52 { AV_CODEC_ID_ADPCM_G726LE, 26 },
53 { AV_CODEC_ID_PCM_ALAW, 27 },
54 { AV_CODEC_ID_ADPCM_G726LE, MKBETAG('7','2','6','2') },
55 { AV_CODEC_ID_NONE, 0 },
60 static int au_probe(AVProbeData *p)
62 if (p->buf[0] == '.' && p->buf[1] == 's' &&
63 p->buf[2] == 'n' && p->buf[3] == 'd')
64 return AVPROBE_SCORE_MAX;
69 #define BLOCK_SIZE 1024
71 static int au_read_header(AVFormatContext *s)
73 int size, data_size = 0;
75 AVIOContext *pb = s->pb;
76 unsigned int id, channels, rate;
82 if (tag != MKTAG('.', 's', 'n', 'd'))
83 return AVERROR_INVALIDDATA;
84 size = avio_rb32(pb); /* header size */
85 data_size = avio_rb32(pb); /* data size in bytes */
87 if (data_size < 0 && data_size != AU_UNKNOWN_SIZE) {
88 av_log(s, AV_LOG_ERROR, "Invalid negative data size '%d' found\n", data_size);
89 return AVERROR_INVALIDDATA;
94 channels = avio_rb32(pb);
97 /* skip unused data */
98 avio_skip(pb, size - 24);
101 codec = ff_codec_get_id(codec_au_tags, id);
103 if (codec == AV_CODEC_ID_NONE) {
104 avpriv_request_sample(s, "unknown or unsupported codec tag: %u", id);
105 return AVERROR_PATCHWELCOME;
108 bps = av_get_bits_per_sample(codec);
109 if (codec == AV_CODEC_ID_ADPCM_G726LE) {
110 if (id == MKBETAG('7','2','6','2')) {
113 const uint8_t bpcss[] = {4, 0, 3, 5};
114 av_assert0(id >= 23 && id < 23 + 4);
115 bps = bpcss[id - 23];
118 avpriv_request_sample(s, "Unknown bits per sample");
119 return AVERROR_PATCHWELCOME;
122 if (channels == 0 || channels >= INT_MAX / (BLOCK_SIZE * bps >> 3)) {
123 av_log(s, AV_LOG_ERROR, "Invalid number of channels %u\n", channels);
124 return AVERROR_INVALIDDATA;
127 if (rate == 0 || rate > INT_MAX) {
128 av_log(s, AV_LOG_ERROR, "Invalid sample rate: %u\n", rate);
129 return AVERROR_INVALIDDATA;
132 st = avformat_new_stream(s, NULL);
134 return AVERROR(ENOMEM);
135 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
136 st->codecpar->codec_tag = id;
137 st->codecpar->codec_id = codec;
138 st->codecpar->channels = channels;
139 st->codecpar->sample_rate = rate;
140 st->codecpar->bits_per_coded_sample = bps;
141 st->codecpar->bit_rate = channels * rate * bps;
142 st->codecpar->block_align = FFMAX(bps * st->codecpar->channels / 8, 1);
143 if (data_size != AU_UNKNOWN_SIZE)
144 st->duration = (((int64_t)data_size)<<3) / (st->codecpar->channels * (int64_t)bps);
147 avpriv_set_pts_info(st, 64, 1, rate);
152 AVInputFormat ff_au_demuxer = {
154 .long_name = NULL_IF_CONFIG_SMALL("Sun AU"),
155 .read_probe = au_probe,
156 .read_header = au_read_header,
157 .read_packet = ff_pcm_read_packet,
158 .read_seek = ff_pcm_read_seek,
159 .codec_tag = (const AVCodecTag* const []) { codec_au_tags, 0 },
162 #endif /* CONFIG_AU_DEMUXER */
166 typedef struct AUContext {
167 uint32_t header_size;
172 static int au_get_annotations(AVFormatContext *s, char **buffer)
174 static const char * keys[] = {
183 AVDictionary *m = s->metadata;
184 AVDictionaryEntry *t = NULL;
187 av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED);
189 for (i = 0; keys[i] != NULL; i++) {
190 t = av_dict_get(m, keys[i], NULL, 0);
193 av_bprint_chars(&bprint, '\n', 1);
194 av_bprint_append_data(&bprint, keys[i], strlen(keys[i]));
195 av_bprint_chars(&bprint, '=', 1);
196 av_bprint_append_data(&bprint, t->value, strlen(t->value));
200 av_bprint_append_data(&bprint, "\0\0\0\0\0\0\0\0", 8);
201 return av_bprint_finalize(&bprint, buffer);
204 static int au_write_header(AVFormatContext *s)
207 AUContext *au = s->priv_data;
208 AVIOContext *pb = s->pb;
209 AVCodecParameters *par = s->streams[0]->codecpar;
210 char *annotations = NULL;
212 au->header_size = AU_DEFAULT_HEADER_SIZE;
214 if (s->nb_streams != 1) {
215 av_log(s, AV_LOG_ERROR, "only one stream is supported\n");
216 return AVERROR(EINVAL);
219 par->codec_tag = ff_codec_get_tag(codec_au_tags, par->codec_id);
220 if (!par->codec_tag) {
221 av_log(s, AV_LOG_ERROR, "unsupported codec\n");
222 return AVERROR(EINVAL);
225 if (av_dict_count(s->metadata) > 0) {
226 ret = au_get_annotations(s, &annotations);
229 if (annotations != NULL) {
230 au->header_size = (24 + strlen(annotations) + 8) & ~7;
231 if (au->header_size < AU_DEFAULT_HEADER_SIZE)
232 au->header_size = AU_DEFAULT_HEADER_SIZE;
235 ffio_wfourcc(pb, ".snd"); /* magic number */
236 avio_wb32(pb, au->header_size); /* header size */
237 avio_wb32(pb, AU_UNKNOWN_SIZE); /* data size */
238 avio_wb32(pb, par->codec_tag); /* codec ID */
239 avio_wb32(pb, par->sample_rate);
240 avio_wb32(pb, par->channels);
241 if (annotations != NULL) {
242 avio_write(pb, annotations, au->header_size - 24);
243 av_freep(&annotations);
245 avio_wb64(pb, 0); /* annotation field */
252 static int au_write_trailer(AVFormatContext *s)
254 AVIOContext *pb = s->pb;
255 AUContext *au = s->priv_data;
256 int64_t file_size = avio_tell(pb);
258 if (s->pb->seekable && file_size < INT32_MAX) {
259 /* update file size */
260 avio_seek(pb, 8, SEEK_SET);
261 avio_wb32(pb, (uint32_t)(file_size - au->header_size));
262 avio_seek(pb, file_size, SEEK_SET);
269 AVOutputFormat ff_au_muxer = {
271 .long_name = NULL_IF_CONFIG_SMALL("Sun AU"),
272 .mime_type = "audio/basic",
274 .priv_data_size = sizeof(AUContext),
275 .audio_codec = AV_CODEC_ID_PCM_S16BE,
276 .video_codec = AV_CODEC_ID_NONE,
277 .write_header = au_write_header,
278 .write_packet = ff_raw_write_packet,
279 .write_trailer = au_write_trailer,
280 .codec_tag = (const AVCodecTag* const []) { codec_au_tags, 0 },
281 .flags = AVFMT_NOTIMESTAMPS,
284 #endif /* CONFIG_AU_MUXER */