-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathADTS.bt
240 lines (213 loc) · 8.22 KB
/
ADTS.bt
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//------------------------------------------------
//--- 010 Editor v2.0 Binary Template
//
// File: ADTS.bt
// Author: zhoubo
// Version: 0.1
// Purpose: Parse AAC ADTS audio files.
// Category: Audio
// File Mask: *.aac
// ID Bytes: FF F? ?? ?? ?? ?? ?? //ADTS(Audio Data Transport Stream) Header, 7 or 9 bytes
// History:
// 0.1 Initial release.
// not ADIF:Audio Data Interchange Format
//
// More information available at:
// https://en.wikipedia.org/wiki/AAC
// https://juejin.cn/post/7032170229732442148
//------------------------------------------------
BigEndian();
// Forward declarations:
struct ADTS_HEADER;
struct ADTS_FRAME;
// Constants:
local int haveCRC = false; // Record whether we have CRC chunk yet
local int64 last_frame_length = 0;
// Enums:
// Funtions:
// https://blog.csdn.net/C1033177205/article/details/107140640/
// https://www.jianshu.com/p/71d92c495ff9
string GetMPEGVersionComment(USHORT &id)
{
local string comment = "";
switch (id)
{
case 0: comment = "MPEG-4"; break;
case 1: comment = "MPEG-2"; break;
}
return comment;
}
string GetProtectionAbsentComment(USHORT &protection_absent)
{
local string comment = "";
switch (protection_absent)
{
case 0: comment = "ADTS has 9 bytes with CRC"; break;
case 1: comment = "ADTS has 7 bytes without CRC"; break;
}
return comment;
}
string GetProfileComment(INT64 &profile)
{
local string comment = "";
switch (profile)
{
case 0x00: comment = "AAC Main"; break;
case 0x01: comment = "AAC LC(Low Complexity)"; break;
case 0x10: comment = "AAC SSR(Scalable Sample Rate)"; break;
case 0x11: comment = "AAC LTP(Long Term Prediction)"; break;
}
return comment;
}
string GetSamplingFrequencyIndexComment(INT64 &sampling_frequency_index)
{
local string comment = "";
switch (sampling_frequency_index)
{
case 0x0: comment = "SampleRate 96000"; break;
case 0x1: comment = "SampleRate 88200"; break;
case 0x2: comment = "SampleRate 64000"; break;
case 0x3: comment = "SampleRate 48000"; break;
case 0x4: comment = "SampleRate 44100"; break;
case 0x5: comment = "SampleRate 32000"; break;
case 0x6: comment = "SampleRate 24000"; break;
case 0x7: comment = "SampleRate 22050"; break;
case 0x8: comment = "SampleRate 16000"; break;
case 0x9: comment = "SampleRate 12000"; break;
case 0xa: comment = "SampleRate 11025"; break;
case 0xb: comment = "SampleRate 8000"; break;
case 0xc: comment = "SampleRate 7350"; break;
case 0xd: comment = "SampleRate reserved"; break;
case 0xe: comment = "SampleRate reserved"; break;
case 0xf: comment = "SampleRate escape value"; break;
}
return comment;
}
string GetChannelComment(INT64 &channel_configuration)
{
//0: Defined in AOT Specifc Config
//1: 1 channel: front-center
//2: 2 channels: front-left, front-right
//3: 3 channels: front-center, front-left, front-right
//4: 4 channels: front-center, front-left, front-right, back-center
//5: 5 channels: front-center, front-left, front-right, back-left, back-right
//6: 6 channels: front-center, front-left, front-right, back-left, back-right, LFE-channel
//7: 8 channels: front-center, front-left, front-right, side-left, side-right, back-left, back-right, LFE-channel
local string comment = "";
switch (channel_configuration)
{
case 0: comment = "Channel escape value"; break;
case 1: comment = "Channel 1"; break;
case 2: comment = "Channel 2"; break;
case 3: comment = "Channel 3"; break;
case 4: comment = "Channel 4"; break;
case 5: comment = "Channel 5"; break;
case 6: comment = "Channel 5.1"; break;
case 7: comment = "Channel 7.1"; break;
}
return comment;
}
string GetBitrateComment(INT64 &adts_buffer_fullness)
{
local string comment = "";
switch (adts_buffer_fullness)
{
case 0x7FF: comment = "VBR"; break;
default: comment = "rate..??"; break;
}
return comment;
}
string GetRawDataBlockComment(INT64 &number_of_raw_data_blocks_in_frame)
{
local string comment = "";
switch (number_of_raw_data_blocks_in_frame)
{
case 0x0: comment = "has 1 AAC data block"; break;
default: comment = "data block number + 1"; break;
}
return comment;
}
//-----------------------------------
// Define structures used in AAC files
//
// [1.adts_fixed_header information]
// syncword: 占12bits。同步头,表示一个ADTS帧的开始,总是0xFFF。正是因为它的存在,才支持解码任意帧
// ID: 占1bit。MPEG的版本,0为MPGE-4,1为MPGE-2
// layer: 占2bits。总是”00”
// protection_absent: 占1bit。=0时,ADTS Header长度占9字节;=1时,ADTS Header占7字节
// profile: 占2bit。使用哪个级别的AAC,值00、01、10分别对应Main、LC、SSR
// sampling_frequency_index: 占4bits。表示使用的采样率下标,通过这个下标在Sampling Frequencies[ ]数组中查找得知采样率的值
// private_bit: 占1bit。
// channel_configuration: 占3bit。表示声道数,如1-单声道,2-立体声
// original_copy: 占1bit。
// home: 占1bit。
//
// [2.adts_variable_header information]
// copyright_identification_bit: 占1bit。
// copyright_identification_start: 占1bit。
// aac_frame_length: 占13bits。表示一个ADTS帧的长度,即ADTS头(7或9字节)+sizeof(AAC Frame);
// adts_buffer_fullness: 占11bits。值0x7FF,说明是码率可变的码流
// number_of_raw_data_blocks_in_frame: 占2bits。表示ADTS帧中有(number_of_raw_data_blocks_In_frame+1)个AAC原始帧
//
// [3.CRC information]
// crc_check: 占16bits。当protection_absent=0时
typedef struct
{
// ADTS_FIXED_HEADER
USHORT syncword : 12;
USHORT id : 1 <comment = GetMPEGVersionComment>;
USHORT layer : 2;
USHORT protection_absent : 1 <comment = GetProtectionAbsentComment>;
INT64 profile : 2 <comment = GetProfileComment>;
INT64 sampling_frequency_index : 4 <comment = GetSamplingFrequencyIndexComment>;
INT64 private_bit : 1;
INT64 channel_configuration : 3 <comment = GetChannelComment>;
INT64 original_copy : 1;
INT64 home : 1;
// ADTS_VARIABLE_HEADER
INT64 copyright_identification_bit : 1;
INT64 copyright_identification_start : 1;
INT64 aac_frame_length : 13;
INT64 adts_buffer_fullness : 11 <comment = GetBitrateComment>;
INT64 number_of_raw_data_blocks_in_frame : 2 <comment = GetRawDataBlockComment>;
// ADTS_CRC_HEADER
if (protection_absent == 0) // CRC if protection_absent = 0
{
//Printf( "Header has CRC.\n" );
haveCRC = true;
INT64 crc : 16;
//INT64 data0 : 8;
} else {
//Printf( "Header has no CRC.\n" );
//INT64 data0 : 8;
//INT64 data1 : 8;
//INT64 data2 : 8;
}
}ADTS_HEADER;
typedef struct
{
SetBackColor( cLtBlue );
ADTS_HEADER header;
if (true != haveCRC)
{
FSeek(last_frame_length + 7);
SetBackColor( cLtPurple );
byte data[header.aac_frame_length - 7];
} else {
FSeek(last_frame_length + 9);
SetBackColor( cLtPurple );
byte data[header.aac_frame_length - 9];
}
}ADTS_FRAME;
//---------------------------------------------
while (!FEof())
{
ADTS_FRAME adtsFrame <name = "ADTS Frame">;
if(adtsFrame.header.syncword != 0xFFF)
{
Warning( "File is not a valid ADTS AAC file. stop!!!" );
return -1;
}
last_frame_length = last_frame_length + adtsFrame.header.aac_frame_length;
FSeek(last_frame_length);
}