115329f1 |
/* |
7fbde343 |
* AU muxer and demuxer |
406792e7 |
* Copyright (c) 2001 Fabrice Bellard |
6cea494e |
* |
ac9362c5 |
* first version by Francois Revol <revol@free.fr>
* |
b78e7197 |
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or |
19720f15 |
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either |
b78e7197 |
* version 2.1 of the License, or (at your option) any later version. |
6cea494e |
* |
b78e7197 |
* FFmpeg is distributed in the hope that it will be useful, |
6cea494e |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
19720f15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details. |
6cea494e |
* |
19720f15 |
* You should have received a copy of the GNU Lesser General Public |
b78e7197 |
* License along with FFmpeg; if not, write to the Free Software |
5509bffa |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
6cea494e |
*/
/*
* Reference documents:
* http://www.opengroup.org/public/pubs/external/auformat.html
* http://www.goice.co.jp/member/mo/formats/au.html
*/
#include "avformat.h" |
c3f9ebf7 |
#include "internal.h" |
0abdb293 |
#include "avio_internal.h" |
e94204df |
#include "pcm.h" |
78e6f83c |
#include "libavutil/avassert.h" |
6cea494e |
/* if we don't know the size in advance */ |
9ff85412 |
#define AU_UNKNOWN_SIZE ((uint32_t)(~0)) |
df651cf4 |
/* the specification requires an annotation field of at least eight bytes */ |
1f8c0e44 |
#define AU_DEFAULT_HEADER_SIZE (24+8) |
6cea494e |
|
7caf0cc6 |
static const AVCodecTag codec_au_tags[] = { |
fd9147f1 |
{ AV_CODEC_ID_PCM_MULAW, 1 },
{ AV_CODEC_ID_PCM_S8, 2 },
{ AV_CODEC_ID_PCM_S16BE, 3 },
{ AV_CODEC_ID_PCM_S24BE, 4 },
{ AV_CODEC_ID_PCM_S32BE, 5 },
{ AV_CODEC_ID_PCM_F32BE, 6 },
{ AV_CODEC_ID_PCM_F64BE, 7 }, |
640c70dd |
{ AV_CODEC_ID_ADPCM_G726LE, 23 }, |
8c7de73e |
{ AV_CODEC_ID_ADPCM_G722,24 }, |
640c70dd |
{ AV_CODEC_ID_ADPCM_G726LE, 25 },
{ AV_CODEC_ID_ADPCM_G726LE, 26 }, |
fd9147f1 |
{ AV_CODEC_ID_PCM_ALAW, 27 }, |
640c70dd |
{ AV_CODEC_ID_ADPCM_G726LE, MKBETAG('7','2','6','2') }, |
fd9147f1 |
{ AV_CODEC_ID_NONE, 0 }, |
6cea494e |
};
|
c35f0e84 |
#if CONFIG_AU_DEMUXER |
6cea494e |
|
c9a65ca8 |
static int au_probe(AVProbeData *p)
{
if (p->buf[0] == '.' && p->buf[1] == 's' &&
p->buf[2] == 'n' && p->buf[3] == 'd')
return AVPROBE_SCORE_MAX;
else
return 0;
}
|
836f3555 |
static int au_read_annotation(AVFormatContext *s, int size)
{
static const char * keys[] = {
"title",
"artist",
"album",
"track",
"genre",
NULL };
AVIOContext *pb = s->pb;
enum { PARSE_KEY, PARSE_VALUE, PARSE_FINISHED } state = PARSE_KEY;
char c;
AVBPrint bprint;
char * key = NULL;
char * value = NULL;
int i;
av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED);
while (size-- > 0) {
c = avio_r8(pb);
switch(state) {
case PARSE_KEY:
if (c == '\0') {
state = PARSE_FINISHED;
} else if (c == '=') {
av_bprint_finalize(&bprint, &key);
av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED);
state = PARSE_VALUE;
} else {
av_bprint_chars(&bprint, c, 1);
}
break;
case PARSE_VALUE:
if (c == '\0' || c == '\n') {
if (av_bprint_finalize(&bprint, &value) != 0) {
av_log(s, AV_LOG_ERROR, "Memory error while parsing AU metadata.\n");
} else {
av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED);
for (i = 0; keys[i] != NULL && key != NULL; i++) {
if (av_strcasecmp(keys[i], key) == 0) {
av_dict_set(&(s->metadata), keys[i], value, AV_DICT_DONT_STRDUP_VAL);
av_freep(&key);
value = NULL;
}
}
}
av_freep(&key);
av_freep(&value);
state = (c == '\0') ? PARSE_FINISHED : PARSE_KEY;
} else {
av_bprint_chars(&bprint, c, 1);
}
break;
case PARSE_FINISHED:
break;
default:
/* should never happen */
av_assert0(0);
}
}
av_bprint_finalize(&bprint, NULL);
av_freep(&key);
return 0;
}
|
fb48f825 |
#define BLOCK_SIZE 1024
|
6e9651d1 |
static int au_read_header(AVFormatContext *s) |
6cea494e |
{ |
70a65eca |
int size, data_size = 0; |
6cea494e |
unsigned int tag; |
ae628ec1 |
AVIOContext *pb = s->pb; |
fb65d2ca |
unsigned int id, channels, rate; |
3f98848d |
int bps; |
36ef5369 |
enum AVCodecID codec; |
6cea494e |
AVStream *st;
|
b7effd4e |
tag = avio_rl32(pb); |
6cea494e |
if (tag != MKTAG('.', 's', 'n', 'd')) |
76877bea |
return AVERROR_INVALIDDATA; |
b7effd4e |
size = avio_rb32(pb); /* header size */ |
93263dc1 |
data_size = avio_rb32(pb); /* data size in bytes */
|
eb8b3252 |
if (data_size < 0 && data_size != AU_UNKNOWN_SIZE) { |
93263dc1 |
av_log(s, AV_LOG_ERROR, "Invalid negative data size '%d' found\n", data_size);
return AVERROR_INVALIDDATA;
} |
115329f1 |
|
fd9147f1 |
id = avio_rb32(pb);
rate = avio_rb32(pb); |
b7effd4e |
channels = avio_rb32(pb); |
115329f1 |
|
c837b38d |
if (size > 24) { |
836f3555 |
/* parse annotation field to get metadata */
au_read_annotation(s, size - 24); |
c837b38d |
}
|
1a40491e |
codec = ff_codec_get_id(codec_au_tags, id); |
6cea494e |
|
3f98848d |
if (codec == AV_CODEC_ID_NONE) { |
1ecdf891 |
avpriv_request_sample(s, "unknown or unsupported codec tag: %u", id); |
3f98848d |
return AVERROR_PATCHWELCOME;
}
bps = av_get_bits_per_sample(codec); |
640c70dd |
if (codec == AV_CODEC_ID_ADPCM_G726LE) {
if (id == MKBETAG('7','2','6','2')) {
bps = 2;
} else {
const uint8_t bpcss[] = {4, 0, 3, 5}; |
78e6f83c |
av_assert0(id >= 23 && id < 23 + 4); |
640c70dd |
bps = bpcss[id - 23];
}
} else if (!bps) { |
1ecdf891 |
avpriv_request_sample(s, "Unknown bits per sample"); |
f3298f12 |
return AVERROR_PATCHWELCOME; |
0a624147 |
}
|
fb48f825 |
if (channels == 0 || channels >= INT_MAX / (BLOCK_SIZE * bps >> 3)) { |
af68a2ba |
av_log(s, AV_LOG_ERROR, "Invalid number of channels %u\n", channels); |
0ca4414d |
return AVERROR_INVALIDDATA;
}
|
47d029a4 |
if (rate == 0 || rate > INT_MAX) {
av_log(s, AV_LOG_ERROR, "Invalid sample rate: %u\n", rate); |
8aa57b7b |
return AVERROR_INVALIDDATA;
}
|
3b3bbdd3 |
st = avformat_new_stream(s, NULL); |
6cea494e |
if (!st) |
76877bea |
return AVERROR(ENOMEM); |
9200514a |
st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
st->codecpar->codec_tag = id;
st->codecpar->codec_id = codec;
st->codecpar->channels = channels;
st->codecpar->sample_rate = rate; |
6f69f7a8 |
st->codecpar->bits_per_coded_sample = bps; |
9200514a |
st->codecpar->bit_rate = channels * rate * bps; |
6f69f7a8 |
st->codecpar->block_align = FFMAX(bps * st->codecpar->channels / 8, 1); |
eb8b3252 |
if (data_size != AU_UNKNOWN_SIZE) |
6f69f7a8 |
st->duration = (((int64_t)data_size)<<3) / (st->codecpar->channels * (int64_t)bps); |
bdd00e2d |
st->start_time = 0; |
c3f9ebf7 |
avpriv_set_pts_info(st, 64, 1, rate); |
fd9147f1 |
|
6cea494e |
return 0;
}
|
c6610a21 |
AVInputFormat ff_au_demuxer = { |
fd9147f1 |
.name = "au",
.long_name = NULL_IF_CONFIG_SMALL("Sun AU"),
.read_probe = au_probe,
.read_header = au_read_header, |
2e230cf1 |
.read_packet = ff_pcm_read_packet, |
fd9147f1 |
.read_seek = ff_pcm_read_seek,
.codec_tag = (const AVCodecTag* const []) { codec_au_tags, 0 }, |
c9a65ca8 |
}; |
fd9147f1 |
|
c35f0e84 |
#endif /* CONFIG_AU_DEMUXER */ |
c9a65ca8 |
|
b250f9c6 |
#if CONFIG_AU_MUXER |
c35f0e84 |
|
1f8c0e44 |
typedef struct AUContext {
uint32_t header_size;
} AUContext;
|
f2214c62 |
#include "rawenc.h"
|
1f8c0e44 |
static int au_get_annotations(AVFormatContext *s, char **buffer)
{
static const char * keys[] = {
"Title",
"Artist",
"Album",
"Track",
"Genre",
NULL };
int i;
int cnt = 0;
AVDictionary *m = s->metadata;
AVDictionaryEntry *t = NULL;
AVBPrint bprint;
av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED);
for (i = 0; keys[i] != NULL; i++) {
t = av_dict_get(m, keys[i], NULL, 0);
if (t != NULL) {
if (cnt++)
av_bprint_chars(&bprint, '\n', 1);
av_bprint_append_data(&bprint, keys[i], strlen(keys[i]));
av_bprint_chars(&bprint, '=', 1);
av_bprint_append_data(&bprint, t->value, strlen(t->value));
}
}
/* pad with 0's */
av_bprint_append_data(&bprint, "\0\0\0\0\0\0\0\0", 8);
return av_bprint_finalize(&bprint, buffer);
}
|
10e4905d |
static int au_write_header(AVFormatContext *s) |
c35f0e84 |
{ |
1f8c0e44 |
int ret;
AUContext *au = s->priv_data; |
10e4905d |
AVIOContext *pb = s->pb; |
6f69f7a8 |
AVCodecParameters *par = s->streams[0]->codecpar; |
1f8c0e44 |
char *annotations = NULL;
au->header_size = AU_DEFAULT_HEADER_SIZE; |
10e4905d |
|
0dcfccaa |
if (s->nb_streams != 1) {
av_log(s, AV_LOG_ERROR, "only one stream is supported\n"); |
2f8207b1 |
return AVERROR(EINVAL); |
0dcfccaa |
}
|
6f69f7a8 |
par->codec_tag = ff_codec_get_tag(codec_au_tags, par->codec_id);
if (!par->codec_tag) { |
0dcfccaa |
av_log(s, AV_LOG_ERROR, "unsupported codec\n");
return AVERROR(EINVAL);
} |
fd9147f1 |
|
1f8c0e44 |
if (av_dict_count(s->metadata) > 0) {
ret = au_get_annotations(s, &annotations);
if (ret < 0)
return ret;
if (annotations != NULL) {
au->header_size = (24 + strlen(annotations) + 8) & ~7;
if (au->header_size < AU_DEFAULT_HEADER_SIZE)
au->header_size = AU_DEFAULT_HEADER_SIZE;
}
} |
fd9147f1 |
ffio_wfourcc(pb, ".snd"); /* magic number */ |
1f8c0e44 |
avio_wb32(pb, au->header_size); /* header size */ |
fd9147f1 |
avio_wb32(pb, AU_UNKNOWN_SIZE); /* data size */ |
9200514a |
avio_wb32(pb, par->codec_tag); /* codec ID */
avio_wb32(pb, par->sample_rate);
avio_wb32(pb, par->channels); |
1f8c0e44 |
if (annotations != NULL) {
avio_write(pb, annotations, au->header_size - 24);
av_freep(&annotations);
} else {
avio_wb64(pb, 0); /* annotation field */
} |
c35f0e84 |
avio_flush(pb);
return 0;
}
static int au_write_trailer(AVFormatContext *s)
{
AVIOContext *pb = s->pb; |
1f8c0e44 |
AUContext *au = s->priv_data; |
50795682 |
int64_t file_size = avio_tell(pb); |
c35f0e84 |
|
4de591e6 |
if ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) && file_size < INT32_MAX) { |
c35f0e84 |
/* update file size */
avio_seek(pb, 8, SEEK_SET); |
1f8c0e44 |
avio_wb32(pb, (uint32_t)(file_size - au->header_size)); |
c35f0e84 |
avio_seek(pb, file_size, SEEK_SET);
avio_flush(pb);
}
return 0;
}
|
c6610a21 |
AVOutputFormat ff_au_muxer = { |
fd9147f1 |
.name = "au",
.long_name = NULL_IF_CONFIG_SMALL("Sun AU"),
.mime_type = "audio/basic",
.extensions = "au", |
1f8c0e44 |
.priv_data_size = sizeof(AUContext), |
fd9147f1 |
.audio_codec = AV_CODEC_ID_PCM_S16BE,
.video_codec = AV_CODEC_ID_NONE,
.write_header = au_write_header,
.write_packet = ff_raw_write_packet,
.write_trailer = au_write_trailer,
.codec_tag = (const AVCodecTag* const []) { codec_au_tags, 0 }, |
f792d3cb |
.flags = AVFMT_NOTIMESTAMPS, |
6cea494e |
}; |
fd9147f1 |
|
c35f0e84 |
#endif /* CONFIG_AU_MUXER */ |