forked from jefetienne/daggerfall-unity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVidFile.cs
More file actions
387 lines (339 loc) · 12.9 KB
/
VidFile.cs
File metadata and controls
387 lines (339 loc) · 12.9 KB
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
// Project: Daggerfall Tools For Unity
// Copyright: Copyright (C) 2009-2020 Daggerfall Workshop
// Web Site: http://www.dfworkshop.net
// License: MIT License (http://www.opensource.org/licenses/mit-license.php)
// Source Code: https://github.com/Interkarma/daggerfall-unity
// Original Author: Gavin Clayton (interkarma@dfworkshop.net)
// Contributors:
//
// Notes:
//
#region Using Statements
using UnityEngine;
using System;
using System.IO;
using DaggerfallConnect.Utility;
#endregion
namespace DaggerfallConnect.Arena2
{
/// <summary>
/// Opens and reads a Daggerfall VID file.
/// Unlike most API classes, this one uses the UnityEngine namespace to
/// directly decode image data to a Color32[] frame buffer for efficiency.
/// </summary>
public class VidFile
{
const string VID = "VID";
const int sampleRate = 11025;
//const int delayMultiplier = 185;
const int paletteDataLength = 768;
const int paletteColorCount = 256;
const int paletteMultiplier = 4;
const float minFrameDelay = (740f / sampleRate);
readonly FileProxy vidFile = new FileProxy();
VidHeader header = new VidHeader();
BinaryReader reader;
Color32[] palette;
Color32[] frameBuffer;
byte[] audioBuffer;
VidBlockTypes lastBlockType;
int currentBlock;
int lastDelay;
double frameDelay;
long streamPosition;
bool endOfFileReached = false;
public int FrameCount { get { return header.FrameCount; } }
public int FrameWidth { get { return header.FrameWidth; } }
public int FrameHeight { get { return header.FrameHeight; } }
public int SampleRate { get { return sampleRate; } }
public byte[] AudioBuffer { get { return audioBuffer; } }
public Color32[] FrameBuffer { get { return frameBuffer; } }
public double FrameDelay { get { return frameDelay; } }
public int CurrentBlock { get { return currentBlock; } }
public VidBlockTypes LastBlockType { get { return lastBlockType; } }
public bool EndOfFile { get { return endOfFileReached; } }
public int LastDelay { get { return lastDelay; } }
/// <summary>
/// Default constructor.
/// </summary>
public VidFile()
{
}
/// <summary>
/// Open constructor.
/// </summary>
/// <param name="filename">Filname of VID to open.</param>
public VidFile(string filename)
{
Open(filename);
}
/// <summary>
/// Open a VID file.
/// Always uses a memory stream for performance.
/// </summary>
/// <param name="filename">Filename of VID to open.</param>
public bool Open(string filename)
{
// Open file proxy
if (!vidFile.Load(filename, FileUsage.UseMemory, true))
{
endOfFileReached = true;
return false;
}
// Close existing reader
if (reader != null)
reader.Close();
// Read header and palette
reader = vidFile.GetReader(0);
ReadHeader();
ReadPalette();
// Cache reader position
streamPosition = reader.BaseStream.Position;
endOfFileReached = false;
currentBlock = 0;
return true;
}
/// <summary>
/// Read next block from VID stream.
/// Daggerfall's VID files are mostly interleaved audio and video streams.
/// Occasionally there is a null block which must be rejected.
/// </summary>
public void ReadNextBlock()
{
ReadBlock();
}
int ReadBlock()
{
if (endOfFileReached)
return 0;
reader.BaseStream.Position = streamPosition;
// Read next block type
VidBlockTypes blockType = (VidBlockTypes)reader.ReadByte();
lastBlockType = blockType;
// Handle null block
if (blockType == VidBlockTypes.Null)
{
//Debug.Log("Null block encountered.");
streamPosition = reader.BaseStream.Position;
return 0;
}
try
{
switch (blockType)
{
case VidBlockTypes.Palette:
//Debug.Log("Skipping extra palette data");
streamPosition += paletteDataLength;
break;
case VidBlockTypes.Audio_StartFrame:
//Debug.Log("Reading Audio_StartFrame");
ReadAudioStartFrame();
break;
case VidBlockTypes.Audio_IncrementalFrame:
//Debug.Log("Reading Audio_IncrementalFrame");
ReadAudioIncrementalFrame();
break;
case VidBlockTypes.Video_StartFrame:
//Debug.Log("Reading Video_StartFrame");
ReadVideoStartFrame();
break;
case VidBlockTypes.Video_IncrementalFrame:
//Debug.Log("Reading Video_IncrementalFrame");
ReadVideoIncrementalFrame();
break;
case VidBlockTypes.Video_IncrementalRowOffsetFrame:
//Debug.Log("Reading Video_IncrementalRowOffsetFrame");
ReadVideoRowOffsetFrame();
break;
case VidBlockTypes.EndOfFile:
Debug.Log("End of VID file reached");
endOfFileReached = true;
break;
default:
throw new Exception("VidFile: Invalid/Unknown block type encountered.");
}
}
catch (Exception ex)
{
Debug.Log(ex.Message);
return 0;
}
// Calculate delay time for this frame
// Generally equivalent to (float)audioBuffer.Length / (float)sampleRate
// Seems to differ mainly at beginning and end of video
//frameDelay = ((float)(header.GlobalDelay + (float)lastDelay) * (float)delayMultiplier) / (float)sampleRate;
frameDelay = (double)audioBuffer.Length / (double)sampleRate;
// Daggerfall .VID files always have at least an audioBuffer.Length of 740 while in the middle of audio portions.
// As the audio portion ends, this length can be much shorter, causing frameDelay to become very short
// and making playback speed up. Enforcing a minimum based on a length of 740 to fix this.
// This fixes the ends of ANIM0000.VID and ANIM0005.VID.
if (frameDelay < minFrameDelay)
{
frameDelay = minFrameDelay;
}
long bytesRead = reader.BaseStream.Position - streamPosition;
streamPosition = reader.BaseStream.Position;
currentBlock++;
return (int)bytesRead;
}
#region Readers
void ReadHeader()
{
// Verify file starts with VID
header.VID = FileProxy.ReadCString(reader, 3);
if (header.VID != VID)
throw new Exception("VidFile: Invalid VID header encountered.");
// Read remaining header
header.Unknown1 = reader.ReadUInt16();
header.FrameCount = reader.ReadUInt16();
header.FrameWidth = reader.ReadUInt16();
header.FrameHeight = reader.ReadUInt16();
header.GlobalDelay = reader.ReadUInt16();
header.Unknown2 = reader.ReadUInt16();
// Init frame buffer
frameBuffer = new Color32[header.FrameWidth * header.FrameHeight];
}
void ReadPalette()
{
VidBlockTypes blockType = (VidBlockTypes)reader.ReadByte();
if (blockType != VidBlockTypes.Palette)
throw new Exception("VidFile: Palette block not encountered after header.");
int pos = 0;
byte[] data = reader.ReadBytes(paletteDataLength);
palette = new Color32[paletteColorCount];
for (int i = 0; i < paletteColorCount; i++)
{
palette[i] = new Color32(
(byte)(data[pos++] * paletteMultiplier),
(byte)(data[pos++] * paletteMultiplier),
(byte)(data[pos++] * paletteMultiplier),
255);
}
}
VidAudioStartFrame ReadAudioStartFrame()
{
VidAudioStartFrame block = new VidAudioStartFrame();
block.Unknown1 = reader.ReadUInt16();
block.PlaybackRate = reader.ReadByte();
block.DataLength = reader.ReadUInt16();
audioBuffer = reader.ReadBytes(block.DataLength);
if (block.PlaybackRate != 166)
throw new Exception("VidFile: Unknown PlaybackRate encountered.");
return block;
}
VidAudioIncrementalFrame ReadAudioIncrementalFrame()
{
VidAudioIncrementalFrame block = new VidAudioIncrementalFrame();
block.DataLength = reader.ReadUInt16();
audioBuffer = reader.ReadBytes(block.DataLength);
return block;
}
void ReadVideoStartFrame()
{
lastDelay = (int)reader.ReadUInt16();
ReadVideoFullFrameData();
}
void ReadVideoRowOffsetFrame()
{
lastDelay = (int)reader.ReadUInt16();
UInt16 row = reader.ReadUInt16();
ReadVideoPartialFrameData(row * FrameWidth);
}
void ReadVideoIncrementalFrame()
{
lastDelay = (int)reader.ReadUInt16();
ReadVideoPartialFrameData(0);
}
void ReadVideoFullFrameData()
{
int pos = 0;
int count = 0;
while (pos < frameBuffer.Length)
{
byte input = reader.ReadByte();
if (input >= 0x80)
{
count = input - 0x80;
input = reader.ReadByte();
for (int i = 0; i < count; i++)
{
frameBuffer[pos++] = palette[input];
}
}
else if (input == 0)
{
break;
}
else
{
count = input;
for (int i = 0; i < count; i++)
{
byte index = reader.ReadByte();
frameBuffer[pos++] = palette[index];
}
}
}
}
void ReadVideoPartialFrameData(int pos)
{
int count = 0;
while (pos < frameBuffer.Length)
{
byte input = reader.ReadByte();
if (input >= 0x80)
{
count = input - 0x80;
pos += count;
}
else if (input == 0)
{
break;
}
else
{
count = input;
for (int i = 0; i < count; i++)
{
byte index = reader.ReadByte();
frameBuffer[pos++] = palette[index];
}
}
}
}
#endregion
}
#region Structs & Enums
public enum VidBlockTypes
{
Null = 0,
Video_IncrementalFrame = 1,
Palette = 2,
Video_StartFrame = 3,
Video_IncrementalRowOffsetFrame = 4,
EndOfFile = 20,
Audio_StartFrame = 124,
Audio_IncrementalFrame = 125,
}
public struct VidHeader
{
public String VID; // "VID"
public UInt16 Unknown1; // Always 512
public UInt16 FrameCount; // Total number of frames
public UInt16 FrameWidth; // Width of each frame (256 or 320)
public UInt16 FrameHeight; // Height of each frame (200)
public UInt16 GlobalDelay; // Frame delay
public UInt16 Unknown2; // Always 14
}
public struct VidAudioStartFrame
{
public UInt16 Unknown1; // Always 0
public Byte PlaybackRate; // Unknown format. Value of 166 = 11025Hz
public UInt16 DataLength; // Length of audio data.
}
public struct VidAudioIncrementalFrame
{
public UInt16 DataLength; // Length of audio data.
}
#endregion
}