Skip to content

Commit

Permalink
Unsafe mode
Browse files Browse the repository at this point in the history
  • Loading branch information
VoidXH committed Oct 31, 2024
1 parent bdd3371 commit b2efaa2
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 51 deletions.
11 changes: 11 additions & 0 deletions Cavern.Format/Common/_CavernFormatGlobal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Cavern.Format.Common {
/// <summary>
/// Settings related to the entirety of the Cavern.Format library.
/// </summary>
public static class CavernFormatGlobal {
/// <summary>
/// Disables checks for conditions that don't inherently break operation, but are mandated by standards.
/// </summary>
public static bool Unsafe { get; set; }
}
}
2 changes: 2 additions & 0 deletions Cavern.Format/Decoders/EnhancedAC3Decoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public class EnhancedAC3Decoder : FrameBasedDecoder, IMetadataSupplier {
/// Converts an Enhanced AC-3 bitstream to raw samples. When the file size is known, the length can be calculated
/// from the bitrate assuming AC-3 is constant bitrate.
/// </summary>
/// <param name="reader">Accesses the linear E-AC-3 bitstream</param>
/// <param name="fileSize">Length of the E-AC-3 bitstream</param>
public EnhancedAC3Decoder(BlockBuffer<byte> reader, long fileSize) : base(reader) =>
Length = fileSize / frameSize * FrameSize;

Expand Down
2 changes: 1 addition & 1 deletion Cavern.Format/Transcoders/EnhancedAC3Body/Coupling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void DecodeCouplingStrategy(bool eac3, int block) {
}
ncplsubnd = 3 + cplendf - cplbegf;
ncplbnd = ncplsubnd;
if (ncplsubnd < 1) {
if (ncplsubnd < 1 && !CavernFormatGlobal.Unsafe) {
throw new DecoderException(3);
}
if (cplbndstrce = !eac3 || extractor.ReadBit()) {
Expand Down
8 changes: 4 additions & 4 deletions Cavern.Format/Transcoders/EnhancedAC3Body/DecodeAudioBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,18 +217,18 @@ internal void DecodeAudioBlock(int block) {

// Error checks
if (block == 0) {
if (!cplstre[block]) {
if (!cplstre[block] && !CavernFormatGlobal.Unsafe) {
throw new DecoderException(1);
}
if (header.LFE && !lfeexpstr[block]) {
if (header.LFE && !lfeexpstr[block] && !CavernFormatGlobal.Unsafe) {
throw new DecoderException(10);
}
}
for (int channel = 0; channel < channels.Length; channel++) {
if (block == 0 && chexpstr[0][channel] == ExpStrat.Reuse) {
if (block == 0 && chexpstr[0][channel] == ExpStrat.Reuse && !CavernFormatGlobal.Unsafe) {
throw new DecoderException(8);
}
if (!chincpl[channel] && chbwcod[channel] > 60) {
if (!chincpl[channel] && chbwcod[channel] > 60 && !CavernFormatGlobal.Unsafe) {
throw new DecoderException(11);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public static Command[] CommandPool {

// Hidden commands
new OverrideBedCommand(),
new UnsafeCommand(),
];
return commandPool;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
using Cavern.Format;
using System.IO;
using System.IO;

using Cavern.Format;

namespace CavernizeGUI.CommandLine.HiddenCommands {
/// <summary>
/// Overrides the PCM source of JOC encoded objects in E-AC-3 tracks.
/// </summary>
internal class OverrideBedCommand : HiddenCommand {
/// <summary>
/// Full name of the command, including a preceding character like '-' if exists.
/// </summary>
/// <inheritdoc/>
public override string Name => "--override-bed";

/// <summary>
/// Number of parameters this command will use.
/// </summary>
/// <inheritdoc/>
public override int Parameters => 1;

/// <summary>
/// Description of the command that is displayed in the command list (help).
/// </summary>
/// <inheritdoc/>
public override string Help => "[Enhanced AC-3] Overrides the PCM source of JOC encoded objects.";

/// <summary>
/// Execute the command.
/// </summary>
/// <param name="args">List of all calling arguments for the software</param>
/// <param name="offset">The index of the first argument that is a parameter of this command</param>
/// <param name="app">Reference to the main window of the application - operations should be performed though the UI</param>
/// <inheritdoc/>
public override void Execute(string[] args, int offset, MainWindow app) {
if (!File.Exists(args[offset])) {
throw new FileNotFoundException(args[offset]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,12 @@ namespace CavernizeGUI.CommandLine.HiddenCommands {
/// <summary>
/// Converts an Enhanced AC-3 bitstream to raw samples, replaces the PCM data with external.
/// </summary>
public class OverrideBedDecoder : EnhancedAC3Decoder {
/// <summary>
/// Stream to override the PCM data with. Only applies to the source PCM data, not the JOC-decoded objects.
/// </summary>
readonly AudioReader overrider;

/// <summary>
/// Converts an Enhanced AC-3 bitstream to raw samples, replaces the PCM data with external.
/// </summary>
public OverrideBedDecoder(BlockBuffer<byte> reader, long fileSize, AudioReader overrider) : base(reader, fileSize) =>
this.overrider = overrider;

/// <summary>
/// Decode a new frame if the cached samples are already fetched.
/// </summary>
/// <param name="reader">Accesses the linear E-AC-3 bitstream</param>
/// <param name="fileSize">Length of the E-AC-3 bitstream</param>
/// <param name="overrider">Stream to override the PCM data with - only applies to the source PCM data,
/// not the JOC-decoded objects</param>
public class OverrideBedDecoder(BlockBuffer<byte> reader, long fileSize, AudioReader overrider) : EnhancedAC3Decoder(reader, fileSize) {
/// <inheritdoc/>
protected override float[] DecodeFrame() {
float[] result = base.DecodeFrame();
overrider?.ReadBlock(result, 0, result.Length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ namespace CavernizeGUI.CommandLine.HiddenCommands {
/// <summary>
/// A hacked version of <see cref="AudioFile"/> that loads the <see cref="overrider"/> stream.
/// </summary>
/// <param name="path">Path of the original E-AC-3 file</param>
/// <param name="overrider">Stream to override the PCM data with - only applies to the source PCM data,
/// not the JOC-decoded objects</param>
public class OverrideBedFile(string path, AudioReader overrider) : AudioFile(path) {
/// <summary>
/// Stream to override the PCM data with. Only applies to the source PCM data, not the JOC-decoded objects.
/// </summary>
readonly AudioReader overrider = overrider;

/// <summary>
/// Reloads the tracklist to be able to start reading from the beginning.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ namespace CavernizeGUI.CommandLine.HiddenCommands {
/// <summary>
/// Enhanced AC-3 file reader that reads the PCM data from another file.
/// </summary>
/// <param name="path">Path of the original E-AC-3 file</param>
/// <param name="overrider">Stream to override the PCM data with - only applies to the source PCM data,
/// not the JOC-decoded objects</param>
public class OverrideBedReader(string path, AudioReader overrider) : EnhancedAC3Reader(path) {
/// <summary>
/// Stream to override the PCM data with. Only applies to the source PCM data, not the JOC-decoded objects.
/// </summary>
readonly AudioReader overrider = overrider;

/// <summary>
/// Read the file header.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Cavern.Format.Common;

namespace CavernizeGUI.CommandLine.HiddenCommands {
/// <summary>
/// Disables checks for conditions that don't inherently break operation, but are mandated by standards.
/// </summary>
internal class UnsafeCommand : HiddenCommand {
/// <inheritdoc/>
public override string Name => "--unsafe";

/// <inheritdoc/>
public override int Parameters => 0;

/// <inheritdoc/>
public override string Help => "Disables checks for conditions that are mandated by standards but might don't break decoding.";

/// <inheritdoc/>
public override void Execute(string[] args, int offset, MainWindow app) => CavernFormatGlobal.Unsafe = true;
}
}
2 changes: 1 addition & 1 deletion CavernSamples/CavernizeGUI/CommandLine/TargetCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public override void Execute(string[] args, int offset, MainWindow app) {
return;
}

for (int i = RenderTarget.Targets.Length - 1; i >= 0; i--) { // Sides before fronts
for (int i = 0; i < RenderTarget.Targets.Length; i++) {
string name = RenderTarget.Targets[i].Name;
if (args[offset].Equals(name)) {
app.renderTarget.SelectedItem = RenderTarget.Targets[i];
Expand Down
6 changes: 3 additions & 3 deletions CavernSamples/CavernizeGUI/Elements/RenderTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ public ReferenceChannel[] GetNameMappedChannels() {
ReferenceChannel.FrontLeft, ReferenceChannel.FrontRight, ReferenceChannel.FrontCenter, ReferenceChannel.ScreenLFE,
ReferenceChannel.RearLeft, ReferenceChannel.RearRight
]),
new DownmixedRenderTarget("5.1.2 front", ChannelPrototype.ref514, (8, 4), (9, 5)),
new RenderTarget("5.1.2 side", [
ReferenceChannel.FrontLeft, ReferenceChannel.FrontRight, ReferenceChannel.FrontCenter, ReferenceChannel.ScreenLFE,
ReferenceChannel.SideLeft, ReferenceChannel.SideRight, ReferenceChannel.TopRearLeft, ReferenceChannel.TopRearRight
]),
new DownmixedRenderTarget("5.1.2 front", ChannelPrototype.ref514, (8, 4), (9, 5)),
new RenderTarget("5.1.4", ChannelPrototype.ref514),
new DownmixedRenderTarget("5.1.4 matrix", [
ReferenceChannel.FrontLeft, ReferenceChannel.FrontRight, ReferenceChannel.FrontCenter, ReferenceChannel.ScreenLFE,
Expand All @@ -124,12 +124,12 @@ public ReferenceChannel[] GetNameMappedChannels() {
new RenderTarget("5.1.6 with top sides", ChannelPrototype.ref516),
new RenderTarget("5.1.6 for WAVE", ChannelPrototype.wav516),
new RenderTarget("7.1", ChannelPrototype.ref710),
new DownmixedRenderTarget("7.1.2 front", ChannelPrototype.ref714, (10, 4), (11, 5)),
new RenderTarget("7.1.2 side", [
ReferenceChannel.FrontLeft, ReferenceChannel.FrontRight, ReferenceChannel.FrontCenter, ReferenceChannel.ScreenLFE,
ReferenceChannel.RearLeft, ReferenceChannel.RearRight, ReferenceChannel.SideLeft, ReferenceChannel.SideRight,
ReferenceChannel.TopRearLeft, ReferenceChannel.TopRearRight
]),
new DownmixedRenderTarget("7.1.2 front", ChannelPrototype.ref714, (10, 4), (11, 5)),
new DownmixedRenderTarget("7.1.2 matrix", [
ReferenceChannel.FrontLeft, ReferenceChannel.FrontRight, ReferenceChannel.FrontCenter, ReferenceChannel.ScreenLFE,
ReferenceChannel.RearLeft, ReferenceChannel.RearRight, ReferenceChannel.SideLeft, ReferenceChannel.SideRight,
Expand All @@ -148,12 +148,12 @@ public ReferenceChannel[] GetNameMappedChannels() {
ReferenceChannel.RearLeft, ReferenceChannel.RearRight, ReferenceChannel.SideLeft, ReferenceChannel.SideRight,
ReferenceChannel.WideLeft, ReferenceChannel.WideRight
]),
new DownmixedRenderTarget("9.1.2 front", ChannelPrototype.ref914, (12, 4), (13, 5)),
new RenderTarget("9.1.2 side", [
ReferenceChannel.FrontLeft, ReferenceChannel.FrontRight, ReferenceChannel.FrontCenter, ReferenceChannel.ScreenLFE,
ReferenceChannel.RearLeft, ReferenceChannel.RearRight, ReferenceChannel.SideLeft, ReferenceChannel.SideRight,
ReferenceChannel.WideLeft, ReferenceChannel.WideRight, ReferenceChannel.TopRearLeft, ReferenceChannel.TopRearRight
]),
new DownmixedRenderTarget("9.1.2 front", ChannelPrototype.ref914, (12, 4), (13, 5)),
new RenderTarget("9.1.4", ChannelPrototype.ref914),
new RenderTarget("9.1.6 with top sides", ChannelPrototype.ref916),
new RenderTarget("9.1.6 for WAVE", ChannelPrototype.wav916),
Expand Down

0 comments on commit b2efaa2

Please sign in to comment.