FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/aacdec.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 93 105 88.6%
Functions: 5 5 100.0%
Branches: 46 60 76.7%

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 7186 static int adts_aac_probe(const AVProbeData *p)
36 {
37 7186 int max_frames = 0, first_frames = 0;
38 int fsize, frames;
39 7186 const uint8_t *buf0 = p->buf;
40 const uint8_t *buf2;
41 const uint8_t *buf;
42 7186 const uint8_t *end = buf0 + p->buf_size - 7;
43
44 7186 buf = buf0;
45
46
2/2
✓ Branch 0 taken 266233530 times.
✓ Branch 1 taken 7186 times.
266240716 for (; buf < end; buf = buf2 + 1) {
47 266233530 buf2 = buf;
48
49
2/2
✓ Branch 0 taken 266267757 times.
✓ Branch 1 taken 1751 times.
266269508 for (frames = 0; buf2 < end; frames++) {
50 266267757 uint32_t header = AV_RB16(buf2);
51
2/2
✓ Branch 0 taken 266230428 times.
✓ Branch 1 taken 37329 times.
266267757 if ((header & 0xFFF6) != 0xFFF0) {
52
2/2
✓ Branch 0 taken 266223270 times.
✓ Branch 1 taken 7158 times.
266230428 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 266223270 frames = 0;
58 }
59 266230428 break;
60 }
61 37329 fsize = (AV_RB32(buf2 + 3) >> 13) & 0x1FFF;
62
2/2
✓ Branch 0 taken 1351 times.
✓ Branch 1 taken 35978 times.
37329 if (fsize < 7)
63 1351 break;
64 35978 fsize = FFMIN(fsize, end - buf2);
65 35978 buf2 += fsize;
66 }
67 266233530 max_frames = FFMAX(max_frames, frames);
68
2/2
✓ Branch 0 taken 7186 times.
✓ Branch 1 taken 266226344 times.
266233530 if (buf == buf0)
69 7186 first_frames = frames;
70 }
71
72
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 7160 times.
7186 if (first_frames >= 3)
73 26 return AVPROBE_SCORE_EXTENSION + 1;
74
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7160 times.
7160 else if (max_frames > 100)
75 return AVPROBE_SCORE_EXTENSION;
76
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7160 times.
7160 else if (max_frames >= 3)
77 return AVPROBE_SCORE_EXTENSION / 2;
78
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 7158 times.
7160 else if (first_frames >= 1)
79 2 return 1;
80 else
81 7158 return 0;
82 }
83
84 32 static int adts_aac_resync(AVFormatContext *s)
85 {
86 uint16_t state;
87 32 int64_t start_pos = avio_tell(s->pb);
88
89 // skip data until an ADTS frame is found
90 32 state = avio_r8(s->pb);
91
2/2
✓ Branch 1 taken 402 times.
✓ Branch 2 taken 1 times.
403 while (!avio_feof(s->pb) &&
92
1/2
✓ Branch 1 taken 402 times.
✗ Branch 2 not taken.
402 (avio_tell(s->pb) - start_pos) < s->probesize) {
93 402 state = (state << 8) | avio_r8(s->pb);
94
2/2
✓ Branch 0 taken 371 times.
✓ Branch 1 taken 31 times.
402 if ((state >> 4) != 0xFFF)
95 371 continue;
96 31 avio_seek(s->pb, -2, SEEK_CUR);
97 31 break;
98 }
99
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 31 times.
32 if (s->pb->eof_reached)
100 1 return AVERROR_EOF;
101
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 if ((state >> 4) != 0xFFF)
102 return AVERROR_INVALIDDATA;
103
104 31 return 0;
105 }
106
107 31 static int adts_aac_read_header(AVFormatContext *s)
108 {
109 AVStream *st;
110 int ret;
111
112 31 st = avformat_new_stream(s, NULL);
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 if (!st)
114 return AVERROR(ENOMEM);
115
116 31 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
117 31 st->codecpar->codec_id = AV_CODEC_ID_AAC;
118 31 ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
119
120 31 ff_id3v1_read(s);
121
3/4
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
✓ Branch 3 taken 1 times.
62 if ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) &&
122 31 !av_dict_count(s->metadata)) {
123 30 int64_t cur = avio_tell(s->pb);
124 30 ff_ape_parse_tag(s);
125 30 avio_seek(s->pb, cur, SEEK_SET);
126 }
127
128 31 ret = adts_aac_resync(s);
129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 if (ret < 0)
130 return ret;
131
132 // LCM of all possible ADTS sample rates
133 31 avpriv_set_pts_info(st, 64, 1, 28224000);
134
135 31 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(&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 5903 static int adts_aac_read_packet(AVFormatContext *s, AVPacket *pkt)
169 {
170 int ret, fsize;
171
172 5906 retry:
173 5906 ret = av_get_packet(s->pb, pkt, ADTS_HEADER_SIZE);
174
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 5861 times.
5906 if (ret < 0)
175 45 return ret;
176
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5861 times.
5861 if (ret < ADTS_HEADER_SIZE)
178 return AVERROR(EIO);
179
180
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 5857 times.
5861 if ((AV_RB16(pkt->data) >> 4) != 0xfff) {
181 // Parse all the ID3 headers between frames
182 4 int append = ID3v2_HEADER_SIZE - ADTS_HEADER_SIZE;
183
184 av_assert2(append > 0);
185 4 ret = av_append_packet(s->pb, pkt, append);
186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret != append)
187 return AVERROR(EIO);
188
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 3 times.
4 if (!ff_id3v2_match(pkt->data, ID3v2_DEFAULT_MAGIC)) {
189 1 av_packet_unref(pkt);
190 1 ret = adts_aac_resync(s);
191 } else
192 3 ret = handle_id3(s, pkt);
193
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
4 if (ret < 0)
194 1 return ret;
195
196 3 goto retry;
197 }
198
199 5857 fsize = (AV_RB32(pkt->data + 3) >> 13) & 0x1FFF;
200
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5857 times.
5857 if (fsize < ADTS_HEADER_SIZE)
201 return AVERROR_INVALIDDATA;
202
203 5857 ret = av_append_packet(s->pb, pkt, fsize - pkt->size);
204
205 5857 return ret;
206 }
207
208 const FFInputFormat ff_aac_demuxer = {
209 .p.name = "aac",
210 .p.long_name = NULL_IF_CONFIG_SMALL("raw ADTS AAC (Advanced Audio Coding)"),
211 .p.flags = AVFMT_GENERIC_INDEX,
212 .p.extensions = "aac",
213 .p.mime_type = "audio/aac,audio/aacp,audio/x-aac",
214 .read_probe = adts_aac_probe,
215 .read_header = adts_aac_read_header,
216 .read_packet = adts_aac_read_packet,
217 .raw_codec_id = AV_CODEC_ID_AAC,
218 };
219