FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/aacdec.c
Date: 2024-04-24 13:31:03
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 7125 static int adts_aac_probe(const AVProbeData *p)
36 {
37 7125 int max_frames = 0, first_frames = 0;
38 int fsize, frames;
39 7125 const uint8_t *buf0 = p->buf;
40 const uint8_t *buf2;
41 const uint8_t *buf;
42 7125 const uint8_t *end = buf0 + p->buf_size - 7;
43
44 7125 buf = buf0;
45
46
2/2
✓ Branch 0 taken 258641963 times.
✓ Branch 1 taken 7125 times.
258649088 for (; buf < end; buf = buf2 + 1) {
47 258641963 buf2 = buf;
48
49
2/2
✓ Branch 0 taken 258675992 times.
✓ Branch 1 taken 1744 times.
258677736 for (frames = 0; buf2 < end; frames++) {
50 258675992 uint32_t header = AV_RB16(buf2);
51
2/2
✓ Branch 0 taken 258638868 times.
✓ Branch 1 taken 37124 times.
258675992 if ((header & 0xFFF6) != 0xFFF0) {
52
2/2
✓ Branch 0 taken 258631771 times.
✓ Branch 1 taken 7097 times.
258638868 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 258631771 frames = 0;
58 }
59 258638868 break;
60 }
61 37124 fsize = (AV_RB32(buf2 + 3) >> 13) & 0x1FFF;
62
2/2
✓ Branch 0 taken 1351 times.
✓ Branch 1 taken 35773 times.
37124 if (fsize < 7)
63 1351 break;
64 35773 fsize = FFMIN(fsize, end - buf2);
65 35773 buf2 += fsize;
66 }
67 258641963 max_frames = FFMAX(max_frames, frames);
68
2/2
✓ Branch 0 taken 7125 times.
✓ Branch 1 taken 258634838 times.
258641963 if (buf == buf0)
69 7125 first_frames = frames;
70 }
71
72
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 7099 times.
7125 if (first_frames >= 3)
73 26 return AVPROBE_SCORE_EXTENSION + 1;
74
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7099 times.
7099 else if (max_frames > 100)
75 return AVPROBE_SCORE_EXTENSION;
76
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7099 times.
7099 else if (max_frames >= 3)
77 return AVPROBE_SCORE_EXTENSION / 2;
78
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 7097 times.
7099 else if (first_frames >= 1)
79 2 return 1;
80 else
81 7097 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_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX)) {
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
150 3 ffio_init_read_context(&pb, pkt->data, pkt->size);
151 3 ff_id3v2_read_dict(&pb.pub, &metadata, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta);
152
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if ((ret = ff_id3v2_parse_priv_dict(&metadata, id3v2_extra_meta)) < 0)
153 goto error;
154
155
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (metadata) {
156
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if ((ret = av_dict_copy(&s->metadata, metadata, 0)) < 0)
157 goto error;
158 3 s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
159 }
160
161 error:
162 3 av_packet_unref(pkt);
163 3 ff_id3v2_free_extra_meta(&id3v2_extra_meta);
164 3 av_dict_free(&metadata);
165
166 3 return ret;
167 }
168
169 5903 static int adts_aac_read_packet(AVFormatContext *s, AVPacket *pkt)
170 {
171 int ret, fsize;
172
173 5906 retry:
174 5906 ret = av_get_packet(s->pb, pkt, ADTS_HEADER_SIZE);
175
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 5861 times.
5906 if (ret < 0)
176 45 return ret;
177
178
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5861 times.
5861 if (ret < ADTS_HEADER_SIZE) {
179 return AVERROR(EIO);
180 }
181
182
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 5857 times.
5861 if ((AV_RB16(pkt->data) >> 4) != 0xfff) {
183 // Parse all the ID3 headers between frames
184 4 int append = ID3v2_HEADER_SIZE - ADTS_HEADER_SIZE;
185
186 av_assert2(append > 0);
187 4 ret = av_append_packet(s->pb, pkt, append);
188
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret != append) {
189 return AVERROR(EIO);
190 }
191
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 3 times.
4 if (!ff_id3v2_match(pkt->data, ID3v2_DEFAULT_MAGIC)) {
192 1 av_packet_unref(pkt);
193 1 ret = adts_aac_resync(s);
194 } else
195 3 ret = handle_id3(s, pkt);
196
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
4 if (ret < 0)
197 1 return ret;
198
199 3 goto retry;
200 }
201
202 5857 fsize = (AV_RB32(pkt->data + 3) >> 13) & 0x1FFF;
203
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5857 times.
5857 if (fsize < ADTS_HEADER_SIZE) {
204 return AVERROR_INVALIDDATA;
205 }
206
207 5857 ret = av_append_packet(s->pb, pkt, fsize - pkt->size);
208
209 5857 return ret;
210 }
211
212 const FFInputFormat ff_aac_demuxer = {
213 .p.name = "aac",
214 .p.long_name = NULL_IF_CONFIG_SMALL("raw ADTS AAC (Advanced Audio Coding)"),
215 .p.flags = AVFMT_GENERIC_INDEX,
216 .p.extensions = "aac",
217 .p.mime_type = "audio/aac,audio/aacp,audio/x-aac",
218 .read_probe = adts_aac_probe,
219 .read_header = adts_aac_read_header,
220 .read_packet = adts_aac_read_packet,
221 .raw_codec_id = AV_CODEC_ID_AAC,
222 };
223