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"
34 uint64_t cumulated_size;
38 static const char AMR_header[] = "#!AMR\n";
39 static const char AMRWB_header[] = "#!AMR-WB\n";
41 static const uint8_t amrnb_packed_size[16] = {
42 13, 14, 16, 18, 20, 21, 27, 32, 6, 1, 1, 1, 1, 1, 1, 1
44 static const uint8_t amrwb_packed_size[16] = {
45 18, 24, 33, 37, 41, 47, 51, 59, 61, 6, 1, 1, 1, 1, 1, 1
49 static int amr_write_header(AVFormatContext *s)
51 AVIOContext *pb = s->pb;
52 AVCodecParameters *par = s->streams[0]->codecpar;
56 if (par->codec_id == AV_CODEC_ID_AMR_NB) {
57 avio_write(pb, AMR_header, sizeof(AMR_header) - 1); /* magic number */
58 } else if (par->codec_id == AV_CODEC_ID_AMR_WB) {
59 avio_write(pb, AMRWB_header, sizeof(AMRWB_header) - 1); /* magic number */
67 static int amr_write_packet(AVFormatContext *s, AVPacket *pkt)
69 avio_write(s->pb, pkt->data, pkt->size);
72 #endif /* CONFIG_AMR_MUXER */
74 static int amr_probe(AVProbeData *p)
76 // Only check for "#!AMR" which could be amr-wb, amr-nb.
77 // This will also trigger multichannel files: "#!AMR_MC1.0\n" and
78 // "#!AMR-WB_MC1.0\n" (not supported)
80 if (!memcmp(p->buf, AMR_header, 5))
81 return AVPROBE_SCORE_MAX;
87 static int amr_read_header(AVFormatContext *s)
89 AVIOContext *pb = s->pb;
93 avio_read(pb, header, 6);
95 st = avformat_new_stream(s, NULL);
97 return AVERROR(ENOMEM);
98 if (memcmp(header, AMR_header, 6)) {
99 avio_read(pb, header + 6, 3);
100 if (memcmp(header, AMRWB_header, 9)) {
104 st->codecpar->codec_tag = MKTAG('s', 'a', 'w', 'b');
105 st->codecpar->codec_id = AV_CODEC_ID_AMR_WB;
106 st->codecpar->sample_rate = 16000;
108 st->codecpar->codec_tag = MKTAG('s', 'a', 'm', 'r');
109 st->codecpar->codec_id = AV_CODEC_ID_AMR_NB;
110 st->codecpar->sample_rate = 8000;
112 st->codecpar->channels = 1;
113 st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
114 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
115 avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
120 static int amr_read_packet(AVFormatContext *s, AVPacket *pkt)
122 AVCodecParameters *par = s->streams[0]->codecpar;
123 int read, size = 0, toc, mode;
124 int64_t pos = avio_tell(s->pb);
125 AMRContext *amr = s->priv_data;
127 if (avio_feof(s->pb)) {
131 // FIXME this is wrong, this should rather be in an AVParser
132 toc = avio_r8(s->pb);
133 mode = (toc >> 3) & 0x0F;
135 if (par->codec_id == AV_CODEC_ID_AMR_NB) {
136 size = amrnb_packed_size[mode];
137 } else if (par->codec_id == AV_CODEC_ID_AMR_WB) {
138 size = amrwb_packed_size[mode];
141 if (!size || av_new_packet(pkt, size))
144 if (amr->cumulated_size < UINT64_MAX - size) {
145 amr->cumulated_size += size;
146 /* Both AMR formats have 50 frames per second */
147 s->streams[0]->codecpar->bit_rate = amr->cumulated_size / ++amr->block_count * 8 * 50;
150 pkt->stream_index = 0;
153 pkt->duration = par->codec_id == AV_CODEC_ID_AMR_NB ? 160 : 320;
154 read = avio_read(s->pb, pkt->data + 1, size - 1);
156 if (read != size - 1) {
157 av_packet_unref(pkt);
166 #if CONFIG_AMR_DEMUXER
167 AVInputFormat ff_amr_demuxer = {
169 .long_name = NULL_IF_CONFIG_SMALL("3GPP AMR"),
170 .priv_data_size = sizeof(AMRContext),
171 .read_probe = amr_probe,
172 .read_header = amr_read_header,
173 .read_packet = amr_read_packet,
174 .flags = AVFMT_GENERIC_INDEX,
178 #if CONFIG_AMRNB_DEMUXER
179 static int amrnb_probe(AVProbeData *p)
181 int mode, i = 0, valid = 0, invalid = 0;
182 const uint8_t *b = p->buf;
184 while (i < p->buf_size) {
185 mode = b[i] >> 3 & 0x0F;
186 if (mode < 9 && (b[i] & 0x4) == 0x4) {
188 int size = amrnb_packed_size[mode];
204 if (valid > 100 && valid > invalid)
205 return AVPROBE_SCORE_EXTENSION / 2 + 1;
209 static int amrnb_read_header(AVFormatContext *s)
211 AVStream *st = avformat_new_stream(s, NULL);
213 return AVERROR(ENOMEM);
214 st->codecpar->codec_id = AV_CODEC_ID_AMR_NB;
215 st->codecpar->sample_rate = 8000;
216 st->codecpar->channels = 1;
217 st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
218 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
219 avpriv_set_pts_info(st, 64, 1, 8000);
224 AVInputFormat ff_amrnb_demuxer = {
226 .long_name = NULL_IF_CONFIG_SMALL("raw AMR-NB"),
227 .priv_data_size = sizeof(AMRContext),
228 .read_probe = amrnb_probe,
229 .read_header = amrnb_read_header,
230 .read_packet = amr_read_packet,
231 .flags = AVFMT_GENERIC_INDEX,
235 #if CONFIG_AMRWB_DEMUXER
236 static int amrwb_probe(AVProbeData *p)
238 int mode, i = 0, valid = 0, invalid = 0;
239 const uint8_t *b = p->buf;
241 while (i < p->buf_size) {
242 mode = b[i] >> 3 & 0x0F;
243 if (mode < 10 && (b[i] & 0x4) == 0x4) {
245 int size = amrwb_packed_size[mode];
261 if (valid > 100 && valid > invalid)
262 return AVPROBE_SCORE_EXTENSION / 2 - 1;
266 static int amrwb_read_header(AVFormatContext *s)
268 AVStream *st = avformat_new_stream(s, NULL);
270 return AVERROR(ENOMEM);
271 st->codecpar->codec_id = AV_CODEC_ID_AMR_WB;
272 st->codecpar->sample_rate = 16000;
273 st->codecpar->channels = 1;
274 st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
275 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
276 avpriv_set_pts_info(st, 64, 1, 16000);
281 AVInputFormat ff_amrwb_demuxer = {
283 .long_name = NULL_IF_CONFIG_SMALL("raw AMR-WB"),
284 .priv_data_size = sizeof(AMRContext),
285 .read_probe = amrwb_probe,
286 .read_header = amrwb_read_header,
287 .read_packet = amr_read_packet,
288 .flags = AVFMT_GENERIC_INDEX,
293 AVOutputFormat ff_amr_muxer = {
295 .long_name = NULL_IF_CONFIG_SMALL("3GPP AMR"),
296 .mime_type = "audio/amr",
298 .audio_codec = AV_CODEC_ID_AMR_NB,
299 .video_codec = AV_CODEC_ID_NONE,
300 .write_header = amr_write_header,
301 .write_packet = amr_write_packet,
302 .flags = AVFMT_NOTIMESTAMPS,