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 "internal.h"
28 #include "id3v1.h"
29 #include "id3v2.h"
30 #include "apetag.h"
31
32 #define ADTS_HEADER_SIZE 7
33
adts_aac_probe(const AVProbeData * p)34 static int adts_aac_probe(const AVProbeData *p)
35 {
36 int max_frames = 0, first_frames = 0;
37 int fsize, frames;
38 const uint8_t *buf0 = p->buf;
39 const uint8_t *buf2;
40 const uint8_t *buf;
41 const uint8_t *end = buf0 + p->buf_size - 7;
42
43 buf = buf0;
44
45 for (; buf < end; buf = buf2 + 1) {
46 buf2 = buf;
47
48 for (frames = 0; buf2 < end; frames++) {
49 uint32_t header = AV_RB16(buf2);
50 if ((header & 0xFFF6) != 0xFFF0) {
51 if (buf != buf0) {
52 // Found something that isn't an ADTS header, starting
53 // from a position other than the start of the buffer.
54 // Discard the count we've accumulated so far since it
55 // probably was a false positive.
56 frames = 0;
57 }
58 break;
59 }
60 fsize = (AV_RB32(buf2 + 3) >> 13) & 0x1FFF;
61 if (fsize < 7)
62 break;
63 fsize = FFMIN(fsize, end - buf2);
64 buf2 += fsize;
65 }
66 max_frames = FFMAX(max_frames, frames);
67 if (buf == buf0)
68 first_frames = frames;
69 }
70
71 if (first_frames >= 3)
72 return AVPROBE_SCORE_EXTENSION + 1;
73 else if (max_frames > 100)
74 return AVPROBE_SCORE_EXTENSION;
75 else if (max_frames >= 3)
76 return AVPROBE_SCORE_EXTENSION / 2;
77 else if (first_frames >= 1)
78 return 1;
79 else
80 return 0;
81 }
82
adts_aac_resync(AVFormatContext * s)83 static int adts_aac_resync(AVFormatContext *s)
84 {
85 uint16_t state;
86 int64_t start_pos = avio_tell(s->pb);
87
88 // skip data until an ADTS frame is found
89 state = avio_r8(s->pb);
90 while (!avio_feof(s->pb) &&
91 (avio_tell(s->pb) - start_pos) < s->probesize) {
92 state = (state << 8) | avio_r8(s->pb);
93 if ((state >> 4) != 0xFFF)
94 continue;
95 avio_seek(s->pb, -2, SEEK_CUR);
96 break;
97 }
98 if (s->pb->eof_reached)
99 return AVERROR_EOF;
100 if ((state >> 4) != 0xFFF)
101 return AVERROR_INVALIDDATA;
102
103 return 0;
104 }
105
106 #ifdef OHOS_OPT_COMPAT
107 /**
108 * ohos.opt.compat.0001
109 * fix duration not accurate in aac.
110 * There is one packet for every 1024 samples,
111 * get the sample num in each frame and sample rate from adts
112 * to calculate duration of each frame, then the summation of
113 * frame duration is the file duration.
114 */
adts_aac_get_frame_length(AVFormatContext * s,int64_t offset)115 static int adts_aac_get_frame_length(AVFormatContext *s, int64_t offset)
116 {
117 const int adts_header_length_no_crc = 7;
118 const int adts_header_length_with_crc = 9;
119 uint8_t syncword[2];
120
121 avio_seek(s->pb, offset, SEEK_SET);
122 // read syncword
123 if (avio_read(s->pb, &syncword, 2) != 2) {
124 return 0;
125 }
126 if ((syncword[0] != 0xff) || ((syncword[1] & 0xf6) != 0xf0)) {
127 return 0;
128 }
129
130 // read protection_absent
131 uint8_t protection_absent;
132 avio_seek(s->pb, offset + 1, SEEK_SET);
133 if (avio_read(s->pb, &protection_absent, 1) < 1) {
134 return 0;
135 }
136 protection_absent &= 0x1;
137
138 // get frame_size
139 uint8_t header[3];
140 avio_seek(s->pb, offset + 3, SEEK_SET);
141 if (avio_read(s->pb, &header, 3) < 3) {
142 return 0;
143 }
144 int frame_size = (header[0] & 0x3) << 11 | header[1] << 3 | header[2] >> 5;
145 // protection_absent is 0 if there is CRC
146 int head_size = protection_absent ? adts_header_length_no_crc : adts_header_length_with_crc;
147 if (head_size > frame_size) {
148 return 0;
149 }
150
151 // get adts_buffer_fullness
152 uint8_t head[2];
153 avio_seek(s->pb, offset + 5, SEEK_SET);
154 if (avio_read(s->pb, &head, 2) < 2) {
155 return 0;
156 }
157 uint16_t adts_buffer_fullness = (head[0] & 0x1F) << 6 | (head[1] >> 2);
158 if (adts_buffer_fullness != 0x7FF) { // not VBR
159 return 0;
160 }
161
162 return frame_size;
163 }
164
adts_aac_get_raw_data_block_num(AVFormatContext * s,int64_t offset)165 static int adts_aac_get_raw_data_block_num(AVFormatContext *s, int64_t offset)
166 {
167 uint8_t raw_data_block_num = 0;
168 avio_seek(s->pb, offset + 6, SEEK_SET);
169 if (avio_read(s->pb, &raw_data_block_num, 1) < 1) {
170 return 0;
171 }
172 raw_data_block_num &= 0x3;
173
174 return raw_data_block_num;
175 }
176
177 // get sample rate by index
get_sample_rate(const uint8_t sr_index)178 static uint32_t get_sample_rate(const uint8_t sr_index)
179 {
180 static const uint32_t sample_rates[] =
181 {
182 96000, 88200, 64000, 48000, 44100, 32000,
183 24000, 22050, 16000, 12000, 11025, 8000
184 };
185
186 if (sr_index < sizeof(sample_rates) / sizeof(sample_rates[0])) {
187 return sample_rates[sr_index];
188 }
189
190 return 0;
191 }
192
adts_aac_get_duration(AVFormatContext * s,AVStream * st)193 static void adts_aac_get_duration(AVFormatContext *s, AVStream *st)
194 {
195 avio_seek(s->pb, 0, SEEK_SET);
196 uint8_t header[2];
197 avio_seek(s->pb, 2, SEEK_SET);
198 if (avio_read(s->pb, &header, 2) < 2) {
199 av_log(NULL, AV_LOG_ERROR, "avio_read header error!\n");
200 return;
201 }
202 int64_t offset = 0;
203 // get sample rate
204 uint8_t sr_index = (header[0] >> 2) & 0xf;
205 uint32_t sr = get_sample_rate(sr_index);
206 if (sr == 0) {
207 av_log(NULL, AV_LOG_ERROR, "adts_aac_read_header read sampletare error!\n");
208 return;
209 }
210
211 avpriv_set_pts_info(st, 64, 1, sr);
212
213 int frame_size = 0;
214 int raw_data_block_num = 0;
215 int64_t frame_duration_us = 0;
216 int64_t duration = 0;
217 int64_t frame_num = 0;
218 int64_t stream_size = avio_size(s->pb);
219 if (stream_size > 0) {
220 while (offset + 7 <= stream_size) {
221 if ((frame_size = adts_aac_get_frame_length(s, offset)) == 0) {
222 break;
223 }
224 raw_data_block_num = adts_aac_get_raw_data_block_num(s, offset);
225 offset += frame_size;
226 frame_num += (raw_data_block_num + 1);
227 }
228 // round up and get the duration
229 frame_duration_us = (1024 * 1000000ll + (sr - 1)) / sr;
230 duration = frame_num * frame_duration_us; // us
231 duration = av_rescale_q(duration, AV_TIME_BASE_Q, st->time_base);
232 if (duration != 0) {
233 st->duration = duration;
234 }
235 }
236 avio_seek(s->pb, 0, SEEK_SET);
237 }
238 #endif
239
adts_aac_read_header(AVFormatContext * s)240 static int adts_aac_read_header(AVFormatContext *s)
241 {
242 AVStream *st;
243 int ret;
244
245 st = avformat_new_stream(s, NULL);
246 if (!st)
247 return AVERROR(ENOMEM);
248
249 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
250 st->codecpar->codec_id = s->iformat->raw_codec_id;
251 ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
252
253 ff_id3v1_read(s);
254 if ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) &&
255 !av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX)) {
256 int64_t cur = avio_tell(s->pb);
257 ff_ape_parse_tag(s);
258 avio_seek(s->pb, cur, SEEK_SET);
259 }
260
261 ret = adts_aac_resync(s);
262 if (ret < 0)
263 return ret;
264
265 #ifdef OHOS_OPT_COMPAT
266 // ohos.opt.compat.0001
267 adts_aac_get_duration(s, st);
268 #else
269 // LCM of all possible ADTS sample rates
270 avpriv_set_pts_info(st, 64, 1, 28224000);
271 #endif
272
273 return 0;
274 }
275
handle_id3(AVFormatContext * s,AVPacket * pkt)276 static int handle_id3(AVFormatContext *s, AVPacket *pkt)
277 {
278 AVDictionary *metadata = NULL;
279 FFIOContext pb;
280 ID3v2ExtraMeta *id3v2_extra_meta;
281 int ret;
282
283 ret = av_append_packet(s->pb, pkt, ff_id3v2_tag_len(pkt->data) - pkt->size);
284 if (ret < 0) {
285 return ret;
286 }
287
288 ffio_init_context(&pb, pkt->data, pkt->size, 0, NULL, NULL, NULL, NULL);
289 ff_id3v2_read_dict(&pb.pub, &metadata, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta);
290 if ((ret = ff_id3v2_parse_priv_dict(&metadata, id3v2_extra_meta)) < 0)
291 goto error;
292
293 if (metadata) {
294 if ((ret = av_dict_copy(&s->metadata, metadata, 0)) < 0)
295 goto error;
296 s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
297 }
298
299 error:
300 av_packet_unref(pkt);
301 ff_id3v2_free_extra_meta(&id3v2_extra_meta);
302 av_dict_free(&metadata);
303
304 return ret;
305 }
306
adts_aac_read_packet(AVFormatContext * s,AVPacket * pkt)307 static int adts_aac_read_packet(AVFormatContext *s, AVPacket *pkt)
308 {
309 int ret, fsize;
310
311 retry:
312 ret = av_get_packet(s->pb, pkt, ADTS_HEADER_SIZE);
313 if (ret < 0)
314 return ret;
315
316 if (ret < ADTS_HEADER_SIZE) {
317 return AVERROR(EIO);
318 }
319
320 if ((AV_RB16(pkt->data) >> 4) != 0xfff) {
321 // Parse all the ID3 headers between frames
322 int append = ID3v2_HEADER_SIZE - ADTS_HEADER_SIZE;
323
324 av_assert2(append > 0);
325 ret = av_append_packet(s->pb, pkt, append);
326 if (ret != append) {
327 return AVERROR(EIO);
328 }
329 if (!ff_id3v2_match(pkt->data, ID3v2_DEFAULT_MAGIC)) {
330 av_packet_unref(pkt);
331 ret = adts_aac_resync(s);
332 } else
333 ret = handle_id3(s, pkt);
334 if (ret < 0)
335 return ret;
336
337 goto retry;
338 }
339
340 fsize = (AV_RB32(pkt->data + 3) >> 13) & 0x1FFF;
341 if (fsize < ADTS_HEADER_SIZE) {
342 return AVERROR_INVALIDDATA;
343 }
344
345 ret = av_append_packet(s->pb, pkt, fsize - pkt->size);
346
347 return ret;
348 }
349
350 const AVInputFormat ff_aac_demuxer = {
351 .name = "aac",
352 .long_name = NULL_IF_CONFIG_SMALL("raw ADTS AAC (Advanced Audio Coding)"),
353 .read_probe = adts_aac_probe,
354 .read_header = adts_aac_read_header,
355 .read_packet = adts_aac_read_packet,
356 .flags = AVFMT_GENERIC_INDEX,
357 .extensions = "aac",
358 .mime_type = "audio/aac,audio/aacp,audio/x-aac",
359 .raw_codec_id = AV_CODEC_ID_AAC,
360 };
361