3 * Copyright (c) 2001 FFmpeg project
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 Write and read amr data according to RFC3267, http://www.ietf.org/rfc/rfc3267.txt?number=3267
25 Only mono files are supported.
29 #include "libavutil/channel_layout.h"
35 uint64_t cumulated_size;
39 static const char AMR_header[] = "#!AMR\n";
40 static const char AMRWB_header[] = "#!AMR-WB\n";
42 static const uint8_t amrnb_packed_size[16] = {
43 13, 14, 16, 18, 20, 21, 27, 32, 6, 1, 1, 1, 1, 1, 1, 1
45 static const uint8_t amrwb_packed_size[16] = {
46 18, 24, 33, 37, 41, 47, 51, 59, 61, 6, 1, 1, 1, 1, 1, 1
50 static int amr_write_header(AVFormatContext *s)
52 AVIOContext *pb = s->pb;
53 AVCodecParameters *par = s->streams[0]->codecpar;
57 if (par->codec_id == AV_CODEC_ID_AMR_NB) {
58 avio_write(pb, AMR_header, sizeof(AMR_header) - 1); /* magic number */
59 } else if (par->codec_id == AV_CODEC_ID_AMR_WB) {
60 avio_write(pb, AMRWB_header, sizeof(AMRWB_header) - 1); /* magic number */
66 #endif /* CONFIG_AMR_MUXER */
68 static int amr_probe(const AVProbeData *p)
70 // Only check for "#!AMR" which could be amr-wb, amr-nb.
71 // This will also trigger multichannel files: "#!AMR_MC1.0\n" and
72 // "#!AMR-WB_MC1.0\n" (not supported)
74 if (!memcmp(p->buf, AMR_header, 5))
75 return AVPROBE_SCORE_MAX;
81 static int amr_read_header(AVFormatContext *s)
83 AVIOContext *pb = s->pb;
87 if (avio_read(pb, header, 6) != 6)
88 return AVERROR_INVALIDDATA;
90 st = avformat_new_stream(s, NULL);
92 return AVERROR(ENOMEM);
93 if (memcmp(header, AMR_header, 6)) {
94 if (avio_read(pb, header + 6, 3) != 3)
95 return AVERROR_INVALIDDATA;
96 if (memcmp(header, AMRWB_header, 9)) {
100 st->codecpar->codec_tag = MKTAG('s', 'a', 'w', 'b');
101 st->codecpar->codec_id = AV_CODEC_ID_AMR_WB;
102 st->codecpar->sample_rate = 16000;
104 st->codecpar->codec_tag = MKTAG('s', 'a', 'm', 'r');
105 st->codecpar->codec_id = AV_CODEC_ID_AMR_NB;
106 st->codecpar->sample_rate = 8000;
108 st->codecpar->channels = 1;
109 st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
110 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
111 avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
116 static int amr_read_packet(AVFormatContext *s, AVPacket *pkt)
118 AVCodecParameters *par = s->streams[0]->codecpar;
119 int read, size = 0, toc, mode;
120 int64_t pos = avio_tell(s->pb);
121 AMRContext *amr = s->priv_data;
123 if (avio_feof(s->pb)) {
127 // FIXME this is wrong, this should rather be in an AVParser
128 toc = avio_r8(s->pb);
129 mode = (toc >> 3) & 0x0F;
131 if (par->codec_id == AV_CODEC_ID_AMR_NB) {
132 size = amrnb_packed_size[mode];
133 } else if (par->codec_id == AV_CODEC_ID_AMR_WB) {
134 size = amrwb_packed_size[mode];
137 if (!size || av_new_packet(pkt, size))
140 if (amr->cumulated_size < UINT64_MAX - size) {
141 amr->cumulated_size += size;
142 /* Both AMR formats have 50 frames per second */
143 s->streams[0]->codecpar->bit_rate = amr->cumulated_size / ++amr->block_count * 8 * 50;
146 pkt->stream_index = 0;
149 pkt->duration = par->codec_id == AV_CODEC_ID_AMR_NB ? 160 : 320;
150 read = avio_read(s->pb, pkt->data + 1, size - 1);
152 if (read != size - 1) {
161 #if CONFIG_AMR_DEMUXER
162 AVInputFormat ff_amr_demuxer = {
164 .long_name = NULL_IF_CONFIG_SMALL("3GPP AMR"),
165 .priv_data_size = sizeof(AMRContext),
166 .read_probe = amr_probe,
167 .read_header = amr_read_header,
168 .read_packet = amr_read_packet,
169 .flags = AVFMT_GENERIC_INDEX,
173 #if CONFIG_AMRNB_DEMUXER
174 static int amrnb_probe(const AVProbeData *p)
176 int mode, i = 0, valid = 0, invalid = 0;
177 const uint8_t *b = p->buf;
179 while (i < p->buf_size) {
180 mode = b[i] >> 3 & 0x0F;
181 if (mode < 9 && (b[i] & 0x4) == 0x4) {
183 int size = amrnb_packed_size[mode];
198 if (valid > 100 && valid >> 4 > invalid)
199 return AVPROBE_SCORE_EXTENSION / 2 + 1;
203 static int amrnb_read_header(AVFormatContext *s)
205 AVStream *st = avformat_new_stream(s, NULL);
207 return AVERROR(ENOMEM);
208 st->codecpar->codec_id = AV_CODEC_ID_AMR_NB;
209 st->codecpar->sample_rate = 8000;
210 st->codecpar->channels = 1;
211 st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
212 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
213 avpriv_set_pts_info(st, 64, 1, 8000);
218 AVInputFormat ff_amrnb_demuxer = {
220 .long_name = NULL_IF_CONFIG_SMALL("raw AMR-NB"),
221 .priv_data_size = sizeof(AMRContext),
222 .read_probe = amrnb_probe,
223 .read_header = amrnb_read_header,
224 .read_packet = amr_read_packet,
225 .flags = AVFMT_GENERIC_INDEX,
229 #if CONFIG_AMRWB_DEMUXER
230 static int amrwb_probe(const AVProbeData *p)
232 int mode, i = 0, valid = 0, invalid = 0;
233 const uint8_t *b = p->buf;
235 while (i < p->buf_size) {
236 mode = b[i] >> 3 & 0x0F;
237 if (mode < 10 && (b[i] & 0x4) == 0x4) {
239 int size = amrwb_packed_size[mode];
254 if (valid > 100 && valid >> 4 > invalid)
255 return AVPROBE_SCORE_EXTENSION / 2 + 1;
259 static int amrwb_read_header(AVFormatContext *s)
261 AVStream *st = avformat_new_stream(s, NULL);
263 return AVERROR(ENOMEM);
264 st->codecpar->codec_id = AV_CODEC_ID_AMR_WB;
265 st->codecpar->sample_rate = 16000;
266 st->codecpar->channels = 1;
267 st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
268 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
269 avpriv_set_pts_info(st, 64, 1, 16000);
274 AVInputFormat ff_amrwb_demuxer = {
276 .long_name = NULL_IF_CONFIG_SMALL("raw AMR-WB"),
277 .priv_data_size = sizeof(AMRContext),
278 .read_probe = amrwb_probe,
279 .read_header = amrwb_read_header,
280 .read_packet = amr_read_packet,
281 .flags = AVFMT_GENERIC_INDEX,
286 AVOutputFormat ff_amr_muxer = {
288 .long_name = NULL_IF_CONFIG_SMALL("3GPP AMR"),
289 .mime_type = "audio/amr",
291 .audio_codec = AV_CODEC_ID_AMR_NB,
292 .video_codec = AV_CODEC_ID_NONE,
293 .write_header = amr_write_header,
294 .write_packet = ff_raw_write_packet,
295 .flags = AVFMT_NOTIMESTAMPS,