• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * amr file format
3  * Copyright (c) 2001 FFmpeg project
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21 
22 /*
23 Write and read amr data according to RFC3267, http://www.ietf.org/rfc/rfc3267.txt?number=3267
24 
25 Only mono files are supported.
26 
27 */
28 
29 #include "libavutil/channel_layout.h"
30 #include "avformat.h"
31 #include "internal.h"
32 #include "rawenc.h"
33 
34 typedef struct {
35     uint64_t cumulated_size;
36     uint64_t block_count;
37 } AMRContext;
38 
39 static const char AMR_header[]   = "#!AMR\n";
40 static const char AMRWB_header[] = "#!AMR-WB\n";
41 
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
44 };
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
47 };
48 
49 #if CONFIG_AMR_MUXER
amr_write_header(AVFormatContext * s)50 static int amr_write_header(AVFormatContext *s)
51 {
52     AVIOContext    *pb  = s->pb;
53     AVCodecParameters *par = s->streams[0]->codecpar;
54 
55     s->priv_data = NULL;
56 
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 */
61     } else {
62         return -1;
63     }
64     return 0;
65 }
66 #endif /* CONFIG_AMR_MUXER */
67 
amr_probe(const AVProbeData * p)68 static int amr_probe(const AVProbeData *p)
69 {
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)
73 
74     if (!memcmp(p->buf, AMR_header, 5))
75         return AVPROBE_SCORE_MAX;
76     else
77         return 0;
78 }
79 
80 /* amr input */
amr_read_header(AVFormatContext * s)81 static int amr_read_header(AVFormatContext *s)
82 {
83     AVIOContext *pb = s->pb;
84     AVStream *st;
85     uint8_t header[9];
86 
87     if (avio_read(pb, header, 6) != 6)
88         return AVERROR_INVALIDDATA;
89 
90     st = avformat_new_stream(s, NULL);
91     if (!st)
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)) {
97             return -1;
98         }
99 
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;
103     } else {
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;
107     }
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);
112 
113     return 0;
114 }
115 
amr_read_packet(AVFormatContext * s,AVPacket * pkt)116 static int amr_read_packet(AVFormatContext *s, AVPacket *pkt)
117 {
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;
122 
123     if (avio_feof(s->pb)) {
124         return AVERROR_EOF;
125     }
126 
127     // FIXME this is wrong, this should rather be in an AVParser
128     toc  = avio_r8(s->pb);
129     mode = (toc >> 3) & 0x0F;
130 
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];
135     }
136 
137     if (!size || av_new_packet(pkt, size))
138         return AVERROR(EIO);
139 
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;
144     }
145 
146     pkt->stream_index = 0;
147     pkt->pos          = pos;
148     pkt->data[0]      = toc;
149     pkt->duration     = par->codec_id == AV_CODEC_ID_AMR_NB ? 160 : 320;
150     read              = avio_read(s->pb, pkt->data + 1, size - 1);
151 
152     if (read != size - 1) {
153         if (read < 0)
154             return read;
155         return AVERROR(EIO);
156     }
157 
158     return 0;
159 }
160 
161 #if CONFIG_AMR_DEMUXER
162 AVInputFormat ff_amr_demuxer = {
163     .name           = "amr",
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,
170 };
171 #endif
172 
173 #if CONFIG_AMRNB_DEMUXER
amrnb_probe(const AVProbeData * p)174 static int amrnb_probe(const AVProbeData *p)
175 {
176     int mode, i = 0, valid = 0, invalid = 0;
177     const uint8_t *b = p->buf;
178 
179     while (i < p->buf_size) {
180         mode = b[i] >> 3 & 0x0F;
181         if (mode < 9 && (b[i] & 0x4) == 0x4) {
182             int last = b[i];
183             int size = amrnb_packed_size[mode];
184             while (size--) {
185                 if (b[++i] != last)
186                     break;
187             }
188             if (size > 0) {
189                 valid++;
190                 i += size;
191             }
192         } else {
193             valid = 0;
194             invalid++;
195             i++;
196         }
197     }
198     if (valid > 100 && valid >> 4 > invalid)
199         return AVPROBE_SCORE_EXTENSION / 2 + 1;
200     return 0;
201 }
202 
amrnb_read_header(AVFormatContext * s)203 static int amrnb_read_header(AVFormatContext *s)
204 {
205     AVStream *st = avformat_new_stream(s, NULL);
206     if (!st)
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);
214 
215     return 0;
216 }
217 
218 AVInputFormat ff_amrnb_demuxer = {
219     .name           = "amrnb",
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,
226 };
227 #endif
228 
229 #if CONFIG_AMRWB_DEMUXER
amrwb_probe(const AVProbeData * p)230 static int amrwb_probe(const AVProbeData *p)
231 {
232     int mode, i = 0, valid = 0, invalid = 0;
233     const uint8_t *b = p->buf;
234 
235     while (i < p->buf_size) {
236         mode = b[i] >> 3 & 0x0F;
237         if (mode < 10 && (b[i] & 0x4) == 0x4) {
238             int last = b[i];
239             int size = amrwb_packed_size[mode];
240             while (size--) {
241                 if (b[++i] != last)
242                     break;
243             }
244             if (size > 0) {
245                 valid++;
246                 i += size;
247             }
248         } else {
249             valid = 0;
250             invalid++;
251             i++;
252         }
253     }
254     if (valid > 100 && valid >> 4 > invalid)
255         return AVPROBE_SCORE_EXTENSION / 2 + 1;
256     return 0;
257 }
258 
amrwb_read_header(AVFormatContext * s)259 static int amrwb_read_header(AVFormatContext *s)
260 {
261     AVStream *st = avformat_new_stream(s, NULL);
262     if (!st)
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);
270 
271     return 0;
272 }
273 
274 AVInputFormat ff_amrwb_demuxer = {
275     .name           = "amrwb",
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,
282 };
283 #endif
284 
285 #if CONFIG_AMR_MUXER
286 AVOutputFormat ff_amr_muxer = {
287     .name              = "amr",
288     .long_name         = NULL_IF_CONFIG_SMALL("3GPP AMR"),
289     .mime_type         = "audio/amr",
290     .extensions        = "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,
296 };
297 #endif
298