115329f1 |
/* |
067cbf31 |
* amr file format
* Copyright (c) 2001 ffmpeg project
* |
b78e7197 |
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or |
067cbf31 |
* 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. |
067cbf31 |
* |
b78e7197 |
* FFmpeg is distributed in the hope that it will be useful, |
067cbf31 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* 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 |
067cbf31 |
*/
/*
Write and read amr data according to RFC3267, http://www.ietf.org/rfc/rfc3267.txt?number=3267
|
d663a1fd |
Only mono files are supported. |
067cbf31 |
*/ |
5595368b |
#include "libavutil/channel_layout.h" |
067cbf31 |
#include "avformat.h" |
c3f9ebf7 |
#include "internal.h" |
067cbf31 |
|
c43222f4 |
typedef struct {
uint64_t cumulated_size;
uint64_t block_count;
} AMRContext;
|
5f26d4d4 |
static const char AMR_header[] = "#!AMR\n";
static const char AMRWB_header[] = "#!AMR-WB\n"; |
067cbf31 |
|
b250f9c6 |
#if CONFIG_AMR_MUXER |
067cbf31 |
static int amr_write_header(AVFormatContext *s)
{ |
5f26d4d4 |
AVIOContext *pb = s->pb; |
01f4895c |
AVCodecContext *enc = s->streams[0]->codec; |
067cbf31 |
s->priv_data = NULL;
|
36ef5369 |
if (enc->codec_id == AV_CODEC_ID_AMR_NB) { |
a1b79792 |
avio_write(pb, AMR_header, sizeof(AMR_header) - 1); /* magic number */ |
36ef5369 |
} else if (enc->codec_id == AV_CODEC_ID_AMR_WB) { |
a1b79792 |
avio_write(pb, AMRWB_header, sizeof(AMRWB_header) - 1); /* magic number */ |
5f26d4d4 |
} else { |
effdc8ef |
return -1; |
067cbf31 |
} |
b7f2fdde |
avio_flush(pb); |
067cbf31 |
return 0;
}
|
e928649b |
static int amr_write_packet(AVFormatContext *s, AVPacket *pkt) |
067cbf31 |
{ |
e9eb8d0b |
avio_write(s->pb, pkt->data, pkt->size); |
067cbf31 |
return 0;
} |
8212568a |
#endif /* CONFIG_AMR_MUXER */ |
067cbf31 |
static int amr_probe(AVProbeData *p)
{ |
5f26d4d4 |
// Only check for "#!AMR" which could be amr-wb, amr-nb.
// This will also trigger multichannel files: "#!AMR_MC1.0\n" and
// "#!AMR-WB_MC1.0\n" (not supported) |
d663a1fd |
|
5f26d4d4 |
if (!memcmp(p->buf, AMR_header, 5)) |
067cbf31 |
return AVPROBE_SCORE_MAX;
else
return 0;
}
/* amr input */ |
6e9651d1 |
static int amr_read_header(AVFormatContext *s) |
067cbf31 |
{ |
471fe57e |
AVIOContext *pb = s->pb; |
067cbf31 |
AVStream *st; |
d663a1fd |
uint8_t header[9]; |
067cbf31 |
|
e63a3628 |
avio_read(pb, header, 6); |
067cbf31 |
|
3b3bbdd3 |
st = avformat_new_stream(s, NULL); |
b4963892 |
if (!st) |
769e10f0 |
return AVERROR(ENOMEM); |
5f26d4d4 |
if (memcmp(header, AMR_header, 6)) {
avio_read(pb, header + 6, 3);
if (memcmp(header, AMRWB_header, 9)) { |
d663a1fd |
return -1;
} |
115329f1 |
|
5f26d4d4 |
st->codec->codec_tag = MKTAG('s', 'a', 'w', 'b'); |
36ef5369 |
st->codec->codec_id = AV_CODEC_ID_AMR_WB; |
01f4895c |
st->codec->sample_rate = 16000; |
5f26d4d4 |
} else {
st->codec->codec_tag = MKTAG('s', 'a', 'm', 'r'); |
36ef5369 |
st->codec->codec_id = AV_CODEC_ID_AMR_NB; |
01f4895c |
st->codec->sample_rate = 8000; |
067cbf31 |
} |
5f26d4d4 |
st->codec->channels = 1; |
5595368b |
st->codec->channel_layout = AV_CH_LAYOUT_MONO; |
72415b2a |
st->codec->codec_type = AVMEDIA_TYPE_AUDIO; |
c3f9ebf7 |
avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate); |
067cbf31 |
return 0;
}
|
5f26d4d4 |
static int amr_read_packet(AVFormatContext *s, AVPacket *pkt) |
067cbf31 |
{ |
01f4895c |
AVCodecContext *enc = s->streams[0]->codec; |
f015e411 |
int read, size = 0, toc, mode; |
026fa81d |
int64_t pos = avio_tell(s->pb); |
c43222f4 |
AMRContext *amr = s->priv_data; |
067cbf31 |
|
d34ec64a |
if (avio_feof(s->pb)) { |
6f3e0b21 |
return AVERROR(EIO); |
b1bf48aa |
} |
115329f1 |
|
cf79f202 |
// FIXME this is wrong, this should rather be in a AVParser |
5f26d4d4 |
toc = avio_r8(s->pb); |
b1bf48aa |
mode = (toc >> 3) & 0x0F; |
115329f1 |
|
36ef5369 |
if (enc->codec_id == AV_CODEC_ID_AMR_NB) { |
5f26d4d4 |
static const uint8_t packed_size[16] = {
12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0
}; |
115329f1 |
|
5f26d4d4 |
size = packed_size[mode] + 1; |
36ef5369 |
} else if (enc->codec_id == AV_CODEC_ID_AMR_WB) { |
eb3918c1 |
static const uint8_t packed_size[16] = { |
5f26d4d4 |
18, 24, 33, 37, 41, 47, 51, 59, 61, 6, 6, 0, 0, 0, 1, 1
}; |
115329f1 |
|
5f26d4d4 |
size = packed_size[mode]; |
067cbf31 |
} |
b1bf48aa |
|
5f26d4d4 |
if (!size || av_new_packet(pkt, size)) |
6f3e0b21 |
return AVERROR(EIO); |
b1bf48aa |
|
c43222f4 |
if (amr->cumulated_size < UINT64_MAX - size) {
amr->cumulated_size += size;
/* Both AMR formats have 50 frames per second */
s->streams[0]->codec->bit_rate = amr->cumulated_size / ++amr->block_count * 8 * 50;
} |
2890cba8 |
|
b1bf48aa |
pkt->stream_index = 0; |
5f26d4d4 |
pkt->pos = pos;
pkt->data[0] = toc; |
36ef5369 |
pkt->duration = enc->codec_id == AV_CODEC_ID_AMR_NB ? 160 : 320; |
5f26d4d4 |
read = avio_read(s->pb, pkt->data + 1, size - 1); |
b1bf48aa |
|
5f26d4d4 |
if (read != size - 1) { |
b1bf48aa |
av_free_packet(pkt); |
6f3e0b21 |
return AVERROR(EIO); |
b1bf48aa |
}
|
effdc8ef |
return 0; |
067cbf31 |
}
|
b250f9c6 |
#if CONFIG_AMR_DEMUXER |
66355be3 |
AVInputFormat ff_amr_demuxer = { |
dfc2c4d9 |
.name = "amr", |
6774247a |
.long_name = NULL_IF_CONFIG_SMALL("3GPP AMR"), |
c43222f4 |
.priv_data_size = sizeof(AMRContext), |
dfc2c4d9 |
.read_probe = amr_probe,
.read_header = amr_read_header,
.read_packet = amr_read_packet, |
20234a4b |
.flags = AVFMT_GENERIC_INDEX, |
067cbf31 |
}; |
ff70e601 |
#endif |
067cbf31 |
|
b250f9c6 |
#if CONFIG_AMR_MUXER |
66355be3 |
AVOutputFormat ff_amr_muxer = { |
dfc2c4d9 |
.name = "amr", |
6774247a |
.long_name = NULL_IF_CONFIG_SMALL("3GPP AMR"), |
dfc2c4d9 |
.mime_type = "audio/amr",
.extensions = "amr", |
36ef5369 |
.audio_codec = AV_CODEC_ID_AMR_NB,
.video_codec = AV_CODEC_ID_NONE, |
dfc2c4d9 |
.write_header = amr_write_header,
.write_packet = amr_write_packet, |
f792d3cb |
.flags = AVFMT_NOTIMESTAMPS, |
067cbf31 |
}; |
0e6c947d |
#endif |