mirror of https://github.com/stenzek/duckstation
CDROM: Move subchannel replacement from CDImage
This will allow you to use SBI/LSD files with real discs, if your drive does not support reading subchannels, or has an incorrect skew.pull/3322/head
parent
cdd230c040
commit
080807e557
@ -0,0 +1,204 @@
|
||||
// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com>
|
||||
// SPDX-License-Identifier: CC-BY-NC-ND-4.0
|
||||
|
||||
#include "cdrom_subq_replacement.h"
|
||||
#include "settings.h"
|
||||
|
||||
#include "common/error.h"
|
||||
#include "common/file_system.h"
|
||||
#include "common/log.h"
|
||||
#include "common/path.h"
|
||||
#include "common/small_string.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <memory>
|
||||
|
||||
LOG_CHANNEL(CDROM);
|
||||
|
||||
#pragma pack(push, 1)
|
||||
struct SBIFileEntry
|
||||
{
|
||||
u8 minute_bcd;
|
||||
u8 second_bcd;
|
||||
u8 frame_bcd;
|
||||
u8 type;
|
||||
u8 data[10];
|
||||
};
|
||||
struct LSDFileEntry
|
||||
{
|
||||
u8 minute_bcd;
|
||||
u8 second_bcd;
|
||||
u8 frame_bcd;
|
||||
u8 data[12];
|
||||
};
|
||||
static_assert(sizeof(LSDFileEntry) == 15);
|
||||
#pragma pack(pop)
|
||||
|
||||
CDROMSubQReplacement::CDROMSubQReplacement() = default;
|
||||
|
||||
CDROMSubQReplacement::~CDROMSubQReplacement() = default;
|
||||
|
||||
std::unique_ptr<CDROMSubQReplacement> CDROMSubQReplacement::LoadSBI(const std::string& path, Error* error)
|
||||
{
|
||||
auto fp = FileSystem::OpenManagedCFile(path.c_str(), "rb", error);
|
||||
if (!fp)
|
||||
return {};
|
||||
|
||||
static constexpr char expected_header[] = {'S', 'B', 'I', '\0'};
|
||||
|
||||
char header[4];
|
||||
if (std::fread(header, sizeof(header), 1, fp.get()) != 1 || std::memcmp(header, expected_header, sizeof(header)) != 0)
|
||||
{
|
||||
Error::SetStringFmt(error, "Invalid header in '{}'", Path::GetFileName(path));
|
||||
return {};
|
||||
}
|
||||
|
||||
std::unique_ptr<CDROMSubQReplacement> ret = std::make_unique<CDROMSubQReplacement>();
|
||||
|
||||
SBIFileEntry entry;
|
||||
while (std::fread(&entry, sizeof(entry), 1, fp.get()) == 1)
|
||||
{
|
||||
if (!IsValidPackedBCD(entry.minute_bcd) || !IsValidPackedBCD(entry.second_bcd) ||
|
||||
!IsValidPackedBCD(entry.frame_bcd))
|
||||
{
|
||||
Error::SetStringFmt(error, "Invalid position [{:02x}:{:02x}:{:02x}] in '{}'", entry.minute_bcd, entry.second_bcd,
|
||||
entry.frame_bcd, Path::GetFileName(path));
|
||||
return {};
|
||||
}
|
||||
|
||||
if (entry.type != 1)
|
||||
{
|
||||
Error::SetStringFmt(error, "Invalid type 0x{:02X} in '{}'", entry.type, Path::GetFileName(path));
|
||||
return {};
|
||||
}
|
||||
|
||||
const u32 lba = CDImage::Position::FromBCD(entry.minute_bcd, entry.second_bcd, entry.frame_bcd).ToLBA();
|
||||
|
||||
CDImage::SubChannelQ subq;
|
||||
std::memcpy(subq.data.data(), entry.data, sizeof(entry.data));
|
||||
|
||||
// generate an invalid crc by flipping all bits from the valid crc (will never collide)
|
||||
const u16 crc = subq.ComputeCRC(subq.data) ^ 0xFFFF;
|
||||
subq.data[10] = Truncate8(crc);
|
||||
subq.data[11] = Truncate8(crc >> 8);
|
||||
|
||||
ret->m_replacement_subq.emplace(lba, subq);
|
||||
}
|
||||
|
||||
INFO_LOG("Loaded {} replacement sectors from SBI '{}'", ret->m_replacement_subq.size(), Path::GetFileName(path));
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::unique_ptr<CDROMSubQReplacement> CDROMSubQReplacement::LoadLSD(const std::string& path, Error* error)
|
||||
{
|
||||
auto fp = FileSystem::OpenManagedCFile(path.c_str(), "rb", error);
|
||||
if (!fp)
|
||||
return {};
|
||||
|
||||
std::unique_ptr<CDROMSubQReplacement> ret = std::make_unique<CDROMSubQReplacement>();
|
||||
|
||||
LSDFileEntry entry;
|
||||
while (std::fread(&entry, sizeof(entry), 1, fp.get()) == 1)
|
||||
{
|
||||
if (!IsValidPackedBCD(entry.minute_bcd) || !IsValidPackedBCD(entry.second_bcd) ||
|
||||
!IsValidPackedBCD(entry.frame_bcd))
|
||||
{
|
||||
Error::SetStringFmt(error, "Invalid position [{:02x}:{:02x}:{:02x}] in '{}'", entry.minute_bcd, entry.second_bcd,
|
||||
entry.frame_bcd, Path::GetFileName(path));
|
||||
return {};
|
||||
}
|
||||
|
||||
const u32 lba = CDImage::Position::FromBCD(entry.minute_bcd, entry.second_bcd, entry.frame_bcd).ToLBA();
|
||||
|
||||
CDImage::SubChannelQ subq;
|
||||
std::memcpy(subq.data.data(), entry.data, sizeof(entry.data));
|
||||
|
||||
DEBUG_LOG("{:02x}:{:02x}:{:02x}: CRC {}", entry.minute_bcd, entry.second_bcd, entry.frame_bcd,
|
||||
subq.IsCRCValid() ? "VALID" : "INVALID");
|
||||
ret->m_replacement_subq.emplace(lba, subq);
|
||||
}
|
||||
|
||||
INFO_LOG("Loaded {} replacement sectors from LSD '{}'", ret->m_replacement_subq.size(), path);
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool CDROMSubQReplacement::LoadForImage(std::unique_ptr<CDROMSubQReplacement>* ret, CDImage* image, std::string_view serial,
|
||||
std::string_view title, Error* error)
|
||||
{
|
||||
struct FileLoader
|
||||
{
|
||||
const char* extension;
|
||||
std::unique_ptr<CDROMSubQReplacement> (*func)(const std::string&, Error*);
|
||||
};
|
||||
static constexpr const FileLoader loaders[] = {
|
||||
{"sbi", &CDROMSubQReplacement::LoadSBI},
|
||||
{"lsd", &CDROMSubQReplacement::LoadLSD},
|
||||
};
|
||||
|
||||
const std::string& image_path = image->GetPath();
|
||||
std::string path;
|
||||
|
||||
// Try sbi/lsd in the directory first.
|
||||
for (const FileLoader& loader : loaders)
|
||||
{
|
||||
path = Path::ReplaceExtension(image_path, loader.extension);
|
||||
if (FileSystem::FileExists(path.c_str()))
|
||||
{
|
||||
*ret = loader.func(path, error);
|
||||
return static_cast<bool>(*ret);
|
||||
}
|
||||
}
|
||||
|
||||
// For subimages, we need to check the suffix too.
|
||||
if (image->HasSubImages())
|
||||
{
|
||||
for (const FileLoader& loader : loaders)
|
||||
{
|
||||
path = Path::BuildRelativePath(image_path,
|
||||
SmallString::from_format("{}_{}.{}", Path::GetFileName(image_path),
|
||||
image->GetCurrentSubImage() + 1, loader.extension));
|
||||
if (FileSystem::FileExists(path.c_str()))
|
||||
{
|
||||
*ret = loader.func(path, error);
|
||||
return static_cast<bool>(*ret);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If this fails, try the subchannel directory with serial/title.
|
||||
if (!serial.empty())
|
||||
{
|
||||
for (const FileLoader& loader : loaders)
|
||||
{
|
||||
path = Path::Combine(EmuFolders::Subchannels, TinyString::from_format("{}.{}", serial, loader.extension));
|
||||
if (FileSystem::FileExists(path.c_str()))
|
||||
{
|
||||
*ret = loader.func(path, error);
|
||||
return static_cast<bool>(*ret);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!title.empty())
|
||||
{
|
||||
for (const FileLoader& loader : loaders)
|
||||
{
|
||||
path = Path::Combine(EmuFolders::Subchannels, TinyString::from_format("{}.{}", title, loader.extension));
|
||||
if (FileSystem::FileExists(path.c_str()))
|
||||
{
|
||||
*ret = loader.func(path, error);
|
||||
return static_cast<bool>(*ret);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Nothing.
|
||||
return true;
|
||||
}
|
||||
|
||||
const CDImage::SubChannelQ* CDROMSubQReplacement::GetReplacementSubQ(u32 lba) const
|
||||
{
|
||||
const auto iter = m_replacement_subq.find(lba);
|
||||
return (iter != m_replacement_subq.end()) ? &iter->second : nullptr;
|
||||
}
|
||||
@ -0,0 +1,32 @@
|
||||
// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com>
|
||||
// SPDX-License-Identifier: CC-BY-NC-ND-4.0
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "util/cd_image.h"
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
class CDROMSubQReplacement
|
||||
{
|
||||
public:
|
||||
CDROMSubQReplacement();
|
||||
~CDROMSubQReplacement();
|
||||
|
||||
// NOTE: Can return true if no sbi is available, false means load/parse error.
|
||||
static bool LoadForImage(std::unique_ptr<CDROMSubQReplacement>* ret, CDImage* image, std::string_view serial,
|
||||
std::string_view title, Error* error);
|
||||
|
||||
size_t GetReplacementSectorCount() const { return m_replacement_subq.size(); }
|
||||
|
||||
/// Returns the replacement subchannel data for the specified sector.
|
||||
const CDImage::SubChannelQ* GetReplacementSubQ(u32 lba) const;
|
||||
|
||||
private:
|
||||
using ReplacementMap = std::unordered_map<u32, CDImage::SubChannelQ>;
|
||||
|
||||
static std::unique_ptr<CDROMSubQReplacement> LoadSBI(const std::string& path, Error* error);
|
||||
static std::unique_ptr<CDROMSubQReplacement> LoadLSD(const std::string& path, Error* error);
|
||||
|
||||
ReplacementMap m_replacement_subq;
|
||||
};
|
||||
@ -1,167 +0,0 @@
|
||||
// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com>
|
||||
// SPDX-License-Identifier: CC-BY-NC-ND-4.0
|
||||
|
||||
#include "cd_subchannel_replacement.h"
|
||||
#include "common/file_system.h"
|
||||
#include "common/log.h"
|
||||
#include "common/path.h"
|
||||
#include <algorithm>
|
||||
#include <memory>
|
||||
LOG_CHANNEL(CDSubChannelReplacement);
|
||||
|
||||
#pragma pack(push, 1)
|
||||
struct SBIFileEntry
|
||||
{
|
||||
u8 minute_bcd;
|
||||
u8 second_bcd;
|
||||
u8 frame_bcd;
|
||||
u8 type;
|
||||
u8 data[10];
|
||||
};
|
||||
struct LSDFileEntry
|
||||
{
|
||||
u8 minute_bcd;
|
||||
u8 second_bcd;
|
||||
u8 frame_bcd;
|
||||
u8 data[12];
|
||||
};
|
||||
static_assert(sizeof(LSDFileEntry) == 15);
|
||||
#pragma pack(pop)
|
||||
|
||||
CDSubChannelReplacement::CDSubChannelReplacement() = default;
|
||||
|
||||
CDSubChannelReplacement::~CDSubChannelReplacement() = default;
|
||||
|
||||
static constexpr u32 MSFToLBA(u8 minute_bcd, u8 second_bcd, u8 frame_bcd)
|
||||
{
|
||||
const u8 minute = PackedBCDToBinary(minute_bcd);
|
||||
const u8 second = PackedBCDToBinary(second_bcd);
|
||||
const u8 frame = PackedBCDToBinary(frame_bcd);
|
||||
|
||||
return (ZeroExtend32(minute) * 60 * 75) + (ZeroExtend32(second) * 75) + ZeroExtend32(frame);
|
||||
}
|
||||
|
||||
bool CDSubChannelReplacement::LoadSBI(const std::string& path)
|
||||
{
|
||||
auto fp = FileSystem::OpenManagedCFile(path.c_str(), "rb");
|
||||
if (!fp)
|
||||
return false;
|
||||
|
||||
char header[4];
|
||||
if (std::fread(header, sizeof(header), 1, fp.get()) != 1)
|
||||
{
|
||||
ERROR_LOG("Failed to read header for '{}'", path);
|
||||
return true;
|
||||
}
|
||||
|
||||
static constexpr char expected_header[] = {'S', 'B', 'I', '\0'};
|
||||
if (std::memcmp(header, expected_header, sizeof(header)) != 0)
|
||||
{
|
||||
ERROR_LOG("Invalid header in '{}'", path);
|
||||
return true;
|
||||
}
|
||||
|
||||
m_replacement_subq.clear();
|
||||
|
||||
SBIFileEntry entry;
|
||||
while (std::fread(&entry, sizeof(entry), 1, fp.get()) == 1)
|
||||
{
|
||||
if (!IsValidPackedBCD(entry.minute_bcd) || !IsValidPackedBCD(entry.second_bcd) ||
|
||||
!IsValidPackedBCD(entry.frame_bcd))
|
||||
{
|
||||
ERROR_LOG("Invalid position [{:02x}:{:02x}:{:02x}] in '{}'", entry.minute_bcd, entry.second_bcd, entry.frame_bcd,
|
||||
path);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (entry.type != 1)
|
||||
{
|
||||
ERROR_LOG("Invalid type 0x{:02X} in '{}'", entry.type, path);
|
||||
return false;
|
||||
}
|
||||
|
||||
const u32 lba = MSFToLBA(entry.minute_bcd, entry.second_bcd, entry.frame_bcd);
|
||||
|
||||
CDImage::SubChannelQ subq;
|
||||
std::memcpy(subq.data.data(), entry.data, sizeof(entry.data));
|
||||
|
||||
// generate an invalid crc by flipping all bits from the valid crc (will never collide)
|
||||
const u16 crc = subq.ComputeCRC(subq.data) ^ 0xFFFF;
|
||||
subq.data[10] = Truncate8(crc);
|
||||
subq.data[11] = Truncate8(crc >> 8);
|
||||
|
||||
m_replacement_subq.emplace(lba, subq);
|
||||
}
|
||||
|
||||
INFO_LOG("Loaded {} replacement sectors from SBI '{}'", m_replacement_subq.size(), path);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CDSubChannelReplacement::LoadLSD(const std::string& path)
|
||||
{
|
||||
auto fp = FileSystem::OpenManagedCFile(path.c_str(), "rb");
|
||||
if (!fp)
|
||||
return false;
|
||||
|
||||
m_replacement_subq.clear();
|
||||
|
||||
LSDFileEntry entry;
|
||||
while (std::fread(&entry, sizeof(entry), 1, fp.get()) == 1)
|
||||
{
|
||||
if (!IsValidPackedBCD(entry.minute_bcd) || !IsValidPackedBCD(entry.second_bcd) ||
|
||||
!IsValidPackedBCD(entry.frame_bcd))
|
||||
{
|
||||
ERROR_LOG("Invalid position [{:02x}:{:02x}:{:02x}] in '{}'", entry.minute_bcd, entry.second_bcd, entry.frame_bcd,
|
||||
path);
|
||||
return false;
|
||||
}
|
||||
|
||||
const u32 lba = MSFToLBA(entry.minute_bcd, entry.second_bcd, entry.frame_bcd);
|
||||
|
||||
CDImage::SubChannelQ subq;
|
||||
std::memcpy(subq.data.data(), entry.data, sizeof(entry.data));
|
||||
|
||||
DEBUG_LOG("{:02x}:{:02x}:{:02x}: CRC {}", entry.minute_bcd, entry.second_bcd, entry.frame_bcd,
|
||||
subq.IsCRCValid() ? "VALID" : "INVALID");
|
||||
m_replacement_subq.emplace(lba, subq);
|
||||
}
|
||||
|
||||
INFO_LOG("Loaded {} replacement sectors from LSD '{}'", m_replacement_subq.size(), path);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CDSubChannelReplacement::LoadFromImagePath(std::string_view image_path)
|
||||
{
|
||||
if (const std::string filename = Path::ReplaceExtension(image_path, "sbi"); LoadSBI(filename.c_str()))
|
||||
return true;
|
||||
|
||||
if (const std::string filename = Path::ReplaceExtension(image_path, "lsd"); LoadLSD(filename.c_str()))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void CDSubChannelReplacement::AddReplacementSubChannelQ(u32 lba, const CDImage::SubChannelQ& subq)
|
||||
{
|
||||
auto iter = m_replacement_subq.find(lba);
|
||||
if (iter != m_replacement_subq.end())
|
||||
iter->second.data = subq.data;
|
||||
else
|
||||
m_replacement_subq.emplace(lba, subq);
|
||||
}
|
||||
|
||||
bool CDSubChannelReplacement::GetReplacementSubChannelQ(u8 minute_bcd, u8 second_bcd, u8 frame_bcd,
|
||||
CDImage::SubChannelQ* subq) const
|
||||
{
|
||||
return GetReplacementSubChannelQ(MSFToLBA(minute_bcd, second_bcd, frame_bcd), subq);
|
||||
}
|
||||
|
||||
bool CDSubChannelReplacement::GetReplacementSubChannelQ(u32 lba, CDImage::SubChannelQ* subq) const
|
||||
{
|
||||
const auto iter = m_replacement_subq.find(lba);
|
||||
if (iter == m_replacement_subq.cend())
|
||||
return false;
|
||||
|
||||
*subq = iter->second;
|
||||
return true;
|
||||
}
|
||||
@ -1,37 +0,0 @@
|
||||
// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com>
|
||||
// SPDX-License-Identifier: CC-BY-NC-ND-4.0
|
||||
|
||||
#pragma once
|
||||
#include "cd_image.h"
|
||||
#include "common/types.h"
|
||||
#include <array>
|
||||
#include <cstdio>
|
||||
#include <unordered_map>
|
||||
|
||||
class CDSubChannelReplacement
|
||||
{
|
||||
public:
|
||||
CDSubChannelReplacement();
|
||||
~CDSubChannelReplacement();
|
||||
|
||||
u32 GetReplacementSectorCount() const { return static_cast<u32>(m_replacement_subq.size()); }
|
||||
|
||||
bool LoadFromImagePath(std::string_view image_path);
|
||||
|
||||
/// Adds a sector to the replacement map.
|
||||
void AddReplacementSubChannelQ(u32 lba, const CDImage::SubChannelQ& subq);
|
||||
|
||||
/// Returns the replacement subchannel data for the specified position (in BCD).
|
||||
bool GetReplacementSubChannelQ(u8 minute_bcd, u8 second_bcd, u8 frame_bcd, CDImage::SubChannelQ* subq) const;
|
||||
|
||||
/// Returns the replacement subchannel data for the specified sector.
|
||||
bool GetReplacementSubChannelQ(u32 lba, CDImage::SubChannelQ* subq) const;
|
||||
|
||||
private:
|
||||
using ReplacementMap = std::unordered_map<u32, CDImage::SubChannelQ>;
|
||||
|
||||
bool LoadSBI(const std::string& path);
|
||||
bool LoadLSD(const std::string& path);
|
||||
|
||||
ReplacementMap m_replacement_subq;
|
||||
};
|
||||
Loading…
Reference in New Issue