FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/aacdec.c
Date: 2026-10-28 19:13:44
Exec Total Coverage
Lines: 95 107 88.8%
Functions: 5 5 100.0%
Branches: 47 62 75.8%

Line Branch Exec Source
1 /*
2 * raw ADTS AAC demuxer
3 * Copyright (c) 2008 Michael Niedermayer <michaelni@gmx.at>
4 * Copyright (c) 2009 Robert Swain ( rob opendot cl )
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include "libavutil/avassert.h"
24 #include "libavutil/intreadwrite.h"
25 #include "avformat.h"
26 #include "avio_internal.h"
27 #include "demux.h"
28 #include "internal.h"
29 #include "id3v1.h"
30 #include "id3v2.h"
31 #include "apetag.h"
32
33 #define ADTS_HEADER_SIZE 7
34
35 8322 static int adts_aac_probe(const AVProbeData *p)
36 {
37 8322 int max_frames = 0, first_frames = 0;
38 int fsize, frames;
39 8322 const uint8_t *buf0 = p->buf;
40 const uint8_t *buf2;
41 const uint8_t *buf;
42 8322 const uint8_t *end = buf0 + p->buf_size - 7;
43
44 8322 buf = buf0;
45
46
2/2
✓ Branch 0 taken 391749960 times.
✓ Branch 1 taken 8322 times.
391758282 for (; buf < end; buf = buf2 + 1) {
47 391749960 buf2 = buf;
48
49
2/2
✓ Branch 0 taken 391790344 times.
✓ Branch 1 taken 1966 times.
391792310 for (frames = 0; buf2 < end; frames++) {
50 391790344 uint32_t header = AV_RB16(buf2);
51
2/2
✓ Branch 0 taken 391746609 times.
✓ Branch 1 taken 43735 times.
391790344 if ((header & 0xFFF6) != 0xFFF0) {
52
2/2
✓ Branch 0 taken 391738358 times.
✓ Branch 1 taken 8251 times.
391746609 if (buf != buf0) {
53 // Found something that isn't an ADTS header, starting
54 // from a position other than the start of the buffer.
55 // Discard the count we've accumulated so far since it
56 // probably was a false positive.
57 391738358 frames = 0;
58 }
59 391746609 break;
60 }
61 43735 fsize = (AV_RB32(buf2 + 3) >> 13) & 0x1FFF;
62
2/2
✓ Branch 0 taken 1385 times.
✓ Branch 1 taken 42350 times.
43735 if (fsize < 7)
63 1385 break;
64 42350 fsize = FFMIN(fsize, end - buf2);
65 42350 buf2 += fsize;
66 }
67 391749960 max_frames = FFMAX(max_frames, frames);
68
2/2
✓ Branch 0 taken 8320 times.
✓ Branch 1 taken 391741640 times.
391749960 if (buf == buf0)
69 8320 first_frames = frames;
70 }
71
72
2/2
✓ Branch 0 taken 55 times.
✓ Branch 1 taken 8267 times.
8322 if (first_frames >= 3)
73 55 return AVPROBE_SCORE_EXTENSION + 1;
74
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8267 times.
8267 else if (max_frames > 100)
75 ✗ return AVPROBE_SCORE_EXTENSION;
76
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8267 times.
8267 else if (max_frames >= 3)
77 ✗ return AVPROBE_SCORE_EXTENSION / 2;
78
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 8253 times.
8267 else if (first_frames >= 1)
79 14 return 1;
80 else
81 8253 return 0;
82 }
83
84 63 static int adts_aac_resync(AVFormatContext *s)
85 {
86 uint16_t state;
87 63 int64_t start_pos = avio_tell(s->pb);
88
89 // skip data until an ADTS frame is found
90 63 state = avio_r8(s->pb);
91
2/2
✓ Branch 1 taken 803 times.
✓ Branch 2 taken 2 times.
805 while (!avio_feof(s->pb) &&
92
1/2
✓ Branch 1 taken 803 times.
✗ Branch 2 not taken.
803 (avio_tell(s->pb) - start_pos) < s->probesize) {
93 803 state = (state << 8) | avio_r8(s->pb);
94
2/2
✓ Branch 0 taken 742 times.
✓ Branch 1 taken 61 times.
803 if ((state >> 4) != 0xFFF)
95 742 continue;
96 61 avio_seek(s->pb, -2, SEEK_CUR);
97 61 break;
98 }
99
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 61 times.
63 if (s->pb->eof_reached)
100 2 return AVERROR_EOF;
101
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
61 if ((state >> 4) != 0xFFF)
102 ✗ return AVERROR_INVALIDDATA;
103
104 61 return 0;
105 }
106
107 61 static int adts_aac_read_header(AVFormatContext *s)
108 {
109 AVStream *st;
110 int ret;
111
112 61 st = avformat_new_stream(s, NULL);
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
61 if (!st)
114 ✗ return AVERROR(ENOMEM);
115
116 61 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
117 61 st->codecpar->codec_id = AV_CODEC_ID_AAC;
118 61 ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
119
120 61 ff_id3v1_read(s);
121
3/4
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 59 times.
✓ Branch 3 taken 2 times.
122 if ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) &&
122 61 !av_dict_count(s->metadata)) {
123 59 int64_t cur = avio_tell(s->pb);
124 59 ff_ape_parse_tag(s);
125 59 avio_seek(s->pb, cur, SEEK_SET);
126 }
127
128 61 ret = adts_aac_resync(s);
129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
61 if (ret < 0)
130 ✗ return ret;
131
132 // LCM of all possible ADTS sample rates
133 61 avpriv_set_pts_info(st, 64, 1, 28224000);
134
135 61 return 0;
136 }
137
138 3 static int handle_id3(AVFormatContext *s, AVPacket *pkt)
139 {
140 3 AVDictionary *metadata = NULL;
141 FFIOContext pb;
142 ID3v2ExtraMeta *id3v2_extra_meta;
143 int ret;
144
145 3 ret = av_append_packet(s->pb, pkt, ff_id3v2_tag_len(pkt->data) - pkt->size);
146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
147 ✗ return ret;
148
149 3 ffio_init_read_context(&pb, pkt->data, pkt->size);
150 3 ff_id3v2_read_dict(NULL, &pb.pub, &metadata, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta);
151
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if ((ret = ff_id3v2_parse_priv_dict(&metadata, id3v2_extra_meta)) < 0)
152 ✗ goto error;
153
154
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (metadata) {
155
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if ((ret = av_dict_copy(&s->metadata, metadata, 0)) < 0)
156 ✗ goto error;
157 3 s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
158 }
159
160 ✗ error:
161 3 av_packet_unref(pkt);
162 3 ff_id3v2_free_extra_meta(&id3v2_extra_meta);
163 3 av_dict_free(&metadata);
164
165 3 return ret;
166 }
167
168 4510 static int adts_aac_read_packet(AVFormatContext *s, AVPacket *pkt)
169 {
170 int ret, fsize;
171
172 4513 retry:
173 4513 ret = av_get_packet(s->pb, pkt, ADTS_HEADER_SIZE);
174
2/2
✓ Branch 0 taken 69 times.
✓ Branch 1 taken 4444 times.
4513 if (ret < 0)
175 69 return ret;
176
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4444 times.
4444 if (ret < ADTS_HEADER_SIZE)
178 ✗ return AVERROR_INVALIDDATA;
179
180
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 4439 times.
4444 if ((AV_RB16(pkt->data) >> 4) != 0xfff) {
181 // Parse all the ID3 headers between frames
182 5 int append = ID3v2_HEADER_SIZE - ADTS_HEADER_SIZE;
183
184 av_assert2(append > 0);
185 5 ret = av_append_packet(s->pb, pkt, append);
186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (ret != append)
187 ✗ return AVERROR_INVALIDDATA;
188
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 3 times.
5 if (!ff_id3v2_match(pkt->data, ID3v2_DEFAULT_MAGIC)) {
189 2 av_packet_unref(pkt);
190 2 ret = adts_aac_resync(s);
191 } else
192 3 ret = handle_id3(s, pkt);
193
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3 times.
5 if (ret < 0)
194 2 return ret;
195
196 3 goto retry;
197 }
198
199 4439 fsize = (AV_RB32(pkt->data + 3) >> 13) & 0x1FFF;
200
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4439 times.
4439 if (fsize < ADTS_HEADER_SIZE)
201 ✗ return AVERROR_INVALIDDATA;
202
203 4439 ret = av_append_packet(s->pb, pkt, fsize - pkt->size);
204
1/2
✓ Branch 0 taken 4439 times.
✗ Branch 1 not taken.
4439 if (ret >= 0)
205 4439 pkt->flags |= AV_PKT_FLAG_KEY;
206
207 4439 return ret;
208 }
209
210 const FFInputFormat ff_aac_demuxer = {
211 .p.name = "aac",
212 .p.long_name = NULL_IF_CONFIG_SMALL("raw ADTS AAC (Advanced Audio Coding)"),
213 .p.flags = AVFMT_GENERIC_INDEX,
214 .p.extensions = "aac",
215 .p.mime_type = "audio/aac,audio/aacp,audio/x-aac",
216 .flags_internal = FF_INFMT_FLAG_ID3V2_AUTO,
217 .read_probe = adts_aac_probe,
218 .read_header = adts_aac_read_header,
219 .read_packet = adts_aac_read_packet,
220 .raw_codec_id = AV_CODEC_ID_AAC,
221 };
222