Support JOC downmix configurations 0..4 and update the vendored kernel
build / windows (push) Has been cancelled
build / release (push) Has been cancelled

This commit is contained in:
TheM14 committed 2026-10-16 19:56:00 +08:00
1 parent cb840af52f
commit 2cfebe914d
32 files changed
+2055 -300

No files matched your search

+112 -5
View File
@@ -249,16 +249,120 @@ bool contains_payload(const std::uint8_t* frame, std::size_t size, std::uint8_t
} // namespace
std::size_t frame_bytes_at(const std::uint8_t* data, std::size_t size, std::size_t offset) {
if (data == nullptr || offset + 4u > size) return 0;
namespace {
std::uint32_t read_bits(const std::uint8_t* data, std::size_t start, std::size_t count) {
std::uint32_t value = 0;
for (std::size_t index = start; index < start + count; ++index) {
value = (value << 1) | ((data[index >> 3] >> (7u - (index & 7u))) & 1u);
}
return value;
}
// AC-3 syncframe length in 16-bit words, indexed by fscod then frmsizecod.
const std::uint16_t kAc3FrameWords[3][38] = {
{64, 64, 80, 80, 96, 96, 112, 112, 128, 128, 160, 160, 192, 192, 224, 224,
256, 256, 320, 320, 384, 384, 448, 448, 512, 512, 640, 640, 768, 768,
896, 896, 1024, 1024, 1152, 1152, 1280, 1280},
{69, 70, 87, 88, 104, 105, 121, 122, 139, 140, 174, 175, 208, 209, 243, 244,
278, 278, 348, 348, 417, 418, 487, 487, 557, 557, 696, 696, 835, 835,
975, 975, 1114, 1114, 1253, 1253, 1393, 1393},
{96, 96, 120, 120, 144, 144, 168, 168, 192, 192, 240, 240, 288, 288, 336, 336,
384, 384, 480, 480, 576, 576, 672, 672, 768, 768, 960, 960, 1152, 1152,
1344, 1344, 1536, 1536, 1728, 1728, 1920, 1920},
};
} // namespace
std::size_t frame_header_at(const std::uint8_t* data, std::size_t size, std::size_t offset,
FrameHeader* out) {
if (data == nullptr || offset + 6u > size) return 0;
const std::uint16_t syncword =
static_cast<std::uint16_t>((static_cast<std::uint16_t>(data[offset]) << 8) | data[offset + 1]);
if (syncword != kEac3Syncword) return 0;
const std::size_t base = offset * 8u;
const std::uint32_t bsid = read_bits(data, base + 40u, 5u);
if (bsid <= 8u) {
const std::uint32_t fscod = read_bits(data, base + 32u, 2u);
const std::uint32_t frmsizecod = read_bits(data, base + 34u, 6u);
if (fscod > 2u || frmsizecod >= 38u) return 0;
if (out != nullptr) {
out->frame_bytes = static_cast<std::size_t>(kAc3FrameWords[fscod][frmsizecod]) * 2u;
out->offset = offset;
out->codec = 0;
out->strmtyp = 0;
out->substreamid = 0;
}
return static_cast<std::size_t>(kAc3FrameWords[fscod][frmsizecod]) * 2u;
}
const std::size_t words =
(static_cast<std::size_t>(data[offset + 2] & 0x07u) << 8) | data[offset + 3];
if (out != nullptr) {
out->frame_bytes = (words + 1u) * 2u;
out->offset = offset;
out->codec = 1;
out->strmtyp = static_cast<std::uint8_t>(read_bits(data, base + 16u, 2u));
out->substreamid = static_cast<std::uint8_t>(read_bits(data, base + 18u, 3u));
}
return (words + 1u) * 2u;
}
std::size_t frame_bytes_at(const std::uint8_t* data, std::size_t size, std::size_t offset) {
return frame_header_at(data, size, offset, nullptr);
}
std::size_t audio_frame_bytes_at(const std::uint8_t* data, std::size_t size, std::size_t offset,
bool end_of_stream) {
constexpr std::size_t kMaxSubstreams = 8;
FrameHeader seen[kMaxSubstreams];
std::size_t count = 0;
std::size_t consumed = 0;
while (count < kMaxSubstreams) {
FrameHeader header;
const std::size_t bytes = frame_header_at(data, size, offset + consumed, &header);
if (bytes == 0 || offset + consumed + bytes > size) break;
bool repeats = false;
for (std::size_t index = 0; index < count; ++index) {
if (seen[index].same_stream(header)) {
repeats = true;
break;
}
}
if (repeats) return consumed;
seen[count++] = header;
consumed += bytes;
}
if (count != 0u && consumed != 0u && end_of_stream) return consumed;
return 0;
}
bool joc_config_idx(const std::uint8_t* frame, std::size_t frame_bytes, unsigned* out) {
if (out == nullptr || frame == nullptr || frame_bytes < 8u) return false;
std::size_t offsets[4096];
std::size_t count = 0;
marker_offsets(frame, frame_bytes, offsets, 4096, &count);
for (std::size_t i = 0; i < count; ++i) {
Container candidate;
if (!parse_container(frame, frame_bytes * 8u, offsets[i], &candidate)) continue;
if (!candidate.has(kIdOamd) || !candidate.has(kIdJoc)) continue;
for (std::size_t p = 0; p < candidate.payload_count; ++p) {
if (candidate.payloads[p].id != kIdJoc) continue;
// joc_dmx_config_idx is the first 3 bits of the JOC payload, followed by
// joc_num_objects_bits (6) and joc_ext_config_idx (3).
const std::size_t bit = candidate.payloads[p].bit_offset;
std::uint32_t value = 0;
for (unsigned index = 0; index < 3u; ++index) {
const std::size_t position = bit + index;
if (position / 8u >= frame_bytes) return false;
value = (value << 1) | ((frame[position / 8u] >> (7u - (position % 8u))) & 1u);
}
*out = value;
return true;
}
}
return false;
}
bool frame_has_joc(const std::uint8_t* frame, std::size_t frame_bytes) {
if (frame == nullptr || frame_bytes < 8u) return false;
@@ -333,12 +437,15 @@ ScanResult scan(const std::uint8_t* data, std::size_t size, std::size_t max_fram
if (frames == 0) {
result.joc = JocState::kUnknown;
if (result.detail[0] == '\0') result.detail = "no syncframes";
} else if (with_joc == frames) {
} else if (with_joc != 0) {
result.joc = JocState::kYes;
result.detail = "every examined syncframe carries the JOC EMDF container";
result.detail = (with_joc == frames)
? "every examined syncframe carries the JOC EMDF container"
: "the JOC EMDF container is present in part of the examined "
"audio frames (dependent substream or a late first frame)";
} else {
result.joc = JocState::kNo;
result.detail = "at least one examined syncframe has no JOC EMDF container";
result.detail = "no examined syncframe carries a JOC EMDF container";
}
return result;
}
+43 -11
View File
@@ -4,12 +4,14 @@
// because container metadata (MP4 dec3, Matroska A_EAC3) says only "E-AC-3" and
// the JOC flag inside it is frequently missing.
//
// The criterion implemented here is deliberately the same one the rendering core
// applies before it will render a frame: a syncframe carries JOC when it holds
// exactly one top-level EMDF container (syncword 0x5838, any of the eight bit
// alignments) whose payload list contains both ID11 (OAMD) and ID14 (JOC).
// Agreeing with the core matters: a looser test would claim files the core then
// refuses, which is worse than declining them.
// A syncframe carries JOC when it holds exactly one top-level EMDF container
// (syncword 0x5838, any of the eight bit alignments) whose payload list contains
// both ID11 (OAMD) and ID14 (JOC) — the same per-frame test the rendering core
// applies. The stream counts as JOC when at least one examined audio frame
// carries one: in a 7-channel program the JOC payload lives in the dependent
// substream while the independent substream is a plain 5.1 core, and an AC-3
// core may precede it, so "every syncframe carries JOC" is not a valid rule.
// The first audio frame may also carry no metadata at all.
#pragma once
#include <cstddef>
@@ -17,16 +19,26 @@
namespace joc_eac3 {
// E-AC-3 syncframe header fields the plugin needs.
// Syncframe header fields the plugin needs. codec/strmtyp/substreamid identify
// the substream: within one audio frame each combination appears once, which is
// what turns a syncframe stream into audio frames.
struct FrameHeader {
std::size_t frame_bytes = 0;
std::size_t offset = 0;
std::uint8_t codec = 0; // 0 = AC-3, 1 = E-AC-3
std::uint8_t strmtyp = 0;
std::uint8_t substreamid = 0;
bool same_stream(const FrameHeader& other) const {
return codec == other.codec && strmtyp == other.strmtyp &&
substreamid == other.substreamid;
}
};
enum class JocState {
kYes, // every frame examined carried a JOC EMDF container
kNo, // at least one frame examined did not
kUnknown, // not enough data, or the stream is not walkable E-AC-3
kYes, // at least one examined syncframe carried a JOC EMDF container
kNo, // no examined syncframe carried one
kUnknown, // not enough data, or the stream is not walkable ac3/eac3
};
struct ScanResult {
@@ -42,9 +54,29 @@ struct ScanResult {
ScanResult scan(const std::uint8_t* data, std::size_t size, std::size_t max_frames);
// Length of the syncframe starting at data[offset], or 0 when there is no valid
// header there. Mirrors the core's frmsiz handling exactly.
// header there. AC-3 and E-AC-3 are told apart by bsid exactly as the core does:
// bsid <= 8 sizes the frame from fscod/frmsizecod, anything above uses frmsiz.
std::size_t frame_bytes_at(const std::uint8_t* data, std::size_t size, std::size_t offset);
// Same, and fills the substream identity. Returns 0 when no header is there.
std::size_t frame_header_at(const std::uint8_t* data, std::size_t size, std::size_t offset,
FrameHeader* out);
// Length of the whole audio frame starting at data[offset]: every syncframe up to
// (and excluding) the first repeated (codec, strmtyp, substreamid) combination.
// A 7-channel program interleaves an independent and a dependent substream, so one
// audio frame is two syncframes and one bed frame.
//
// Returns 0 when the group is still open inside the buffer -- the caller has to ask
// again with more data -- unless `end_of_stream` says no more bytes will arrive, in
// which case the trailing group is complete by definition.
std::size_t audio_frame_bytes_at(const std::uint8_t* data, std::size_t size, std::size_t offset,
bool end_of_stream);
// Reads joc_dmx_config_idx (the first 3 bits of the JOC payload) out of one
// syncframe. False when the frame carries no JOC payload.
bool joc_config_idx(const std::uint8_t* frame, std::size_t frame_bytes, unsigned* out);
// True when this single syncframe carries the JOC EMDF container.
bool frame_has_joc(const std::uint8_t* frame, std::size_t frame_bytes);
+177 -49
View File
@@ -27,7 +27,11 @@ constexpr std::uint64_t kEac3FramesPerRead = 3u;
// Rendered audio the caller has not taken yet.
constexpr std::size_t kMaxRenderedAheadSamples = 4096u;
constexpr std::size_t kBedFramesChunk = 8192u; // staging capacity, in frames
constexpr std::size_t kBedChannels = 6; // ffmpeg -ac 6
// The bed channel count follows joc_dmx_config_idx: the 5.1 configurations
// (0 and 3) decode to 6 channels through `ffmpeg -ac 6`, the 7-channel ones
// (1, 2 and 4) to 8 channels in the decoder's own layout. See bed_channels.
constexpr std::size_t kDefaultBedChannels = 6;
constexpr std::size_t kMaxBedChannels = 8;
// A read on an anonymous pipe only completes once the whole request is available,
// so each read asks for a slice ffmpeg can always fill, and the pipe buffer is
// sized well above that slice. Requesting the whole staging buffer deadlocks:
@@ -63,6 +67,30 @@ bool has_mov_timeline(const std::string& path) {
return false;
}
// joc_dmx_config_idx of the first syncframe in the window that carries JOC, or -1
// when the window holds none. Only the first three bits of the JOC payload are
// read, so this costs nothing next to a decode.
int scan_config_idx(const std::uint8_t* data, std::size_t size) {
std::size_t offset = 0;
while (offset + 6u <= size) {
const std::size_t bytes = joc_eac3::frame_bytes_at(data, size, offset);
if (bytes == 0 || offset + bytes > size) break;
if (joc_eac3::frame_has_joc(data + offset, bytes)) {
unsigned config = 0;
if (joc_eac3::joc_config_idx(data + offset, bytes, &config)) {
return static_cast<int>(config);
}
}
offset += bytes;
}
return -1;
}
// True when the JOC configuration declares 7 core channels.
bool seven_channel_config(int config) {
return config == 1 || config == 2 || config == 4;
}
// ---------------------------------------------------------------------------
// Kernel entry points.
//
@@ -83,6 +111,7 @@ struct CoreApi {
std::uint32_t(JOC_CALL* abi_version)() = joc_abi_version;
const char*(JOC_CALL* version_string)() = joc_version_string;
const char*(JOC_CALL* error_name)(joc_error) = joc_error_name;
const char*(JOC_CALL* last_error_detail)() = joc_last_error_detail;
};
bool file_exists(const std::string& path) {
@@ -111,6 +140,22 @@ const char* error_text(const CoreApi& api, joc_error code) {
return "unknown core error";
}
// The core keeps a structured detail for the last failing call ("eac3: chanmap
// declares [Lts,Rts] but joc_dmx_config_idx=2 expects [Vhl,Vhr]"). The plugin
// reports the variant rather than only its error code, so a stream the renderer
// deliberately refuses says why.
std::string error_with_detail(const CoreApi& api, joc_error code) {
std::string text = error_text(api, code);
if (api.last_error_detail != nullptr) {
const char* detail = api.last_error_detail();
if (detail != nullptr && *detail != '\0') {
text += ": ";
text += detail;
}
}
return text;
}
// ---------------------------------------------------------------------------
// ffmpeg child process producing the 5.1 core PCM on a pipe.
// ---------------------------------------------------------------------------
@@ -426,40 +471,47 @@ FileProbe probe_file(const std::string& path, std::size_t max_scan_bytes,
return probe;
}
// The renderer pairs one audio frame with exactly 1536 bed samples, so the
// stream length is counted in audio frames: a 7-channel program interleaves an
// independent and a dependent substream, and one audio frame is two syncframes.
const bool whole_file = (buffer.size() == stream_bytes);
std::size_t offset = 0;
std::size_t first_audio_frame = 0;
bool same_audio_frame_size = true;
std::uint64_t seen = 0;
while (true) {
const std::size_t bytes =
joc_eac3::audio_frame_bytes_at(buffer.data(), got, offset, whole_file);
if (bytes == 0 || offset + bytes > got) break;
if (seen == 0) {
first_audio_frame = bytes;
} else if (bytes != first_audio_frame) {
same_audio_frame_size = false;
}
offset += bytes;
++seen;
}
std::uint64_t frames = 0;
if (buffer.size() == stream_bytes) {
std::size_t offset = 0;
while (true) {
const std::size_t bytes = joc_eac3::frame_bytes_at(buffer.data(), got, offset);
if (bytes == 0 || offset + bytes > got) break;
offset += bytes;
++frames;
}
if (whole_file) {
frames = seen;
probe.detail = "frame count walked over the whole file";
} else if (scan.all_frames_same_size) {
frames = stream_bytes / scan.first_frame_bytes;
} else if (seen != 0 && same_audio_frame_size) {
frames = stream_bytes / first_audio_frame;
probe.detail = "frame count extrapolated from a constant frame size";
} else {
} else if (offset != 0) {
// Variable frame size: count in the window and scale by the byte ratio.
std::size_t offset = 0;
std::uint64_t seen = 0;
while (true) {
const std::size_t bytes = joc_eac3::frame_bytes_at(buffer.data(), got, offset);
if (bytes == 0 || offset + bytes > got) break;
offset += bytes;
++seen;
}
frames = (offset != 0) ? static_cast<std::uint64_t>(
(static_cast<double>(stream_bytes) /
static_cast<double>(offset)) *
static_cast<double>(seen))
: 0;
frames = static_cast<std::uint64_t>(
(static_cast<double>(stream_bytes) / static_cast<double>(offset)) *
static_cast<double>(seen));
probe.detail = "frame count estimated from a variable frame size";
}
const int config = scan_config_idx(buffer.data(), got);
probe.frames = frames;
probe.sample_rate = 48000;
probe.channels = 6; // the core audio of an E-AC-3 JOC stream
// Core audio channel count: the 5.1 configurations give 6, the 7-channel ones 8.
probe.channels = static_cast<unsigned>(seven_channel_config(config) ? 8 : 6);
probe.duration_seconds =
static_cast<double>(frames) * static_cast<double>(kFrameSamples) / 48000.0;
return probe;
@@ -570,7 +622,10 @@ struct Engine::Impl {
}
unsigned channels = 0;
std::uint64_t frames_queued = 0; // E-AC-3 frames handed to the core
// Decoded bed channel count, resolved from joc_dmx_config_idx before the bed
// decoder starts: 6 for configurations 0/3, 8 for 1/2/4.
std::size_t bed_channels = kDefaultBedChannels;
std::uint64_t frames_queued = 0; // audio frames handed to the core
std::uint64_t bed_frames_pushed = 0;
bool eac3_eof = false;
bool bed_eof = false;
@@ -650,6 +705,10 @@ struct Engine::Impl {
bool position_bare(std::uint64_t frame, std::string* error);
bool start_bed(std::uint64_t source_sample, std::string* error);
bool start_metadata(std::string* error);
// Resolves bed_channels from the stream's own joc_dmx_config_idx. For a
// container this starts the metadata pipe once, reads a prefix and stops it
// again; for a bare stream it reads a prefix from the file and seeks back.
bool resolve_bed_channels(std::string* error);
};
bool Engine::Impl::grow_frame_index(std::uint64_t wanted, std::string* error) {
@@ -681,9 +740,14 @@ bool Engine::Impl::grow_frame_index(std::uint64_t wanted, std::string* error) {
index_complete = true;
break;
}
// The index holds audio frames, not syncframes: the renderer consumes one
// bed frame per audio frame, and a 7-channel program makes two syncframes
// into one audio frame.
const bool at_end = (index_file_offset + got >= size);
std::size_t consumed = 0;
while (frame_offsets.size() < wanted) {
const std::size_t bytes = joc_eac3::frame_bytes_at(buffer.data(), got, consumed);
const std::size_t bytes =
joc_eac3::audio_frame_bytes_at(buffer.data(), got, consumed, at_end);
if (bytes == 0 || consumed + bytes > got) break;
frame_offsets.push_back(index_stream_offset + consumed);
consumed += bytes;
@@ -734,13 +798,49 @@ bool Engine::Impl::position_bare(std::uint64_t frame, std::string* error) {
return true;
}
bool Engine::Impl::resolve_bed_channels(std::string* error) {
bed_channels = kDefaultBedChannels;
std::vector<std::uint8_t> buffer(1u << 20);
int config = -1;
if (eac3_from_pipe) {
// The metadata stream has to run once to be sniffed; it is stopped again and
// restarted from the beginning, which costs one extra ffmpeg spawn per track.
if (!start_metadata(error)) return false;
const std::size_t got = eac3_pipe.read(buffer.data(), buffer.size());
eac3_pipe.stop();
config = scan_config_idx(buffer.data(), got);
} else {
if (!eac3.is_open() && !eac3.open(input_path)) {
if (error != nullptr) *error = "cannot open the input file";
return false;
}
const std::size_t got = eac3.read(buffer.data(), buffer.size());
config = scan_config_idx(buffer.data(), got);
// Put the file back where the feed loop expects it.
if (!eac3.seek(index_base)) {
if (error != nullptr) *error = "cannot position the input file";
return false;
}
}
bed_channels = seven_channel_config(config)
? static_cast<std::size_t>(7u + 1u)
: kDefaultBedChannels;
if (bed_channels > kMaxBedChannels) bed_channels = kMaxBedChannels;
joc_log::line("core: joc_dmx_config_idx=%d -> %llu bed channel(s)", config,
static_cast<unsigned long long>(bed_channels));
return true;
}
bool Engine::Impl::start_bed(std::uint64_t source_sample, std::string* error) {
// The 5.1 core PCM, exactly as the reference renderer's own core decode does
// it: 5.1 interleaved float32 at 48 kHz, the layout the renderer expects
// (L R C LFE Ls Rs).
// The core PCM the kernel expects: interleaved float32 at 48 kHz, laid out as
// L R C LFE Ls Rs for 5.1 and L R C LFE BL BR SL SR for 7.1.
// -drc_scale 0 -target_level 0: the bed is taken as stored, without the
// stream's dynrng or target-level metadata being applied by the decoder.
//
// -ac is only passed for the 5.1 configurations: `-ac 6` downmixes 5.1.2/7.1
// into 5.1 and drops the extra pair, and `-ac 8` would remap 5.1.2 into 7.1.
// The 7-channel configurations take the decoder's own layout instead.
//
// -ss is an input option: the demuxer is positioned on the frame boundary and
// decoding starts there, so a seek costs the same wherever it lands. The price is
// that the bed is carried by a decoder that started at the seek point rather than
@@ -752,7 +852,11 @@ bool Engine::Impl::start_bed(std::uint64_t source_sample, std::string* error) {
if (!offset.empty()) input_arguments += L" -ss " + offset;
std::wstring bed_arguments = L"-map 0:a:";
bed_arguments += std::to_wstring(settings.audio_index);
bed_arguments += L" -vn -ac 6 -ar 48000 -c:a pcm_f32le -f f32le -";
bed_arguments += L" -vn";
if (bed_channels == kDefaultBedChannels) {
bed_arguments += L" -ac 6";
}
bed_arguments += L" -ar 48000 -c:a pcm_f32le -f f32le -";
return bed.start(settings.ffmpeg_path, input_path, input_arguments, bed_arguments, "bed",
stderr_path_for(L"joc_ffmpeg_bed.log"), error);
}
@@ -804,7 +908,6 @@ bool Engine::start(const std::string& input_path, const Settings& settings, std:
impl.reset_feed_state();
impl.end_sample = sample_of_seconds(settings.length_seconds);
impl.eac3_buffer.resize(kEac3Chunk);
impl.bed_buffer.resize(kBedFramesChunk * kBedChannels);
if (!load_api(std::string(), &impl.api, error)) return false;
const std::uint32_t abi = impl.api.abi_version();
@@ -834,6 +937,11 @@ bool Engine::start(const std::string& input_path, const Settings& settings, std:
}
}
// The bed decoder has to know how many channels to produce before it starts,
// and that comes from the stream's own joc_dmx_config_idx.
if (!impl.resolve_bed_channels(error)) return false;
impl.bed_buffer.resize(kBedFramesChunk * impl.bed_channels);
joc_stream_config config{};
config.struct_size = sizeof(config);
config.struct_version = 1;
@@ -888,7 +996,7 @@ bool Engine::start(const std::string& input_path, const Settings& settings, std:
if (created != JOC_OK) {
if (error != nullptr) {
*error = std::string("cannot create the render stream: ") +
error_text(impl.api, created);
error_with_detail(impl.api, created);
}
return false;
}
@@ -972,7 +1080,7 @@ bool Engine::seek(double seconds, double total_seconds, std::string* error) {
if (reset != JOC_OK) {
if (error != nullptr) {
*error = std::string("cannot reset the render stream: ") +
error_text(impl.api, reset);
error_with_detail(impl.api, reset);
}
return false;
}
@@ -1042,6 +1150,10 @@ std::size_t Engine::read(float* destination, std::size_t frames, std::string* er
const std::size_t got = impl.read_eac3(impl.eac3_buffer.data() + impl.eac3_carry,
impl.eac3_buffer.size() - impl.eac3_carry);
std::size_t total = impl.eac3_carry + got;
// A read that returns nothing means no more bytes will arrive, so the
// audio frame still open in the buffer is complete by definition. Without
// this the last audio frame of every file is left in the carry.
const bool at_eof = (got == 0) || impl.eac3_eof;
if (total == 0) {
impl.eac3_eof = true;
} else {
@@ -1051,8 +1163,8 @@ std::size_t Engine::read(float* destination, std::size_t frames, std::string* er
if (impl.metadata_skip != 0) {
std::size_t dropped = 0;
while (impl.metadata_skip != 0) {
const std::size_t bytes =
joc_eac3::frame_bytes_at(impl.eac3_buffer.data(), total, dropped);
const std::size_t bytes = joc_eac3::audio_frame_bytes_at(
impl.eac3_buffer.data(), total, dropped, at_eof);
if (bytes == 0 || dropped + bytes > total) break;
dropped += bytes;
--impl.metadata_skip;
@@ -1074,8 +1186,8 @@ std::size_t Engine::read(float* destination, std::size_t frames, std::string* er
std::size_t offset = 0;
std::uint64_t complete = 0;
while (true) {
const std::size_t bytes =
joc_eac3::frame_bytes_at(impl.eac3_buffer.data(), total, offset);
const std::size_t bytes = joc_eac3::audio_frame_bytes_at(
impl.eac3_buffer.data(), total, offset, at_eof);
if (bytes == 0 || offset + bytes > total) break;
offset += bytes;
++complete;
@@ -1096,8 +1208,8 @@ std::size_t Engine::read(float* destination, std::size_t frames, std::string* er
pushed_frames = allowed;
std::size_t walk = 0;
for (std::uint64_t index = 0; index < pushed_frames; ++index) {
const std::size_t bytes = joc_eac3::frame_bytes_at(
impl.eac3_buffer.data(), total, walk);
const std::size_t bytes = joc_eac3::audio_frame_bytes_at(
impl.eac3_buffer.data(), total, walk, at_eof);
if (bytes == 0 || walk + bytes > total) break;
walk += bytes;
}
@@ -1113,7 +1225,7 @@ std::size_t Engine::read(float* destination, std::size_t frames, std::string* er
if (pushed != JOC_OK) {
if (error != nullptr) {
*error = std::string("E-AC-3 push failed: ") +
error_text(impl.api, pushed);
error_with_detail(impl.api, pushed);
}
return 0;
}
@@ -1136,27 +1248,27 @@ std::size_t Engine::read(float* destination, std::size_t frames, std::string* er
// return any number of bytes, and rounding a read down to whole samples
// quietly drops the rest of the stream's alignment.
while (!impl.bed_eof && impl.bed_frames_pushed < impl.frames_queued) {
constexpr std::size_t kFrameBytes = kFrameSamples * kBedChannels * sizeof(float);
const std::size_t whole_frames = impl.bed_staged_bytes / kFrameBytes;
const std::size_t frame_bytes = kFrameSamples * impl.bed_channels * sizeof(float);
const std::size_t whole_frames = impl.bed_staged_bytes / frame_bytes;
if (whole_frames != 0) {
joc_stream_buffer input{};
input.struct_size = sizeof(input);
input.struct_version = 1;
input.kind = JOC_STREAM_IN_CORE_PCM;
input.pcm = impl.bed_buffer.data();
input.channels = static_cast<std::uint32_t>(kBedChannels);
input.channels = static_cast<std::uint32_t>(impl.bed_channels);
input.sample_rate = 48000;
input.sample_count = static_cast<std::uint32_t>(whole_frames * kFrameSamples);
const joc_error pushed = impl.api.push(impl.stream, &input, nullptr, nullptr);
if (pushed != JOC_OK) {
if (error != nullptr) {
*error = std::string("core PCM push failed: ") +
error_text(impl.api, pushed);
error_with_detail(impl.api, pushed);
}
return 0;
}
impl.bed_frames_pushed += whole_frames;
const std::size_t pushed_bytes = whole_frames * kFrameBytes;
const std::size_t pushed_bytes = whole_frames * frame_bytes;
impl.bed_staged_bytes -= pushed_bytes;
if (impl.bed_staged_bytes != 0) {
std::memmove(impl.bed_buffer.data(),
@@ -1198,7 +1310,7 @@ std::size_t Engine::read(float* destination, std::size_t frames, std::string* er
const joc_error pulled = impl.api.pull(impl.stream, &output, &produced);
if (pulled != JOC_OK) {
if (error != nullptr) {
*error = std::string("pull failed: ") + error_text(impl.api, pulled);
*error = std::string("pull failed: ") + error_with_detail(impl.api, pulled);
}
return 0;
}
@@ -1248,7 +1360,7 @@ std::size_t Engine::read(float* destination, std::size_t frames, std::string* er
const joc_error flushed = impl.api.flush(impl.stream);
if (flushed != JOC_OK) {
if (error != nullptr) {
*error = std::string("flush failed: ") + error_text(impl.api, flushed);
*error = std::string("flush failed: ") + error_with_detail(impl.api, flushed);
}
return 0;
}
@@ -1267,6 +1379,22 @@ std::size_t Engine::read(float* destination, std::size_t frames, std::string* er
static_cast<unsigned long long>(status.frames_out),
static_cast<unsigned long long>(status.samples_out),
static_cast<unsigned long long>(status.buffered_samples));
// Metadata the renderer had to work around; each one is per audio
// frame and a non-zero count is worth reporting with the rest.
if (status.missing_joc_frames != 0u || status.objects_clamped_frames != 0u ||
status.extra_container_frames != 0u ||
status.chanmap_pair_mismatch_frames != 0u ||
status.bed_mask_mismatch_frames != 0u) {
joc_log::line(
"core: metadata workarounds: missing_joc_frames=%llu "
"objects_clamped_frames=%llu extra_container_frames=%llu "
"chanmap_pair_mismatch_frames=%llu bed_mask_mismatch_frames=%llu",
static_cast<unsigned long long>(status.missing_joc_frames),
static_cast<unsigned long long>(status.objects_clamped_frames),
static_cast<unsigned long long>(status.extra_container_frames),
static_cast<unsigned long long>(status.chanmap_pair_mismatch_frames),
static_cast<unsigned long long>(status.bed_mask_mismatch_frames));
}
joc_log::line("core: bed bytes read=%llu, staged samples left=%llu",
static_cast<unsigned long long>(impl.bed_bytes_read),
static_cast<unsigned long long>(impl.bed_staged_bytes));
+6 -9
View File
@@ -4,17 +4,14 @@
// drives when the output has to be compared byte for byte against joc_cli.
//
// Two inputs are needed to render, and they come from different places:
// * the bare E-AC-3 syncframes -- the metadata stream -- read straight from the
// file, since the plugin only claims bare .eac3/.ec3 today;
// * the E-AC-3 syncframes -- the metadata stream -- read straight from the file;
// * the 5.1 core PCM of those same frames -- the rendering core does not decode
// the core (it has no AC-3 decoder at all), so ffmpeg produces it exactly the
// way the reference CLI does.
// the core (it has no AC-3 decoder at all), so ffmpeg produces it.
//
// The core is loaded from joc_core.dll through the public C ABI and called via
// function pointers. Static linking is not an option: the core's static library
// carries the internals but not the API layer in src/api, and the plugin is not
// allowed to compile core sources. Loading explicitly also lets the component
// ship its own copy of the DLL and check its ABI version at start-up.
// The core is the C++ kernel under kernel/, compiled into this component and called
// through its public C API via the CoreApi table. No DLL is shipped or loaded: the
// table is filled with the linked entry points and start-up only checks the ABI
// version.
#pragma once
#include <cstddef>
+1 -1
View File
@@ -12,7 +12,7 @@
// Kept in one place: the string reported to foobar2000 and written to the log
// must not drift apart.
#define JOC_VERSION "0.3.0"
#define JOC_VERSION "0.4.0"
DECLARE_COMPONENT_VERSION("JOC decoder (E-AC-3 JOC)", JOC_VERSION,
"Plays E-AC-3 JOC (Dolby Atmos) files: the JOC objects are "