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 */
#define AU_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;
}
|
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) {
/* skip unused data */
avio_skip(pb, size - 24);
}
|
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); |
fd9147f1 |
st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
st->codec->codec_tag = id;
st->codec->codec_id = codec;
st->codec->channels = channels; |
01f4895c |
st->codec->sample_rate = rate; |
640c70dd |
st->codec->bits_per_coded_sample = bps; |
9a7b5688 |
st->codec->bit_rate = channels * rate * bps; |
c090b542 |
st->codec->block_align = FFMAX(bps * st->codec->channels / 8, 1); |
eb8b3252 |
if (data_size != AU_UNKNOWN_SIZE) |
52fdaf27 |
st->duration = (((int64_t)data_size)<<3) / (st->codec->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 |
|
f2214c62 |
#include "rawenc.h"
|
10e4905d |
static int au_write_header(AVFormatContext *s) |
c35f0e84 |
{ |
10e4905d |
AVIOContext *pb = s->pb;
AVCodecContext *enc = s->streams[0]->codec;
|
0dcfccaa |
if (s->nb_streams != 1) {
av_log(s, AV_LOG_ERROR, "only one stream is supported\n"); |
2f8207b1 |
return AVERROR(EINVAL); |
0dcfccaa |
}
enc->codec_tag = ff_codec_get_tag(codec_au_tags, enc->codec_id);
if (!enc->codec_tag) {
av_log(s, AV_LOG_ERROR, "unsupported codec\n");
return AVERROR(EINVAL);
} |
fd9147f1 |
ffio_wfourcc(pb, ".snd"); /* magic number */ |
8c7de73e |
avio_wb32(pb, AU_HEADER_SIZE); /* header size */ |
fd9147f1 |
avio_wb32(pb, AU_UNKNOWN_SIZE); /* data size */ |
f7a3c540 |
avio_wb32(pb, enc->codec_tag); /* codec ID */ |
c35f0e84 |
avio_wb32(pb, enc->sample_rate); |
f7a3c540 |
avio_wb32(pb, enc->channels); |
94ecbe23 |
avio_wb64(pb, 0); /* annotation field */ |
c35f0e84 |
avio_flush(pb);
return 0;
}
static int au_write_trailer(AVFormatContext *s)
{
AVIOContext *pb = s->pb; |
50795682 |
int64_t file_size = avio_tell(pb); |
c35f0e84 |
|
50795682 |
if (s->pb->seekable && file_size < INT32_MAX) { |
c35f0e84 |
/* update file size */
avio_seek(pb, 8, SEEK_SET); |
94ecbe23 |
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",
.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 }, |
6cea494e |
}; |
fd9147f1 |
|
c35f0e84 |
#endif /* CONFIG_AU_MUXER */ |