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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
|
{WMA file format}
unit fmt_WMA;
{$include compilers.inc}
interface
uses wat_api;
function ReadWMA(var Info:tSongInfo):boolean; cdecl;
implementation
uses windows,common,io,srv_format,utils;
const
ASF_Header_Object :tGUID='{75B22630-668E-11CF-A6D9-00AA0062CE6C}';
ASF_Header_Extension_Object :tGUID='{5FBF03B5-A92E-11CF-8EE3-00C00C205365}';
ASF_Content_Description_Object :tGUID='{75B22633-668E-11CF-A6D9-00AA0062CE6C}';
ASF_Extended_Content_Description_Object:tGUID='{D2D0A440-E307-11D2-97F0-00A0C95EA850}';
ASF_File_Properties_Object :tGUID='{8CABDCA1-A947-11CF-8EE4-00C00C205365}';
ASF_Stream_Properties_Object :tGUID='{B7DC0791-A9B7-11CF-8EE6-00C00C205365}';
ASF_Metadata_Library_Object :tGUID='{44231C94-9498-49D1-A141-1D134E457054}';
ASF_Audio_Media :tGUID='{F8699E40-5B4D-11CF-A8FD-00805F5C442B}';
ASF_Video_Media :tGUID='{BC19EFC0-5B4D-11CF-A8FD-00805F5C442B}';
type
tSize=Int64;
function CompareGUID(const guid1,guid2:tGUID):boolean;
var
i:integer;
p1,p2:PAnsiChar;
begin
p1:=PAnsiChar(@guid1);
p2:=PAnsiChar(@guid2);
for i:=0 to 15 do
begin
if p1^<>p2^ then
begin
result:=false;
exit;
end;
inc(p1);
inc(p2);
end;
result:=true;
end;
function ReadGUID(var buf:PAnsiChar; var guid:pGUID):dword;
var
size:tSize;
begin
guid:=pointer(buf);
inc(buf,SizeOf(tGUID));
move(buf^,size,SizeOf(size));
inc(buf,SizeOf(size));
result:=size-SizeOf(tGUID)-SizeOf(size);
end;
procedure ReadWMATagStr(var dst:pWideChar;ptr:PAnsiChar;alen:word);
begin
if pword(ptr)^<>0 then
begin
mGetMem(dst,alen);
move(pWideChar(ptr{+2})^,dst^,alen);
end;
end;
function ReadWMATagStr1(var dst:pWideChar;var ptr:PAnsiChar;value:boolean=true):integer;
var
len,typ:word;
begin
if value then
begin
typ:=pword(ptr)^;
inc(ptr,2); //value type
end
else
typ:=0;
len:=pword(ptr)^;
result:=-1;
dst:=nil;
if len<>0 then
begin
if typ=0 then
begin
mGetMem(dst,len);
move(PAnsiChar(ptr+2)^,PAnsiChar(dst)^,len);
end
else
begin
result:=pword(ptr+2)^;
if typ<5 then
result:=pword(ptr+4)^*$10000+result;
end;
end;
inc(ptr,len+2);
end;
procedure ProcessPicture(ptr:PAnsiChar;var Info:tSongInfo);
var
extw:int64;
aSize:dword;
begin
if Info.cover<>nil then exit;
case ptr^ of
#0,#3,#4,#6: ;
else
exit;
end;
inc(ptr);
aSize:=pdword(ptr)^; inc(ptr,4);
extw:=GetImageTypeW(nil,pWideChar(ptr));
while pWideChar(ptr)^<>#0 do inc(ptr,2); inc(ptr,2); // mime
while pWideChar(ptr)^<>#0 do inc(ptr,2); inc(ptr,2); // descr
if extw=0 then
extw:=GetImageTypeW(pByte(ptr));
Info.cover:=SaveTemporaryW(ptr,aSize,pWideChar(@extw));
end;
procedure ReadHdrExtended(ptr:PAnsiChar;size:dword;var Info:tSongInfo);
var
buf:PAnsiChar;
ls:pWideChar;
cnt,tmp:integer;
tmpguid:pGUID;
lsize:dword;
begin
inc(ptr,SizeOf(tGUID)+2);
size:=pdword(ptr)^; inc(ptr,4);
while size>0 do
begin
if Info.cover<>nil then break;
lsize:=ReadGUID(ptr,tmpguid);
dec(size,lsize+SizeOf(tGUID)+SizeOf(tSize));
if CompareGUID(tmpguid^,ASF_Metadata_Library_Object) then
begin
buf:=ptr;
cnt:=pdword(buf)^; inc(buf,2);
while cnt>0 do
begin
inc(buf,4); // lang & stream
{tmp:=pword (buf)^;} inc(buf,2); // namelen
{tmp:=pword (buf)^;} inc(buf,2); // datatype
tmp:=pdword(buf)^; inc(buf,4); // datalen
ls:=PWideChar(buf);
while pWideChar(buf)^<>#0 do inc(buf,2); inc(buf,2);
if lstrcmpiw(ls,'WM/Picture')=0 then
begin
ProcessPicture(buf,Info);
inc(buf,tmp);
end;
dec(cnt);
end;
end;
inc(ptr,lsize);
end;
end;
procedure ReadExtended(ptr:PAnsiChar;size:dword;var Info:tSongInfo);
var
ls,ls1,ls2:pWideChar;
cnt,tmp:integer;
begin
cnt:=pword(ptr)^; inc(ptr,2);
while cnt>0 do
begin
dec(cnt);
ReadWMATagStr1(ls,ptr,false);
if lstrcmpiw(ls,'WM/AlbumTitle')=0 then
ReadWMATagStr1(Info.album,ptr)
else if (Info.lyric=nil) and (lstrcmpiw(ls,'WM/Lyrics')=0) then
ReadWMATagStr1(Info.lyric,ptr)
else if (Info.lyric=nil) and (lstrcmpiw(ls,'WM/Lyrics_Synchronised')=0) then
begin
inc(ptr,2+2);
inc(ptr); // timestamp type
if ptr^=#1 then // lyric
begin
inc(ptr);
tmp:=pdword(ptr)^; inc(ptr,4);
mGetMem(ls2,tmp);
Info.lyric:=ls2;
ls1:=pWideChar(ptr);
inc(ptr,tmp);
while ls1^<>#0 do // description
begin
inc(ls1);
dec(tmp,SizeOf(WideChar));
end;
inc(ls1);
dec(tmp,SizeOf(WideChar));
while tmp>0 do
begin
if PAnsiChar(ls1)^=#$0A then
begin
inc(PAnsiChar(ls1));
ls2^:=#$0A;
dec(tmp);
inc(ls2);
end;
while ls1^<>#0 do
begin
ls2^:=ls1^; inc(ls2); inc(ls1);
dec(tmp,SizeOf(WideChar));
end;
inc(ls1,1+2); // terminator + timestamp
dec(tmp,SizeOf(WideChar)+4);
end;
ls2^:=#0;
// ptr:=PAnsiChar(ls1);
end
end
else if lstrcmpiw(ls,'WM/Genre')=0 then
ReadWMATagStr1(Info.genre,ptr)
else if lstrcmpiw(ls,'WM/Year')=0 then
begin
tmp:=ReadWMATagStr1(Info.year,ptr);
if tmp<>-1 then
IntToStr(Info.year,tmp);
end
else if lstrcmpiw(ls,'WM/Track')=0 then
begin
tmp:=ReadWMATagStr1(ls1,ptr);
if tmp=-1 then
begin
Info.track:=StrToInt(ls1)+1;
mFreeMem(ls1);
end
else
Info.track:=tmp;
end
else if lstrcmpiw(ls,'WM/TrackNumber')=0 then
begin
tmp:=ReadWMATagStr1(ls1,ptr);
if tmp=-1 then
begin
Info.track:=StrToInt(ls1);
mFreeMem(ls1);
end
else
Info.track:=tmp;
end
else if lstrcmpiw(ls,'WM/Picture')=0 then
begin
inc(ptr,2); // data type
tmp:=pword(ptr)^; inc(ptr,2);
ProcessPicture(ptr,Info);
inc(ptr,tmp);
end
else
inc(ptr,4+pword(ptr+2)^);
mFreeMem(ls);
end;
end;
procedure ReadFileProp(ptr:PAnsiChar;var Info:tSongInfo);
type
pFileProp = ^tFileProp;
tFileProp = packed record
FileGUID :tGUID;
FileSize :tSize;
Creation :tSize;
Packets :tSize;
Play :tSize;
Send :tSize;
PreRoll :tSize;
Flags :dword;
minpacket :dword;
maxpacket :dword;
maxbitrate:dword;
end;
begin
Info.total:=pFileProp(ptr)^.Play div 10000000;
end;
procedure ReadStreamProp(ptr:PAnsiChar;size:dword;var Info:tSongInfo);
type
pAudio = ^tAudio;
tAudio=packed record // WAVEFORMATEX
Codec :word;
Channels :word;
Samples :dword;
AvgBPS :dword;
BlockAlign :word;
BitsPerSample:word;
size :word;
end;
pVideo = ^tVideo;
tVideo = packed record
width :dword;
height :dword;
reserved:byte;
size :word;
bitmap :BITMAPINFOHEADER;
end;
tPrefix = packed record
StreamType :tGUID;
ECGUID :tGUID; // Error Correction
TimeOffset :int64;
DataLength :dword;
ECDataLength:dword;
Flags :word;
Reserved :dword;
end;
var
tmpguid:pGUID;
begin
tmpguid:=pointer(ptr);
inc(ptr,SizeOf(tPrefix)); //ofset to Type-Specific Data
if CompareGUID(tmpguid^,ASF_Audio_Media) then
begin
Info.channels:=pAudio(ptr)^.Channels;
Info.khz :=pAudio(ptr)^.Samples div 1000;
Info.kbps :=(pAudio(ptr)^.AvgBPS*8) div 1000;
end
else if CompareGUID(tmpguid^,ASF_Video_Media) then
begin
Info.width :=pVideo(ptr)^.bitmap.biWidth; // pVideo(ptr)^.width
Info.height:=pVideo(ptr)^.bitmap.biHeight; // pVideo(ptr)^.height
Info.codec :=pVideo(ptr)^.bitmap.biCompression;
end
end;
procedure ReadContent(ptr:PAnsiChar;var Info:tSongInfo);
type
pContent = ^tContent;
tContent = packed record
TitleLength :word;
AuthorLength :word;
CopyrightLength :word;
DescriptionLength:word;
RatingLength :word;
end;
var
cont:pContent;
begin
cont:=pointer(ptr);
inc(ptr,SizeOf(tContent));
if cont^.TitleLength>0 then //title
begin
ReadWMATagStr(Info.title,ptr,cont^.TitleLength);
inc(ptr,cont^.TitleLength);
end;
if cont^.AuthorLength>0 then //artist
begin
ReadWMATagStr(Info.artist,ptr,cont^.AuthorLength);
inc(ptr,cont^.AuthorLength);
end;
inc(ptr,cont^.CopyrightLength); //copyright
if cont^.DescriptionLength>0 then //comment
ReadWMATagStr(Info.comment,ptr,cont^.DescriptionLength);
end;
function ReadWMA(var Info:tSongInfo):boolean; cdecl;
var
f:THANDLE;
tmpguid:pGUID;
size:int64;
buf1,buf2:PAnsiChar;
HdrObjects:dword;
base:tGUID;
begin
result:=false;
f:=Reset(Info.mfile);
if f=THANDLE(INVALID_HANDLE_VALUE) then
exit;
BlockRead(f,base,SizeOf(tGUID));
if CompareGUID(base,ASF_Header_Object) then
begin
BlockRead(f,size,SizeOf(size));
dec(size,SizeOf(tGUID)+SizeOf(size));
GetMem(buf1,size);
buf2:=buf1;
BlockRead(f,buf1^,size);
HdrObjects:=pdword(buf2)^; inc(buf2,6);
while HdrObjects>0 do
begin
size:=ReadGUID(buf2,tmpguid);
if CompareGUID(tmpguid^,ASF_Content_Description_Object) then
ReadContent(buf2,Info)
else if CompareGUID(tmpguid^,ASF_Extended_Content_Description_Object) then
ReadExtended(buf2,size,Info)
else if CompareGUID(tmpguid^,ASF_Header_Extension_Object) then
ReadHdrExtended(buf2,size,Info)
else if CompareGUID(tmpguid^,ASF_File_Properties_Object) then
ReadFileProp(buf2,Info)
else if CompareGUID(tmpguid^,ASF_Stream_Properties_Object) then
ReadStreamProp(buf2,size,Info);
inc(buf2,size);
dec(HdrObjects);
end;
FreeMem(buf1);
result:=true;
end;
CloseHandle(f);
end;
var
LocalFormatLinkWMA,
LocalFormatLinkWMV,
LocalFormatLinkASF:twFormat;
procedure InitLink;
begin
LocalFormatLinkWMA.Next:=FormatLink;
LocalFormatLinkWMA.This.proc :=@ReadWMA;
LocalFormatLinkWMA.This.ext :='WMA';
LocalFormatLinkWMA.This.flags:=0;
FormatLink:=@LocalFormatLinkWMA;
LocalFormatLinkWMV.Next:=FormatLink;
LocalFormatLinkWMV.This.proc :=@ReadWMA;
LocalFormatLinkWMV.This.ext :='WMV';
LocalFormatLinkWMV.This.flags:=WAT_OPT_VIDEO;
FormatLink:=@LocalFormatLinkWMV;
LocalFormatLinkASF.Next:=FormatLink;
LocalFormatLinkASF.This.proc :=@ReadWMA;
LocalFormatLinkASF.This.ext :='ASF';
LocalFormatLinkASF.This.flags:=WAT_OPT_VIDEO;
FormatLink:=@LocalFormatLinkASF;
end;
initialization
InitLink;
end.
|