FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/amr.c
Date: 2026-04-20 20:24:43
Exec Total Coverage
Lines: 80 121 66.1%
Functions: 5 7 71.4%
Branches: 48 64 75.0%

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