commit
ad62a914e6
127 files changed
+38545
No files matched your search
@@ -0,0 +1,333 @@
|
||||
#include "adm/adm_metadata.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
|
||||
#include "foundation/py_num.h"
|
||||
|
||||
namespace joc::adm {
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr const char* kBedNames[10] = {
|
||||
"RoomCentricLeft", "RoomCentricRight", "RoomCentricCenter", "RoomCentricLFE",
|
||||
"RoomCentricLeftSideSurround", "RoomCentricRightSideSurround",
|
||||
"RoomCentricLeftRearSurround", "RoomCentricRightRearSurround",
|
||||
"RoomCentricLeftTopSurround", "RoomCentricRightTopSurround"};
|
||||
constexpr const char* kBedLabels[10] = {"RC_L", "RC_R", "RC_C", "RC_LFE", "RC_Lss",
|
||||
"RC_Rss", "RC_Lrs", "RC_Rrs", "RC_Lts", "RC_Rts"};
|
||||
constexpr double kBedPos[10][3] = {{-1.0, 1.0, 0.0}, {1.0, 1.0, 0.0}, {0.0, 1.0, 0.0},
|
||||
{-1.0, 1.0, -1.0}, {-1.0, 0.0, 0.0}, {1.0, 0.0, 0.0},
|
||||
{-1.0, -1.0, 0.0}, {1.0, -1.0, 0.0}, {-1.0, 0.0, 1.0},
|
||||
{1.0, 0.0, 1.0}};
|
||||
|
||||
std::string hex4(std::uint32_t value) {
|
||||
char buffer[16];
|
||||
std::snprintf(buffer, sizeof(buffer), "%04x", value);
|
||||
return std::string(buffer);
|
||||
}
|
||||
|
||||
std::string hex8(std::uint32_t value) {
|
||||
char buffer[16];
|
||||
std::snprintf(buffer, sizeof(buffer), "%08x", value);
|
||||
return std::string(buffer);
|
||||
}
|
||||
|
||||
void put_u16(std::string* out, std::uint16_t value) {
|
||||
char buffer[2];
|
||||
std::memcpy(buffer, &value, 2);
|
||||
out->append(buffer, 2);
|
||||
}
|
||||
|
||||
void put_u32(std::string* out, std::uint32_t value) {
|
||||
char buffer[4];
|
||||
std::memcpy(buffer, &value, 4);
|
||||
out->append(buffer, 4);
|
||||
}
|
||||
|
||||
std::uint8_t checksum(const std::string& segment) {
|
||||
int sum = static_cast<int>(segment.size());
|
||||
for (const char raw : segment) {
|
||||
sum += static_cast<unsigned char>(raw);
|
||||
}
|
||||
return static_cast<std::uint8_t>((~sum + 1) & 0xFF);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
std::string ts(double seconds) {
|
||||
long long whole = static_cast<long long>(seconds);
|
||||
long long fraction = pynum::py_round((seconds - static_cast<double>(whole)) * 100000.0);
|
||||
if (fraction >= 100000) {
|
||||
whole += 1;
|
||||
fraction = 0;
|
||||
}
|
||||
char buffer[32];
|
||||
std::snprintf(buffer, sizeof(buffer), "%02lld:%02lld:%02lld.%05lld", whole / 3600,
|
||||
(whole % 3600) / 60, whole % 60, fraction);
|
||||
return std::string(buffer);
|
||||
}
|
||||
|
||||
bool binaural_mode_from_name(const char* name, BinauralMode* out) {
|
||||
if (name == nullptr || out == nullptr) {
|
||||
return false;
|
||||
}
|
||||
if (std::strcmp(name, "off") == 0) { *out = BinauralMode::Off; return true; }
|
||||
if (std::strcmp(name, "near") == 0) { *out = BinauralMode::Near; return true; }
|
||||
if (std::strcmp(name, "far") == 0) { *out = BinauralMode::Far; return true; }
|
||||
if (std::strcmp(name, "mid") == 0) { *out = BinauralMode::Mid; return true; }
|
||||
if (std::strcmp(name, "unspecified") == 0) { *out = BinauralMode::Unspecified; return true; }
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string build_chna() {
|
||||
std::string out;
|
||||
put_u16(&out, static_cast<std::uint16_t>(kTrackCount));
|
||||
put_u16(&out, static_cast<std::uint16_t>(kTrackCount));
|
||||
for (std::uint32_t i = 0; i < 10; ++i) {
|
||||
put_u16(&out, static_cast<std::uint16_t>(i + 1));
|
||||
out += "ATU_" + hex8(i + 1);
|
||||
out += "AT_0001" + hex4(0x1001 + i) + "_01";
|
||||
out += "AP_00011001";
|
||||
out.push_back('\0');
|
||||
}
|
||||
for (std::uint32_t i = 0; i < kObjectCount; ++i) {
|
||||
put_u16(&out, static_cast<std::uint16_t>(i + 11));
|
||||
out += "ATU_" + hex8(i + 11);
|
||||
out += "AT_0003" + hex4(0x1001 + i) + "_01";
|
||||
out += "AP_0003" + hex4(0x1001 + i);
|
||||
out.push_back('\0');
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
Status build_dbmd(std::uint32_t object_count, BinauralMode mode, std::string* out) {
|
||||
if (out == nullptr) {
|
||||
return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kOutput, "null output");
|
||||
}
|
||||
const std::uint32_t mode_value = static_cast<std::uint32_t>(mode);
|
||||
if (mode_value > 4u) {
|
||||
return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kOutput,
|
||||
"invalid JOC binaural render mode");
|
||||
}
|
||||
out->clear();
|
||||
put_u32(out, 0x01000006u);
|
||||
|
||||
std::string segment7(96, '\0');
|
||||
segment7[1] = static_cast<char>(0x47);
|
||||
segment7[5] = static_cast<char>(0x60);
|
||||
segment7[8] = static_cast<char>(0x24);
|
||||
segment7[9] = static_cast<char>(0x24);
|
||||
out->push_back(7);
|
||||
put_u16(out, 96);
|
||||
out->append(segment7);
|
||||
out->push_back(static_cast<char>(checksum(segment7)));
|
||||
|
||||
std::string segment9(248, '\0');
|
||||
const std::string creator = "Created with EAC3JOC";
|
||||
const std::string renderer = "EAC3JOC Python Renderer";
|
||||
std::memcpy(&segment9[0], creator.data(), creator.size());
|
||||
std::memcpy(&segment9[32], renderer.data(), renderer.size());
|
||||
segment9[96] = 2;
|
||||
segment9[97] = 1;
|
||||
segment9[98] = 0;
|
||||
segment9[103] = 0x03;
|
||||
segment9[106] = 0x01;
|
||||
segment9[111] = 0x22;
|
||||
segment9[112] = static_cast<char>(0xFF);
|
||||
out->push_back(9);
|
||||
put_u16(out, 248);
|
||||
out->append(segment9);
|
||||
out->push_back(static_cast<char>(checksum(segment9)));
|
||||
|
||||
// The reference allocates the body zeroed and then fills only the trailing
|
||||
// `object_count` bytes with 0x84, so the template region stays zero.
|
||||
const std::size_t object_body = 5u + 262u + object_count;
|
||||
std::string segment10(object_body, '\0');
|
||||
const std::uint32_t sync = 0xF8726FBDu;
|
||||
std::memcpy(&segment10[0], &sync, 4);
|
||||
segment10[4] = static_cast<char>(object_count);
|
||||
for (std::size_t i = 5u + 262u; i < segment10.size(); ++i) {
|
||||
segment10[i] = static_cast<char>(0x84);
|
||||
}
|
||||
const std::size_t object_modes = 4u + 2u + 1u + 9u * 15u + object_count;
|
||||
for (std::uint32_t i = 10; i < std::min<std::uint32_t>(object_count, 10u + kObjectCount); ++i) {
|
||||
const std::size_t index = object_modes + i;
|
||||
if (index >= segment10.size()) {
|
||||
return Status::fail(JOC_ERR_INTERNAL, stage::kOutput, "dbmd object slot out of range");
|
||||
}
|
||||
segment10[index] = static_cast<char>((static_cast<unsigned char>(segment10[index]) & 0xF8u) |
|
||||
mode_value);
|
||||
}
|
||||
out->push_back(10);
|
||||
put_u16(out, static_cast<std::uint16_t>(segment10.size()));
|
||||
out->append(segment10);
|
||||
out->push_back(static_cast<char>(checksum(segment10)));
|
||||
out->append("\0\0", 2);
|
||||
return Status::success();
|
||||
}
|
||||
|
||||
std::string build_axml(const std::vector<Track>& tracks, double duration_sec, std::uint32_t rate) {
|
||||
const double scale = static_cast<double>(rate);
|
||||
std::string out;
|
||||
out.reserve(64u * 1024u);
|
||||
const std::string duration_ts = ts(duration_sec);
|
||||
|
||||
out += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
|
||||
out += "<ebuCoreMain xsi:schemaLocation=\"urn:ebu:metadata-schema:ebuCore_2016 ebucore.xsd\" "
|
||||
"lang=\"en\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
|
||||
"xmlns=\"urn:ebu:metadata-schema:ebuCore_2016\">";
|
||||
out += "<coreMetadata><format><audioFormatExtended>";
|
||||
out += "<audioProgramme audioProgrammeID=\"APR_1001\" audioProgrammeName=\"EAC3JOC_Export\" "
|
||||
"start=\"" +
|
||||
ts(0.0) + "\" end=\"" + duration_ts + "\">";
|
||||
out += "<audioContentIDRef>ACO_1001</audioContentIDRef>";
|
||||
out += "<audioContentIDRef>ACO_1002</audioContentIDRef>";
|
||||
out += "</audioProgramme>";
|
||||
out += "<audioContent audioContentID=\"ACO_1001\" "
|
||||
"audioContentName=\"EAC3JOC_Master_Content\">";
|
||||
out += "<audioObjectIDRef>AO_1001</audioObjectIDRef>";
|
||||
out += "<dialogue mixedContentKind=\"0\">2</dialogue>";
|
||||
out += "</audioContent>";
|
||||
out += "<audioContent audioContentID=\"ACO_1002\" audioContentName=\"Objects\">";
|
||||
for (std::uint32_t i = 0; i < kObjectCount; ++i) {
|
||||
out += "<audioObjectIDRef>AO_" + hex4(0x100b + i) + "</audioObjectIDRef>";
|
||||
}
|
||||
out += "<dialogue mixedContentKind=\"0\">2</dialogue>";
|
||||
out += "</audioContent>";
|
||||
out += "<audioObject audioObjectID=\"AO_1001\" audioObjectName=\"Bed\" start=\"" + ts(0.0) +
|
||||
"\" duration=\"" + duration_ts + "\">";
|
||||
out += "<audioPackFormatIDRef>AP_00011001</audioPackFormatIDRef>";
|
||||
for (std::uint32_t i = 0; i < 10; ++i) {
|
||||
out += "<audioTrackUIDRef>ATU_" + hex8(i + 1) + "</audioTrackUIDRef>";
|
||||
}
|
||||
out += "</audioObject>";
|
||||
for (std::uint32_t i = 0; i < kObjectCount; ++i) {
|
||||
out += "<audioObject audioObjectID=\"AO_" + hex4(0x100b + i) +
|
||||
"\" audioObjectName=\"Audio Object " + std::to_string(i + 1) + "\" start=\"" +
|
||||
ts(0.0) + "\" duration=\"" + duration_ts + "\">";
|
||||
out += "<audioPackFormatIDRef>AP_0003" + hex4(0x1001 + i) + "</audioPackFormatIDRef>";
|
||||
out += "<audioTrackUIDRef>ATU_" + hex8(11 + i) + "</audioTrackUIDRef>";
|
||||
out += "</audioObject>";
|
||||
}
|
||||
out += "<audioPackFormat audioPackFormatID=\"AP_00011001\" "
|
||||
"audioPackFormatName=\"EAC3JOCBedPack\" typeDefinition=\"DirectSpeakers\" "
|
||||
"typeLabel=\"0001\">";
|
||||
for (std::uint32_t i = 0; i < 10; ++i) {
|
||||
out += "<audioChannelFormatIDRef>AC_0001" + hex4(0x1001 + i) +
|
||||
"</audioChannelFormatIDRef>";
|
||||
}
|
||||
out += "</audioPackFormat>";
|
||||
for (std::uint32_t i = 0; i < kObjectCount; ++i) {
|
||||
out += "<audioPackFormat audioPackFormatID=\"AP_0003" + hex4(0x1001 + i) +
|
||||
"\" audioPackFormatName=\"JOC_Object_" + std::to_string(i + 1) +
|
||||
"\" typeDefinition=\"Objects\" typeLabel=\"0003\">";
|
||||
out += "<audioChannelFormatIDRef>AC_0003" + hex4(0x1001 + i) +
|
||||
"</audioChannelFormatIDRef>";
|
||||
out += "</audioPackFormat>";
|
||||
}
|
||||
for (std::uint32_t i = 0; i < 10; ++i) {
|
||||
out += "<audioChannelFormat audioChannelFormatID=\"AC_0001" + hex4(0x1001 + i) +
|
||||
"\" audioChannelFormatName=\"" + kBedNames[i] +
|
||||
"\" typeDefinition=\"DirectSpeakers\" typeLabel=\"0001\">";
|
||||
out += "<audioBlockFormat audioBlockFormatID=\"AB_0001" + hex4(0x1001 + i) +
|
||||
"_00000001\">";
|
||||
out += "<cartesian>1</cartesian>";
|
||||
out += "<position coordinate=\"X\">" + pynum::format_fixed(kBedPos[i][0], 10) +
|
||||
"</position>";
|
||||
out += "<position coordinate=\"Y\">" + pynum::format_fixed(kBedPos[i][1], 10) +
|
||||
"</position>";
|
||||
if (kBedPos[i][2] != 0.0) {
|
||||
out += "<position coordinate=\"Z\">" + pynum::format_fixed(kBedPos[i][2], 10) +
|
||||
"</position>";
|
||||
}
|
||||
out += std::string("<speakerLabel>") + kBedLabels[i] + "</speakerLabel>";
|
||||
out += "</audioBlockFormat>";
|
||||
out += "</audioChannelFormat>";
|
||||
}
|
||||
for (std::size_t i = 0; i < tracks.size(); ++i) {
|
||||
const Track& track = tracks[i];
|
||||
out += "<audioChannelFormat audioChannelFormatID=\"AC_0003" +
|
||||
hex4(0x1001 + static_cast<std::uint32_t>(i)) + "\" audioChannelFormatName=\"" +
|
||||
track.name + "\" typeDefinition=\"Objects\" typeLabel=\"0003\">";
|
||||
for (std::size_t k = 0; k < track.blocks.size(); ++k) {
|
||||
const Keyframe& block = track.blocks[k];
|
||||
out += "<audioBlockFormat audioBlockFormatID=\"AB_0003" +
|
||||
hex4(0x1001 + static_cast<std::uint32_t>(i)) + "_" +
|
||||
hex8(static_cast<std::uint32_t>(k + 1)) + "\" rtime=\"" +
|
||||
ts(static_cast<double>(block.rtime_samples) / scale) + "\" duration=\"" +
|
||||
ts(static_cast<double>(block.duration_samples) / scale) + "\">";
|
||||
out += "<cartesian>1</cartesian>";
|
||||
out += "<position coordinate=\"X\">" + pynum::format_fixed(block.x, 10) + "</position>";
|
||||
out += "<position coordinate=\"Y\">" + pynum::format_fixed(block.y, 10) + "</position>";
|
||||
if (block.z != 0.0) {
|
||||
out += "<position coordinate=\"Z\">" + pynum::format_fixed(block.z, 10) +
|
||||
"</position>";
|
||||
}
|
||||
out += "<jumpPosition interpolationLength=\"" +
|
||||
pynum::format_fixed(static_cast<double>(block.interpolation_samples) / scale,
|
||||
5) +
|
||||
"\">1</jumpPosition>";
|
||||
out += "</audioBlockFormat>";
|
||||
}
|
||||
out += "</audioChannelFormat>";
|
||||
}
|
||||
for (std::uint32_t i = 0; i < 10; ++i) {
|
||||
out += "<audioTrackUID UID=\"ATU_" + hex8(i + 1) +
|
||||
"\" bitDepth=\"24\" sampleRate=\"48000\">";
|
||||
out += "<audioTrackFormatIDRef>AT_0001" + hex4(0x1001 + i) + "_01</audioTrackFormatIDRef>";
|
||||
out += "<audioPackFormatIDRef>AP_00011001</audioPackFormatIDRef>";
|
||||
out += "</audioTrackUID>";
|
||||
}
|
||||
for (std::uint32_t i = 0; i < kObjectCount; ++i) {
|
||||
out += "<audioTrackUID UID=\"ATU_" + hex8(11 + i) +
|
||||
"\" bitDepth=\"24\" sampleRate=\"48000\">";
|
||||
out += "<audioTrackFormatIDRef>AT_0003" + hex4(0x1001 + i) + "_01</audioTrackFormatIDRef>";
|
||||
out += "<audioPackFormatIDRef>AP_0003" + hex4(0x1001 + i) + "</audioPackFormatIDRef>";
|
||||
out += "</audioTrackUID>";
|
||||
}
|
||||
for (std::uint32_t i = 0; i < 10; ++i) {
|
||||
out += "<audioTrackFormat audioTrackFormatID=\"AT_0001" + hex4(0x1001 + i) +
|
||||
"_01\" audioTrackFormatName=\"PCM_" + kBedNames[i] +
|
||||
"\" formatDefinition=\"PCM\" formatLabel=\"0001\">";
|
||||
out += "<audioStreamFormatIDRef>AS_0001" + hex4(0x1001 + i) +
|
||||
"</audioStreamFormatIDRef>";
|
||||
out += "</audioTrackFormat>";
|
||||
}
|
||||
for (std::uint32_t i = 0; i < kObjectCount; ++i) {
|
||||
out += "<audioTrackFormat audioTrackFormatID=\"AT_0003" + hex4(0x1001 + i) +
|
||||
"_01\" audioTrackFormatName=\"PCM_JOC_Object_" + std::to_string(i + 1) +
|
||||
"\" formatDefinition=\"PCM\" formatLabel=\"0001\">";
|
||||
out += "<audioStreamFormatIDRef>AS_0003" + hex4(0x1001 + i) +
|
||||
"</audioStreamFormatIDRef>";
|
||||
out += "</audioTrackFormat>";
|
||||
}
|
||||
for (std::uint32_t i = 0; i < 10; ++i) {
|
||||
out += "<audioStreamFormat audioStreamFormatID=\"AS_0001" + hex4(0x1001 + i) +
|
||||
"\" audioStreamFormatName=\"PCM_" + kBedNames[i] +
|
||||
"\" formatDefinition=\"PCM\" formatLabel=\"0001\">";
|
||||
out += "<audioChannelFormatIDRef>AC_0001" + hex4(0x1001 + i) +
|
||||
"</audioChannelFormatIDRef>";
|
||||
out += "<audioPackFormatIDRef>AP_00011001</audioPackFormatIDRef>";
|
||||
out += "<audioTrackFormatIDRef>AT_0001" + hex4(0x1001 + i) +
|
||||
"_01</audioTrackFormatIDRef>";
|
||||
out += "</audioStreamFormat>";
|
||||
}
|
||||
for (std::uint32_t i = 0; i < kObjectCount; ++i) {
|
||||
out += "<audioStreamFormat audioStreamFormatID=\"AS_0003" + hex4(0x1001 + i) +
|
||||
"\" audioStreamFormatName=\"PCM_JOC_Object_" + std::to_string(i + 1) +
|
||||
"\" formatDefinition=\"PCM\" formatLabel=\"0001\">";
|
||||
out += "<audioChannelFormatIDRef>AC_0003" + hex4(0x1001 + i) +
|
||||
"</audioChannelFormatIDRef>";
|
||||
out += "<audioPackFormatIDRef>AP_0003" + hex4(0x1001 + i) + "</audioPackFormatIDRef>";
|
||||
out += "<audioTrackFormatIDRef>AT_0003" + hex4(0x1001 + i) +
|
||||
"_01</audioTrackFormatIDRef>";
|
||||
out += "</audioStreamFormat>";
|
||||
}
|
||||
out += "</audioFormatExtended></format></coreMetadata>";
|
||||
out += "</ebuCoreMain>";
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace joc::adm
|
||||
@@ -0,0 +1,43 @@
|
||||
// Port of src/adm_atmos.py.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "foundation/status.h"
|
||||
|
||||
namespace joc::adm {
|
||||
|
||||
inline constexpr int kObjectCount = 15;
|
||||
inline constexpr std::uint32_t kTrackCount = 25;
|
||||
|
||||
struct Keyframe {
|
||||
std::int64_t rtime_samples = 0;
|
||||
double x = 0.0;
|
||||
double y = 0.0;
|
||||
double z = 0.0;
|
||||
std::int64_t duration_samples = 0;
|
||||
std::int64_t interpolation_samples = 0;
|
||||
};
|
||||
|
||||
struct Track {
|
||||
std::string name;
|
||||
std::vector<Keyframe> blocks;
|
||||
};
|
||||
|
||||
// HH:MM:SS.fffff with the reference's truncation + round-half-even carry.
|
||||
std::string ts(double seconds);
|
||||
|
||||
enum class BinauralMode : std::uint32_t { Off = 0, Near = 1, Far = 2, Mid = 3, Unspecified = 4 };
|
||||
|
||||
bool binaural_mode_from_name(const char* name, BinauralMode* out);
|
||||
|
||||
std::string build_axml(const std::vector<Track>& tracks, double duration_sec, std::uint32_t rate);
|
||||
|
||||
std::string build_chna();
|
||||
|
||||
Status build_dbmd(std::uint32_t object_count, BinauralMode mode, std::string* out);
|
||||
|
||||
} // namespace joc::adm
|
||||
@@ -0,0 +1,281 @@
|
||||
#include "adm/adm_tracks.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
|
||||
#include "foundation/geometry.h"
|
||||
|
||||
namespace joc::adm {
|
||||
|
||||
namespace {
|
||||
|
||||
struct Point {
|
||||
std::int64_t sample = 0;
|
||||
double x = 0.0;
|
||||
double y = 0.0;
|
||||
double z = 0.0;
|
||||
std::int64_t interpolation_samples = 0;
|
||||
};
|
||||
|
||||
// otherwise (the reference drops silently).
|
||||
void append_point(std::vector<Point>* points, std::int64_t sample, double x, double y, double z,
|
||||
std::int64_t interpolation_samples) {
|
||||
if (!points->empty() && sample == points->back().sample) {
|
||||
points->back() = Point{sample, x, y, z, interpolation_samples};
|
||||
} else if (points->empty() || sample > points->back().sample) {
|
||||
points->push_back(Point{sample, x, y, z, interpolation_samples});
|
||||
}
|
||||
}
|
||||
|
||||
void lerp(double ax, double ay, double az, double bx, double by, double bz, double amount,
|
||||
double* x, double* y, double* z) {
|
||||
*x = ax + (bx - ax) * amount;
|
||||
*y = ay + (by - ay) * amount;
|
||||
*z = az + (bz - az) * amount;
|
||||
}
|
||||
|
||||
void points_to_blocks(const std::vector<Point>& points, std::int64_t total_samples,
|
||||
std::vector<Keyframe>* out) {
|
||||
for (std::size_t index = 0; index < points.size(); ++index) {
|
||||
const Point& point = points[index];
|
||||
const std::int64_t end =
|
||||
(index + 1 < points.size()) ? points[index + 1].sample : total_samples;
|
||||
const std::int64_t duration = std::max<std::int64_t>(0, end - point.sample);
|
||||
if (duration == 0) {
|
||||
continue;
|
||||
}
|
||||
Keyframe keyframe;
|
||||
keyframe.rtime_samples = point.sample;
|
||||
keyframe.x = point.x;
|
||||
keyframe.y = point.y;
|
||||
keyframe.z = point.z;
|
||||
keyframe.duration_samples = duration;
|
||||
keyframe.interpolation_samples = std::min(point.interpolation_samples, duration);
|
||||
out->push_back(keyframe);
|
||||
}
|
||||
}
|
||||
|
||||
Status non_monotonic(const char* name, const char* message, int object_index, std::int64_t sample,
|
||||
std::int64_t previous_sample) {
|
||||
return Status::fail(JOC_ERR_OAMD_UNSUPPORTED_VARIANT, stage::kOamd,
|
||||
std::string(name) + ": " + message + " (object " +
|
||||
std::to_string(object_index) + ", sample " + std::to_string(sample) +
|
||||
", previous " + std::to_string(previous_sample) + ")");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Status expand_compact(const std::vector<OamdEvent>& events, std::int64_t total_samples, std::uint32_t rate,
|
||||
std::int64_t update_quantum_samples, std::int64_t object_delay_samples,
|
||||
int object_index, std::vector<Keyframe>* out) {
|
||||
out->clear();
|
||||
const double scale = static_cast<double>(rate);
|
||||
if (events.empty()) {
|
||||
Keyframe keyframe;
|
||||
keyframe.rtime_samples = 0;
|
||||
keyframe.duration_samples = total_samples;
|
||||
keyframe.interpolation_samples = 0;
|
||||
keyframe.x = 0.0;
|
||||
keyframe.y = 0.0;
|
||||
keyframe.z = 0.0;
|
||||
out->push_back(keyframe);
|
||||
return Status::success();
|
||||
}
|
||||
|
||||
std::vector<Point> points;
|
||||
double current[3] = {events[0].x, events[0].y, events[0].z};
|
||||
append_point(&points, 0, current[0], current[1], current[2], 0);
|
||||
|
||||
for (std::size_t index = 1; index < events.size(); ++index) {
|
||||
const OamdEvent& event = events[index];
|
||||
const std::int64_t event_start = event.sample + object_delay_samples;
|
||||
if (event_start >= total_samples) {
|
||||
break;
|
||||
}
|
||||
const std::int64_t effective_ramp =
|
||||
std::max<std::int64_t>(0, event.ramp_samples - update_quantum_samples);
|
||||
const std::int64_t block_start =
|
||||
event_start + (effective_ramp != 0 ? update_quantum_samples : 0);
|
||||
if (block_start >= total_samples) {
|
||||
break;
|
||||
}
|
||||
const std::int64_t ramp_end = block_start + effective_ramp;
|
||||
|
||||
if (block_start < points.back().sample) {
|
||||
return non_monotonic("non_monotonic_compact_position_updates",
|
||||
"compact object position update moved backwards", object_index,
|
||||
block_start, points.back().sample);
|
||||
}
|
||||
if (index + 1 < events.size()) {
|
||||
const std::int64_t next_event_start = events[index + 1].sample + object_delay_samples;
|
||||
const std::int64_t next_effective =
|
||||
std::max<std::int64_t>(0, events[index + 1].ramp_samples - update_quantum_samples);
|
||||
const std::int64_t next_block_start =
|
||||
next_event_start + (next_effective != 0 ? update_quantum_samples : 0);
|
||||
if (next_block_start < ramp_end) {
|
||||
return non_monotonic("overlapping_compact_position_ramps",
|
||||
"a new position update arrived before the previous compact "
|
||||
"ramp finished",
|
||||
object_index, block_start, ramp_end);
|
||||
}
|
||||
}
|
||||
|
||||
double target[3] = {event.x, event.y, event.z};
|
||||
std::int64_t interpolation = effective_ramp;
|
||||
const std::int64_t available = total_samples - block_start;
|
||||
if (effective_ramp > available) {
|
||||
const double amount =
|
||||
static_cast<double>(available) / static_cast<double>(effective_ramp);
|
||||
double x = 0.0;
|
||||
double y = 0.0;
|
||||
double z = 0.0;
|
||||
lerp(current[0], current[1], current[2], event.x, event.y, event.z, amount, &x, &y, &z);
|
||||
target[0] = x;
|
||||
target[1] = y;
|
||||
target[2] = z;
|
||||
interpolation = available;
|
||||
}
|
||||
append_point(&points, block_start, target[0], target[1], target[2], interpolation);
|
||||
current[0] = event.x;
|
||||
current[1] = event.y;
|
||||
current[2] = event.z;
|
||||
}
|
||||
|
||||
points_to_blocks(points, total_samples, out);
|
||||
(void)scale;
|
||||
return Status::success();
|
||||
}
|
||||
|
||||
Status expand_dense64(const std::vector<OamdEvent>& events, std::int64_t total_samples, std::uint32_t rate,
|
||||
std::int64_t update_quantum_samples, std::int64_t object_delay_samples,
|
||||
int object_index, std::vector<Keyframe>* out) {
|
||||
out->clear();
|
||||
(void)rate;
|
||||
if (events.empty()) {
|
||||
Keyframe keyframe;
|
||||
keyframe.duration_samples = total_samples;
|
||||
out->push_back(keyframe);
|
||||
return Status::success();
|
||||
}
|
||||
|
||||
std::vector<Point> points;
|
||||
double current[3] = {events[0].x, events[0].y, events[0].z};
|
||||
append_point(&points, 0, current[0], current[1], current[2], 0);
|
||||
|
||||
for (std::size_t index = 1; index < events.size(); ++index) {
|
||||
const OamdEvent& event = events[index];
|
||||
const std::int64_t start = event.sample + object_delay_samples;
|
||||
if (start >= total_samples) {
|
||||
break;
|
||||
}
|
||||
if (start < points.back().sample) {
|
||||
return non_monotonic("non_monotonic_position_updates",
|
||||
"object position update moved backwards", object_index, start,
|
||||
points.back().sample);
|
||||
}
|
||||
if (start > points.back().sample) {
|
||||
append_point(&points, start, current[0], current[1], current[2], 0);
|
||||
}
|
||||
const std::int64_t effective_ramp =
|
||||
std::max<std::int64_t>(0, event.ramp_samples - update_quantum_samples);
|
||||
if (effective_ramp == 0) {
|
||||
append_point(&points, start, event.x, event.y, event.z, 0);
|
||||
current[0] = event.x;
|
||||
current[1] = event.y;
|
||||
current[2] = event.z;
|
||||
continue;
|
||||
}
|
||||
const std::int64_t steps =
|
||||
(effective_ramp + update_quantum_samples - 1) / update_quantum_samples;
|
||||
const std::int64_t end = start + steps * update_quantum_samples;
|
||||
if (index + 1 < events.size()) {
|
||||
const std::int64_t next_start = events[index + 1].sample + object_delay_samples;
|
||||
if (next_start < end) {
|
||||
return non_monotonic("overlapping_position_ramps",
|
||||
"a new position update arrived before the previous ramp "
|
||||
"finished",
|
||||
object_index, start, end);
|
||||
}
|
||||
}
|
||||
std::int64_t future = effective_ramp;
|
||||
std::int64_t elapsed = 0;
|
||||
double position[3] = {current[0], current[1], current[2]};
|
||||
while (future > 0) {
|
||||
const double amount =
|
||||
std::min(static_cast<double>(update_quantum_samples) / static_cast<double>(future),
|
||||
1.0);
|
||||
double x = 0.0;
|
||||
double y = 0.0;
|
||||
double z = 0.0;
|
||||
lerp(position[0], position[1], position[2], event.x, event.y, event.z, amount, &x, &y,
|
||||
&z);
|
||||
position[0] = x;
|
||||
position[1] = y;
|
||||
position[2] = z;
|
||||
elapsed += update_quantum_samples;
|
||||
const std::int64_t sample = start + elapsed;
|
||||
if (sample >= total_samples) {
|
||||
break;
|
||||
}
|
||||
append_point(&points, sample, position[0], position[1], position[2],
|
||||
update_quantum_samples);
|
||||
future -= update_quantum_samples;
|
||||
}
|
||||
current[0] = event.x;
|
||||
current[1] = event.y;
|
||||
current[2] = event.z;
|
||||
}
|
||||
|
||||
points_to_blocks(points, total_samples, out);
|
||||
return Status::success();
|
||||
}
|
||||
|
||||
void TrajectoryBuilder::submit_frame(std::int64_t frame_index, const oamd::OamdUpdate* update,
|
||||
std::int64_t outer_offset) {
|
||||
std::int64_t event_sample = frame_index * 1536;
|
||||
std::int64_t ramp_samples = 0;
|
||||
if (update != nullptr) {
|
||||
state_.apply(*update);
|
||||
event_sample += outer_offset + static_cast<std::int64_t>(update->block_offset_samples);
|
||||
ramp_samples = static_cast<std::int64_t>(update->ramp_duration_samples);
|
||||
}
|
||||
for (int object = 1; object <= kObjectCount; ++object) {
|
||||
double x = 0.0;
|
||||
double y = 0.0;
|
||||
double z = 0.0;
|
||||
geometry::q_to_adm_xyz(state_.q(object, 0), state_.q(object, 1), state_.q(object, 2), &x,
|
||||
&y, &z);
|
||||
const int slot = object - 1;
|
||||
if (!has_previous_[slot] || previous_[slot][0] != x || previous_[slot][1] != y ||
|
||||
previous_[slot][2] != z) {
|
||||
events_[slot].push_back(OamdEvent{event_sample, x, y, z, ramp_samples});
|
||||
previous_[slot][0] = x;
|
||||
previous_[slot][1] = y;
|
||||
previous_[slot][2] = z;
|
||||
has_previous_[slot] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Status TrajectoryBuilder::build(std::int64_t total_samples, TrajectoryMode mode,
|
||||
std::vector<Track>* out) const {
|
||||
out->clear();
|
||||
out->reserve(kObjectCount);
|
||||
for (int object = 1; object <= kObjectCount; ++object) {
|
||||
Track track;
|
||||
track.name = "JOC_Object_" + std::to_string(object);
|
||||
const Status status =
|
||||
(mode == TrajectoryMode::Compact)
|
||||
? expand_compact(events_[object - 1], total_samples, rate_, quantum_,
|
||||
object_delay_, object, &track.blocks)
|
||||
: expand_dense64(events_[object - 1], total_samples, rate_, quantum_,
|
||||
object_delay_, object, &track.blocks);
|
||||
if (!status.ok()) {
|
||||
return status;
|
||||
}
|
||||
out->push_back(std::move(track));
|
||||
}
|
||||
return Status::success();
|
||||
}
|
||||
|
||||
} // namespace joc::adm
|
||||
@@ -0,0 +1,61 @@
|
||||
// Port of src/oamd_tracks.py.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "adm/adm_metadata.h"
|
||||
#include "foundation/status.h"
|
||||
#include "oamd/oamd_parser.h"
|
||||
|
||||
namespace joc::adm {
|
||||
|
||||
struct OamdEvent {
|
||||
std::int64_t sample = 0;
|
||||
double x = 0.0;
|
||||
double y = 0.0;
|
||||
double z = 0.0;
|
||||
std::int64_t ramp_samples = 0;
|
||||
};
|
||||
|
||||
enum class TrajectoryMode { Compact, Dense64 };
|
||||
|
||||
// Feeds the same per-frame OAMD state machine the reference's build_adm_tracks
|
||||
// runs, and records one event per object whenever its coordinates change.
|
||||
class TrajectoryBuilder {
|
||||
public:
|
||||
TrajectoryBuilder(std::uint32_t rate = 48000, std::int64_t update_quantum_samples = 64,
|
||||
std::int64_t object_delay_samples = 1473)
|
||||
: rate_(rate),
|
||||
quantum_(update_quantum_samples),
|
||||
object_delay_(object_delay_samples) {}
|
||||
|
||||
void submit_frame(std::int64_t frame_index, const oamd::OamdUpdate* update, std::int64_t outer_offset);
|
||||
|
||||
Status build(std::int64_t total_samples, TrajectoryMode mode, std::vector<Track>* out) const;
|
||||
|
||||
const std::vector<OamdEvent>& events(int object_index) const { return events_[object_index]; }
|
||||
std::uint32_t rate() const { return rate_; }
|
||||
std::int64_t object_delay_samples() const { return object_delay_; }
|
||||
|
||||
private:
|
||||
std::uint32_t rate_;
|
||||
std::int64_t quantum_;
|
||||
std::int64_t object_delay_;
|
||||
oamd::OamdState state_;
|
||||
std::vector<OamdEvent> events_[kObjectCount];
|
||||
bool has_previous_[kObjectCount] = {};
|
||||
double previous_[kObjectCount][3] = {};
|
||||
};
|
||||
|
||||
Status expand_compact(const std::vector<OamdEvent>& events, std::int64_t total_samples, std::uint32_t rate,
|
||||
std::int64_t update_quantum_samples, std::int64_t object_delay_samples,
|
||||
int object_index, std::vector<Keyframe>* out);
|
||||
|
||||
Status expand_dense64(const std::vector<OamdEvent>& events, std::int64_t total_samples, std::uint32_t rate,
|
||||
std::int64_t update_quantum_samples, std::int64_t object_delay_samples,
|
||||
int object_index, std::vector<Keyframe>* out);
|
||||
|
||||
} // namespace joc::adm
|
||||
Reference in new issue
Block a user