2020-01-10 03:31:12 +00:00
|
|
|
#include "assert.h"
|
2019-10-18 08:18:04 +00:00
|
|
|
#include "cd_image.h"
|
2019-12-06 06:23:08 +00:00
|
|
|
#include "cd_subchannel_replacement.h"
|
2021-03-18 15:52:00 +00:00
|
|
|
#include "error.h"
|
2020-01-10 03:31:12 +00:00
|
|
|
#include "file_system.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include <algorithm>
|
2020-05-06 13:42:04 +00:00
|
|
|
#include <cerrno>
|
2020-01-30 03:18:16 +00:00
|
|
|
#include <libcue/libcue.h>
|
2019-10-18 08:18:04 +00:00
|
|
|
#include <map>
|
|
|
|
Log_SetChannel(CDImageCueSheet);
|
|
|
|
|
|
|
|
class CDImageCueSheet : public CDImage
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CDImageCueSheet();
|
|
|
|
~CDImageCueSheet() override;
|
|
|
|
|
2021-03-18 15:52:00 +00:00
|
|
|
bool OpenAndParse(const char* filename, Common::Error* error);
|
2019-10-18 08:18:04 +00:00
|
|
|
|
2021-03-26 16:19:23 +00:00
|
|
|
bool ReadSubChannelQ(SubChannelQ* subq, const Index& index, LBA lba_in_index) override;
|
2020-12-17 17:01:57 +00:00
|
|
|
bool HasNonStandardSubchannel() const override;
|
2019-12-06 06:23:08 +00:00
|
|
|
|
2020-01-30 05:50:00 +00:00
|
|
|
protected:
|
|
|
|
bool ReadSectorFromIndex(void* buffer, const Index& index, LBA lba_in_index) override;
|
|
|
|
|
2019-10-18 08:18:04 +00:00
|
|
|
private:
|
|
|
|
Cd* m_cd = nullptr;
|
2020-01-30 05:50:00 +00:00
|
|
|
|
|
|
|
struct TrackFile
|
|
|
|
{
|
|
|
|
std::string filename;
|
|
|
|
std::FILE* file;
|
|
|
|
u64 file_position;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<TrackFile> m_files;
|
2019-12-06 06:23:08 +00:00
|
|
|
CDSubChannelReplacement m_sbi;
|
2019-10-18 08:18:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
CDImageCueSheet::CDImageCueSheet() = default;
|
|
|
|
|
|
|
|
CDImageCueSheet::~CDImageCueSheet()
|
|
|
|
{
|
2020-01-30 05:50:00 +00:00
|
|
|
std::for_each(m_files.begin(), m_files.end(), [](TrackFile& t) { std::fclose(t.file); });
|
2019-10-18 08:18:04 +00:00
|
|
|
cd_delete(m_cd);
|
|
|
|
}
|
|
|
|
|
2021-03-18 15:52:00 +00:00
|
|
|
bool CDImageCueSheet::OpenAndParse(const char* filename, Common::Error* error)
|
2019-10-18 08:18:04 +00:00
|
|
|
{
|
2020-08-04 17:14:06 +00:00
|
|
|
std::optional<std::string> cuesheet_string = FileSystem::ReadFileToString(filename);
|
|
|
|
if (!cuesheet_string.has_value())
|
2019-10-18 08:18:04 +00:00
|
|
|
{
|
2020-05-06 13:42:04 +00:00
|
|
|
Log_ErrorPrintf("Failed to open cuesheet '%s': errno %d", filename, errno);
|
2021-03-18 15:52:00 +00:00
|
|
|
if (error)
|
|
|
|
error->SetErrno(errno);
|
|
|
|
|
2019-10-18 08:18:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-04 17:14:06 +00:00
|
|
|
// work around cuesheet parsing issue - ensure the last character is a newline.
|
|
|
|
if (!cuesheet_string->empty() && cuesheet_string->at(cuesheet_string->size() - 1) != '\n')
|
|
|
|
*cuesheet_string += '\n';
|
|
|
|
|
|
|
|
m_cd = cue_parse_string(cuesheet_string->c_str());
|
2019-10-18 08:18:04 +00:00
|
|
|
if (!m_cd)
|
|
|
|
{
|
|
|
|
Log_ErrorPrintf("Failed to parse cuesheet '%s'", filename);
|
2021-03-18 15:52:00 +00:00
|
|
|
if (error)
|
|
|
|
error->SetMessage("Failed to parse cuesheet");
|
|
|
|
|
2019-10-18 08:18:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-18 12:04:25 +00:00
|
|
|
m_filename = filename;
|
2019-10-18 08:18:04 +00:00
|
|
|
|
|
|
|
u32 disc_lba = 0;
|
|
|
|
|
|
|
|
// for each track..
|
|
|
|
const int num_tracks = cd_get_ntrack(m_cd);
|
|
|
|
for (int track_num = 1; track_num <= num_tracks; track_num++)
|
|
|
|
{
|
|
|
|
const ::Track* track = cd_get_track(m_cd, track_num);
|
2020-04-08 01:58:05 +00:00
|
|
|
if (!track || !track_get_filename(track))
|
|
|
|
{
|
2020-04-08 11:26:51 +00:00
|
|
|
Log_ErrorPrintf("Track/filename missing for track %d", track_num);
|
2021-03-18 15:52:00 +00:00
|
|
|
if (error)
|
|
|
|
error->SetFormattedMessage("Track/filename missing for track %d", track_num);
|
|
|
|
|
2020-04-08 01:58:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-18 08:18:04 +00:00
|
|
|
const std::string track_filename = track_get_filename(track);
|
|
|
|
long track_start = track_get_start(track);
|
|
|
|
long track_length = track_get_length(track);
|
|
|
|
|
2020-01-30 05:50:00 +00:00
|
|
|
u32 track_file_index = 0;
|
|
|
|
for (; track_file_index < m_files.size(); track_file_index++)
|
|
|
|
{
|
|
|
|
const TrackFile& t = m_files[track_file_index];
|
|
|
|
if (t.filename == track_filename)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (track_file_index == m_files.size())
|
2019-10-18 08:18:04 +00:00
|
|
|
{
|
2021-04-16 17:47:40 +00:00
|
|
|
const std::string track_full_filename(FileSystem::BuildRelativePath(m_filename, track_filename));
|
2020-01-10 03:31:12 +00:00
|
|
|
std::FILE* track_fp = FileSystem::OpenCFile(track_full_filename.c_str(), "rb");
|
2020-12-17 16:09:24 +00:00
|
|
|
if (!track_fp && track_file_index == 0)
|
|
|
|
{
|
|
|
|
// many users have bad cuesheets, or they're renamed the files without updating the cuesheet.
|
|
|
|
// so, try searching for a bin with the same name as the cue, but only for the first referenced file.
|
|
|
|
const std::string alternative_filename(FileSystem::ReplaceExtension(filename, "bin"));
|
|
|
|
track_fp = FileSystem::OpenCFile(alternative_filename.c_str(), "rb");
|
|
|
|
if (track_fp)
|
|
|
|
{
|
|
|
|
Log_WarningPrintf("Your cue sheet references an invalid file '%s', but this was found at '%s' instead.",
|
|
|
|
track_filename.c_str(), alternative_filename.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-18 08:18:04 +00:00
|
|
|
if (!track_fp)
|
|
|
|
{
|
2020-05-06 13:42:04 +00:00
|
|
|
Log_ErrorPrintf("Failed to open track filename '%s' (from '%s' and '%s'): errno %d",
|
|
|
|
track_full_filename.c_str(), track_filename.c_str(), filename, errno);
|
2021-03-18 15:52:00 +00:00
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
error->SetFormattedMessage("Failed to open track filename '%s' (from '%s' and '%s'): errno %d",
|
|
|
|
track_full_filename.c_str(), track_filename.c_str(), filename, errno);
|
|
|
|
}
|
|
|
|
|
2019-10-18 08:18:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-01-30 05:50:00 +00:00
|
|
|
m_files.push_back(TrackFile{std::move(track_filename), track_fp, 0});
|
2019-10-18 08:18:04 +00:00
|
|
|
}
|
|
|
|
|
2019-11-10 12:44:53 +00:00
|
|
|
// data type determines the sector size
|
|
|
|
const TrackMode mode = static_cast<TrackMode>(track_get_mode(track));
|
|
|
|
const u32 track_sector_size = GetBytesPerSector(mode);
|
|
|
|
|
|
|
|
// precompute subchannel q flags for the whole track
|
|
|
|
SubChannelQ::Control control{};
|
|
|
|
control.data = mode != TrackMode::Audio;
|
|
|
|
control.audio_preemphasis = track_is_set_flag(track, FLAG_PRE_EMPHASIS);
|
|
|
|
control.digital_copy_permitted = track_is_set_flag(track, FLAG_COPY_PERMITTED);
|
|
|
|
control.four_channel_audio = track_is_set_flag(track, FLAG_FOUR_CHANNEL);
|
|
|
|
|
|
|
|
// determine the length from the file
|
2019-10-18 08:18:04 +00:00
|
|
|
if (track_length < 0)
|
|
|
|
{
|
2020-01-30 05:50:00 +00:00
|
|
|
std::fseek(m_files[track_file_index].file, 0, SEEK_END);
|
|
|
|
long file_size = std::ftell(m_files[track_file_index].file);
|
|
|
|
std::fseek(m_files[track_file_index].file, 0, SEEK_SET);
|
2019-10-18 08:18:04 +00:00
|
|
|
|
|
|
|
file_size /= track_sector_size;
|
2020-10-21 10:17:48 +00:00
|
|
|
if (track_start >= file_size)
|
|
|
|
{
|
2021-03-18 15:52:00 +00:00
|
|
|
Log_ErrorPrintf("Failed to open track %u in '%s': track start is out of range (%ld vs %ld)", track_num,
|
|
|
|
filename, track_start, file_size);
|
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
error->SetFormattedMessage("Failed to open track %u in '%s': track start is out of range (%ld vs %ld)",
|
|
|
|
track_num, filename, track_start, file_size);
|
|
|
|
}
|
2020-10-21 10:17:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-18 08:18:04 +00:00
|
|
|
track_length = file_size - track_start;
|
|
|
|
}
|
|
|
|
|
2020-09-20 01:55:43 +00:00
|
|
|
// Two seconds pregap for track 1 is assumed if not specified.
|
|
|
|
// Some people have broken (older) dumps where a two second pregap was implicit but not specified in the cuesheet.
|
|
|
|
// The problem is we can't tell between a missing implicit two second pregap and a zero second pregap. Most of these
|
|
|
|
// seem to be a single bin file for all tracks. So if this is the case, we add the two seconds in if it's not
|
2021-01-01 15:33:11 +00:00
|
|
|
// specified. If this is an audio CD (likely when track 1 is not data), we don't add these pregaps, and rely on the
|
|
|
|
// cuesheet. If we did add them, it causes issues in some games (e.g. Dancing Stage featuring DREAMS COME TRUE).
|
2019-10-18 08:18:04 +00:00
|
|
|
long pregap_frames = track_get_zero_pre(track);
|
2020-09-20 01:55:43 +00:00
|
|
|
const bool pregap_in_file = pregap_frames > 0 && track_start >= pregap_frames;
|
|
|
|
const bool is_multi_track_bin = (track_num > 1 && track_file_index == m_indices[0].file_index);
|
2021-01-01 15:33:11 +00:00
|
|
|
const bool likely_audio_cd = static_cast<TrackMode>(track_get_mode(cd_get_track(m_cd, 1))) == TrackMode::Audio;
|
|
|
|
if ((track_num == 1 || is_multi_track_bin) && pregap_frames < 0 && (track_num == 1 || !likely_audio_cd))
|
2019-10-18 08:18:04 +00:00
|
|
|
pregap_frames = 2 * FRAMES_PER_SECOND;
|
|
|
|
|
|
|
|
// create the index for the pregap
|
|
|
|
if (pregap_frames > 0)
|
|
|
|
{
|
|
|
|
Index pregap_index = {};
|
|
|
|
pregap_index.start_lba_on_disc = disc_lba;
|
|
|
|
pregap_index.start_lba_in_track = static_cast<LBA>(static_cast<unsigned long>(-pregap_frames));
|
|
|
|
pregap_index.length = pregap_frames;
|
|
|
|
pregap_index.track_number = track_num;
|
2019-10-18 12:44:28 +00:00
|
|
|
pregap_index.index_number = 0;
|
2019-11-10 12:44:53 +00:00
|
|
|
pregap_index.mode = mode;
|
2020-03-29 14:05:16 +00:00
|
|
|
pregap_index.control.bits = control.bits;
|
2019-10-18 08:18:04 +00:00
|
|
|
pregap_index.is_pregap = true;
|
2019-11-10 05:01:41 +00:00
|
|
|
if (pregap_in_file)
|
|
|
|
{
|
2020-01-30 05:50:00 +00:00
|
|
|
pregap_index.file_index = track_file_index;
|
2019-12-05 14:33:21 +00:00
|
|
|
pregap_index.file_offset = static_cast<u64>(static_cast<s64>(track_start - pregap_frames)) * track_sector_size;
|
2019-11-10 05:01:41 +00:00
|
|
|
pregap_index.file_sector_size = track_sector_size;
|
|
|
|
}
|
|
|
|
|
2019-10-18 08:18:04 +00:00
|
|
|
m_indices.push_back(pregap_index);
|
|
|
|
|
|
|
|
disc_lba += pregap_index.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
// add the track itself
|
2020-03-29 14:05:16 +00:00
|
|
|
m_tracks.push_back(Track{static_cast<u32>(track_num), disc_lba, static_cast<u32>(m_indices.size()),
|
2020-07-24 16:36:41 +00:00
|
|
|
static_cast<u32>(track_length + pregap_frames), mode, control});
|
2019-10-18 08:18:04 +00:00
|
|
|
|
|
|
|
// how many indices in this track?
|
|
|
|
Index last_index;
|
|
|
|
last_index.start_lba_on_disc = disc_lba;
|
2019-10-18 13:06:13 +00:00
|
|
|
last_index.start_lba_in_track = 0;
|
2019-10-18 08:18:04 +00:00
|
|
|
last_index.track_number = track_num;
|
2019-10-18 12:44:28 +00:00
|
|
|
last_index.index_number = 1;
|
2020-01-30 05:50:00 +00:00
|
|
|
last_index.file_index = track_file_index;
|
2019-10-18 08:18:04 +00:00
|
|
|
last_index.file_sector_size = track_sector_size;
|
2019-12-05 14:33:21 +00:00
|
|
|
last_index.file_offset = static_cast<u64>(static_cast<s64>(track_start)) * track_sector_size;
|
2019-11-10 12:44:53 +00:00
|
|
|
last_index.mode = mode;
|
|
|
|
last_index.control.bits = control.bits;
|
2019-10-18 13:06:13 +00:00
|
|
|
last_index.is_pregap = false;
|
2019-10-18 08:18:04 +00:00
|
|
|
|
|
|
|
long last_index_offset = track_start;
|
|
|
|
for (int index_num = 1;; index_num++)
|
|
|
|
{
|
|
|
|
long index_offset = track_get_index(track, index_num);
|
|
|
|
if (index_offset < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// add an index between the track indices
|
|
|
|
if (index_offset > last_index_offset)
|
|
|
|
{
|
|
|
|
last_index.length = index_offset - last_index_offset;
|
|
|
|
m_indices.push_back(last_index);
|
|
|
|
|
|
|
|
disc_lba += last_index.length;
|
|
|
|
last_index.start_lba_in_track += last_index.length;
|
|
|
|
last_index.start_lba_on_disc = disc_lba;
|
|
|
|
last_index.length = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
last_index.file_offset = index_offset * last_index.file_sector_size;
|
2019-10-18 12:44:28 +00:00
|
|
|
last_index.index_number = static_cast<u32>(index_num);
|
2019-10-18 08:18:04 +00:00
|
|
|
last_index_offset = index_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
// and the last index is added here
|
|
|
|
const long track_end_index = track_start + track_length;
|
|
|
|
DebugAssert(track_end_index >= last_index_offset);
|
|
|
|
if (track_end_index > last_index_offset)
|
|
|
|
{
|
|
|
|
last_index.length = track_end_index - last_index_offset;
|
|
|
|
m_indices.push_back(last_index);
|
|
|
|
|
|
|
|
disc_lba += last_index.length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-15 08:08:18 +00:00
|
|
|
if (m_tracks.empty())
|
|
|
|
{
|
|
|
|
Log_ErrorPrintf("File '%s' contains no tracks", filename);
|
2021-03-18 15:52:00 +00:00
|
|
|
if (error)
|
|
|
|
error->SetFormattedMessage("File '%s' contains no tracks", filename);
|
2020-10-15 08:08:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-18 08:18:04 +00:00
|
|
|
m_lba_count = disc_lba;
|
2020-05-08 00:50:22 +00:00
|
|
|
AddLeadOutIndex();
|
2019-12-06 06:23:08 +00:00
|
|
|
|
2020-01-30 03:18:16 +00:00
|
|
|
m_sbi.LoadSBI(FileSystem::ReplaceExtension(filename, "sbi").c_str());
|
2019-12-06 06:23:08 +00:00
|
|
|
|
2019-10-18 08:18:04 +00:00
|
|
|
return Seek(1, Position{0, 0, 0});
|
|
|
|
}
|
|
|
|
|
2021-03-26 16:19:23 +00:00
|
|
|
bool CDImageCueSheet::ReadSubChannelQ(SubChannelQ* subq, const Index& index, LBA lba_in_index)
|
2019-12-06 06:23:08 +00:00
|
|
|
{
|
2021-03-26 16:19:23 +00:00
|
|
|
if (m_sbi.GetReplacementSubChannelQ(index.start_lba_on_disc + lba_in_index, subq))
|
2019-12-06 06:23:08 +00:00
|
|
|
return true;
|
|
|
|
|
2021-03-26 16:19:23 +00:00
|
|
|
return CDImage::ReadSubChannelQ(subq, index, lba_in_index);
|
2019-12-06 06:23:08 +00:00
|
|
|
}
|
|
|
|
|
2020-12-17 17:01:57 +00:00
|
|
|
bool CDImageCueSheet::HasNonStandardSubchannel() const
|
|
|
|
{
|
|
|
|
return (m_sbi.GetReplacementSectorCount() > 0);
|
|
|
|
}
|
|
|
|
|
2020-01-30 05:50:00 +00:00
|
|
|
bool CDImageCueSheet::ReadSectorFromIndex(void* buffer, const Index& index, LBA lba_in_index)
|
|
|
|
{
|
|
|
|
DebugAssert(index.file_index < m_files.size());
|
|
|
|
|
|
|
|
TrackFile& tf = m_files[index.file_index];
|
|
|
|
const u64 file_position = index.file_offset + (static_cast<u64>(lba_in_index) * index.file_sector_size);
|
|
|
|
if (tf.file_position != file_position)
|
|
|
|
{
|
|
|
|
if (std::fseek(tf.file, static_cast<long>(file_position), SEEK_SET) != 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
tf.file_position = file_position;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (std::fread(buffer, index.file_sector_size, 1, tf.file) != 1)
|
|
|
|
{
|
|
|
|
std::fseek(tf.file, static_cast<long>(tf.file_position), SEEK_SET);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
tf.file_position += index.file_sector_size;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-18 15:52:00 +00:00
|
|
|
std::unique_ptr<CDImage> CDImage::OpenCueSheetImage(const char* filename, Common::Error* error)
|
2019-10-18 08:18:04 +00:00
|
|
|
{
|
|
|
|
std::unique_ptr<CDImageCueSheet> image = std::make_unique<CDImageCueSheet>();
|
2021-03-18 15:52:00 +00:00
|
|
|
if (!image->OpenAndParse(filename, error))
|
2019-10-18 08:18:04 +00:00
|
|
|
return {};
|
|
|
|
|
|
|
|
return image;
|
|
|
|
}
|