FFmpeg
Loading...
Searching...
No Matches
au.c
Go to the documentation of this file.
1/*
2 * AU muxer and demuxer
3 * Copyright (c) 2001 Fabrice Bellard
4 *
5 * first version by Francois Revol <revol@free.fr>
6 *
7 * This file is part of FFmpeg.
8 *
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.
13 *
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.
18 *
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
22 */
23
24/*
25 * Reference documents:
26 * http://www.opengroup.org/public/pubs/external/auformat.html
27 * http://www.goice.co.jp/member/mo/formats/au.html
28 */
29
30#include "config_components.h"
31
32#include "libavutil/bprint.h"
34#include "libavutil/mem.h"
35#include "avformat.h"
36#include "demux.h"
37#include "internal.h"
38#include "avio_internal.h"
39#include "mux.h"
40#include "pcm.h"
41#include "libavutil/avassert.h"
42
43/* if we don't know the size in advance */
44#define AU_UNKNOWN_SIZE ((uint32_t)(~0))
45
62
63static const AVCodecTag *const au_codec_tags[] = { codec_au_tags, NULL };
64
65#if CONFIG_AU_DEMUXER
66
67static int au_probe(const AVProbeData *p)
68{
69 if (p->buf_size < 24 ||
70 AV_RL32(p->buf) != MKTAG('.', 's', 'n', 'd') ||
71 AV_RN32(p->buf+4) == 0 ||
72 AV_RN32(p->buf+8) == 0 ||
73 AV_RN32(p->buf+12) == 0 ||
74 AV_RN32(p->buf+16) == 0 ||
75 AV_RN32(p->buf+20) == 0)
76 return 0;
77 return AVPROBE_SCORE_MAX;
78}
79
80static int au_read_annotation(AVFormatContext *s, int size)
81{
82 static const char keys[][7] = {
83 "title",
84 "artist",
85 "album",
86 "track",
87 "genre",
88 };
89 AVIOContext *pb = s->pb;
90 enum { PARSE_KEY, PARSE_VALUE, PARSE_FINISHED } state = PARSE_KEY;
91 char c;
92 AVBPrint bprint;
93 char * key = NULL;
94 char * value = NULL;
95 int ret, i;
96
98
99 while (size-- > 0) {
100 if (avio_feof(pb)) {
101 av_bprint_finalize(&bprint, NULL);
102 av_freep(&key);
103 return AVERROR_EOF;
104 }
105 c = avio_r8(pb);
106 switch(state) {
107 case PARSE_KEY:
108 if (c == '\0') {
109 state = PARSE_FINISHED;
110 } else if (c == '=') {
111 ret = av_bprint_finalize(&bprint, &key);
112 if (ret < 0)
113 return ret;
115 state = PARSE_VALUE;
116 } else {
117 av_bprint_chars(&bprint, c, 1);
118 }
119 break;
120 case PARSE_VALUE:
121 if (c == '\0' || c == '\n') {
122 if (av_bprint_finalize(&bprint, &value) != 0) {
123 av_log(s, AV_LOG_ERROR, "Memory error while parsing AU metadata.\n");
124 } else {
126 for (i = 0; i < FF_ARRAY_ELEMS(keys); i++) {
127 if (av_strcasecmp(keys[i], key) == 0) {
128 av_dict_set(&(s->metadata), keys[i], value, AV_DICT_DONT_STRDUP_VAL);
129 value = NULL;
130 break;
131 }
132 }
133 }
134 av_freep(&key);
135 av_freep(&value);
136 state = (c == '\0') ? PARSE_FINISHED : PARSE_KEY;
137 } else {
138 av_bprint_chars(&bprint, c, 1);
139 }
140 break;
141 case PARSE_FINISHED:
142 break;
143 default:
144 /* should never happen */
145 av_assert0(0);
146 }
147 }
148 av_bprint_finalize(&bprint, NULL);
149 av_freep(&key);
150 return 0;
151}
152
153#define BLOCK_SIZE 1024
154
155static int au_read_header(AVFormatContext *s)
156{
157 int size, data_size = 0;
158 unsigned int tag;
159 AVIOContext *pb = s->pb;
160 unsigned int id, channels, rate;
161 int bps, ba = 0;
162 enum AVCodecID codec;
163 AVStream *st;
164 int ret;
165
166 tag = avio_rl32(pb);
167 if (tag != MKTAG('.', 's', 'n', 'd'))
168 return AVERROR_INVALIDDATA;
169 size = avio_rb32(pb); /* header size */
170 data_size = avio_rb32(pb); /* data size in bytes */
171
172 if (data_size < 0 && data_size != AU_UNKNOWN_SIZE) {
173 av_log(s, AV_LOG_ERROR, "Invalid negative data size '%d' found\n", data_size);
174 return AVERROR_INVALIDDATA;
175 }
176
177 id = avio_rb32(pb);
178 rate = avio_rb32(pb);
179 channels = avio_rb32(pb);
180
181 if (size > 24) {
182 /* parse annotation field to get metadata */
183 ret = au_read_annotation(s, size - 24);
184 if (ret < 0)
185 return ret;
186 }
187
188 codec = ff_codec_get_id(codec_au_tags, id);
189
190 if (codec == AV_CODEC_ID_NONE) {
191 avpriv_request_sample(s, "unknown or unsupported codec tag: %u", id);
193 }
194
196 if (codec == AV_CODEC_ID_ADPCM_G726LE) {
197 if (id == MKBETAG('7','2','6','2')) {
198 bps = 2;
199 } else {
200 const uint8_t bpcss[] = {4, 0, 3, 5};
201 av_assert0(id >= 23 && id < 23 + 4);
202 ba = bpcss[id - 23];
203 bps = bpcss[id - 23];
204 }
205 } else if (!bps) {
206 avpriv_request_sample(s, "Unknown bits per sample");
208 }
209
210 if (channels == 0 || channels >= INT_MAX / (BLOCK_SIZE * bps >> 3)) {
211 av_log(s, AV_LOG_ERROR, "Invalid number of channels %u\n", channels);
212 return AVERROR_INVALIDDATA;
213 }
214
215 if (rate == 0 || rate > INT_MAX) {
216 av_log(s, AV_LOG_ERROR, "Invalid sample rate: %u\n", rate);
217 return AVERROR_INVALIDDATA;
218 }
219
221 if (!st)
222 return AVERROR(ENOMEM);
224 st->codecpar->codec_tag = id;
225 st->codecpar->codec_id = codec;
227 st->codecpar->sample_rate = rate;
229 st->codecpar->bit_rate = channels * rate * bps;
230 st->codecpar->block_align = ba ? ba : FFMAX(bps * channels / 8, 1);
231 if (data_size != AU_UNKNOWN_SIZE)
232 st->duration = (((int64_t)data_size)<<3) / (channels * (int64_t)bps);
233
234 st->start_time = 0;
235 avpriv_set_pts_info(st, 64, 1, rate);
236
237 return 0;
238}
239
241 .p.name = "au",
242 .p.long_name = NULL_IF_CONFIG_SMALL("Sun AU"),
243 .p.codec_tag = au_codec_tags,
244 .read_probe = au_probe,
245 .read_header = au_read_header,
246 .read_packet = ff_pcm_read_packet,
247 .read_seek = ff_pcm_read_seek,
248};
249
250#endif /* CONFIG_AU_DEMUXER */
251
252#if CONFIG_AU_MUXER
253
254typedef struct AUContext {
255 uint32_t header_size;
256} AUContext;
257
258#include "rawenc.h"
259
260static int au_get_annotations(AVFormatContext *s, AVBPrint *annotations)
261{
262 static const char keys[][7] = {
263 "Title",
264 "Artist",
265 "Album",
266 "Track",
267 "Genre",
268 };
269 int cnt = 0;
270 AVDictionary *m = s->metadata;
272
273 for (int i = 0; i < FF_ARRAY_ELEMS(keys); i++) {
274 t = av_dict_get(m, keys[i], NULL, 0);
275 if (t != NULL) {
276 if (cnt++)
277 av_bprint_chars(annotations, '\n', 1);
278 av_bprintf(annotations, "%s=%s", keys[i], t->value);
279 }
280 }
281 /* The specification requires the annotation field to be zero-terminated
282 * and its length to be a multiple of eight, so pad with 0's */
283 av_bprint_chars(annotations, '\0', 8);
284 return av_bprint_is_complete(annotations) ? 0 : AVERROR(ENOMEM);
285}
286
287static int au_write_header(AVFormatContext *s)
288{
289 int ret;
290 AUContext *au = s->priv_data;
291 AVIOContext *pb = s->pb;
292 AVCodecParameters *par = s->streams[0]->codecpar;
293 AVBPrint annotations;
294
296 if (!par->codec_tag) {
297 av_log(s, AV_LOG_ERROR, "unsupported codec\n");
298 return AVERROR(EINVAL);
299 }
300
301 av_bprint_init(&annotations, 0, INT_MAX - 24);
302 ret = au_get_annotations(s, &annotations);
303 if (ret < 0)
304 goto fail;
305 au->header_size = 24 + annotations.len & ~7;
306
307 ffio_wfourcc(pb, ".snd"); /* magic number */
308 avio_wb32(pb, au->header_size); /* header size */
309 avio_wb32(pb, AU_UNKNOWN_SIZE); /* data size */
310 avio_wb32(pb, par->codec_tag); /* codec ID */
311 avio_wb32(pb, par->sample_rate);
313 avio_write(pb, annotations.str, annotations.len & ~7);
314
315fail:
316 av_bprint_finalize(&annotations, NULL);
317
318 return ret;
319}
320
321static int au_write_trailer(AVFormatContext *s)
322{
323 AVIOContext *pb = s->pb;
324 AUContext *au = s->priv_data;
325 int64_t file_size = avio_tell(pb);
326
327 if ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) && file_size < INT32_MAX) {
328 /* update file size */
329 avio_seek(pb, 8, SEEK_SET);
330 avio_wb32(pb, (uint32_t)(file_size - au->header_size));
331 avio_seek(pb, file_size, SEEK_SET);
332 }
333
334 return 0;
335}
336
338 .p.name = "au",
339 .p.long_name = NULL_IF_CONFIG_SMALL("Sun AU"),
340 .p.mime_type = "audio/basic",
341 .p.extensions = "au",
342 .p.codec_tag = au_codec_tags,
343 .p.audio_codec = AV_CODEC_ID_PCM_S16BE,
344 .p.video_codec = AV_CODEC_ID_NONE,
345 .p.subtitle_codec = AV_CODEC_ID_NONE,
346 .p.flags = AVFMT_NOTIMESTAMPS,
347 .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH,
348 .priv_data_size = sizeof(AUContext),
349 .write_header = au_write_header,
351 .write_trailer = au_write_trailer,
352};
353
354#endif /* CONFIG_AU_MUXER */
#define BLOCK_SIZE
Definition adx.h:51
const FFInputFormat ff_au_demuxer
const FFOutputFormat ff_au_muxer
channels
Definition aptx.h:31
static const AVCodecTag *const au_codec_tags[]
Definition au.c:63
#define AU_UNKNOWN_SIZE
Definition au.c:44
static const AVCodecTag codec_au_tags[]
Definition au.c:46
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:483
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition avformat.h:498
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition avio.h:41
void avio_wb32(AVIOContext *s, unsigned int val)
Definition aviobuf.c:368
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
unsigned int avio_rl32(AVIOContext *s)
Definition aviobuf.c:733
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition aviobuf.c:206
unsigned int avio_rb32(AVIOContext *s)
Definition aviobuf.c:764
int avio_r8(AVIOContext *s)
Definition aviobuf.c:606
static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
void av_bprintf(AVBPrint *buf, const char *fmt,...)
Definition bprint.c:122
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition bprint.c:69
AVBPrint public header.
#define AV_BPRINT_SIZE_UNLIMITED
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
enum AVCodecID id
Definition dts2pts.c:607
double value
Definition eval.c:102
static struct @346255127015250356166251341105367306144006377143 state
static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
Definition ffmpeg_mux.c:204
const char * key
static void write_header(FFV1Context *f)
Definition ffv1enc.c:384
#define fail
Definition test.h:479
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition utils.c:556
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition codec_id.h:47
@ AV_CODEC_ID_PCM_S24BE
Definition codec_id.h:343
@ AV_CODEC_ID_PCM_F32BE
Definition codec_id.h:350
@ AV_CODEC_ID_ADPCM_G722
Definition codec_id.h:398
@ AV_CODEC_ID_NONE
Definition codec_id.h:48
@ AV_CODEC_ID_PCM_S16BE
Definition codec_id.h:331
@ AV_CODEC_ID_ADPCM_G726LE
Definition codec_id.h:405
@ AV_CODEC_ID_PCM_S8
Definition codec_id.h:334
@ AV_CODEC_ID_PCM_ALAW
Definition codec_id.h:337
@ AV_CODEC_ID_PCM_F64BE
Definition codec_id.h:352
@ AV_CODEC_ID_PCM_S32BE
Definition codec_id.h:339
@ AV_CODEC_ID_PCM_MULAW
Definition codec_id.h:336
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
static int av_bprint_is_complete(const AVBPrint *buf)
Test if the print buffer is complete (not truncated).
Definition bprint.h:218
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition bprint.c:235
void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
Append char c n times to a print buffer.
Definition bprint.c:130
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition dict.c:60
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() or another memory allocation functio...
Definition dict.h:79
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition dict.c:86
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
Definition avstring.c:208
#define AV_RN32(p)
#define AV_RL32(p)
unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum AVCodecID id)
Definition utils.c:133
enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
Definition utils.c:143
int ff_pcm_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition pcm.c:57
int ff_pcm_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition pcm.c:73
int ff_raw_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition rawenc.c:31
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define MKTAG(a, b, c, d)
Definition macros.h:55
#define MKBETAG(a, b, c, d)
Definition macros.h:56
#define FFMAX(a, b)
Definition macros.h:47
Memory handling functions.
uint32_t tag
Definition movenc.c:2087
unsigned bps
Definition movenc.c:2088
#define FF_OFMT_FLAG_MAX_ONE_OF_EACH
If this flag is set, it indicates that for each codec type whose corresponding default codec (i....
Definition mux.h:50
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
#define FF_ARRAY_ELEMS(a)
int nb_channels
Number of channels in this layout.
This struct describes the properties of an encoded stream.
Definition codec_par.h:49
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition codec_par.h:113
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition codec_par.h:99
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
int block_align
The number of bytes per coded audio frame, required by some formats.
Definition codec_par.h:221
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition codec_par.h:61
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
char * value
Definition dict.h:92
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition avformat.h:825
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition avformat.h:815
#define avpriv_request_sample(...)
#define av_freep(p)
#define av_log(a,...)
int size
static int write_trailer(AVFormatContext *s1)
Definition v4l2enc.c:101
static double c[64]