summaryrefslogtreecommitdiff
path: root/plugins/Watrack/formats/fmt_avi.pas
blob: 3646236cf7c03bb70a4fb8cd4a168731e82df93d (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
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
{AVI file format}
unit fmt_AVI;
{$include compilers.inc}

interface
uses wat_api;

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

implementation
uses windows,common,io,srv_format;

type
  FOURCC = array [0..3] of AnsiChar;
type
  tChunkHeader = packed record
    case byte of
      0: (Lo,Hi:dword);  {Common}
      1: (ID:FOURCC;     {RIFF}
          Length:dword);
  end;

const
  sRIFF = $46464952;
  sLIST = $5453494C;
  savih = $68697661; { avi header }
  sstrf = $66727473; { stream format }
  sstrh = $68727473; { stream header }
const
  smovi = $69766F6D; {movi list type}
const
  svids = $73646976; {video}
  sauds = $73647561; {audio}
const
  sIART = $54524149; {director}
  sICMT = $544D4349; {comment}
  sICRD = $44524349; {creation date}
  sIGNR = $524E4749; {genre}
  sINAM = $4D414E49; {title}
  sIPRT = $54525049; {part}
  sIPRO = $4F525049; {produced by}
  sISBJ = $4A425349; {subject description}

type
  tWaveFormatEx = packed record
    wFormatTag     :word;
    nChannels      :word;
    nSamplesPerSec :dword;
    nAvgBytesPerSec:dword;
    nBlockAlign    :word;
    wBitsPerSample :word;
    cbSize         :word;

    Reserved1      :word;
    wID            :word;
    fwFlags        :word;
    nBlockSize     :word;
    nFramesPerBlock:word;
    nCodecDelay    :word; {ms}
  end;

type
  tMainAVIHeader = packed record {avih}
    dwMicroSecPerFrame   :dword;
    dwMaxBytesPerSec     :dword;
    dwPaddingGranularity :dword;
    dwFlags              :dword;
    dwTotalFrames        :dword;  { # frames in first movi list}
    dwInitialFrames      :dword;
    dwStreams            :dword;
    dwSuggestedBufferSize:dword;
    dwWidth              :dword;
    dwHeight             :dword;
    dwScale              :dword;
    dwRate               :dword;
    dwStart              :dword;
    dwLength             :dword;
  end;

type
  TAVIExtHeader = packed record {dmlh}
    dwGrandFrames:dword;        {total number of frames in the file}
    dwFuture:array[0..60] of dword;
  end;

type
  tAVIStreamHeader = packed record {strh}
    fccType              :FOURCC; {vids|auds}
    fccHandler           :FOURCC;
    dwFlags              :dword;
    wPriority            :word;
    wLanguage            :word;
    dwInitialFrames      :dword;
    dwScale              :dword;
    dwRate               :dword;
    dwStart              :dword;
    dwLength             :dword;
    dwSuggestedBufferSize:dword;
    dwQuality            :dword;
    dwSampleSize         :dword;
    rcFrame: packed record
      left  :word;
      top   :word;
      right :word;
      bottom:word;
    end;
  end;

var
  vora:dword;

procedure Skip(f:THANDLE;bytes:dword);
var
  i:dword;
begin
  i:=FilePos(f);
  if bytes=0 then
  begin
    if odd(i) then
      Seek(f,i+1);
  end
  else
    Seek(f,i+bytes+(bytes mod 2));
end;

procedure ProcessVideoFormat(f:THANDLE;Size:dword;var Info:tSongInfo);
var
  bih:BitmapInfoHeader;
begin
  BlockRead(f,bih,SizeOf(bih));
  Info.codec :=bih.biCompression;
  Info.width :=bih.biWidth;
  Info.height:=bih.biHeight;
  Skip(f,Size-SizeOf(bih));
end;

procedure ProcessAudioFormat(f:THANDLE;Size:dword;var Info:tSongInfo);
{WAVEFORMATEX or PCMWAVEFORMAT}
var
  AF:tWaveFormatEx;
begin
  BlockRead(f,AF,SizeOf(AF));
  Info.channels:=AF.nChannels;
  Info.khz     :=AF.nSamplesPerSec div 1000;
  Info.kbps    :=(AF.nAvgBytesPerSec*8) div 1000;
  Skip(f,Size-SizeOf(AF));
end;

function ProcessASH(f:THANDLE;var Info:tSongInfo):dword;
var
  ASH:tAVIStreamHeader;
begin
  BlockRead(f,ASH,SizeOf(ASH));
  with ASH do
  begin
    if dword(fccType)=svids then
    begin
      if ASH.dwScale<>0 then
        Info.fps:=(ASH.dwRate*100) div ASH.dwScale;
      if Info.fps<>0 then
        Info.total:=(ASH.dwLength*100) div Info.fps;
      ProcessASH:=1
    end
    else if dword(fccType)=sauds then ProcessASH:=2
    else ProcessASH:=0;
  end;
end;

procedure ProcessMAH(f:THANDLE;var Info:tSongInfo);
var
  MAH:tMainAVIHeader;
begin
  BlockRead(f,MAH,SizeOf(MAH));
//  Info.width:=MAH.dwWidth;
//  Info.height:=MAH.dwHeight;
//  Info.fps:=100000000 div MAH.dwMicroSecPerFrame;
end;

function ProcessChunk(f:THANDLE;var Info:tSongInfo):dword;
var
  lTotal:dword;
  Chunk:tChunkHeader;
  cType:FOURCC;
  ls:PAnsiChar;
begin
  Skip(f,0);
  if (BlockRead(f,Chunk,SizeOF(Chunk))=0) or (Chunk.Lo=0) then
  begin
    result:=FileSize(f);
    Seek(f,FileSize(f));
    exit;
  end;
  result:=Chunk.Length+SizeOf(Chunk);
  case Chunk.Lo of
    sRIFF,sLIST: begin
      BlockRead(f,cType,SizeOf(cType));
      if dword(cType)=smovi then
        Skip(f,Chunk.Length-SizeOf(cType)) // result:=FileSize(f)
      else
      begin
        lTotal:=SizeOf(FOURCC);
        while lTotal<Chunk.Length do
          inc(lTotal,ProcessChunk(f,Info));
      end;
    end;
    sIART,sICMT,sICRD,sIGNR,sINAM,sIPRT,sIPRO,sISBJ: begin
      mGetMem(ls,Chunk.Length);
      BlockRead(f,ls^,Chunk.Length);
      case Chunk.Lo of
        sIART: begin
          AnsiToWide(ls,Info.artist);
        end;
        sICMT: begin
          if Info.comment=NIL then
            AnsiToWide(ls,Info.comment);
        end;
        sICRD: begin
          AnsiToWide(ls,Info.year);
        end;
        sIGNR: begin
          AnsiToWide(ls,Info.genre);
        end;
        sINAM: begin
          AnsiToWide(ls,Info.title);
        end;
        sIPRT: begin
          Info.track:=StrToInt(ls);
        end;
        sIPRO: begin
          if Info.artist=NIL then
            AnsiToWide(ls,Info.artist);
        end;
        sISBJ: begin
          AnsiToWide(ls,Info.comment);
        end;
      end;
      mFreeMem(ls);
    end;
    savih: begin
      ProcessMAH(f,Info);
    end;
    sstrh: begin
      vora:=ProcessASH(f,Info);
    end;
    sstrf: begin
      case vora of
        1: ProcessVideoFormat(f,Chunk.Hi,Info);
        2: ProcessAudioFormat(f,Chunk.Hi,Info);
      else
      end;
    end;
    else
      Skip(f,Chunk.Length);
  end;
end;

function ReadAVI(var Info:tSongInfo):boolean; cdecl;
var
  f:THANDLE;
begin
  result:=false;
  f:=Reset(Info.mfile);
  if f=THANDLE(INVALID_HANDLE_VALUE) then
    exit;
  ProcessChunk(f,Info);
  CloseHandle(f);
  result:=true;
end;

var
  LocalFormatLinkAVI,
  LocalFormatLinkDIVX:twFormat;

procedure InitLink;
begin
  LocalFormatLinkAVI.Next:=FormatLink;

  LocalFormatLinkAVI.This.proc :=@ReadAVI;
  LocalFormatLinkAVI.This.ext  :='AVI';
  LocalFormatLinkAVI.This.flags:=WAT_OPT_VIDEO;

  FormatLink:=@LocalFormatLinkAVI;

  LocalFormatLinkDIVX.Next:=FormatLink;

  LocalFormatLinkDIVX.This.proc :=@ReadAVI;
  LocalFormatLinkDIVX.This.ext  :='DIVX';
  LocalFormatLinkDIVX.This.flags:=WAT_OPT_VIDEO;

  FormatLink:=@LocalFormatLinkDIVX;
end;

initialization
  InitLink;
end.