Debian Codegraph @

plugins/wdx/audioinfo/src/atl/AACfile.pas

raw
1 { *************************************************************************** }
2 { }
3 { Audio Tools Library }
4 { Class TAACfile - for manipulating with AAC file information }
5 { }
6 { http://mac.sourceforge.net/atl/ }
7 { e-mail: [email protected] }
8 { }
9 { Copyright (c) 2000-2002 by Jurgen Faul }
10 { Copyright (c) 2003-2005 by The MAC Team }
11 { }
12 { Version 1.2 (April 2005) by Gambit }
13 { - updated to unicode file access }
14 { }
15 { Version 1.1 (April 2004) by Gambit }
16 { - Added Ratio and TotalFrames property }
17 { }
18 { Version 1.01 (September 2003) by Gambit }
19 { - fixed the bitrate/duration bug (scans the whole file) }
20 { }
21 { Version 1.0 (2 October 2002) }
22 { - Support for AAC files with ADIF or ADTS header }
23 { - File info: file size, type, channels, sample rate, bit rate, duration }
24 { - Class TID3v1: reading & writing support for ID3v1 tags }
25 { - Class TID3v2: reading & writing support for ID3v2 tags }
26 { }
27 { This library is free software; you can redistribute it and/or }
28 { modify it under the terms of the GNU Lesser General Public }
29 { License as published by the Free Software Foundation; either }
30 { version 2.1 of the License, or (at your option) any later version. }
31 { }
32 { This library is distributed in the hope that it will be useful, }
33 { but WITHOUT ANY WARRANTY; without even the implied warranty of }
34 { MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU }
35 { Lesser General Public License for more details. }
36 { }
37 { You should have received a copy of the GNU Lesser General Public }
38 { License along with this library; if not, write to the Free Software }
39 { Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA }
40 { }
41 { *************************************************************************** }
42
43 unit AACfile;
44
45 interface
46
47 uses
48 Classes, SysUtils, ID3v1, ID3v2, APEtag, DCClassesUtf8;
49
50 const
51 { Header type codes }
52 AAC_HEADER_TYPE_UNKNOWN = 0; { Unknown }
53 AAC_HEADER_TYPE_ADIF = 1; { ADIF }
54 AAC_HEADER_TYPE_ADTS = 2; { ADTS }
55
56 { Header type names }
57 AAC_HEADER_TYPE: array [0..2] of string =
58 ('Unknown', 'ADIF', 'ADTS');
59
60 { MPEG version codes }
61 AAC_MPEG_VERSION_UNKNOWN = 0; { Unknown }
62 AAC_MPEG_VERSION_2 = 1; { MPEG-2 }
63 AAC_MPEG_VERSION_4 = 2; { MPEG-4 }
64
65 { MPEG version names }
66 AAC_MPEG_VERSION: array [0..2] of string =
67 ('Unknown', 'MPEG-2', 'MPEG-4');
68
69 { Profile codes }
70 AAC_PROFILE_UNKNOWN = 0; { Unknown }
71 AAC_PROFILE_MAIN = 1; { Main }
72 AAC_PROFILE_LC = 2; { LC }
73 AAC_PROFILE_SSR = 3; { SSR }
74 AAC_PROFILE_LTP = 4; { LTP }
75
76 { Profile names }
77 AAC_PROFILE: array [0..4] of string =
78 ('Unknown', 'AAC Main', 'AAC LC', 'AAC SSR', 'AAC LTP');
79
80 { Bit rate type codes }
81 AAC_BITRATE_TYPE_UNKNOWN = 0; { Unknown }
82 AAC_BITRATE_TYPE_CBR = 1; { CBR }
83 AAC_BITRATE_TYPE_VBR = 2; { VBR }
84
85 { Bit rate type names }
86 AAC_BITRATE_TYPE: array [0..2] of string =
87 ('Unknown', 'CBR', 'VBR');
88
89 type
90 { Class TAACfile }
91 TAACfile = class(TObject)
92 private
93 { Private declarations }
94 FFileSize: Integer;
95 FHeaderTypeID: Byte;
96 FMPEGVersionID: Byte;
97 FProfileID: Byte;
98 FChannels: Byte;
99 FSampleRate: Integer;
100 FBitRate: Integer;
101 FBitRateTypeID: Byte;
102 FID3v1: TID3v1;
103 FID3v2: TID3v2;
104 FAPEtag: TAPEtag;
105 FTotalFrames: Integer;
106 procedure FResetData;
107 function FGetHeaderType: string;
108 function FGetMPEGVersion: string;
109 function FGetProfile: string;
110 function FGetBitRateType: string;
111 function FGetDuration: Double;
112 function FIsValid: Boolean;
113 function FRecognizeHeaderType(const Source: TFileStreamEx): Byte;
114 procedure FReadADIF(const Source: TFileStreamEx);
115 procedure FReadADTS(const Source: TFileStreamEx);
116 function FGetRatio: Double;
117 public
118 { Public declarations }
119 constructor Create; { Create object }
120 destructor Destroy; override; { Destroy object }
121 function ReadFromFile(const FileName: String): Boolean;{ Load header }
122 property FileSize: Integer read FFileSize; { File size (bytes) }
123 property HeaderTypeID: Byte read FHeaderTypeID; { Header type code }
124 property HeaderType: string read FGetHeaderType; { Header type name }
125 property MPEGVersionID: Byte read FMPEGVersionID; { MPEG version code }
126 property MPEGVersion: string read FGetMPEGVersion; { MPEG version name }
127 property ProfileID: Byte read FProfileID; { Profile code }
128 property Profile: string read FGetProfile; { Profile name }
129 property Channels: Byte read FChannels; { Number of channels }
130 property SampleRate: Integer read FSampleRate; { Sample rate (hz) }
131 property BitRate: Integer read FBitRate; { Bit rate (bit/s) }
132 property BitRateTypeID: Byte read FBitRateTypeID; { Bit rate type code }
133 property BitRateType: string read FGetBitRateType; { Bit rate type name }
134 property Duration: Double read FGetDuration; { Duration (seconds) }
135 property Valid: Boolean read FIsValid; { True if data valid }
136 property ID3v1: TID3v1 read FID3v1; { ID3v1 tag data }
137 property ID3v2: TID3v2 read FID3v2; { ID3v2 tag data }
138 property APEtag: TAPEtag read FAPEtag; { APE tag data }
139 property Ratio: Double read FGetRatio; { Compression ratio (%) }
140 property TotalFrames: Integer read FTotalFrames;{ Total number of frames }
141 end;
142
143 implementation
144
145 const
146 { Sample rate values }
147 SAMPLE_RATE: array [0..15] of Integer =
148 (96000, 88200, 64000, 48000, 44100, 32000,
149 24000, 22050, 16000, 12000, 11025, 8000, 0, 0, 0, 0);
150
151 { ********************* Auxiliary functions & procedures ******************** }
152
153 function ReadBits(Source: TFileStreamEx; Position, Count: Integer): Integer;
154 var
155 Buffer: array [1..4] of Byte;
156 begin
157 { Read a number of bits from file at the given position }
158 Source.Seek(Position div 8, soFromBeginning);
159 Source.Read(Buffer, SizeOf(Buffer));
160 Result :=
161 Buffer[1] * $1000000 +
162 Buffer[2] * $10000 +
163 Buffer[3] * $100 +
164 Buffer[4];
165 Result := (Result shl (Position mod 8)) shr (32 - Count);
166 end;
167
168 { ********************** Private functions & procedures ********************* }
169
170 procedure TAACfile.FResetData;
171 begin
172 { Reset all variables }
173 FFileSize := 0;
174 FHeaderTypeID := AAC_HEADER_TYPE_UNKNOWN;
175 FMPEGVersionID := AAC_MPEG_VERSION_UNKNOWN;
176 FProfileID := AAC_PROFILE_UNKNOWN;
177 FChannels := 0;
178 FSampleRate := 0;
179 FBitRate := 0;
180 FBitRateTypeID := AAC_BITRATE_TYPE_UNKNOWN;
181 FID3v1.ResetData;
182 FID3v2.ResetData;
183 FAPEtag.ResetData;
184 FTotalFrames := 0;
185 end;
186
187 { --------------------------------------------------------------------------- }
188
189 function TAACfile.FGetHeaderType: string;
190 begin
191 { Get header type name }
192 Result := AAC_HEADER_TYPE[FHeaderTypeID];
193 end;
194
195 { --------------------------------------------------------------------------- }
196
197 function TAACfile.FGetMPEGVersion: string;
198 begin
199 { Get MPEG version name }
200 Result := AAC_MPEG_VERSION[FMPEGVersionID];
201 end;
202
203 { --------------------------------------------------------------------------- }
204
205 function TAACfile.FGetProfile: string;
206 begin
207 { Get profile name }
208 Result := AAC_PROFILE[FProfileID];
209 end;
210
211 { --------------------------------------------------------------------------- }
212
213 function TAACfile.FGetBitRateType: string;
214 begin
215 { Get bit rate type name }
216 Result := AAC_BITRATE_TYPE[FBitRateTypeID];
217 end;
218
219 { --------------------------------------------------------------------------- }
220
221 function TAACfile.FGetDuration: Double;
222 begin
223 { Calculate duration time }
224 if FBitRate = 0 then Result := 0
225 else Result := 8 * (FFileSize - ID3v2.Size) / FBitRate;
226 end;
227
228 { --------------------------------------------------------------------------- }
229
230 function TAACfile.FIsValid: Boolean;
231 begin
232 { Check for file correctness }
233 Result := (FHeaderTypeID <> AAC_HEADER_TYPE_UNKNOWN) and
234 (FChannels > 0) and (FSampleRate > 0) and (FBitRate > 0);
235 end;
236
237 { --------------------------------------------------------------------------- }
238
239 function TAACfile.FRecognizeHeaderType(const Source: TFileStreamEx): Byte;
240 var
241 Header: array [1..4] of Char;
242 begin
243 { Get header type of the file }
244 Result := AAC_HEADER_TYPE_UNKNOWN;
245 Source.Seek(FID3v2.Size, soFromBeginning);
246 Source.Read(Header, SizeOf(Header));
247 if Header[1] + Header[2] + Header[3] + Header[4] = 'ADIF' then
248 Result := AAC_HEADER_TYPE_ADIF
249 else if (Byte(Header[1]) = $FF) and (Byte(Header[1]) and $F0 = $F0) then
250 Result := AAC_HEADER_TYPE_ADTS;
251 end;
252
253 { --------------------------------------------------------------------------- }
254
255 procedure TAACfile.FReadADIF(const Source: TFileStreamEx);
256 var
257 Position: Integer;
258 begin
259 { Read ADIF header data }
260 Position := FID3v2.Size * 8 + 32;
261 if ReadBits(Source, Position, 1) = 0 then Inc(Position, 3)
262 else Inc(Position, 75);
263 if ReadBits(Source, Position, 1) = 0 then
264 FBitRateTypeID := AAC_BITRATE_TYPE_CBR
265 else
266 FBitRateTypeID := AAC_BITRATE_TYPE_VBR;
267 Inc(Position, 1);
268 FBitRate := ReadBits(Source, Position, 23);
269 if FBitRateTypeID = AAC_BITRATE_TYPE_CBR then Inc(Position, 51)
270 else Inc(Position, 31);
271 FMPEGVersionID := AAC_MPEG_VERSION_4;
272 FProfileID := ReadBits(Source, Position, 2) + 1;
273 Inc(Position, 2);
274 FSampleRate := SAMPLE_RATE[ReadBits(Source, Position, 4)];
275 Inc(Position, 4);
276 Inc(FChannels, ReadBits(Source, Position, 4));
277 Inc(Position, 4);
278 Inc(FChannels, ReadBits(Source, Position, 4));
279 Inc(Position, 4);
280 Inc(FChannels, ReadBits(Source, Position, 4));
281 Inc(Position, 4);
282 Inc(FChannels, ReadBits(Source, Position, 2));
283 end;
284
285 { --------------------------------------------------------------------------- }
286
287 procedure TAACfile.FReadADTS(const Source: TFileStreamEx);
288 var
289 Frames, TotalSize, Position: Integer;
290 begin
291 { Read ADTS header data }
292 Frames := 0;
293 TotalSize := 0;
294 repeat
295 Inc(Frames);
296 Position := (FID3v2.Size + TotalSize) * 8;
297 if ReadBits(Source, Position, 12) <> $FFF then break;
298 Inc(Position, 12);
299 if ReadBits(Source, Position, 1) = 0 then
300 FMPEGVersionID := AAC_MPEG_VERSION_4
301 else
302 FMPEGVersionID := AAC_MPEG_VERSION_2;
303 Inc(Position, 4);
304 FProfileID := ReadBits(Source, Position, 2) + 1;
305 Inc(Position, 2);
306 FSampleRate := SAMPLE_RATE[ReadBits(Source, Position, 4)];
307 Inc(Position, 5);
308 FChannels := ReadBits(Source, Position, 3);
309 if FMPEGVersionID = AAC_MPEG_VERSION_4 then Inc(Position, 9)
310 else Inc(Position, 7);
311 Inc(TotalSize, ReadBits(Source, Position, 13));
312 Inc(Position, 13);
313 if ReadBits(Source, Position, 11) = $7FF then
314 FBitRateTypeID := AAC_BITRATE_TYPE_VBR
315 else
316 FBitRateTypeID := AAC_BITRATE_TYPE_CBR;
317 if FBitRateTypeID = AAC_BITRATE_TYPE_CBR then break;
318 // more accurate
319 //until (Frames = 1000) or (Source.Size <= FID3v2.Size + TotalSize);
320 until (Source.Size <= FID3v2.Size + TotalSize);
321 FTotalFrames := Frames;
322 FBitRate := Round(8 * TotalSize / 1024 / Frames * FSampleRate);
323 end;
324
325 { ********************** Public functions & procedures ********************** }
326
327 constructor TAACfile.Create;
328 begin
329 { Create object }
330 FID3v1 := TID3v1.Create;
331 FID3v2 := TID3v2.Create;
332 FAPEtag := TAPEtag.Create;
333 FResetData;
334 inherited;
335 end;
336
337 { --------------------------------------------------------------------------- }
338
339 destructor TAACfile.Destroy;
340 begin
341 { Destroy object }
342 FID3v1.Free;
343 FID3v2.Free;
344 FAPEtag.Free;
345 inherited;
346 end;
347
348 { --------------------------------------------------------------------------- }
349
350 function TAACfile.ReadFromFile(const FileName: String): Boolean;
351 var
352 SourceFile: TFileStreamEx;
353 begin
354 { Read data from file }
355 Result := false;
356 FResetData;
357 { At first search for tags, then try to recognize header type }
358 if (FID3v2.ReadFromFile(FileName)) and (FID3v1.ReadFromFile(FileName)) and (FAPEtag.ReadFromFile(FileName)) then
359 try
360 SourceFile := TFileStreamEx.Create(FileName, fmOpenRead or fmShareDenyWrite);
361 FFileSize := SourceFile.Size;
362 FHeaderTypeID := FRecognizeHeaderType(SourceFile);
363 { Read header data }
364 if FHeaderTypeID = AAC_HEADER_TYPE_ADIF then FReadADIF(SourceFile);
365 if FHeaderTypeID = AAC_HEADER_TYPE_ADTS then FReadADTS(SourceFile);
366 SourceFile.Free;
367 Result := true;
368 except
369 end;
370 end;
371
372 { --------------------------------------------------------------------------- }
373
374 function TAACfile.FGetRatio: Double;
375 begin
376 { Get compression ratio }
377 if FIsValid then
378 Result := FFileSize / ((FTotalFrames * 1024) * (FChannels * 16 / 8) + 44) * 100
379 else
380 Result := 0;
381 end;
382
383 { --------------------------------------------------------------------------- }
384
385 end.