FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/amr.c
Date: 2022-12-06 04:36:11
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 "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
53 2 static int amr_write_header(AVFormatContext *s)
54 {
55 2 AVIOContext *pb = s->pb;
56 2 AVCodecParameters *par = s->streams[0]->codecpar;
57
58
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (par->codec_id == AV_CODEC_ID_AMR_NB) {
59 1 avio_write(pb, AMR_header, sizeof(AMR_header)); /* magic number */
60
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 } else if (par->codec_id == AV_CODEC_ID_AMR_WB) {
61 1 avio_write(pb, AMRWB_header, sizeof(AMRWB_header)); /* magic number */
62 } else {
63 return -1;
64 }
65 2 return 0;
66 }
67 #endif /* CONFIG_AMR_MUXER */
68
69 #if CONFIG_AMR_DEMUXER
70 6782 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
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 6772 times.
6782 if (!memcmp(p->buf, AMR_header, 5))
77 10 return AVPROBE_SCORE_MAX;
78 else
79 6772 return 0;
80 }
81
82 /* amr input */
83 12 static int amr_read_header(AVFormatContext *s)
84 {
85 12 AVIOContext *pb = s->pb;
86 AVStream *st;
87 12 uint8_t header[19] = { 0 };
88 12 int read, back = 0, ret;
89
90 12 ret = ffio_ensure_seekback(s->pb, sizeof(header));
91
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (ret < 0)
92 return ret;
93
94 12 read = avio_read(pb, header, sizeof(header));
95
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (read < 0)
96 return read;
97
98 12 st = avformat_new_stream(s, NULL);
99
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (!st)
100 return AVERROR(ENOMEM);
101
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 1 times.
12 if (!memcmp(header, AMR_header, sizeof(AMR_header))) {
102 11 st->codecpar->codec_tag = MKTAG('s', 'a', 'm', 'r');
103 11 st->codecpar->codec_id = AV_CODEC_ID_AMR_NB;
104 11 st->codecpar->sample_rate = 8000;
105 11 st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
106 11 back = read - sizeof(AMR_header);
107
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 } else if (!memcmp(header, AMRWB_header, sizeof(AMRWB_header))) {
108 1 st->codecpar->codec_tag = MKTAG('s', 'a', 'w', 'b');
109 1 st->codecpar->codec_id = AV_CODEC_ID_AMR_WB;
110 1 st->codecpar->sample_rate = 16000;
111 1 st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
112 1 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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (st->codecpar->ch_layout.nb_channels < 1)
130 return AVERROR_INVALIDDATA;
131
132 12 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
133 12 ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
134 12 avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
135
136
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 if (back > 0)
137 12 avio_seek(pb, -back, SEEK_CUR);
138
139 12 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
155 6782 static int amrnb_probe(const AVProbeData *p)
156 {
157 6782 int mode, i = 0, valid = 0, invalid = 0;
158 6782 const uint8_t *b = p->buf;
159
160
2/2
✓ Branch 0 taken 35426133 times.
✓ Branch 1 taken 6782 times.
35432915 while (i < p->buf_size) {
161 35426133 mode = b[i] >> 3 & 0x0F;
162
4/4
✓ Branch 0 taken 19222636 times.
✓ Branch 1 taken 16203497 times.
✓ Branch 2 taken 6223516 times.
✓ Branch 3 taken 12999120 times.
41649649 if (mode < 9 && (b[i] & 0x4) == 0x4) {
163 6223516 int last = b[i];
164 6223516 int size = amrnb_packed_size[mode];
165
2/2
✓ Branch 0 taken 6260281 times.
✓ Branch 1 taken 407 times.
6260688 while (size--) {
166
2/2
✓ Branch 0 taken 6223109 times.
✓ Branch 1 taken 37172 times.
6260281 if (b[++i] != last)
167 6223109 break;
168 }
169
2/2
✓ Branch 0 taken 6223080 times.
✓ Branch 1 taken 436 times.
6223516 if (size > 0) {
170 6223080 valid++;
171 6223080 i += size;
172 }
173 } else {
174 29202617 valid = 0;
175 29202617 invalid++;
176 29202617 i++;
177 }
178 }
179
4/4
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 6767 times.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 5 times.
6782 if (valid > 100 && valid >> 4 > invalid)
180 10 return AVPROBE_SCORE_EXTENSION / 2 + 1;
181 6772 return 0;
182 }
183
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
212 6782 static int amrwb_probe(const AVProbeData *p)
213 {
214 6782 int mode, i = 0, valid = 0, invalid = 0;
215 6782 const uint8_t *b = p->buf;
216
217
2/2
✓ Branch 0 taken 21718825 times.
✓ Branch 1 taken 6782 times.
21725607 while (i < p->buf_size) {
218 21718825 mode = b[i] >> 3 & 0x0F;
219
4/4
✓ Branch 0 taken 13061934 times.
✓ Branch 1 taken 8656891 times.
✓ Branch 2 taken 3446294 times.
✓ Branch 3 taken 9615640 times.
25165119 if (mode < 10 && (b[i] & 0x4) == 0x4) {
220 3446294 int last = b[i];
221 3446294 int size = amrwb_packed_size[mode];
222
2/2
✓ Branch 0 taken 3470839 times.
✓ Branch 1 taken 252 times.
3471091 while (size--) {
223
2/2
✓ Branch 0 taken 3446042 times.
✓ Branch 1 taken 24797 times.
3470839 if (b[++i] != last)
224 3446042 break;
225 }
226
2/2
✓ Branch 0 taken 3446037 times.
✓ Branch 1 taken 257 times.
3446294 if (size > 0) {
227 3446037 valid++;
228 3446037 i += size;
229 }
230 } else {
231 18272531 valid = 0;
232 18272531 invalid++;
233 18272531 i++;
234 }
235 }
236
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6779 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
6782 if (valid > 100 && valid >> 4 > invalid)
237 return AVPROBE_SCORE_EXTENSION / 2 + 1;
238 6782 return 0;
239 }
240
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