Merge pull request #6638 from DeadlySurprise/tsVqa

Tiberian Sun .vqa support
This commit is contained in:
Paul Chote
2014-11-29 22:01:28 +13:00
8 changed files with 330 additions and 77 deletions

4
.gitignore vendored
View File

@@ -60,4 +60,6 @@ Lua-API.md
# SublimeText # SublimeText
*.sublime-project *.sublime-project
*.sublime-workspace *.sublime-workspace
*.mix
*.vqa

View File

@@ -60,11 +60,10 @@ namespace OpenRA.FileFormats
} }
} }
public static int DecodeInto(byte[] src, byte[] dest, int srcOffset = 0) public static int DecodeInto(byte[] src, byte[] dest, int srcOffset = 0, bool reverse = false)
{ {
var ctx = new FastByteReader(src, srcOffset); var ctx = new FastByteReader(src, srcOffset);
var destIndex = 0; var destIndex = 0;
while (true) while (true)
{ {
var i = ctx.ReadByte(); var i = ctx.ReadByte();
@@ -104,7 +103,7 @@ namespace OpenRA.FileFormats
{ {
// case 5 // case 5
var count = ctx.ReadWord(); var count = ctx.ReadWord();
var srcIndex = ctx.ReadWord(); var srcIndex = reverse ? destIndex - ctx.ReadWord() : ctx.ReadWord();
if (srcIndex >= destIndex) if (srcIndex >= destIndex)
throw new NotImplementedException("srcIndex >= destIndex {0} {1}".F(srcIndex, destIndex)); throw new NotImplementedException("srcIndex >= destIndex {0} {1}".F(srcIndex, destIndex));
@@ -115,7 +114,7 @@ namespace OpenRA.FileFormats
{ {
// case 3 // case 3
var count = count3 + 3; var count = count3 + 3;
var srcIndex = ctx.ReadWord(); var srcIndex = reverse ? destIndex - ctx.ReadWord() : ctx.ReadWord();
if (srcIndex >= destIndex) if (srcIndex >= destIndex)
throw new NotImplementedException("srcIndex >= destIndex {0} {1}".F(srcIndex, destIndex)); throw new NotImplementedException("srcIndex >= destIndex {0} {1}".F(srcIndex, destIndex));
@@ -174,10 +173,13 @@ namespace OpenRA.FileFormats
// Command 4: Repeat byte n times // Command 4: Repeat byte n times
ms.WriteByte(0xFE); ms.WriteByte(0xFE);
// Low byte // Low byte
ms.WriteByte((byte)(repeatCount & 0xFF)); ms.WriteByte((byte)(repeatCount & 0xFF));
// High byte // High byte
ms.WriteByte((byte)(repeatCount >> 8)); ms.WriteByte((byte)(repeatCount >> 8));
// Value to repeat // Value to repeat
ms.WriteByte(src[offset]); ms.WriteByte(src[offset]);

View File

@@ -25,16 +25,24 @@ namespace OpenRA.FileFormats
ushort numColors; ushort numColors;
ushort blockWidth; ushort blockWidth;
ushort blockHeight; ushort blockHeight;
byte cbParts; byte chunkBufferParts;
int2 blocks; int2 blocks;
UInt32[] offsets; uint[] offsets;
uint[] palette; uint[] palette;
uint videoFlags; // if 0x10 is set the video is a 16 bit hq video (ts and later)
int sampleRate;
int sampleBits;
int audioChannels;
// Stores a list of subpixels, referenced by the VPTZ chunk // Stores a list of subpixels, referenced by the VPTZ chunk
byte[] cbf; byte[] cbf;
byte[] cbp; byte[] cbp;
int cbChunk = 0; byte[] cbfBuffer;
int cbOffset = 0; byte[] fileBuffer = new byte[256000]; // Buffer for loading file subchunks, the maximum chunk size of a file is not defined
int maxCbfzSize = 256000; // and the header definition for the size of the biggest chunks (color data) isn't accurate. But 256k is large enough for all TS videos(< 200k).
int vtprSize = 0;
int currentChunkBuffer = 0;
int chunkBufferOffset = 0;
// Top half contains block info, bottom half contains references to cbf array // Top half contains block info, bottom half contains references to cbf array
byte[] origData; byte[] origData;
@@ -42,9 +50,14 @@ namespace OpenRA.FileFormats
// Final frame output // Final frame output
uint[,] frameData; uint[,] frameData;
byte[] audioData; // audio for this frame: 22050Hz 16bit mono pcm, uncompressed. byte[] audioData; // audio for this frame: 22050Hz 16bit mono pcm, uncompressed.
bool hasAudio;
public byte[] AudioData { get { return audioData; } } public byte[] AudioData { get { return audioData; } }
public int CurrentFrame { get { return currentFrame; } } public int CurrentFrame { get { return currentFrame; } }
public int SampleRate { get { return sampleRate; } }
public int SampleBits { get { return sampleBits; } }
public int AudioChannels { get { return audioChannels; } }
public bool HasAudio { get { return hasAudio; } }
public VqaReader(Stream stream) public VqaReader(Stream stream)
{ {
@@ -53,14 +66,14 @@ namespace OpenRA.FileFormats
// Decode FORM chunk // Decode FORM chunk
if (stream.ReadASCII(4) != "FORM") if (stream.ReadASCII(4) != "FORM")
throw new InvalidDataException("Invalid vqa (invalid FORM section)"); throw new InvalidDataException("Invalid vqa (invalid FORM section)");
/*var length = */ stream.ReadUInt32(); /*var length = */stream.ReadUInt32();
if (stream.ReadASCII(8) != "WVQAVQHD") if (stream.ReadASCII(8) != "WVQAVQHD")
throw new InvalidDataException("Invalid vqa (not WVQAVQHD)"); throw new InvalidDataException("Invalid vqa (not WVQAVQHD)");
/* var length = */stream.ReadUInt32(); /*var length2 = */stream.ReadUInt32();
/*var version = */stream.ReadUInt16(); /*var version = */stream.ReadUInt16();
/*var flags = */stream.ReadUInt16(); videoFlags = stream.ReadUInt16();
Frames = stream.ReadUInt16(); Frames = stream.ReadUInt16();
Width = stream.ReadUInt16(); Width = stream.ReadUInt16();
Height = stream.ReadUInt16(); Height = stream.ReadUInt16();
@@ -68,7 +81,7 @@ namespace OpenRA.FileFormats
blockWidth = stream.ReadUInt8(); blockWidth = stream.ReadUInt8();
blockHeight = stream.ReadUInt8(); blockHeight = stream.ReadUInt8();
Framerate = stream.ReadUInt8(); Framerate = stream.ReadUInt8();
cbParts = stream.ReadUInt8(); chunkBufferParts = stream.ReadUInt8();
blocks = new int2(Width / blockWidth, Height / blockHeight); blocks = new int2(Width / blockWidth, Height / blockHeight);
numColors = stream.ReadUInt16(); numColors = stream.ReadUInt16();
@@ -77,18 +90,34 @@ namespace OpenRA.FileFormats
/*var unknown2 = */stream.ReadUInt32(); /*var unknown2 = */stream.ReadUInt32();
// Audio // Audio
/*var freq = */stream.ReadUInt16(); sampleRate = stream.ReadUInt16();
/*var channels = */stream.ReadByte(); audioChannels = stream.ReadByte();
/*var bits = */stream.ReadByte(); sampleBits = stream.ReadByte();
/*var unknown3 = */stream.ReadBytes(14);
/*var unknown3 =*/stream.ReadUInt32();
/*var unknown4 =*/stream.ReadUInt16();
/*maxCbfzSize =*/stream.ReadUInt32(); // Unreliable
/*var unknown5 =*/stream.ReadUInt32();
var frameSize = Exts.NextPowerOf2(Math.Max(Width, Height)); var frameSize = Exts.NextPowerOf2(Math.Max(Width, Height));
cbf = new byte[Width*Height];
cbp = new byte[Width*Height];
palette = new uint[numColors];
origData = new byte[2*blocks.X*blocks.Y];
frameData = new uint[frameSize, frameSize];
if (IsHqVqa)
{
cbfBuffer = new byte[maxCbfzSize];
cbf = new byte[maxCbfzSize * 3];
origData = new byte[maxCbfzSize];
}
else
{
cbfBuffer = new byte[Width * Height];
cbf = new byte[Width * Height];
cbp = new byte[Width * Height];
origData = new byte[2 * blocks.X * blocks.Y];
}
palette = new uint[numColors];
frameData = new uint[frameSize, frameSize];
var type = stream.ReadASCII(4); var type = stream.ReadASCII(4);
while (type != "FINF") while (type != "FINF")
{ {
@@ -107,7 +136,7 @@ namespace OpenRA.FileFormats
/*var unknown4 = */stream.ReadUInt16(); /*var unknown4 = */stream.ReadUInt16();
// Frame offsets // Frame offsets
offsets = new UInt32[Frames]; offsets = new uint[Frames];
for (var i = 0; i < Frames; i++) for (var i = 0; i < Frames; i++)
{ {
offsets[i] = stream.ReadUInt32(); offsets[i] = stream.ReadUInt32();
@@ -123,15 +152,15 @@ namespace OpenRA.FileFormats
public void Reset() public void Reset()
{ {
currentFrame = cbOffset = cbChunk = 0; currentFrame = chunkBufferOffset = currentChunkBuffer = 0;
LoadFrame(); LoadFrame();
} }
void CollectAudioData() void CollectAudioData()
{ {
var ms = new MemoryStream(); var audio1 = new MemoryStream(); // left channel / mono
var audio2 = new MemoryStream(); // right channel
var adpcmIndex = 0; var adpcmIndex = 0;
var compressed = false; var compressed = false;
for (var i = 0; i < Frames; i++) for (var i = 0; i < Frames; i++)
{ {
@@ -141,19 +170,37 @@ namespace OpenRA.FileFormats
while (stream.Position < end) while (stream.Position < end)
{ {
var type = stream.ReadASCII(4); var type = stream.ReadASCII(4);
if (type == "SN2J")
{
var jmp = int2.Swap(stream.ReadUInt32());
stream.Seek(jmp, SeekOrigin.Current);
type = stream.ReadASCII(4);
}
var length = int2.Swap(stream.ReadUInt32()); var length = int2.Swap(stream.ReadUInt32());
switch (type) switch (type)
{ {
case "SND0": case "SND0":
case "SND2": case "SND2":
var rawAudio = stream.ReadBytes((int)length); if (audioChannels == 1)
ms.Write(rawAudio); {
compressed = (type == "SND2"); var rawAudio = stream.ReadBytes((int)length);
break; audio1.Write(rawAudio);
}
else
{
var rawAudio = stream.ReadBytes((int)length / 2);
audio1.Write(rawAudio);
rawAudio = stream.ReadBytes((int)length / 2);
audio2.Write(rawAudio);
}
compressed = type == "SND2";
break;
default: default:
stream.ReadBytes((int)length); stream.ReadBytes((int)length);
break; break;
} }
// Chunks are aligned on even bytes; advance by a byte if the next one is null // Chunks are aligned on even bytes; advance by a byte if the next one is null
@@ -161,7 +208,39 @@ namespace OpenRA.FileFormats
} }
} }
audioData = (compressed) ? AudLoader.LoadSound(ms.ToArray(), ref adpcmIndex) : ms.ToArray(); if (audioChannels == 1)
{
audioData = compressed ? AudLoader.LoadSound(audio1.ToArray(), ref adpcmIndex) : audio1.ToArray();
}
else
{
byte[] leftData, rightData;
if (!compressed)
{
leftData = audio1.ToArray();
rightData = audio2.ToArray();
}
else
{
adpcmIndex = 0;
leftData = AudLoader.LoadSound(audio1.ToArray(), ref adpcmIndex);
adpcmIndex = 0;
rightData = AudLoader.LoadSound(audio2.ToArray(), ref adpcmIndex);
}
audioData = new byte[rightData.Length + leftData.Length];
var rightIndex = 0;
var leftIndex = 0;
for (var i = 0; i < audioData.Length;)
{
audioData[i++] = leftData[leftIndex++];
audioData[i++] = leftData[leftIndex++];
audioData[i++] = rightData[rightIndex++];
audioData[i++] = rightData[rightIndex++];
}
}
hasAudio = audioData.Length > 0;
} }
public void AdvanceFrame() public void AdvanceFrame()
@@ -177,22 +256,45 @@ namespace OpenRA.FileFormats
// Seek to the start of the frame // Seek to the start of the frame
stream.Seek(offsets[currentFrame], SeekOrigin.Begin); stream.Seek(offsets[currentFrame], SeekOrigin.Begin);
var end = (currentFrame < Frames - 1) ? offsets[currentFrame+1] : stream.Length; var end = (currentFrame < Frames - 1) ? offsets[currentFrame + 1] : stream.Length;
while (stream.Position < end) while (stream.Position < end)
{ {
var type = stream.ReadASCII(4); var type = stream.ReadASCII(4);
var length = int2.Swap(stream.ReadUInt32()); var length = 0U;
if (type == "SN2J")
switch(type) {
var jmp = int2.Swap(stream.ReadUInt32());
stream.Seek(jmp, SeekOrigin.Current);
type = stream.ReadASCII(4);
if (type == "SND2")
{
length = int2.Swap(stream.ReadUInt32());
stream.Seek(length, SeekOrigin.Current);
type = stream.ReadASCII(4);
}
else
throw new NotSupportedException();
}
length = int2.Swap(stream.ReadUInt32());
switch (type)
{ {
case "VQFR": case "VQFR":
DecodeVQFR(stream); DecodeVQFR(stream);
break; break;
case "\0VQF":
stream.ReadByte();
DecodeVQFR(stream);
break;
case "VQFL":
DecodeVQFR(stream, "VQFL");
break;
default: default:
// Don't parse sound here. // Don't parse sound here.
stream.ReadBytes((int)length); stream.ReadBytes((int)length);
break; break;
} }
// Chunks are aligned on even bytes; advance by a byte if the next one is null // Chunks are aligned on even bytes; advance by a byte if the next one is null
@@ -201,7 +303,7 @@ namespace OpenRA.FileFormats
} }
// VQA Frame // VQA Frame
public void DecodeVQFR(Stream s) public void DecodeVQFR(Stream s, string parentType = "VQFR")
{ {
while (true) while (true)
{ {
@@ -210,11 +312,37 @@ namespace OpenRA.FileFormats
var type = s.ReadASCII(4); var type = s.ReadASCII(4);
var subchunkLength = (int)int2.Swap(s.ReadUInt32()); var subchunkLength = (int)int2.Swap(s.ReadUInt32());
switch(type) switch (type)
{ {
// Full frame-modifier // Full frame-modifier
case "CBFZ": case "CBFZ":
Format80.DecodeInto(s.ReadBytes(subchunkLength), cbf); var decodeMode = s.Peek() == 0;
s.Read(fileBuffer, 0, subchunkLength);
Array.Clear(cbf, 0, cbf.Length);
Array.Clear(cbfBuffer, 0, cbfBuffer.Length);
var decodeCount = 0;
decodeCount = Format80.DecodeInto(fileBuffer, cbfBuffer, decodeMode ? 1 : 0, decodeMode);
if ((videoFlags & 0x10) == 16)
{
var p = 0;
for (var i = 0; i < decodeCount; i += 2)
{
var packed = cbfBuffer[i + 1] << 8 | cbfBuffer[i];
/* 15 bit 0
0rrrrrgg gggbbbbb
HI byte LO byte*/
cbf[p++] = (byte)((packed & 0x7C00) >> 7);
cbf[p++] = (byte)((packed & 0x3E0) >> 2);
cbf[p++] = (byte)((packed & 0x1f) << 3);
}
}
else
{
cbf = cbfBuffer;
}
if (parentType == "VQFL")
return;
break; break;
case "CBF0": case "CBF0":
cbf = s.ReadBytes(subchunkLength); cbf = s.ReadBytes(subchunkLength);
@@ -224,20 +352,20 @@ namespace OpenRA.FileFormats
case "CBP0": case "CBP0":
case "CBPZ": case "CBPZ":
// Partial buffer is full; dump and recreate // Partial buffer is full; dump and recreate
if (cbChunk == cbParts) if (currentChunkBuffer == chunkBufferParts)
{ {
if (type == "CBP0") if (type == "CBP0")
cbf = (byte[])cbp.Clone(); cbf = (byte[])cbp.Clone();
else else
Format80.DecodeInto(cbp, cbf); Format80.DecodeInto(cbp, cbf);
cbOffset = cbChunk = 0; chunkBufferOffset = currentChunkBuffer = 0;
} }
var bytes = s.ReadBytes(subchunkLength); var bytes = s.ReadBytes(subchunkLength);
bytes.CopyTo(cbp,cbOffset); bytes.CopyTo(cbp, chunkBufferOffset);
cbOffset += subchunkLength; chunkBufferOffset += subchunkLength;
cbChunk++; currentChunkBuffer++;
break; break;
// Palette // Palette
@@ -249,13 +377,28 @@ namespace OpenRA.FileFormats
var b = (byte)(s.ReadUInt8() << 2); var b = (byte)(s.ReadUInt8() << 2);
palette[i] = (uint)((255 << 24) | (r << 16) | (g << 8) | b); palette[i] = (uint)((255 << 24) | (r << 16) | (g << 8) | b);
} }
break; break;
// Frame data // Frame data
case "VPTZ": case "VPTZ":
Format80.DecodeInto(s.ReadBytes(subchunkLength), origData); Format80.DecodeInto(s.ReadBytes(subchunkLength), origData);
// This is the last subchunk // This is the last subchunk
return; return;
case "VPRZ":
Array.Clear(origData, 0, origData.Length);
s.Read(fileBuffer, 0, subchunkLength);
if (fileBuffer[0] != 0)
vtprSize = Format80.DecodeInto(fileBuffer, origData);
else
Format80.DecodeInto(fileBuffer, origData, 1, true);
return;
case "VPTR":
Array.Clear(origData, 0, origData.Length);
s.Read(origData, 0, subchunkLength);
vtprSize = subchunkLength;
return;
default: default:
throw new InvalidDataException("Unknown sub-chunk {0}".F(type)); throw new InvalidDataException("Unknown sub-chunk {0}".F(type));
} }
@@ -267,19 +410,76 @@ namespace OpenRA.FileFormats
void DecodeFrameData() void DecodeFrameData()
{ {
cachedFrame = currentFrame; cachedFrame = currentFrame;
for (var y = 0; y < blocks.Y; y++) if (IsHqVqa)
for (var x = 0; x < blocks.X; x++) {
/* The VP?? chunks of the video file contains an array of instructions for
* how the blocks of the finished frame will be filled with color data blocks
* contained in the CBF? chunks.
*/
var p = 0;
for (var y = 0; y < blocks.Y;)
{ {
var px = origData[x + y*blocks.X]; for (var x = 0; x < blocks.X;)
var mod = origData[x + (y + blocks.Y)*blocks.X]; {
for (var j = 0; j < blockHeight; j++) if (y >= blocks.Y)
for (var i = 0; i < blockWidth; i++) break;
// The first 3 bits of the short determine the type of instruction with the rest being one or two parameters.
var val = (int)origData[p++];
val |= origData[p++] << 8;
var para_A = val & 0x1fff;
var para_B1 = val & 0xFF;
var para_B2 = (((val / 256) & 0x1f) + 1) * 2;
switch (val >> 13)
{ {
var cbfi = (mod*256 + px)*8 + j*blockWidth + i; case 0:
var color = (mod == 0x0f) ? px : cbf[cbfi]; x += para_A;
frameData[y*blockHeight + j, x*blockWidth + i] = palette[color]; break;
case 1:
WriteBlock(para_B1, para_B2, ref x, ref y);
break;
case 2:
WriteBlock(para_B1, 1, ref x, ref y);
for (var i = 0; i < para_B2; i++)
WriteBlock(origData[p++], 1, ref x, ref y);
break;
case 3:
WriteBlock(para_A, 1, ref x, ref y);
break;
case 5:
WriteBlock(para_A, origData[p++], ref x, ref y);
break;
default:
throw new NotSupportedException();
} }
}
y++;
} }
if (p != vtprSize)
throw new IndexOutOfRangeException();
}
else
{
for (var y = 0; y < blocks.Y; y++)
{
for (var x = 0; x < blocks.X; x++)
{
var px = origData[x + y * blocks.X];
var mod = origData[x + (y + blocks.Y) * blocks.X];
for (var j = 0; j < blockHeight; j++)
{
for (var i = 0; i < blockWidth; i++)
{
var cbfi = (mod * 256 + px) * 8 + j * blockWidth + i;
var color = (mod == 0x0f) ? px : cbf[cbfi];
frameData[y * blockHeight + j, x * blockWidth + i] = palette[color];
}
}
}
}
}
} }
public uint[,] FrameData public uint[,] FrameData
@@ -292,5 +492,33 @@ namespace OpenRA.FileFormats
return frameData; return frameData;
} }
} }
bool IsHqVqa { get { return (videoFlags & 0x10) == 16; } }
void WriteBlock(int blockNumber, int count, ref int x, ref int y)
{
for (var i = 0; i < count; i++)
{
var frameX = x * blockWidth;
var frameY = y * blockHeight;
var offset = blockNumber * blockHeight * blockWidth * 3;
for (var by = 0; by < blockHeight; by++)
for (var bx = 0; bx < blockWidth; bx++)
{
var p = (bx + by * blockWidth) * 3;
frameData[frameY + by, frameX + bx] = (uint)(0xFF << 24 | cbf[offset + p] << 16 | cbf[offset + p + 1] << 8 | cbf[offset + p + 2]);
}
x++;
if (x >= blocks.X)
{
x = 0;
y++;
if (y >= blocks.Y && i != count - 1)
throw new IndexOutOfRangeException();
}
}
}
} }
} }

View File

@@ -43,7 +43,7 @@ namespace OpenRA
return LoadWave(new WavLoader(s)); return LoadWave(new WavLoader(s));
using (var s = GlobalFileSystem.Open(filename)) using (var s = GlobalFileSystem.Open(filename))
return LoadSoundRaw(AudLoader.LoadSound(s)); return LoadSoundRaw(AudLoader.LoadSound(s), 1, 16, 22050);
} }
static ISoundSource LoadWave(WavLoader wave) static ISoundSource LoadWave(WavLoader wave)
@@ -51,9 +51,9 @@ namespace OpenRA
return soundEngine.AddSoundSourceFromMemory(wave.RawOutput, wave.Channels, wave.BitsPerSample, wave.SampleRate); return soundEngine.AddSoundSourceFromMemory(wave.RawOutput, wave.Channels, wave.BitsPerSample, wave.SampleRate);
} }
static ISoundSource LoadSoundRaw(byte[] rawData) static ISoundSource LoadSoundRaw(byte[] rawData, int channels, int sampleBits, int sampleRate)
{ {
return soundEngine.AddSoundSourceFromMemory(rawData, 1, 16, 22050); return soundEngine.AddSoundSourceFromMemory(rawData, channels, sampleBits, sampleRate);
} }
static ISoundEngine CreateEngine(string engine) static ISoundEngine CreateEngine(string engine)
@@ -120,9 +120,9 @@ namespace OpenRA
public static ISound PlayToPlayer(Player player, string name) { return Play(player, name, true, WPos.Zero, 1); } public static ISound PlayToPlayer(Player player, string name) { return Play(player, name, true, WPos.Zero, 1); }
public static ISound PlayToPlayer(Player player, string name, WPos pos) { return Play(player, name, false, pos, 1); } public static ISound PlayToPlayer(Player player, string name, WPos pos) { return Play(player, name, false, pos, 1); }
public static void PlayVideo(byte[] raw) public static void PlayVideo(byte[] raw, int channels, int sampleBits, int sampleRate)
{ {
rawSource = LoadSoundRaw(raw); rawSource = LoadSoundRaw(raw, channels, sampleBits, sampleRate);
video = soundEngine.Play2D(rawSource, false, true, WPos.Zero, InternalSoundVolume, false); video = soundEngine.Play2D(rawSource, false, true, WPos.Zero, InternalSoundVolume, false);
} }
@@ -356,7 +356,7 @@ namespace OpenRA
if (!string.IsNullOrEmpty(name) && (p == null || p == p.World.LocalPlayer)) if (!string.IsNullOrEmpty(name) && (p == null || p == p.World.LocalPlayer))
soundEngine.Play2D(sounds[name], soundEngine.Play2D(sounds[name],
false, relative, pos, false, relative, pos,
(InternalSoundVolume * volumeModifier), attenuateVolume); InternalSoundVolume * volumeModifier, attenuateVolume);
return true; return true;
} }

View File

@@ -65,13 +65,20 @@ namespace OpenRA.Widgets
videoSheet.Texture.ScaleFilter = TextureScaleFilter.Linear; videoSheet.Texture.ScaleFilter = TextureScaleFilter.Linear;
videoSheet.Texture.SetData(video.FrameData); videoSheet.Texture.SetData(video.FrameData);
videoSprite = new Sprite(videoSheet, new Rectangle(0, 0, video.Width, video.Height), TextureChannel.Alpha);
var scale = Math.Min(RenderBounds.Width / video.Width, RenderBounds.Height / (video.Height * AspectRatio)); videoSprite = new Sprite(videoSheet,
videoOrigin = new float2(RenderBounds.X + (RenderBounds.Width - scale * video.Width) / 2, RenderBounds.Y + (RenderBounds.Height - scale * AspectRatio * video.Height) / 2); new Rectangle(
0,
0,
video.Width,
video.Height),
TextureChannel.Alpha);
var scale = Math.Min((float)RenderBounds.Width / video.Width, (float)RenderBounds.Height / video.Height * AspectRatio);
videoOrigin = new float2(RenderBounds.X + (RenderBounds.Width - scale * video.Width) / 2, RenderBounds.Y + (RenderBounds.Height - scale * video.Height * AspectRatio) / 2);
// Round size to integer pixels. Round up to be consistent with the scale calcuation. // Round size to integer pixels. Round up to be consistent with the scale calcuation.
videoSize = new float2((int)Math.Ceiling(video.Width * scale), (int)Math.Ceiling(video.Height * scale * AspectRatio)); videoSize = new float2((int)Math.Ceiling(video.Width * scale), (int)Math.Ceiling(video.Height * AspectRatio * scale));
if (!DrawOverlay) if (!DrawOverlay)
return; return;
@@ -94,23 +101,35 @@ namespace OpenRA.Widgets
if (!stopped && !paused) if (!stopped && !paused)
{ {
var nextFrame = (int)float2.Lerp(0, video.Frames, Sound.VideoSeekPosition * invLength); var nextFrame = 0;
if (video.HasAudio)
nextFrame = (int)float2.Lerp(0, video.Frames, Sound.VideoSeekPosition * invLength);
else
nextFrame = video.CurrentFrame + 1;
if (nextFrame > video.Frames) if (nextFrame > video.Frames)
{ {
Stop(); Stop();
return; return;
} }
var skippedFrames = 0;
while (nextFrame > video.CurrentFrame) while (nextFrame > video.CurrentFrame)
{ {
video.AdvanceFrame(); video.AdvanceFrame();
if (nextFrame == video.CurrentFrame) videoSprite.sheet.Texture.SetData(video.FrameData);
videoSprite.sheet.Texture.SetData(video.FrameData); skippedFrames++;
} }
if (skippedFrames > 1)
Log.Write("perf", "VqaPlayer : {0} skipped {1} frames at position {2}", cachedVideo, skippedFrames, video.CurrentFrame);
} }
Game.Renderer.RgbaSpriteRenderer.DrawSprite(videoSprite, videoOrigin, videoSize); Game.Renderer.RgbaSpriteRenderer.DrawSprite(
videoSprite,
videoOrigin,
videoSize);
if (DrawOverlay) if (DrawOverlay)
Game.Renderer.RgbaSpriteRenderer.DrawSprite(overlaySprite, videoOrigin, videoSize); Game.Renderer.RgbaSpriteRenderer.DrawSprite(overlaySprite, videoOrigin, videoSize);
} }
@@ -141,7 +160,7 @@ namespace OpenRA.Widgets
onComplete = after; onComplete = after;
if (stopped) if (stopped)
Sound.PlayVideo(video.AudioData); Sound.PlayVideo(video.AudioData, video.AudioChannels, video.SampleBits, video.SampleRate);
else else
Sound.PlayVideo(); Sound.PlayVideo();

View File

@@ -21,6 +21,10 @@ namespace OpenRA.Mods.Common.Widgets.Logic
{ {
public class AssetBrowserLogic public class AssetBrowserLogic
{ {
static readonly string[] AllowedExtensions = { ".shp", ".r8", "tmp", ".tem", ".des", ".sno", ".int", ".jun", ".vqa" };
readonly World world;
Widget panel; Widget panel;
TextFieldWidget filenameInput; TextFieldWidget filenameInput;
@@ -39,10 +43,6 @@ namespace OpenRA.Mods.Common.Widgets.Logic
bool isVideoLoaded = false; bool isVideoLoaded = false;
int currentFrame; int currentFrame;
readonly World world;
static readonly string[] AllowedExtensions = { ".shp", ".r8", "tmp", ".tem", ".des", ".sno", ".int", ".jun", ".vqa" };
[ObjectCreator.UseCtor] [ObjectCreator.UseCtor]
public AssetBrowserLogic(Widget widget, Action onExit, World world) public AssetBrowserLogic(Widget widget, Action onExit, World world)
{ {
@@ -112,7 +112,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
var frameContainer = panel.GetOrNull("FRAME_SELECTOR"); var frameContainer = panel.GetOrNull("FRAME_SELECTOR");
if (frameContainer != null) if (frameContainer != null)
frameContainer.IsVisible = () => (currentSprites != null && currentSprites.Length > 1) || (player != null && player.Video != null && player.Video.Frames > 1); frameContainer.IsVisible = () => (currentSprites != null && currentSprites.Length > 1) || (isVideoLoaded && player != null && player.Video != null && player.Video.Frames > 1);
frameSlider = panel.Get<SliderWidget>("FRAME_SLIDER"); frameSlider = panel.Get<SliderWidget>("FRAME_SLIDER");
if (frameSlider != null) if (frameSlider != null)
@@ -301,9 +301,11 @@ namespace OpenRA.Mods.Common.Widgets.Logic
player = panel.Get<VqaPlayerWidget>("PLAYER"); player = panel.Get<VqaPlayerWidget>("PLAYER");
currentFilename = filename; currentFilename = filename;
player.Load(filename); player.Load(filename);
player.DrawOverlay = false;
isVideoLoaded = true; isVideoLoaded = true;
frameSlider.MaximumValue = (float)player.Video.Frames - 1; frameSlider.MaximumValue = (float)player.Video.Frames - 1;
frameSlider.Ticks = 0; frameSlider.Ticks = 0;
return true;
} }
else else
{ {
@@ -330,7 +332,6 @@ namespace OpenRA.Mods.Common.Widgets.Logic
// TODO: Re-enable "All Packages" once list generation is done in a background thread // TODO: Re-enable "All Packages" once list generation is done in a background thread
// var sources = new[] { (IFolder)null }.Concat(GlobalFileSystem.MountedFolders); // var sources = new[] { (IFolder)null }.Concat(GlobalFileSystem.MountedFolders);
var sources = GlobalFileSystem.MountedFolders; var sources = GlobalFileSystem.MountedFolders;
dropdown.ShowDropDown("LABEL_DROPDOWN_TEMPLATE", 280, sources, setupItem); dropdown.ShowDropDown("LABEL_DROPDOWN_TEMPLATE", 280, sources, setupItem);
return true; return true;
@@ -344,7 +345,6 @@ namespace OpenRA.Mods.Common.Widgets.Logic
// TODO: This is too slow to run in the main thread // TODO: This is too slow to run in the main thread
// var files = AssetSource != null ? AssetSource.AllFileNames() : // var files = AssetSource != null ? AssetSource.AllFileNames() :
// GlobalFileSystem.MountedFolders.SelectMany(f => f.AllFileNames()); // GlobalFileSystem.MountedFolders.SelectMany(f => f.AllFileNames());
if (assetSource == null) if (assetSource == null)
return; return;

View File

@@ -103,6 +103,7 @@ Container@ASSETBROWSER_PANEL:
VqaPlayer@PLAYER: VqaPlayer@PLAYER:
Width: PARENT_RIGHT Width: PARENT_RIGHT
Height: PARENT_BOTTOM Height: PARENT_BOTTOM
AspectRatio: 1
Container@FRAME_SELECTOR: Container@FRAME_SELECTOR:
X: 190 X: 190
Y: 395 Y: 395

View File

@@ -98,6 +98,7 @@ Background@ASSETBROWSER_PANEL:
VqaPlayer@PLAYER: VqaPlayer@PLAYER:
Width: PARENT_RIGHT Width: PARENT_RIGHT
Height: PARENT_BOTTOM Height: PARENT_BOTTOM
AspectRatio: 1
Container@FRAME_SELECTOR: Container@FRAME_SELECTOR:
X: 190 X: 190
Y: 425 Y: 425