2020-01-15 12:07:48 +00:00
|
|
|
#include "Runtime/Character/CFBStreamedCompression.hpp"
|
|
|
|
|
2020-08-02 07:19:05 +00:00
|
|
|
#include <cstring>
|
|
|
|
#include <type_traits>
|
2020-01-15 12:07:48 +00:00
|
|
|
#include "Runtime/Character/CFBStreamedAnimReader.hpp"
|
2016-08-26 22:23:59 +00:00
|
|
|
|
2021-04-10 08:42:06 +00:00
|
|
|
namespace metaforce {
|
2020-08-02 07:19:05 +00:00
|
|
|
namespace {
|
|
|
|
template <typename T>
|
|
|
|
T ReadValue(const u8* data) {
|
|
|
|
static_assert(std::is_trivially_copyable_v<T>);
|
|
|
|
|
|
|
|
T value = 0;
|
|
|
|
std::memcpy(&value, data, sizeof(value));
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void WriteValue(u8* data, T value) {
|
|
|
|
static_assert(std::is_trivially_copyable_v<T>);
|
|
|
|
std::memcpy(data, &value, sizeof(value));
|
|
|
|
}
|
|
|
|
} // Anonymous namespace
|
2016-08-26 22:23:59 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
CFBStreamedCompression::CFBStreamedCompression(CInputStream& in, IObjectStore& objStore, bool pc) : m_pc(pc) {
|
|
|
|
x0_scratchSize = in.readUint32Big();
|
|
|
|
x4_evnt = in.readUint32Big();
|
2016-08-26 22:23:59 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
xc_rotsAndOffs = GetRotationsAndOffsets(x0_scratchSize / 4 + 1, in);
|
2016-08-26 22:23:59 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
if (x4_evnt.IsValid())
|
|
|
|
x8_evntToken = objStore.GetObj(SObjectTag{FOURCC('EVNT'), x4_evnt});
|
2016-09-04 02:27:35 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
x10_averageVelocity = CalculateAverageVelocity(GetPerChannelHeaders());
|
2016-08-26 22:23:59 +00:00
|
|
|
}
|
|
|
|
|
2020-08-02 07:19:05 +00:00
|
|
|
const u32* CFBStreamedCompression::GetTimes() const { return xc_rotsAndOffs.get() + 9; }
|
2016-08-28 19:57:55 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
const u8* CFBStreamedCompression::GetPerChannelHeaders() const {
|
2020-08-02 07:19:05 +00:00
|
|
|
const u32* bitmap = GetTimes();
|
|
|
|
const u32 bitmapWordCount = (bitmap[0] + 31) / 32;
|
2018-12-08 05:30:43 +00:00
|
|
|
return reinterpret_cast<const u8*>(bitmap + bitmapWordCount + 1);
|
2016-08-28 04:45:45 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
const u8* CFBStreamedCompression::GetBitstreamPointer() const {
|
2020-08-02 07:19:05 +00:00
|
|
|
const u32* bitmap = GetTimes();
|
|
|
|
const u32 bitmapWordCount = (bitmap[0] + 31) / 32;
|
2018-12-08 05:30:43 +00:00
|
|
|
|
|
|
|
const u8* chans = reinterpret_cast<const u8*>(bitmap + bitmapWordCount + 1);
|
2020-08-02 07:19:05 +00:00
|
|
|
const u32 boneChanCount = ReadValue<u32>(chans);
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
chans += 4;
|
|
|
|
|
|
|
|
if (m_pc) {
|
2020-08-02 07:19:05 +00:00
|
|
|
for (u32 b = 0; b < boneChanCount; ++b) {
|
2018-12-08 05:30:43 +00:00
|
|
|
chans += 20;
|
|
|
|
|
2020-08-02 07:19:05 +00:00
|
|
|
const u32 tCount = ReadValue<u32>(chans);
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
chans += 4;
|
2020-08-02 07:19:05 +00:00
|
|
|
if (tCount != 0) {
|
2018-12-08 05:30:43 +00:00
|
|
|
chans += 12;
|
2020-08-02 07:19:05 +00:00
|
|
|
}
|
2016-08-28 04:45:45 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
} else {
|
2020-08-02 07:19:05 +00:00
|
|
|
for (u32 b = 0; b < boneChanCount; ++b) {
|
2018-12-08 05:30:43 +00:00
|
|
|
chans += 15;
|
|
|
|
|
2020-08-02 07:19:05 +00:00
|
|
|
const u16 tCount = ReadValue<u16>(chans);
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
chans += 2;
|
2020-08-02 07:19:05 +00:00
|
|
|
if (tCount != 0) {
|
2018-12-08 05:30:43 +00:00
|
|
|
chans += 9;
|
2020-08-02 07:19:05 +00:00
|
|
|
}
|
2016-08-28 04:45:45 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
2016-08-28 04:45:45 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
return chans;
|
2016-08-28 04:45:45 +00:00
|
|
|
}
|
|
|
|
|
2019-08-14 10:04:11 +00:00
|
|
|
std::unique_ptr<u32[]> CFBStreamedCompression::GetRotationsAndOffsets(u32 words, CInputStream& in) const {
|
2018-12-08 05:30:43 +00:00
|
|
|
std::unique_ptr<u32[]> ret(new u32[words]);
|
2016-08-26 22:23:59 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
Header head;
|
|
|
|
head.read(in);
|
2020-08-02 07:19:05 +00:00
|
|
|
std::memcpy(ret.get(), &head, sizeof(head));
|
2016-08-26 22:23:59 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
u32* bitmapOut = &ret[9];
|
2020-08-02 07:19:05 +00:00
|
|
|
const u32 bitmapBitCount = in.readUint32Big();
|
2018-12-08 05:30:43 +00:00
|
|
|
bitmapOut[0] = bitmapBitCount;
|
2020-08-02 07:19:05 +00:00
|
|
|
const u32 bitmapWordCount = (bitmapBitCount + 31) / 32;
|
|
|
|
for (u32 i = 0; i < bitmapWordCount; ++i) {
|
2018-12-08 05:30:43 +00:00
|
|
|
bitmapOut[i + 1] = in.readUint32Big();
|
2020-08-02 07:19:05 +00:00
|
|
|
}
|
2016-08-26 22:23:59 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
in.readUint32Big();
|
|
|
|
u8* chans = reinterpret_cast<u8*>(bitmapOut + bitmapWordCount + 1);
|
|
|
|
u8* bs = ReadBoneChannelDescriptors(chans, in);
|
2020-08-02 07:19:05 +00:00
|
|
|
const u32 bsWords = ComputeBitstreamWords(chans);
|
2016-08-26 22:23:59 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
u32* bsPtr = reinterpret_cast<u32*>(bs);
|
|
|
|
for (u32 w = 0; w < bsWords; ++w)
|
|
|
|
bsPtr[w] = in.readUint32Big();
|
2016-08-26 22:23:59 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
return ret;
|
2016-08-26 22:23:59 +00:00
|
|
|
}
|
|
|
|
|
2019-08-14 10:04:11 +00:00
|
|
|
u8* CFBStreamedCompression::ReadBoneChannelDescriptors(u8* out, CInputStream& in) const {
|
2020-08-02 07:19:05 +00:00
|
|
|
const u32 boneChanCount = in.readUint32Big();
|
|
|
|
WriteValue(out, boneChanCount);
|
2018-12-08 05:30:43 +00:00
|
|
|
out += 4;
|
|
|
|
|
|
|
|
if (m_pc) {
|
2020-08-02 07:19:05 +00:00
|
|
|
for (u32 b = 0; b < boneChanCount; ++b) {
|
|
|
|
WriteValue(out, in.readUint32Big());
|
2018-12-08 05:30:43 +00:00
|
|
|
out += 4;
|
|
|
|
|
2020-08-02 07:19:05 +00:00
|
|
|
WriteValue(out, in.readUint32Big());
|
2018-12-08 05:30:43 +00:00
|
|
|
out += 4;
|
|
|
|
|
|
|
|
for (int i = 0; i < 3; ++i) {
|
2020-08-02 07:19:05 +00:00
|
|
|
WriteValue(out, in.readUint32Big());
|
2018-12-08 05:30:43 +00:00
|
|
|
out += 4;
|
|
|
|
}
|
|
|
|
|
2020-08-02 07:19:05 +00:00
|
|
|
const u32 tCount = in.readUint32Big();
|
|
|
|
WriteValue(out, tCount);
|
2018-12-08 05:30:43 +00:00
|
|
|
out += 4;
|
|
|
|
|
2020-08-02 07:19:05 +00:00
|
|
|
if (tCount != 0) {
|
2018-12-08 05:30:43 +00:00
|
|
|
for (int i = 0; i < 3; ++i) {
|
2020-08-02 07:19:05 +00:00
|
|
|
WriteValue(out, in.readUint32Big());
|
2018-12-08 05:30:43 +00:00
|
|
|
out += 4;
|
2016-08-27 01:02:12 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
2016-08-27 01:02:12 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
} else {
|
2020-08-02 07:19:05 +00:00
|
|
|
for (u32 b = 0; b < boneChanCount; ++b) {
|
|
|
|
WriteValue(out, in.readUint32Big());
|
2018-12-08 05:30:43 +00:00
|
|
|
out += 4;
|
|
|
|
|
2020-08-02 07:19:05 +00:00
|
|
|
WriteValue(out, in.readUint16Big());
|
2018-12-08 05:30:43 +00:00
|
|
|
out += 2;
|
|
|
|
|
|
|
|
for (int i = 0; i < 3; ++i) {
|
2020-08-02 07:19:05 +00:00
|
|
|
WriteValue(out, in.readInt16Big());
|
2018-12-08 05:30:43 +00:00
|
|
|
out += 2;
|
2020-08-02 07:19:05 +00:00
|
|
|
WriteValue(out, in.readUByte());
|
2018-12-08 05:30:43 +00:00
|
|
|
out += 1;
|
|
|
|
}
|
|
|
|
|
2020-08-02 07:19:05 +00:00
|
|
|
const u16 tCount = in.readUint16Big();
|
|
|
|
WriteValue(out, tCount);
|
2018-12-08 05:30:43 +00:00
|
|
|
out += 2;
|
|
|
|
|
2020-08-02 07:19:05 +00:00
|
|
|
if (tCount != 0) {
|
2018-12-08 05:30:43 +00:00
|
|
|
for (int i = 0; i < 3; ++i) {
|
2020-08-02 07:19:05 +00:00
|
|
|
WriteValue(out, in.readInt16Big());
|
2018-12-08 05:30:43 +00:00
|
|
|
out += 2;
|
2020-08-02 07:19:05 +00:00
|
|
|
WriteValue(out, in.readUByte());
|
2018-12-08 05:30:43 +00:00
|
|
|
out += 1;
|
2016-08-26 22:23:59 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
2016-08-26 22:23:59 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
2016-08-26 22:23:59 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
return out;
|
2016-08-26 22:23:59 +00:00
|
|
|
}
|
|
|
|
|
2019-08-14 10:04:11 +00:00
|
|
|
u32 CFBStreamedCompression::ComputeBitstreamWords(const u8* chans) const {
|
2020-08-02 07:19:05 +00:00
|
|
|
const u32 boneChanCount = ReadValue<u32>(chans);
|
2018-12-08 05:30:43 +00:00
|
|
|
chans += 4;
|
|
|
|
|
|
|
|
u32 keyCount;
|
|
|
|
|
|
|
|
u32 totalBits = 0;
|
|
|
|
if (m_pc) {
|
2020-08-02 07:19:05 +00:00
|
|
|
keyCount = ReadValue<u32>(chans + 0x4);
|
2018-12-08 05:30:43 +00:00
|
|
|
for (u32 c = 0; c < boneChanCount; ++c) {
|
|
|
|
chans += 0x8;
|
|
|
|
totalBits += 1;
|
2020-08-02 07:19:05 +00:00
|
|
|
totalBits += ReadValue<u32>(chans) & 0xff;
|
|
|
|
totalBits += ReadValue<u32>(chans + 0x4) & 0xff;
|
|
|
|
totalBits += ReadValue<u32>(chans + 0x8) & 0xff;
|
|
|
|
const u32 tKeyCount = ReadValue<u32>(chans + 0xc);
|
2018-12-08 05:30:43 +00:00
|
|
|
chans += 0x10;
|
2020-08-02 07:19:05 +00:00
|
|
|
if (tKeyCount != 0) {
|
|
|
|
totalBits += ReadValue<u32>(chans) & 0xff;
|
|
|
|
totalBits += ReadValue<u32>(chans + 0x4) & 0xff;
|
|
|
|
totalBits += ReadValue<u32>(chans + 0x8) & 0xff;
|
2018-12-08 05:30:43 +00:00
|
|
|
chans += 0xc;
|
|
|
|
}
|
2016-08-27 01:02:12 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
} else {
|
2020-08-02 07:19:05 +00:00
|
|
|
keyCount = ReadValue<u16>(chans + 0x4);
|
2018-12-08 05:30:43 +00:00
|
|
|
for (u32 c = 0; c < boneChanCount; ++c) {
|
|
|
|
chans += 0x6;
|
|
|
|
totalBits += 1;
|
2020-08-02 07:19:05 +00:00
|
|
|
totalBits += ReadValue<u8>(chans + 0x2);
|
|
|
|
totalBits += ReadValue<u8>(chans + 0x5);
|
|
|
|
totalBits += ReadValue<u8>(chans + 0x8);
|
|
|
|
const u16 tKeyCount = ReadValue<u16>(chans + 0x9);
|
2018-12-08 05:30:43 +00:00
|
|
|
chans += 0xb;
|
2020-08-02 07:19:05 +00:00
|
|
|
if (tKeyCount != 0) {
|
|
|
|
totalBits += ReadValue<u8>(chans + 0x2);
|
|
|
|
totalBits += ReadValue<u8>(chans + 0x5);
|
|
|
|
totalBits += ReadValue<u8>(chans + 0x8);
|
2018-12-08 05:30:43 +00:00
|
|
|
chans += 0x9;
|
|
|
|
}
|
2016-08-26 22:23:59 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
2016-08-26 22:23:59 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
return (totalBits * keyCount + 31) / 32;
|
2016-08-26 22:23:59 +00:00
|
|
|
}
|
|
|
|
|
2019-08-14 10:04:11 +00:00
|
|
|
float CFBStreamedCompression::CalculateAverageVelocity(const u8* chans) const {
|
2020-08-02 07:19:05 +00:00
|
|
|
const u32 boneChanCount = ReadValue<u32>(chans);
|
2018-12-08 05:30:43 +00:00
|
|
|
chans += 4;
|
|
|
|
|
|
|
|
u32 keyCount;
|
|
|
|
u32 rootIdx = 0;
|
|
|
|
if (m_pc) {
|
2020-08-02 07:19:05 +00:00
|
|
|
keyCount = ReadValue<u32>(chans + 0x4);
|
2018-12-08 05:30:43 +00:00
|
|
|
for (u32 c = 0; c < boneChanCount; ++c) {
|
2020-08-02 07:19:05 +00:00
|
|
|
const u32 boneId = ReadValue<u32>(chans);
|
|
|
|
if (boneId == 3) {
|
2018-12-08 05:30:43 +00:00
|
|
|
break;
|
2020-08-02 07:19:05 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
++rootIdx;
|
|
|
|
|
|
|
|
chans += 0x8;
|
2020-08-02 07:19:05 +00:00
|
|
|
const u32 tKeyCount = ReadValue<u32>(chans + 0xc);
|
2018-12-08 05:30:43 +00:00
|
|
|
chans += 0x10;
|
2020-08-02 07:19:05 +00:00
|
|
|
if (tKeyCount != 0) {
|
2018-12-08 05:30:43 +00:00
|
|
|
chans += 0xc;
|
2020-08-02 07:19:05 +00:00
|
|
|
}
|
2016-09-04 02:27:35 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
} else {
|
2020-08-02 07:19:05 +00:00
|
|
|
keyCount = ReadValue<u16>(chans + 0x4);
|
2018-12-08 05:30:43 +00:00
|
|
|
for (u32 c = 0; c < boneChanCount; ++c) {
|
2020-08-02 07:19:05 +00:00
|
|
|
const u32 boneId = ReadValue<u32>(chans);
|
|
|
|
if (boneId == 3) {
|
2018-12-08 05:30:43 +00:00
|
|
|
break;
|
2020-08-02 07:19:05 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
++rootIdx;
|
|
|
|
|
|
|
|
chans += 0x6;
|
2020-08-02 07:19:05 +00:00
|
|
|
const u16 tKeyCount = ReadValue<u16>(chans + 0x9);
|
2018-12-08 05:30:43 +00:00
|
|
|
chans += 0xb;
|
2020-08-02 07:19:05 +00:00
|
|
|
if (tKeyCount != 0) {
|
2018-12-08 05:30:43 +00:00
|
|
|
chans += 0x9;
|
2020-08-02 07:19:05 +00:00
|
|
|
}
|
2016-09-04 02:27:35 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CBitLevelLoader loader(GetBitstreamPointer());
|
|
|
|
CFBStreamedAnimReaderTotals tempTotals(*this);
|
|
|
|
tempTotals.CalculateDown();
|
|
|
|
const float* floats = tempTotals.GetFloats(rootIdx);
|
|
|
|
zeus::CVector3f transCompA(floats[4], floats[5], floats[6]);
|
2016-09-04 02:27:35 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
float accumMag = 0.f;
|
|
|
|
for (u32 i = 0; i < keyCount; ++i) {
|
|
|
|
tempTotals.IncrementInto(loader, *this, tempTotals);
|
2016-09-04 02:27:35 +00:00
|
|
|
tempTotals.CalculateDown();
|
2018-12-08 05:30:43 +00:00
|
|
|
zeus::CVector3f transCompB(floats[4], floats[5], floats[6]);
|
|
|
|
accumMag += (transCompB - transCompA).magnitude();
|
|
|
|
transCompA = transCompB;
|
|
|
|
}
|
2016-09-04 02:27:35 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
return accumMag / GetAnimationDuration().GetSeconds();
|
2016-09-04 02:27:35 +00:00
|
|
|
}
|
|
|
|
|
2021-04-10 08:42:06 +00:00
|
|
|
} // namespace metaforce
|