FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/amr.c
Date: 2023-12-04 05:51:44
Exec Total Coverage
Lines: 80 121 66.1%
Functions: 5 7 71.4%
Branches: 44 60 73.3%

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