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
26 #include "config_components.h"
27
28 #include "libavutil/channel_layout.h"
29 #include "libavutil/intreadwrite.h"
30 #include "avformat.h"
31 #include "avio_internal.h"
32 #include "internal.h"
33 #include "rawdec.h"
34 #include "rawenc.h"
35
36 typedef struct AMRContext {
37 FFRawDemuxerContext rawctx;
38 } AMRContext;
39
40 static const uint8_t AMR_header[6] = "#!AMR\x0a";
41 static const uint8_t AMRMC_header[12] = "#!AMR_MC1.0\x0a";
42 static const uint8_t AMRWB_header[9] = "#!AMR-WB\x0a";
43 static const uint8_t AMRWBMC_header[15] = "#!AMR-WB_MC1.0\x0a";
44
45 static const uint8_t amrnb_packed_size[16] = {
46 13, 14, 16, 18, 20, 21, 27, 32, 6, 1, 1, 1, 1, 1, 1, 1
47 };
48 static const uint8_t amrwb_packed_size[16] = {
49 18, 24, 33, 37, 41, 47, 51, 59, 61, 6, 1, 1, 1, 1, 1, 1
50 };
51
52 #if CONFIG_AMR_MUXER
amr_write_header(AVFormatContext *s)53 static int amr_write_header(AVFormatContext *s)
54 {
55 AVIOContext *pb = s->pb;
56 AVCodecParameters *par = s->streams[0]->codecpar;
57
58 if (par->codec_id == AV_CODEC_ID_AMR_NB) {
59 avio_write(pb, AMR_header, sizeof(AMR_header)); /* magic number */
60 } else if (par->codec_id == AV_CODEC_ID_AMR_WB) {
61 avio_write(pb, AMRWB_header, sizeof(AMRWB_header)); /* magic number */
62 } else {
63 return -1;
64 }
65 return 0;
66 }
67 #endif /* CONFIG_AMR_MUXER */
68
69 #if CONFIG_AMR_DEMUXER
amr_probe(const AVProbeData *p)70 static int amr_probe(const AVProbeData *p)
71 {
72 // Only check for "#!AMR" which could be amr-wb, amr-nb.
73 // This will also trigger multichannel files: "#!AMR_MC1.0\n" and
74 // "#!AMR-WB_MC1.0\n"
75
76 if (!memcmp(p->buf, AMR_header, 5))
77 return AVPROBE_SCORE_MAX;
78 else
79 return 0;
80 }
81
82 /* amr input */
amr_read_header(AVFormatContext *s)83 static int amr_read_header(AVFormatContext *s)
84 {
85 AVIOContext *pb = s->pb;
86 AVStream *st;
87 uint8_t header[19] = { 0 };
88 int read, back = 0, ret;
89
90 ret = ffio_ensure_seekback(s->pb, sizeof(header));
91 if (ret < 0)
92 return ret;
93
94 read = avio_read(pb, header, sizeof(header));
95 if (read < 0)
96 return read;
97
98 st = avformat_new_stream(s, NULL);
99 if (!st)
100 return AVERROR(ENOMEM);
101 if (!memcmp(header, AMR_header, sizeof(AMR_header))) {
102 st->codecpar->codec_tag = MKTAG('s', 'a', 'm', 'r');
103 st->codecpar->codec_id = AV_CODEC_ID_AMR_NB;
104 st->codecpar->sample_rate = 8000;
105 st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
106 back = read - sizeof(AMR_header);
107 } else if (!memcmp(header, AMRWB_header, sizeof(AMRWB_header))) {
108 st->codecpar->codec_tag = MKTAG('s', 'a', 'w', 'b');
109 st->codecpar->codec_id = AV_CODEC_ID_AMR_WB;
110 st->codecpar->sample_rate = 16000;
111 st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
112 back = read - sizeof(AMRWB_header);
113 } else if (!memcmp(header, AMRMC_header, sizeof(AMRMC_header))) {
114 st->codecpar->codec_tag = MKTAG('s', 'a', 'm', 'r');
115 st->codecpar->codec_id = AV_CODEC_ID_AMR_NB;
116 st->codecpar->sample_rate = 8000;
117 st->codecpar->ch_layout.nb_channels = AV_RL32(header + 12);
118 back = read - 4 - sizeof(AMRMC_header);
119 } else if (!memcmp(header, AMRWBMC_header, sizeof(AMRWBMC_header))) {
120 st->codecpar->codec_tag = MKTAG('s', 'a', 'w', 'b');
121 st->codecpar->codec_id = AV_CODEC_ID_AMR_WB;
122 st->codecpar->sample_rate = 16000;
123 st->codecpar->ch_layout.nb_channels = AV_RL32(header + 15);
124 back = read - 4 - sizeof(AMRWBMC_header);
125 } else {
126 return AVERROR_INVALIDDATA;
127 }
128
129 if (st->codecpar->ch_layout.nb_channels < 1)
130 return AVERROR_INVALIDDATA;
131
132 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
133 ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
134 avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
135
136 if (back > 0)
137 avio_seek(pb, -back, SEEK_CUR);
138
139 return 0;
140 }
141
142 const AVInputFormat ff_amr_demuxer = {
143 .name = "amr",
144 .long_name = NULL_IF_CONFIG_SMALL("3GPP AMR"),
145 .priv_data_size = sizeof(AMRContext),
146 .read_probe = amr_probe,
147 .read_header = amr_read_header,
148 .read_packet = ff_raw_read_partial_packet,
149 .flags = AVFMT_GENERIC_INDEX,
150 .priv_class = &ff_raw_demuxer_class,
151 };
152 #endif
153
154 #if CONFIG_AMRNB_DEMUXER
amrnb_probe(const AVProbeData *p)155 static int amrnb_probe(const AVProbeData *p)
156 {
157 int mode, i = 0, valid = 0, invalid = 0;
158 const uint8_t *b = p->buf;
159
160 while (i < p->buf_size) {
161 mode = b[i] >> 3 & 0x0F;
162 if (mode < 9 && (b[i] & 0x4) == 0x4) {
163 int last = b[i];
164 int size = amrnb_packed_size[mode];
165 while (size--) {
166 if (b[++i] != last)
167 break;
168 }
169 if (size > 0) {
170 valid++;
171 i += size;
172 }
173 } else {
174 valid = 0;
175 invalid++;
176 i++;
177 }
178 }
179 if (valid > 100 && valid >> 4 > invalid)
180 return AVPROBE_SCORE_EXTENSION / 2 + 1;
181 return 0;
182 }
183
amrnb_read_header(AVFormatContext *s)184 static int amrnb_read_header(AVFormatContext *s)
185 {
186 AVStream *st = avformat_new_stream(s, NULL);
187 if (!st)
188 return AVERROR(ENOMEM);
189 st->codecpar->codec_id = AV_CODEC_ID_AMR_NB;
190 st->codecpar->sample_rate = 8000;
191 st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
192 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
193 ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
194 avpriv_set_pts_info(st, 64, 1, 8000);
195
196 return 0;
197 }
198
199 const AVInputFormat ff_amrnb_demuxer = {
200 .name = "amrnb",
201 .long_name = NULL_IF_CONFIG_SMALL("raw AMR-NB"),
202 .priv_data_size = sizeof(AMRContext),
203 .read_probe = amrnb_probe,
204 .read_header = amrnb_read_header,
205 .read_packet = ff_raw_read_partial_packet,
206 .flags = AVFMT_GENERIC_INDEX,
207 .priv_class = &ff_raw_demuxer_class,
208 };
209 #endif
210
211 #if CONFIG_AMRWB_DEMUXER
amrwb_probe(const AVProbeData *p)212 static int amrwb_probe(const AVProbeData *p)
213 {
214 int mode, i = 0, valid = 0, invalid = 0;
215 const uint8_t *b = p->buf;
216
217 while (i < p->buf_size) {
218 mode = b[i] >> 3 & 0x0F;
219 if (mode < 10 && (b[i] & 0x4) == 0x4) {
220 int last = b[i];
221 int size = amrwb_packed_size[mode];
222 while (size--) {
223 if (b[++i] != last)
224 break;
225 }
226 if (size > 0) {
227 valid++;
228 i += size;
229 }
230 } else {
231 valid = 0;
232 invalid++;
233 i++;
234 }
235 }
236 if (valid > 100 && valid >> 4 > invalid)
237 return AVPROBE_SCORE_EXTENSION / 2 + 1;
238 return 0;
239 }
240
amrwb_read_header(AVFormatContext *s)241 static int amrwb_read_header(AVFormatContext *s)
242 {
243 AVStream *st = avformat_new_stream(s, NULL);
244 if (!st)
245 return AVERROR(ENOMEM);
246 st->codecpar->codec_id = AV_CODEC_ID_AMR_WB;
247 st->codecpar->sample_rate = 16000;
248 st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
249 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
250 ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
251 avpriv_set_pts_info(st, 64, 1, 16000);
252
253 return 0;
254 }
255
256 const AVInputFormat ff_amrwb_demuxer = {
257 .name = "amrwb",
258 .long_name = NULL_IF_CONFIG_SMALL("raw AMR-WB"),
259 .priv_data_size = sizeof(AMRContext),
260 .read_probe = amrwb_probe,
261 .read_header = amrwb_read_header,
262 .read_packet = ff_raw_read_partial_packet,
263 .flags = AVFMT_GENERIC_INDEX,
264 .priv_class = &ff_raw_demuxer_class,
265 };
266 #endif
267
268 #if CONFIG_AMR_MUXER
269 const AVOutputFormat ff_amr_muxer = {
270 .name = "amr",
271 .long_name = NULL_IF_CONFIG_SMALL("3GPP AMR"),
272 .mime_type = "audio/amr",
273 .extensions = "amr",
274 .audio_codec = AV_CODEC_ID_AMR_NB,
275 .video_codec = AV_CODEC_ID_NONE,
276 .write_header = amr_write_header,
277 .write_packet = ff_raw_write_packet,
278 .flags = AVFMT_NOTIMESTAMPS,
279 };
280 #endif
281