FFmpeg
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 "avformat.h"
31 #include "internal.h"
32 #include "avio_internal.h"
33 #include "pcm.h"
34 #include "libavutil/avassert.h"
35 
36 /* if we don't know the size in advance */
37 #define AU_UNKNOWN_SIZE ((uint32_t)(~0))
38 
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 },
51  { AV_CODEC_ID_PCM_ALAW, 27 },
52  { AV_CODEC_ID_ADPCM_G726LE, MKBETAG('7','2','6','2') },
53  { AV_CODEC_ID_NONE, 0 },
54 };
55 
56 #if CONFIG_AU_DEMUXER
57 
58 static int au_probe(const AVProbeData *p)
59 {
60  if (p->buf[0] == '.' && p->buf[1] == 's' &&
61  p->buf[2] == 'n' && p->buf[3] == 'd')
62  return AVPROBE_SCORE_MAX;
63  else
64  return 0;
65 }
66 
67 static int au_read_annotation(AVFormatContext *s, int size)
68 {
69  static const char keys[][7] = {
70  "title",
71  "artist",
72  "album",
73  "track",
74  "genre",
75  };
76  AVIOContext *pb = s->pb;
77  enum { PARSE_KEY, PARSE_VALUE, PARSE_FINISHED } state = PARSE_KEY;
78  char c;
79  AVBPrint bprint;
80  char * key = NULL;
81  char * value = NULL;
82  int ret, i;
83 
85 
86  while (size-- > 0) {
87  if (avio_feof(pb)) {
88  av_bprint_finalize(&bprint, NULL);
89  av_freep(&key);
90  return AVERROR_EOF;
91  }
92  c = avio_r8(pb);
93  switch(state) {
94  case PARSE_KEY:
95  if (c == '\0') {
96  state = PARSE_FINISHED;
97  } else if (c == '=') {
98  ret = av_bprint_finalize(&bprint, &key);
99  if (ret < 0)
100  return ret;
102  state = PARSE_VALUE;
103  } else {
104  av_bprint_chars(&bprint, c, 1);
105  }
106  break;
107  case PARSE_VALUE:
108  if (c == '\0' || c == '\n') {
109  if (av_bprint_finalize(&bprint, &value) != 0) {
110  av_log(s, AV_LOG_ERROR, "Memory error while parsing AU metadata.\n");
111  } else {
113  for (i = 0; i < FF_ARRAY_ELEMS(keys); i++) {
114  if (av_strcasecmp(keys[i], key) == 0) {
115  av_dict_set(&(s->metadata), keys[i], value, AV_DICT_DONT_STRDUP_VAL);
116  value = NULL;
117  break;
118  }
119  }
120  }
121  av_freep(&key);
122  av_freep(&value);
123  state = (c == '\0') ? PARSE_FINISHED : PARSE_KEY;
124  } else {
125  av_bprint_chars(&bprint, c, 1);
126  }
127  break;
128  case PARSE_FINISHED:
129  break;
130  default:
131  /* should never happen */
132  av_assert0(0);
133  }
134  }
135  av_bprint_finalize(&bprint, NULL);
136  av_freep(&key);
137  return 0;
138 }
139 
140 #define BLOCK_SIZE 1024
141 
142 static int au_read_header(AVFormatContext *s)
143 {
144  int size, data_size = 0;
145  unsigned int tag;
146  AVIOContext *pb = s->pb;
147  unsigned int id, channels, rate;
148  int bps, ba = 0;
149  enum AVCodecID codec;
150  AVStream *st;
151  int ret;
152 
153  tag = avio_rl32(pb);
154  if (tag != MKTAG('.', 's', 'n', 'd'))
155  return AVERROR_INVALIDDATA;
156  size = avio_rb32(pb); /* header size */
157  data_size = avio_rb32(pb); /* data size in bytes */
158 
159  if (data_size < 0 && data_size != AU_UNKNOWN_SIZE) {
160  av_log(s, AV_LOG_ERROR, "Invalid negative data size '%d' found\n", data_size);
161  return AVERROR_INVALIDDATA;
162  }
163 
164  id = avio_rb32(pb);
165  rate = avio_rb32(pb);
166  channels = avio_rb32(pb);
167 
168  if (size > 24) {
169  /* parse annotation field to get metadata */
170  ret = au_read_annotation(s, size - 24);
171  if (ret < 0)
172  return ret;
173  }
174 
175  codec = ff_codec_get_id(codec_au_tags, id);
176 
177  if (codec == AV_CODEC_ID_NONE) {
178  avpriv_request_sample(s, "unknown or unsupported codec tag: %u", id);
179  return AVERROR_PATCHWELCOME;
180  }
181 
182  bps = av_get_bits_per_sample(codec);
183  if (codec == AV_CODEC_ID_ADPCM_G726LE) {
184  if (id == MKBETAG('7','2','6','2')) {
185  bps = 2;
186  } else {
187  const uint8_t bpcss[] = {4, 0, 3, 5};
188  av_assert0(id >= 23 && id < 23 + 4);
189  ba = bpcss[id - 23];
190  bps = bpcss[id - 23];
191  }
192  } else if (!bps) {
193  avpriv_request_sample(s, "Unknown bits per sample");
194  return AVERROR_PATCHWELCOME;
195  }
196 
197  if (channels == 0 || channels >= INT_MAX / (BLOCK_SIZE * bps >> 3)) {
198  av_log(s, AV_LOG_ERROR, "Invalid number of channels %u\n", channels);
199  return AVERROR_INVALIDDATA;
200  }
201 
202  if (rate == 0 || rate > INT_MAX) {
203  av_log(s, AV_LOG_ERROR, "Invalid sample rate: %u\n", rate);
204  return AVERROR_INVALIDDATA;
205  }
206 
207  st = avformat_new_stream(s, NULL);
208  if (!st)
209  return AVERROR(ENOMEM);
211  st->codecpar->codec_tag = id;
212  st->codecpar->codec_id = codec;
213  st->codecpar->channels = channels;
214  st->codecpar->sample_rate = rate;
216  st->codecpar->bit_rate = channels * rate * bps;
217  st->codecpar->block_align = ba ? ba : FFMAX(bps * st->codecpar->channels / 8, 1);
218  if (data_size != AU_UNKNOWN_SIZE)
219  st->duration = (((int64_t)data_size)<<3) / (st->codecpar->channels * (int64_t)bps);
220 
221  st->start_time = 0;
222  avpriv_set_pts_info(st, 64, 1, rate);
223 
224  return 0;
225 }
226 
228  .name = "au",
229  .long_name = NULL_IF_CONFIG_SMALL("Sun AU"),
230  .read_probe = au_probe,
231  .read_header = au_read_header,
232  .read_packet = ff_pcm_read_packet,
233  .read_seek = ff_pcm_read_seek,
234  .codec_tag = (const AVCodecTag* const []) { codec_au_tags, 0 },
235 };
236 
237 #endif /* CONFIG_AU_DEMUXER */
238 
239 #if CONFIG_AU_MUXER
240 
241 typedef struct AUContext {
242  uint32_t header_size;
243 } AUContext;
244 
245 #include "rawenc.h"
246 
247 static int au_get_annotations(AVFormatContext *s, AVBPrint *annotations)
248 {
249  static const char keys[][7] = {
250  "Title",
251  "Artist",
252  "Album",
253  "Track",
254  "Genre",
255  };
256  int cnt = 0;
257  AVDictionary *m = s->metadata;
258  AVDictionaryEntry *t = NULL;
259 
260  for (int i = 0; i < FF_ARRAY_ELEMS(keys); i++) {
261  t = av_dict_get(m, keys[i], NULL, 0);
262  if (t != NULL) {
263  if (cnt++)
264  av_bprint_chars(annotations, '\n', 1);
265  av_bprintf(annotations, "%s=%s", keys[i], t->value);
266  }
267  }
268  /* The specification requires the annotation field to be zero-terminated
269  * and its length to be a multiple of eight, so pad with 0's */
270  av_bprint_chars(annotations, '\0', 8);
271  return av_bprint_is_complete(annotations) ? 0 : AVERROR(ENOMEM);
272 }
273 
274 static int au_write_header(AVFormatContext *s)
275 {
276  int ret;
277  AUContext *au = s->priv_data;
278  AVIOContext *pb = s->pb;
279  AVCodecParameters *par = s->streams[0]->codecpar;
280  AVBPrint annotations;
281 
282  if (s->nb_streams != 1) {
283  av_log(s, AV_LOG_ERROR, "only one stream is supported\n");
284  return AVERROR(EINVAL);
285  }
286 
287  par->codec_tag = ff_codec_get_tag(codec_au_tags, par->codec_id);
288  if (!par->codec_tag) {
289  av_log(s, AV_LOG_ERROR, "unsupported codec\n");
290  return AVERROR(EINVAL);
291  }
292 
293  av_bprint_init(&annotations, 0, INT_MAX - 24);
294  ret = au_get_annotations(s, &annotations);
295  if (ret < 0)
296  goto fail;
297  au->header_size = 24 + annotations.len & ~7;
298 
299  ffio_wfourcc(pb, ".snd"); /* magic number */
300  avio_wb32(pb, au->header_size); /* header size */
301  avio_wb32(pb, AU_UNKNOWN_SIZE); /* data size */
302  avio_wb32(pb, par->codec_tag); /* codec ID */
303  avio_wb32(pb, par->sample_rate);
304  avio_wb32(pb, par->channels);
305  avio_write(pb, annotations.str, annotations.len & ~7);
306 
307 fail:
308  av_bprint_finalize(&annotations, NULL);
309 
310  return ret;
311 }
312 
313 static int au_write_trailer(AVFormatContext *s)
314 {
315  AVIOContext *pb = s->pb;
316  AUContext *au = s->priv_data;
317  int64_t file_size = avio_tell(pb);
318 
319  if ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) && file_size < INT32_MAX) {
320  /* update file size */
321  avio_seek(pb, 8, SEEK_SET);
322  avio_wb32(pb, (uint32_t)(file_size - au->header_size));
323  avio_seek(pb, file_size, SEEK_SET);
324  }
325 
326  return 0;
327 }
328 
330  .name = "au",
331  .long_name = NULL_IF_CONFIG_SMALL("Sun AU"),
332  .mime_type = "audio/basic",
333  .extensions = "au",
334  .priv_data_size = sizeof(AUContext),
335  .audio_codec = AV_CODEC_ID_PCM_S16BE,
336  .video_codec = AV_CODEC_ID_NONE,
337  .write_header = au_write_header,
339  .write_trailer = au_write_trailer,
340  .codec_tag = (const AVCodecTag* const []) { codec_au_tags, 0 },
341  .flags = AVFMT_NOTIMESTAMPS,
342 };
343 
344 #endif /* CONFIG_AU_MUXER */
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:726
#define NULL
Definition: coverity.c:32
Bytestream IO Context.
Definition: avio.h:161
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
void av_bprintf(AVBPrint *buf, const char *fmt,...)
Definition: bprint.c:94
enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
Definition: utils.c:3103
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4900
#define avpriv_request_sample(...)
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:253
unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum AVCodecID id)
Definition: utils.c:3093
const char * key
#define BLOCK_SIZE
Definition: adx.h:53
This struct describes the properties of an encoded stream.
Definition: codec_par.h:52
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:235
AVOutputFormat ff_au_muxer
Format I/O context.
Definition: avformat.h:1239
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
uint8_t
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:785
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:108
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4481
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1307
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:40
uint32_t tag
Definition: movenc.c:1597
#define AVERROR_EOF
End of file.
Definition: error.h:55
ptrdiff_t size
Definition: opengl_enc.c:100
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:225
static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
Definition: avio_internal.h:58
channels
Definition: aptx.h:33
#define av_log(a,...)
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:89
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:46
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:1610
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1479
#define AV_BPRINT_SIZE_UNLIMITED
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:754
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:115
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition: bprint.c:69
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
simple assert() macros that are a bit more flexible than ISO C assert().
#define FFMAX(a, b)
Definition: common.h:102
#define fail()
Definition: checkasm.h:133
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:628
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:443
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1295
int block_align
Audio only.
Definition: codec_par.h:177
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:260
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
Definition: avstring.c:215
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that&#39;s been allocated with av_malloc() or another memory allocation functio...
Definition: dict.h:76
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:98
const char * name
Definition: avformat.h:500
static const AVCodecTag codec_au_tags[]
Definition: au.c:39
#define s(width, name)
Definition: cbs_vp9.c:257
int ff_raw_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rawenc.c:29
static int av_bprint_is_complete(const AVBPrint *buf)
Test if the print buffer is complete (not truncated).
Definition: bprint.h:185
int ff_pcm_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: pcm.c:56
#define FF_ARRAY_ELEMS(a)
Stream structure.
Definition: avformat.h:880
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:462
int ff_pcm_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: pcm.c:29
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:40
static struct @320 state
AVIOContext * pb
I/O context.
Definition: avformat.h:1281
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:70
AVInputFormat ff_au_demuxer
This structure contains the data a format has to probe a file.
Definition: avformat.h:441
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:929
int sample_rate
Audio only.
Definition: codec_par.h:170
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:453
Main libavformat public API header.
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base...
Definition: avformat.h:919
unsigned bps
Definition: movenc.c:1598
#define MKBETAG(a, b, c, d)
Definition: common.h:414
char * value
Definition: dict.h:87
void * priv_data
Format private data.
Definition: avformat.h:1267
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:346
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:102
int channels
Audio only.
Definition: codec_par.h:166
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:387
#define av_freep(p)
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:652
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1045
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:368
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:64
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
#define MKTAG(a, b, c, d)
Definition: common.h:413
enum AVCodecID id
int i
Definition: input.c:407
#define AU_UNKNOWN_SIZE
Definition: au.c:37
void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
Append char c n times to a print buffer.
Definition: bprint.c:140