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 profile
204 uint8_t profile = (header[0] >> 6) & 0x3;
205 st->codecpar->profile = profile;
206
207 // get sample rate
208 uint8_t sr_index = (header[0] >> 2) & 0xf;
209 uint32_t sr = get_sample_rate(sr_index);
210 if (sr == 0) {
211 av_log(NULL, AV_LOG_ERROR, "adts_aac_read_header read sampletare error!\n");
212 return;
213 }
214
215 // get channel
216 uint8_t channel = (header[0] & 0x1) << 2 | (header[1] >> 6);
217 if(channel == 0) {
218 av_log(NULL, AV_LOG_ERROR, "adts_aac_read_header read channel error!\n");
219 return;
220 }
221
222 st->codecpar->channels = channel;
223 st->codecpar->sample_rate = sr;
224 avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
225
226 int frame_size = 0;
227 int raw_data_block_num = 0;
228 int64_t frame_duration_us = 0;
229 int64_t duration = 0;
230 int64_t frame_num = 0;
231 int64_t stream_size = avio_size(s->pb);
232 if (stream_size > 0) {
233 while (offset < stream_size) {
234 if ((frame_size = adts_aac_get_frame_length(s, offset)) == 0) {
235 break;
236 }
237 raw_data_block_num = adts_aac_get_raw_data_block_num(s, offset);
238 offset += frame_size;
239 frame_num += (raw_data_block_num + 1);
240 }
241 // round up and get the duration
242 frame_duration_us = (1024 * 1000000ll + (sr - 1)) / sr;
243 duration = frame_num * frame_duration_us; // us
244 duration = av_rescale_q(duration, AV_TIME_BASE_Q, st->time_base);
245 if (duration != 0) {
246 st->duration = duration;
247 }
248 }
249 avio_seek(s->pb, 0, SEEK_SET);
250 }
251 #endif
252
adts_aac_read_header(AVFormatContext *s)253 static int adts_aac_read_header(AVFormatContext *s)
254 {
255 AVStream *st;
256 int ret;
257
258 st = avformat_new_stream(s, NULL);
259 if (!st)
260 return AVERROR(ENOMEM);
261
262 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
263 st->codecpar->codec_id = s->iformat->raw_codec_id;
264 ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
265
266 ff_id3v1_read(s);
267 if ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) &&
268 !av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX)) {
269 int64_t cur = avio_tell(s->pb);
270 ff_ape_parse_tag(s);
271 avio_seek(s->pb, cur, SEEK_SET);
272 }
273
274 ret = adts_aac_resync(s);
275 if (ret < 0)
276 return ret;
277
278 #ifdef OHOS_OPT_COMPAT
279 // ohos.opt.compat.0001
280 adts_aac_get_duration(s, st);
281 #else
282 // LCM of all possible ADTS sample rates
283 avpriv_set_pts_info(st, 64, 1, 28224000);
284 #endif
285
286 return 0;
287 }
288
handle_id3(AVFormatContext *s, AVPacket *pkt)289 static int handle_id3(AVFormatContext *s, AVPacket *pkt)
290 {
291 AVDictionary *metadata = NULL;
292 FFIOContext pb;
293 ID3v2ExtraMeta *id3v2_extra_meta;
294 int ret;
295
296 ret = av_append_packet(s->pb, pkt, ff_id3v2_tag_len(pkt->data) - pkt->size);
297 if (ret < 0) {
298 return ret;
299 }
300
301 ffio_init_context(&pb, pkt->data, pkt->size, 0, NULL, NULL, NULL, NULL);
302 ff_id3v2_read_dict(&pb.pub, &metadata, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta);
303 if ((ret = ff_id3v2_parse_priv_dict(&metadata, id3v2_extra_meta)) < 0)
304 goto error;
305
306 if (metadata) {
307 if ((ret = av_dict_copy(&s->metadata, metadata, 0)) < 0)
308 goto error;
309 s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
310 }
311
312 error:
313 av_packet_unref(pkt);
314 ff_id3v2_free_extra_meta(&id3v2_extra_meta);
315 av_dict_free(&metadata);
316
317 return ret;
318 }
319
adts_aac_read_packet(AVFormatContext *s, AVPacket *pkt)320 static int adts_aac_read_packet(AVFormatContext *s, AVPacket *pkt)
321 {
322 int ret, fsize;
323
324 retry:
325 ret = av_get_packet(s->pb, pkt, ADTS_HEADER_SIZE);
326 if (ret < 0)
327 return ret;
328
329 if (ret < ADTS_HEADER_SIZE) {
330 return AVERROR(EIO);
331 }
332
333 if ((AV_RB16(pkt->data) >> 4) != 0xfff) {
334 // Parse all the ID3 headers between frames
335 int append = ID3v2_HEADER_SIZE - ADTS_HEADER_SIZE;
336
337 av_assert2(append > 0);
338 ret = av_append_packet(s->pb, pkt, append);
339 if (ret != append) {
340 return AVERROR(EIO);
341 }
342 if (!ff_id3v2_match(pkt->data, ID3v2_DEFAULT_MAGIC)) {
343 av_packet_unref(pkt);
344 ret = adts_aac_resync(s);
345 } else
346 ret = handle_id3(s, pkt);
347 if (ret < 0)
348 return ret;
349
350 goto retry;
351 }
352
353 fsize = (AV_RB32(pkt->data + 3) >> 13) & 0x1FFF;
354 if (fsize < ADTS_HEADER_SIZE) {
355 return AVERROR_INVALIDDATA;
356 }
357
358 ret = av_append_packet(s->pb, pkt, fsize - pkt->size);
359
360 return ret;
361 }
362
363 const AVInputFormat ff_aac_demuxer = {
364 .name = "aac",
365 .long_name = NULL_IF_CONFIG_SMALL("raw ADTS AAC (Advanced Audio Coding)"),
366 .read_probe = adts_aac_probe,
367 .read_header = adts_aac_read_header,
368 .read_packet = adts_aac_read_packet,
369 .flags = AVFMT_GENERIC_INDEX,
370 .extensions = "aac",
371 .mime_type = "audio/aac,audio/aacp,audio/x-aac",
372 .raw_codec_id = AV_CODEC_ID_AAC,
373 };
374