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