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"
36#include "avformat.h"
37#include "demux.h"
38#include "internal.h"
39#include "avio_internal.h"
40#include "mux.h"
41#include "pcm.h"
42#include "libavutil/avassert.h"
43
44/* if we don't know the size in advance */
45#define AU_UNKNOWN_SIZE ((uint32_t)(~0))
46
63
64static const AVCodecTag *const au_codec_tags[] = { codec_au_tags, NULL };
65
66#if CONFIG_AU_DEMUXER
67
68static int au_probe(const AVProbeData *p)
69{
70 if (p->buf_size < 24 ||
71 AV_RL32(p->buf) != MKTAG('.', 's', 'n', 'd') ||
72 AV_RN32(p->buf+4) == 0 ||
73 AV_RN32(p->buf+8) == 0 ||
74 AV_RN32(p->buf+12) == 0 ||
75 AV_RN32(p->buf+16) == 0 ||
76 AV_RN32(p->buf+20) == 0)
77 return 0;
78 return AVPROBE_SCORE_MAX;
79}
80
81static int au_read_annotation(AVFormatContext *s, int size)
82{
83 static const char keys[][7] = {
84 "title",
85 "artist",
86 "album",
87 "track",
88 "genre",
89 };
90 AVIOContext *pb = s->pb;
91 enum { PARSE_KEY, PARSE_VALUE, PARSE_FINISHED } state = PARSE_KEY;
92 char c;
93 AVBPrint bprint;
94 char * key = NULL;
95 char * value = NULL;
96 int ret, i;
97
99
100 while (size-- > 0) {
101 if (avio_feof(pb)) {
102 av_bprint_finalize(&bprint, NULL);
103 av_freep(&key);
104 return AVERROR_EOF;
105 }
106 c = avio_r8(pb);
107 switch(state) {
108 case PARSE_KEY:
109 if (c == '\0') {
110 state = PARSE_FINISHED;
111 } else if (c == '=') {
112 ret = av_bprint_finalize(&bprint, &key);
113 if (ret < 0)
114 return ret;
116 state = PARSE_VALUE;
117 } else {
118 av_bprint_chars(&bprint, c, 1);
119 }
120 break;
121 case PARSE_VALUE:
122 if (c == '\0' || c == '\n') {
123 if (av_bprint_finalize(&bprint, &value) != 0) {
124 av_log(s, AV_LOG_ERROR, "Memory error while parsing AU metadata.\n");
125 } else {
127 for (i = 0; i < FF_ARRAY_ELEMS(keys); i++) {
128 if (av_strcasecmp(keys[i], key) == 0) {
129 av_dict_set(&(s->metadata), keys[i], value, AV_DICT_DONT_STRDUP_VAL);
130 value = NULL;
131 break;
132 }
133 }
134 }
135 av_freep(&key);
136 av_freep(&value);
137 state = (c == '\0') ? PARSE_FINISHED : PARSE_KEY;
138 } else {
139 av_bprint_chars(&bprint, c, 1);
140 }
141 break;
142 case PARSE_FINISHED:
143 break;
144 default:
145 /* should never happen */
146 av_assert0(0);
147 }
148 }
149 av_bprint_finalize(&bprint, NULL);
150 av_freep(&key);
151 return 0;
152}
153
154#define BLOCK_SIZE 1024
155
156static int au_read_header(AVFormatContext *s)
157{
158 int size, data_size = 0;
159 unsigned int tag;
160 AVIOContext *pb = s->pb;
161 unsigned int id, channels, rate;
162 int bps, ba = 0;
163 enum AVCodecID codec;
164 AVStream *st;
165 int ret;
166
167 tag = avio_rl32(pb);
168 if (tag != MKTAG('.', 's', 'n', 'd'))
169 return AVERROR_INVALIDDATA;
170 size = avio_rb32(pb); /* header size */
171 data_size = avio_rb32(pb); /* data size in bytes */
172
173 if (data_size < 0 && data_size != AU_UNKNOWN_SIZE) {
174 av_log(s, AV_LOG_ERROR, "Invalid negative data size '%d' found\n", data_size);
175 return AVERROR_INVALIDDATA;
176 }
177
178 id = avio_rb32(pb);
179 rate = avio_rb32(pb);
180 channels = avio_rb32(pb);
181
182 if (size > 24) {
183 /* parse annotation field to get metadata */
184 ret = au_read_annotation(s, size - 24);
185 if (ret < 0)
186 return ret;
187 }
188
189 codec = ff_codec_get_id(codec_au_tags, id);
190
191 if (codec == AV_CODEC_ID_NONE) {
192 avpriv_request_sample(s, "unknown or unsupported codec tag: %u", id);
194 }
195
197 if (codec == AV_CODEC_ID_ADPCM_G726LE) {
198 if (id == MKBETAG('7','2','6','2')) {
199 bps = 2;
200 } else {
201 const uint8_t bpcss[] = {4, 0, 3, 5};
202 av_assert0(id >= 23 && id < 23 + 4);
203 ba = bpcss[id - 23];
204 bps = bpcss[id - 23];
205 }
206 } else if (!bps) {
207 avpriv_request_sample(s, "Unknown bits per sample");
209 }
210
211 if (channels == 0 || channels >= INT_MAX / (BLOCK_SIZE * bps >> 3)) {
212 av_log(s, AV_LOG_ERROR, "Invalid number of channels %u\n", channels);
213 return AVERROR_INVALIDDATA;
214 }
215
216 if (rate == 0 || rate > INT_MAX) {
217 av_log(s, AV_LOG_ERROR, "Invalid sample rate: %u\n", rate);
218 return AVERROR_INVALIDDATA;
219 }
220
222 if (!st)
223 return AVERROR(ENOMEM);
225 st->codecpar->codec_tag = id;
226 st->codecpar->codec_id = codec;
228 st->codecpar->sample_rate = rate;
230 st->codecpar->bit_rate = channels * rate * bps;
231 st->codecpar->block_align = ba ? ba : FFMAX(bps * channels / 8, 1);
232 if (data_size != AU_UNKNOWN_SIZE)
233 st->duration = (((int64_t)data_size)<<3) / (channels * (int64_t)bps);
234
235 st->start_time = 0;
236 avpriv_set_pts_info(st, 64, 1, rate);
237
238 return 0;
239}
240
242 .p.name = "au",
243 .p.long_name = NULL_IF_CONFIG_SMALL("Sun AU"),
244 .p.codec_tag = au_codec_tags,
245 .read_probe = au_probe,
246 .read_header = au_read_header,
247 .read_packet = ff_pcm_read_packet,
248 .read_seek = ff_pcm_read_seek,
249};
250
251#endif /* CONFIG_AU_DEMUXER */
252
253#if CONFIG_AU_MUXER
254
255typedef struct AUContext {
256 uint32_t header_size;
257} AUContext;
258
259#include "rawenc.h"
260
261static int au_get_annotations(AVFormatContext *s, AVBPrint *annotations)
262{
263 static const char keys[][7] = {
264 "Title",
265 "Artist",
266 "Album",
267 "Track",
268 "Genre",
269 };
270 int cnt = 0;
271 AVDictionary *m = s->metadata;
273
274 for (int i = 0; i < FF_ARRAY_ELEMS(keys); i++) {
275 t = av_dict_get(m, keys[i], NULL, 0);
276 if (t != NULL) {
277 if (cnt++)
278 av_bprint_chars(annotations, '\n', 1);
279 av_bprintf(annotations, "%s=%s", keys[i], t->value);
280 }
281 }
282 /* The specification requires the annotation field to be zero-terminated
283 * and its length to be a multiple of eight, so pad with 0's */
284 av_bprint_chars(annotations, '\0', 8);
285 return av_bprint_is_complete(annotations) ? 0 : AVERROR(ENOMEM);
286}
287
288/* Sun encodings 25, 23 and 26 are 3-, 4- and 5-bit G.726. There is no Sun
289 * number for 2-bit G.726; '7262' is the private tag the demuxer reads. */
290static int au_g726le_tag(AVFormatContext *s, AVCodecParameters *par)
291{
293
294 if (bps <= 0) {
295 if (par->bit_rate <= 0 || par->sample_rate <= 0 ||
296 par->ch_layout.nb_channels <= 0) {
297 av_log(s, AV_LOG_ERROR, "Cannot determine G.726LE bit width\n");
298 return AVERROR(EINVAL);
299 }
300 bps = av_rescale(par->bit_rate, 1,
302 }
303
304 switch (bps) {
305 case 2:
307 "Sun AU has no standard encoding for 2-bit G.726LE; "
308 "writing non-standard '7262' tag\n");
309 par->codec_tag = MKBETAG('7', '2', '6', '2');
310 break;
311 case 3:
312 par->codec_tag = 25;
313 break;
314 case 4:
315 par->codec_tag = 23;
316 break;
317 case 5:
318 par->codec_tag = 26;
319 break;
320 default:
322 "Unsupported G.726LE bit width %"PRId64"\n", bps);
323 return AVERROR(EINVAL);
324 }
325 return 0;
326}
327
328static int au_write_header(AVFormatContext *s)
329{
330 int ret;
331 AUContext *au = s->priv_data;
332 AVIOContext *pb = s->pb;
333 AVCodecParameters *par = s->streams[0]->codecpar;
334 AVBPrint annotations;
335
337 ret = au_g726le_tag(s, par);
338 if (ret < 0)
339 return ret;
340 } else {
342 if (!par->codec_tag) {
343 av_log(s, AV_LOG_ERROR, "unsupported codec\n");
344 return AVERROR(EINVAL);
345 }
346 }
347
348 av_bprint_init(&annotations, 0, INT_MAX - 24);
349 ret = au_get_annotations(s, &annotations);
350 if (ret < 0)
351 goto fail;
352 au->header_size = 24 + annotations.len & ~7;
353
354 ffio_wfourcc(pb, ".snd"); /* magic number */
355 avio_wb32(pb, au->header_size); /* header size */
356 avio_wb32(pb, AU_UNKNOWN_SIZE); /* data size */
357 avio_wb32(pb, par->codec_tag); /* codec ID */
358 avio_wb32(pb, par->sample_rate);
360 avio_write(pb, annotations.str, annotations.len & ~7);
361
362fail:
363 av_bprint_finalize(&annotations, NULL);
364
365 return ret;
366}
367
368static int au_write_trailer(AVFormatContext *s)
369{
370 AVIOContext *pb = s->pb;
371 AUContext *au = s->priv_data;
373
374 if ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) && file_size < INT32_MAX) {
375 /* update file size */
376 avio_seek(pb, 8, SEEK_SET);
377 avio_wb32(pb, (uint32_t)(file_size - au->header_size));
378 avio_seek(pb, file_size, SEEK_SET);
379 }
380
381 return 0;
382}
383
385 .p.name = "au",
386 .p.long_name = NULL_IF_CONFIG_SMALL("Sun AU"),
387 .p.mime_type = "audio/basic",
388 .p.extensions = "au",
389 .p.codec_tag = au_codec_tags,
390 .p.audio_codec = AV_CODEC_ID_PCM_S16BE,
391 .p.video_codec = AV_CODEC_ID_NONE,
392 .p.subtitle_codec = AV_CODEC_ID_NONE,
393 .p.flags = AVFMT_NOTIMESTAMPS,
394 .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH,
395 .priv_data_size = sizeof(AUContext),
396 .write_header = au_write_header,
398 .write_trailer = au_write_trailer,
399};
400
401#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:64
#define AU_UNKNOWN_SIZE
Definition au.c:45
static const AVCodecTag codec_au_tags[]
Definition au.c:47
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:865
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:485
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition avformat.h:500
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:507
unsigned int avio_rl32(AVIOContext *s)
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition aviobuf.c:206
unsigned int avio_rb32(AVIOContext *s)
int avio_r8(AVIOContext *s)
Definition aviobuf.c:611
static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
void av_bprintf(AVBPrint *buf, const char *fmt,...)
Definition bprint.c:121
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition bprint.c:68
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:612
double value
Definition eval.c:102
static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
Definition ffmpeg_mux.c:203
const char * key
int ret
Definition ffplay.c:391
static void write_header(FFV1Context *f)
Definition ffv1enc.c:397
#define fail
Definition test.h:479
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition utils.c:557
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition codec_id.h:47
@ AV_CODEC_ID_PCM_S24BE
Definition codec_id.h:346
@ AV_CODEC_ID_PCM_F32BE
Definition codec_id.h:353
@ AV_CODEC_ID_ADPCM_G722
Definition codec_id.h:402
@ AV_CODEC_ID_NONE
Definition codec_id.h:48
@ AV_CODEC_ID_PCM_S16BE
Definition codec_id.h:334
@ AV_CODEC_ID_ADPCM_G726LE
Definition codec_id.h:409
@ AV_CODEC_ID_PCM_S8
Definition codec_id.h:337
@ AV_CODEC_ID_PCM_ALAW
Definition codec_id.h:340
@ AV_CODEC_ID_PCM_F64BE
Definition codec_id.h:355
@ AV_CODEC_ID_PCM_S32BE
Definition codec_id.h:342
@ AV_CODEC_ID_PCM_MULAW
Definition codec_id.h:339
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:234
void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
Append char c n times to a print buffer.
Definition bprint.c:129
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:61
#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:87
#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_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
@ 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:97
#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:2157
unsigned bps
Definition movenc.c:2158
#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
static size_t file_size
#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:1401
Bytestream IO Context.
Definition avio.h:160
This structure contains the data a format has to probe a file.
Definition avformat.h:473
Stream structure.
Definition avformat.h:768
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:791
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition avformat.h:827
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition avformat.h:817
#define avpriv_request_sample(...)
#define av_freep(p)
#define av_log(a,...)
static struct @232240247175332074307271266321204261331170211232 state
int size
static int write_trailer(AVFormatContext *s1)
Definition v4l2enc.c:101
static double c[64]