-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathadts_parser.cc
203 lines (166 loc) · 5.37 KB
/
adts_parser.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//
// Created by wlanjie on 2018/3/15.
//
#include "adts_parser.h"
namespace mp4 {
#define ADTS_HEADER_SIZE 7
#define ADTS_SYNC_MASK 0xFFF6 /* 12 sync bits plus 2 layer bits */
#define ADTS_SYNC_PATTERN 0xFFF0 /* 12 sync bits=1 layer=0 */
const UI32
AdtsSamplingFrequencyTable[16] =
{
96000,
88200,
64000,
48000,
44100,
32000,
24000,
22050,
16000,
12000,
11025,
8000,
7350,
0, /* Reserved */
0, /* Reserved */
0 /* Escape code */
};
AdtsHeader::AdtsHeader(const UI08 *bytes) {
id = ( bytes[1] & 0x08) >> 3;
protectionAbsent = bytes[1] & 0x01;
profileObjectType = ( bytes[2] & 0xC0) >> 6;
samplingFrequencyIndex = ( bytes[2] & 0x3C) >> 2;
channelConfiguration = ((bytes[2] & 0x01) << 2) | ((bytes[3] & 0xC0) >> 6);
frameLength = ((unsigned int)(bytes[3] & 0x03) << 11) | ((unsigned int)(bytes[4]) << 3) | ((unsigned int)(bytes[5] & 0xE0) >> 5);
rawDataBlocks = bytes[6] & 0x03;
}
Result AdtsHeader::check() {
if (samplingFrequencyIndex >= 0xD) {
return FAILURE;
}
if (id == 1 && profileObjectType == 3) {
return FAILURE;
}
return SUCCESS;
}
bool AdtsHeader::matchFixed(unsigned char *a, unsigned char *b) {
if (a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && (a[3] & 0xF0) == (b[3] & 0xF0)) {
return true;
}
return false;
}
AdtsParser::AdtsParser() : frameCount(0) {
}
AdtsParser::~AdtsParser() {
}
Result AdtsParser::reset() {
frameCount = 0;
return SUCCESS;
}
Result AdtsParser::feed(const UI08 *buffer, Size *bufferSize, Flags flags) {
Size freeSpace;
bits.m_Flags = flags;
if (buffer == nullptr || bufferSize == nullptr || *bufferSize == 0) {
return SUCCESS;
}
freeSpace = bits.getBytesFree();
if (*bufferSize > freeSpace) {
*bufferSize = freeSpace;
}
if (*bufferSize == 0) {
return SUCCESS;
}
return bits.writeBytes(buffer, *bufferSize);
}
Result AdtsParser::findFrame(AacFrame &frame) {
unsigned int available;
unsigned char raw_header[ADTS_HEADER_SIZE];
Result result;
/* align to the start of the next byte */
bits.byteAlign();
/* find a frame header */
result = findHeader(raw_header);
if (FAILED(result)) return result;
/* parse the header */
AdtsHeader adts_header(raw_header);
/* check the header */
result = adts_header.check();
if (FAILED(result)) {
return ERROR_CORRUPTED_BITSTREAM;
}
/* check if we have enough data to peek at the next header */
available = bits.getBytesAvailable();
if (available >= adts_header.frameLength + ADTS_HEADER_SIZE) {
// enough to peek at the header of the next frame
unsigned char peek_raw_header[ADTS_HEADER_SIZE];
bits.skipBytes(adts_header.frameLength);
bits.peekBytes(peek_raw_header, ADTS_HEADER_SIZE);
bits.skipBytes(-((int) adts_header.frameLength));
/* check the header */
AdtsHeader peek_adts_header(peek_raw_header);
result = peek_adts_header.check();
if (FAILED(result)) {
return ERROR_CORRUPTED_BITSTREAM;
}
/* check that the fixed part of this header is the same as the */
/* fixed part of the previous header */
if (!AdtsHeader::matchFixed(peek_raw_header, raw_header)) {
return ERROR_CORRUPTED_BITSTREAM;
}
} else if (available < adts_header.frameLength || (bits.m_Flags & BITSTREAM_FLAG_EOS) == 0) {
// not enough for a frame, or not at the end (in which case we'll want to peek at the next header)
return ERROR_NOT_ENOUGH_DATA;
}
bits.skipBytes(ADTS_HEADER_SIZE);
/* fill in the frame info */
frame.info.standard = (adts_header.id == 1 ? AAC_STANDARD_MPEG2 : AAC_STANDARD_MPEG4);
switch (adts_header.profileObjectType) {
case 0:
frame.info.profile = AAC_PROFILE_MAIN;
break;
case 1:
frame.info.profile = AAC_PROFILE_LC;
break;
case 2:
frame.info.profile = AAC_PROFILE_SSR;
break;
case 3:
frame.info.profile = AAC_PROFILE_LTP;
}
frame.info.frameLength = adts_header.frameLength - ADTS_HEADER_SIZE;
frame.info.channelConfiguration = adts_header.channelConfiguration;
frame.info.samplingFrequencyIndex = adts_header.samplingFrequencyIndex;
frame.info.samplingFrequency = AdtsSamplingFrequencyTable[adts_header.samplingFrequencyIndex];
/* skip crc if present */
if (adts_header.protectionAbsent == 0) {
bits.skipBits(16);
}
/* set the frame source */
frame.source = &bits;
return SUCCESS;
}
Result AdtsParser::skip(Size size) {
return 0;
}
Size AdtsParser::getBytesFree() {
return bits.getBytesFree();
}
Size AdtsParser::getBytesAvailable() {
return bits.getBytesAvailable();
}
Result AdtsParser::findHeader(UI08 *header) {
Size available = bits.getBytesAvailable();
while (available-- >= ADTS_HEADER_SIZE) {
bits.peekBytes(header, 2);
if ((((header[0] << 8) | header[1]) & ADTS_SYNC_MASK) == ADTS_SYNC_PATTERN) {
/* found a sync pattern, read the entire the header */
bits.peekBytes(header, ADTS_HEADER_SIZE);
return SUCCESS;
} else {
bits.readByte(); // skip
}
}
return ERROR_NOT_ENOUGH_DATA;
}
}