Skip to content

Commit

Permalink
Merge pull request #459 from ZuluSCSI/dev_fix_422
Browse files Browse the repository at this point in the history
CD-ROM: Support PREGAP in .cue files
  • Loading branch information
aperezbios authored Sep 26, 2024
2 parents fd06dec + e274c17 commit 356bbe8
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 8 deletions.
7 changes: 6 additions & 1 deletion lib/CUEParser/src/CUEParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ const CUETrackInfo *CUEParser::next_track()
}
else if (strncasecmp(m_parse_pos, "PREGAP ", 7) == 0)
{
// Unstored pregap, which offsets the data start on CD but does not
// affect the offset in data file.
const char *time_str = skip_space(m_parse_pos + 7);
m_track_info.unstored_pregap_length = parse_time(time_str);
}
Expand All @@ -104,11 +106,13 @@ const CUETrackInfo *CUEParser::next_track()

if (index == 0)
{
// Stored pregap that is present both on CD and in data file
m_track_info.track_start = time;
got_pause = true;
}
else if (index == 1)
{
// Data content of the track
m_track_info.data_start = time;
got_data = true;
}
Expand All @@ -120,6 +124,7 @@ const CUETrackInfo *CUEParser::next_track()
if (got_data && !got_pause)
{
m_track_info.track_start = m_track_info.data_start;
m_track_info.data_start += m_track_info.unstored_pregap_length;
}

if (got_track && got_data)
Expand Down Expand Up @@ -279,4 +284,4 @@ uint32_t CUEParser::get_sector_length(CUEFileMode filemode, CUETrackMode trackmo
{
return 0;
}
}
}
3 changes: 2 additions & 1 deletion lib/CUEParser/src/CUEParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ struct CUETrackInfo
// Source file name and file type, and offset to start of track data in bytes.
char filename[CUE_MAX_FILENAME+1];
CUEFileMode file_mode;
uint64_t file_offset; // corresponds to track_start below
uint64_t file_offset; // corresponds to data_start below

// Track number and mode in CD format
int track_number;
Expand All @@ -70,6 +70,7 @@ struct CUETrackInfo
uint32_t data_start;

// LBA for the beginning of the track, which will be INDEX 00 if that is present.
// If there is unstored PREGAP, it's added between track_start and data_start.
// Otherwise this will be INDEX 01 matching data_start above.
uint32_t track_start;
};
Expand Down
57 changes: 55 additions & 2 deletions lib/CUEParser/test/CUEParser_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ FILE "Sound.wav" WAVE
TEST(track->track_mode == CUETrack_AUDIO);
TEST(track->sector_length == 2352);
TEST(track->unstored_pregap_length == 2 * 75);
TEST(track->data_start == start2);
TEST(track->data_start == start2 + 2 * 75);
}

COMMENT("Test TRACK 03 (audio with index 0)");
Expand Down Expand Up @@ -177,10 +177,63 @@ TRACK 03 MODE1/2352
return status;
}

bool test_datatrackpregap()
{
bool status = true;
const char *cue_sheet = R"(
FILE "issue422.bin" BINARY
TRACK 01 AUDIO
INDEX 01 00:00:00
TRACK 02 MODE1/2352
PREGAP 00:02:00
INDEX 01 01:06:19
)";

CUEParser parser(cue_sheet);

COMMENT("test_datatrackpregap()");
COMMENT("Test TRACK 01 (audio)");
const CUETrackInfo *track = parser.next_track();
TEST(track != NULL);
if (track)
{
TEST(strcmp(track->filename, "issue422.bin") == 0);
TEST(track->file_mode == CUEFile_BINARY);
TEST(track->file_offset == 0);
TEST(track->track_number == 1);
TEST(track->track_mode == CUETrack_AUDIO);
TEST(track->sector_length == 2352);
TEST(track->unstored_pregap_length == 0);
TEST(track->data_start == 0);
TEST(track->track_start == 0);
}

COMMENT("Test TRACK 02 (data)");
track = parser.next_track();
TEST(track != NULL);
if (track)
{
TEST(strcmp(track->filename, "issue422.bin") == 0);
TEST(track->file_mode == CUEFile_BINARY);
TEST(track->file_offset == 0xB254B0);
TEST(track->track_number == 2);
TEST(track->track_mode == CUETrack_MODE1_2352);
TEST(track->sector_length == 2352);
TEST(track->unstored_pregap_length == 75 * 2);
TEST(track->data_start == (60 + 6 + 2) * 75 + 19);
TEST(track->track_start == (60 + 6) * 75 + 19);
}

track = parser.next_track();
TEST(track == NULL);

return status;
}


int main()
{
if (test_basics() && test_datatracks())
if (test_basics() && test_datatracks() && test_datatrackpregap())
{
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/CUEParser/test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ all: CUEParser_test
./CUEParser_test

CUEParser_test: CUEParser_test.cpp ../src/CUEParser.cpp
g++ -Wall -Wextra -o $@ -I ../src $^
g++ -Wall -Wextra -g -ggdb -o $@ -I ../src $^
2 changes: 1 addition & 1 deletion src/ImageBackingStore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ bool ImageBackingStore::close()

uint64_t ImageBackingStore::size()
{
if (m_iscontiguous && m_blockdev)
if (m_iscontiguous && m_blockdev && m_israw)
{
return (uint64_t)(m_endsector - m_bgnsector + 1) * SD_SECTOR_SIZE;
}
Expand Down
4 changes: 2 additions & 2 deletions src/ZuluSCSI_cdrom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ static uint32_t getLeadOutLBA(const CUETrackInfo* lasttrack)
image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
uint32_t lastTrackBlocks = (img.file.size() - lasttrack->file_offset)
/ lasttrack->sector_length;
return lasttrack->track_start + lastTrackBlocks;
return lasttrack->data_start + lastTrackBlocks;
}
else
{
Expand Down Expand Up @@ -1500,7 +1500,7 @@ static void doReadCD(uint32_t lba, uint32_t length, uint8_t sector_type,
}
else
{
offset = trackinfo.file_offset + trackinfo.sector_length * (lba - trackinfo.track_start);
offset = trackinfo.file_offset + trackinfo.sector_length * (lba - trackinfo.data_start);
dbgmsg("------ Read CD: ", (int)length, " sectors starting at ", (int)lba,
", track number ", trackinfo.track_number, ", sector size ", (int)trackinfo.sector_length,
", main channel ", main_channel, ", sub channel ", sub_channel,
Expand Down

0 comments on commit 356bbe8

Please sign in to comment.