FFmpeg AAC ATDS流探测
ADTS
Audio Data Transport Stream (ADTS) 是一种格式, 通常用来传输AAC流.
头结构如下:
头由7个或者9个字节组成 (有或者没有CRC校验).
AAAAAAAA AAAABCCD EEFFFFGH HHIJKLMM MMMMMMMM MMMOOOOO OOOOOOPP (QQQQQQQQ QQQQQQQQ)
字母 | 长度(bits) | 描述 |
---|---|---|
A | 12 | 同步字0xFFF, 所有位必须为 1 |
B | 1 | MPEG 版本: 0 MPEG-4, 1 MPEG-2 |
C | 2 | MPEG层: 通常 0 |
D | 1 | 缺失保护; 警告, 设置成1代表没有CRC,0代表有CRC |
E | 2 | 简介, MPEG-4 音频类型 减1 |
F | 4 | MPEG-4 采样率 |
G | 1 | 保留位, 确保MPEG从不使用, 编码时设置成 0,解码时忽略。 |
H | 3 | MPEG-4 通道配置 (设置成0时由内部PCE配置) |
I | 1 | ?, 编码时设置成 0,解码时忽略。 |
J | 1 | ?, 编码时设置成 0,解码时忽略。 |
K | 1 | ?, 编码时设置成 0,解码时忽略。 |
L | 1 | |
M | 13 | 包括头的帧长度,头+AAC帧长 |
O | 11 | |
P | 2 | ATDS帧中的AAC帧数减1,通常一帧ATDS包含一帧AAC |
Q | 16 | CRC |
采样率:
支持13种采样率:
- 0: 96000 Hz
- 1: 88200 Hz
- 2: 64000 Hz
- 3: 48000 Hz
- 4: 44100 Hz
- 5: 32000 Hz
- 6: 24000 Hz
- 7: 22050 Hz
- 8: 16000 Hz
- 9: 12000 Hz
- 10: 11025 Hz
- 11: 8000 Hz
- 12: 7350 Hz
- 13: Reserved
- 14: Reserved
- 15: frequency is written explictly
AAC流探测API如下,主要通过同步字来进行判断:
1 static int adts_aac_probe(AVProbeData *p) 2 { 3 int max_frames = 0, first_frames = 0; 4 int fsize, frames; 5 const uint8_t *buf0 = p->buf; 6 const uint8_t *buf2; 7 const uint8_t *buf; 8 const uint8_t *end = buf0 + p->buf_size - 7; 9 10 buf = buf0; 11 12 for (; buf < end; buf = buf2 + 1) { 13 buf2 = buf; 14 15 for (frames = 0; buf2 < end; frames++) { 16 uint32_t header = AV_RB16(buf2); 17 if ((header & 0xFFF6) != 0xFFF0) { 18 if (buf != buf0) { 19 // Found something that isn't an ADTS header, starting 20 // from a position other than the start of the buffer. 21 // Discard the count we've accumulated so far since it 22 // probably was a false positive. 23 frames = 0; 24 } 25 break; 26 } 27 fsize = (AV_RB32(buf2 + 3) >> 13) & 0x1FFF; 28 if (fsize < 7) 29 break; 30 fsize = FFMIN(fsize, end - buf2); 31 buf2 += fsize; 32 } 33 max_frames = FFMAX(max_frames, frames); 34 if (buf == buf0) 35 first_frames = frames; 36 } 37 38 if (first_frames >= 3) 39 return AVPROBE_SCORE_EXTENSION + 1; 40 else if (max_frames > 100) 41 return AVPROBE_SCORE_EXTENSION; 42 else if (max_frames >= 3) 43 return AVPROBE_SCORE_EXTENSION / 2; 44 else if (max_frames >= 1) 45 return 1; 46 else 47 return 0; 48 }
附上ATDS流的一个检测网址:http://www.p23.nl/projects/aac-header/