summaryrefslogtreecommitdiff
path: root/plugins/Watrack/formats/fmt_aac.pas
blob: db8e03b15370d593d95443440907fd00790c4e55 (plain)
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
{AAC file process}
unit fmt_AAC;
{$include compilers.inc}

interface
uses wat_api;

function ReadAAC(var Info:tSongInfo):boolean; cdecl;

implementation
uses windows,common,io,tags,srv_format;

const
  SampleRates:array [0..15] of dword = (
    96000,88200,64000,48000,44100,32000,24000,22050,
    16000,12000,11025,8000,0,0,0,0);

procedure ReadADIFheader(f:THANDLE;var Info:tSongInfo);
var
  buf:array [0..29] of byte;
  bs,sf_idx,skip:dword;
begin
  BlockRead(f,buf,30);
  if (buf[0] and $80)<>0 then
    skip:=9
  else
    skip:=0;
  Info.kbps:=(((buf[0+skip] and $0F) shl 19)+(buf[1+skip] shl 11)+
        (buf[2+skip] shl 3)+{or}((buf[3+skip] and $E0){shr 5})) div 1000;
  bs:=buf[0+skip] and $10;
  if bs=0 then
    sf_idx:=(buf[7+skip] and $78) shr 3
  else
    sf_idx:=((buf[4+skip] and $07) shl 1)+((buf[5+skip] and $80) shr 7);
  Info.khz:=SampleRates[sf_idx];
end;

procedure ReadADTSheader(var Info:tSongInfo;sign:dword);
type
  l2b=record
    b:array [0..3] of byte;
  end;
var
  sr_idx:integer;
begin
  Info.channels:=((l2b(sign).b[2] and $01) shl 2)+
                 ((l2b(sign).b[3] and $C0) shr 6);
  sr_idx:=(l2b(sign).b[2] and $3C) shr 2;
  Info.khz:=SampleRates[sr_idx] div 1000;
end;

function ReadAAC(var Info:tSongInfo):boolean; cdecl;
var
  f:THANDLE;
  sign:dword;
begin
  result:=false;
  f:=Reset(Info.mfile);
  if f=THANDLE(INVALID_HANDLE_VALUE) then
    exit;
  ReadID3v2(f,Info);
  BlockRead(f,sign,4);
  Info.khz:=44;
  Info.kbps:=128;
  Info.channels:=2;
  if (lobyte(sign)=$FF) and ((hibyte(sign) and $F6)=$F0) then
    ReadADTSheader(Info,sign)
  else if sign=$46494441 then // 'ADIF'
    ReadADIFheader(f,Info);

  ReadAPEv2(f,Info);
  ReadID3v1(f,Info);
  CloseHandle(f);
  result:=true;
end;

var
  LocalFormatLink:twFormat;

procedure InitLink;
begin
  LocalFormatLink.Next:=FormatLink;

  LocalFormatLink.This.proc :=@ReadAAC;
  LocalFormatLink.This.ext  :='AAC';
  LocalFormatLink.This.flags:=0;

  FormatLink:=@LocalFormatLink;
end;

initialization
  InitLink;
end.