FFmpeg  3.4.9
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 /* the specification requires an annotation field of at least eight bytes */
39 #define AU_DEFAULT_HEADER_SIZE (24+8)
40 
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 },
53  { AV_CODEC_ID_PCM_ALAW, 27 },
54  { AV_CODEC_ID_ADPCM_G726LE, MKBETAG('7','2','6','2') },
55  { AV_CODEC_ID_NONE, 0 },
56 };
57 
58 #if CONFIG_AU_DEMUXER
59 
60 static int au_probe(AVProbeData *p)
61 {
62  if (p->buf[0] == '.' && p->buf[1] == 's' &&
63  p->buf[2] == 'n' && p->buf[3] == 'd')
64  return AVPROBE_SCORE_MAX;
65  else
66  return 0;
67 }
68 
69 static int au_read_annotation(AVFormatContext *s, int size)
70 {
71  static const char * keys[] = {
72  "title",
73  "artist",
74  "album",
75  "track",
76  "genre",
77  NULL };
78  AVIOContext *pb = s->pb;
79  enum { PARSE_KEY, PARSE_VALUE, PARSE_FINISHED } state = PARSE_KEY;
80  char c;
81  AVBPrint bprint;
82  char * key = NULL;
83  char * value = NULL;
84  int i;
85 
87 
88  while (size-- > 0) {
89  if (avio_feof(pb)) {
90  av_bprint_finalize(&bprint, NULL);
91  av_freep(&key);
92  return AVERROR_EOF;
93  }
94  c = avio_r8(pb);
95  switch(state) {
96  case PARSE_KEY:
97  if (c == '\0') {
98  state = PARSE_FINISHED;
99  } else if (c == '=') {
100  av_bprint_finalize(&bprint, &key);
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; keys[i] != NULL && key != NULL; i++) {
114  if (av_strcasecmp(keys[i], key) == 0) {
115  av_dict_set(&(s->metadata), keys[i], value, AV_DICT_DONT_STRDUP_VAL);
116  av_freep(&key);
117  value = NULL;
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;
149  enum AVCodecID codec;
150  AVStream *st;
151 
152  tag = avio_rl32(pb);
153  if (tag != MKTAG('.', 's', 'n', 'd'))
154  return AVERROR_INVALIDDATA;
155  size = avio_rb32(pb); /* header size */
156  data_size = avio_rb32(pb); /* data size in bytes */
157 
158  if (data_size < 0 && data_size != AU_UNKNOWN_SIZE) {
159  av_log(s, AV_LOG_ERROR, "Invalid negative data size '%d' found\n", data_size);
160  return AVERROR_INVALIDDATA;
161  }
162 
163  id = avio_rb32(pb);
164  rate = avio_rb32(pb);
165  channels = avio_rb32(pb);
166 
167  if (size > 24) {
168  /* parse annotation field to get metadata */
169  au_read_annotation(s, size - 24);
170  }
171 
172  codec = ff_codec_get_id(codec_au_tags, id);
173 
174  if (codec == AV_CODEC_ID_NONE) {
175  avpriv_request_sample(s, "unknown or unsupported codec tag: %u", id);
176  return AVERROR_PATCHWELCOME;
177  }
178 
179  bps = av_get_bits_per_sample(codec);
180  if (codec == AV_CODEC_ID_ADPCM_G726LE) {
181  if (id == MKBETAG('7','2','6','2')) {
182  bps = 2;
183  } else {
184  const uint8_t bpcss[] = {4, 0, 3, 5};
185  av_assert0(id >= 23 && id < 23 + 4);
186  bps = bpcss[id - 23];
187  }
188  } else if (!bps) {
189  avpriv_request_sample(s, "Unknown bits per sample");
190  return AVERROR_PATCHWELCOME;
191  }
192 
193  if (channels == 0 || channels >= INT_MAX / (BLOCK_SIZE * bps >> 3)) {
194  av_log(s, AV_LOG_ERROR, "Invalid number of channels %u\n", channels);
195  return AVERROR_INVALIDDATA;
196  }
197 
198  if (rate == 0 || rate > INT_MAX) {
199  av_log(s, AV_LOG_ERROR, "Invalid sample rate: %u\n", rate);
200  return AVERROR_INVALIDDATA;
201  }
202 
203  st = avformat_new_stream(s, NULL);
204  if (!st)
205  return AVERROR(ENOMEM);
207  st->codecpar->codec_tag = id;
208  st->codecpar->codec_id = codec;
209  st->codecpar->channels = channels;
210  st->codecpar->sample_rate = rate;
212  st->codecpar->bit_rate = channels * rate * bps;
213  st->codecpar->block_align = FFMAX(bps * st->codecpar->channels / 8, 1);
214  if (data_size != AU_UNKNOWN_SIZE)
215  st->duration = (((int64_t)data_size)<<3) / (st->codecpar->channels * (int64_t)bps);
216 
217  st->start_time = 0;
218  avpriv_set_pts_info(st, 64, 1, rate);
219 
220  return 0;
221 }
222 
223 AVInputFormat ff_au_demuxer = {
224  .name = "au",
225  .long_name = NULL_IF_CONFIG_SMALL("Sun AU"),
226  .read_probe = au_probe,
227  .read_header = au_read_header,
228  .read_packet = ff_pcm_read_packet,
229  .read_seek = ff_pcm_read_seek,
230  .codec_tag = (const AVCodecTag* const []) { codec_au_tags, 0 },
231 };
232 
233 #endif /* CONFIG_AU_DEMUXER */
234 
235 #if CONFIG_AU_MUXER
236 
237 typedef struct AUContext {
238  uint32_t header_size;
239 } AUContext;
240 
241 #include "rawenc.h"
242 
243 static int au_get_annotations(AVFormatContext *s, char **buffer)
244 {
245  static const char * keys[] = {
246  "Title",
247  "Artist",
248  "Album",
249  "Track",
250  "Genre",
251  NULL };
252  int i;
253  int cnt = 0;
254  AVDictionary *m = s->metadata;
255  AVDictionaryEntry *t = NULL;
256  AVBPrint bprint;
257 
259 
260  for (i = 0; keys[i] != NULL; i++) {
261  t = av_dict_get(m, keys[i], NULL, 0);
262  if (t != NULL) {
263  if (cnt++)
264  av_bprint_chars(&bprint, '\n', 1);
265  av_bprint_append_data(&bprint, keys[i], strlen(keys[i]));
266  av_bprint_chars(&bprint, '=', 1);
267  av_bprint_append_data(&bprint, t->value, strlen(t->value));
268  }
269  }
270  /* pad with 0's */
271  av_bprint_append_data(&bprint, "\0\0\0\0\0\0\0\0", 8);
272  return av_bprint_finalize(&bprint, buffer);
273 }
274 
275 static int au_write_header(AVFormatContext *s)
276 {
277  int ret;
278  AUContext *au = s->priv_data;
279  AVIOContext *pb = s->pb;
280  AVCodecParameters *par = s->streams[0]->codecpar;
281  char *annotations = NULL;
282 
283  au->header_size = AU_DEFAULT_HEADER_SIZE;
284 
285  if (s->nb_streams != 1) {
286  av_log(s, AV_LOG_ERROR, "only one stream is supported\n");
287  return AVERROR(EINVAL);
288  }
289 
290  par->codec_tag = ff_codec_get_tag(codec_au_tags, par->codec_id);
291  if (!par->codec_tag) {
292  av_log(s, AV_LOG_ERROR, "unsupported codec\n");
293  return AVERROR(EINVAL);
294  }
295 
296  if (av_dict_count(s->metadata) > 0) {
297  ret = au_get_annotations(s, &annotations);
298  if (ret < 0)
299  return ret;
300  if (annotations != NULL) {
301  au->header_size = (24 + strlen(annotations) + 8) & ~7;
302  if (au->header_size < AU_DEFAULT_HEADER_SIZE)
303  au->header_size = AU_DEFAULT_HEADER_SIZE;
304  }
305  }
306  ffio_wfourcc(pb, ".snd"); /* magic number */
307  avio_wb32(pb, au->header_size); /* header size */
308  avio_wb32(pb, AU_UNKNOWN_SIZE); /* data size */
309  avio_wb32(pb, par->codec_tag); /* codec ID */
310  avio_wb32(pb, par->sample_rate);
311  avio_wb32(pb, par->channels);
312  if (annotations != NULL) {
313  avio_write(pb, annotations, au->header_size - 24);
314  av_freep(&annotations);
315  } else {
316  avio_wb64(pb, 0); /* annotation field */
317  }
318  avio_flush(pb);
319 
320  return 0;
321 }
322 
323 static int au_write_trailer(AVFormatContext *s)
324 {
325  AVIOContext *pb = s->pb;
326  AUContext *au = s->priv_data;
327  int64_t file_size = avio_tell(pb);
328 
329  if ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) && file_size < INT32_MAX) {
330  /* update file size */
331  avio_seek(pb, 8, SEEK_SET);
332  avio_wb32(pb, (uint32_t)(file_size - au->header_size));
333  avio_seek(pb, file_size, SEEK_SET);
334  avio_flush(pb);
335  }
336 
337  return 0;
338 }
339 
340 AVOutputFormat ff_au_muxer = {
341  .name = "au",
342  .long_name = NULL_IF_CONFIG_SMALL("Sun AU"),
343  .mime_type = "audio/basic",
344  .extensions = "au",
345  .priv_data_size = sizeof(AUContext),
346  .audio_codec = AV_CODEC_ID_PCM_S16BE,
347  .video_codec = AV_CODEC_ID_NONE,
348  .write_header = au_write_header,
350  .write_trailer = au_write_trailer,
351  .codec_tag = (const AVCodecTag* const []) { codec_au_tags, 0 },
352  .flags = AVFMT_NOTIMESTAMPS,
353 };
354 
355 #endif /* CONFIG_AU_MUXER */
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:672
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:468
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
Bytestream IO Context.
Definition: avio.h:161
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int size
static struct @260 state
enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
Definition: utils.c:3067
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:4756
#define avpriv_request_sample(...)
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:4152
int av_dict_count(const AVDictionary *m)
Get number of entries in dictionary.
Definition: dict.c:35
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:244
unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum AVCodecID id)
Definition: utils.c:3057
#define BLOCK_SIZE
Definition: adx.h:53
This struct describes the properties of an encoded stream.
Definition: avcodec.h:4144
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:235
Format I/O context.
Definition: avformat.h:1349
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
static char buffer[20]
Definition: seek.c:32
uint8_t
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:789
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4383
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1417
void av_bprint_append_data(AVBPrint *buf, const char *data, unsigned size)
Append data to a print buffer.
Definition: bprint.c:158
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:1414
#define AVERROR_EOF
End of file.
Definition: error.h:55
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:556
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:216
static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
Definition: avio_internal.h:58
#define av_log(a,...)
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: avcodec.h:4181
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:214
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:1709
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1567
#define AV_BPRINT_SIZE_UNLIMITED
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:758
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:181
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: avcodec.h:4148
simple assert() macros that are a bit more flexible than ISO C assert().
#define FFMAX(a, b)
Definition: common.h:94
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:627
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:463
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1405
int block_align
Audio only.
Definition: avcodec.h:4269
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:261
int void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition: aviobuf.c:236
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
Definition: avstring.c:213
#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:94
const char * name
Definition: avformat.h:524
static const AVCodecTag codec_au_tags[]
Definition: au.c:41
int ff_raw_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rawenc.c:29
int ff_pcm_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: pcm.c:45
Stream structure.
Definition: avformat.h:889
#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:486
#define AU_DEFAULT_HEADER_SIZE
Definition: au.c:39
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
AVIOContext * pb
I/O context.
Definition: avformat.h:1391
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
double value
Definition: eval.c:91
This structure contains the data a format has to probe a file.
Definition: avformat.h:461
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:946
int sample_rate
Audio only.
Definition: avcodec.h:4262
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:473
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:936
static double c[64]
unsigned bps
Definition: movenc.c:1415
#define MKBETAG(a, b, c, d)
Definition: common.h:343
char * value
Definition: dict.h:87
void * priv_data
Format private data.
Definition: avformat.h:1377
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:337
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: avcodec.h:4194
int channels
Audio only.
Definition: avcodec.h:4258
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:382
#define av_freep(p)
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:664
AVCodecParameters * codecpar
Definition: avformat.h:1252
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:356
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: avcodec.h:4156
#define MKTAG(a, b, c, d)
Definition: common.h:342
enum AVCodecID id
#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