CDImage: Support generating Q Sub-Channel

This commit is contained in:
Connor McLaughlin 2019-11-10 22:44:53 +10:00
parent 2cc5084dd1
commit 6bb4f8e728
4 changed files with 178 additions and 9 deletions

View File

@ -1,12 +1,19 @@
#include "cd_image.h"
#include "YBaseLib/ByteStream.h"
#include "YBaseLib/Log.h"
#include <array>
Log_SetChannel(CDImage);
CDImage::CDImage() = default;
CDImage::~CDImage() = default;
u32 CDImage::GetBytesPerSector(TrackMode mode)
{
static constexpr std::array<u32, 8> sizes = {{2352, 2048, 2352, 2336, 2048, 2324, 2332, 2352}};
return sizes[static_cast<u32>(mode)];
}
std::unique_ptr<CDImage> CDImage::Open(const char* filename)
{
const char* extension = std::strrchr(filename, '.');
@ -60,7 +67,7 @@ bool CDImage::Seek(LBA lba)
return false;
}
const u32 new_index_offset = lba - new_index->start_lba_on_disc;
const LBA new_index_offset = lba - new_index->start_lba_on_disc;
if (new_index_offset >= new_index->length)
return false;
@ -81,7 +88,7 @@ bool CDImage::Seek(u32 track_number, const Position& pos_in_track)
return false;
const Track& track = m_tracks[track_number - 1];
const u32 pos_lba = pos_in_track.ToLBA();
const LBA pos_lba = pos_in_track.ToLBA();
if (pos_lba >= track.length)
return false;
@ -182,6 +189,17 @@ bool CDImage::ReadRawSector(void* buffer)
return true;
}
bool CDImage::ReadSubChannelQ(SubChannelQ* subq)
{
// handle case where we're at the end of the track/index
if (m_position_in_index == m_current_index->length)
return GenerateSubChannelQ(subq, m_position_on_disc);
// otherwise save the index lookup
GenerateSubChannelQ(subq, m_current_index, m_position_in_index);
return true;
}
const CDImage::Index* CDImage::GetIndexForDiscPosition(LBA pos)
{
for (const Index& index : m_indices)
@ -210,3 +228,60 @@ const CDImage::Index* CDImage::GetIndexForTrackPosition(u32 track_number, LBA tr
return GetIndexForDiscPosition(track.start_lba + track_pos);
}
bool CDImage::GenerateSubChannelQ(SubChannelQ* subq, LBA lba)
{
const Index* index = GetIndexForDiscPosition(lba);
if (!index)
return false;
const u32 index_offset = index->start_lba_on_disc - lba;;
GenerateSubChannelQ(subq, index, index_offset);
return true;
}
void CDImage::GenerateSubChannelQ(SubChannelQ* subq, const Index* index, u32 index_offset)
{
subq->control.bits = index->control.bits;
subq->track_number_bcd = DecimalToBCD(index->track_number);
subq->index_number_bcd = DecimalToBCD(index->index_number);
const Position relative_position =
Position::FromLBA(std::abs(static_cast<s32>(index->start_lba_in_track + index_offset)));
std::tie(subq->relative_minute_bcd, subq->relative_second_bcd, subq->relative_frame_bcd) = relative_position.ToBCD();
subq->reserved = 0;
const Position absolute_position = Position::FromLBA(index->start_lba_on_disc + index_offset);
std::tie(subq->absolute_minute_bcd, subq->absolute_second_bcd, subq->absolute_frame_bcd) = absolute_position.ToBCD();
subq->crc = subq->ComputeCRC();
}
u16 CDImage::SubChannelQ::ComputeCRC() const
{
static constexpr std::array<u16, 256> crc16_table = {
{0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7, 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD,
0xE1CE, 0xF1EF, 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6, 0x9339, 0x8318, 0xB37B, 0xA35A,
0xD3BD, 0xC39C, 0xF3FF, 0xE3DE, 0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485, 0xA56A, 0xB54B,
0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D, 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC, 0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861,
0x2802, 0x3823, 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B, 0x5AF5, 0x4AD4, 0x7AB7, 0x6A96,
0x1A71, 0x0A50, 0x3A33, 0x2A12, 0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A, 0x6CA6, 0x7C87,
0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41, 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70, 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A,
0x9F59, 0x8F78, 0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F, 0x1080, 0x00A1, 0x30C2, 0x20E3,
0x5004, 0x4025, 0x7046, 0x6067, 0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E, 0x02B1, 0x1290,
0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256, 0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, 0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E,
0xC71D, 0xD73C, 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634, 0xD94C, 0xC96D, 0xF90E, 0xE92F,
0x99C8, 0x89E9, 0xB98A, 0xA9AB, 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3, 0xCB7D, 0xDB5C,
0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A, 0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9, 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83,
0x1CE0, 0x0CC1, 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8, 0x6E17, 0x7E36, 0x4E55, 0x5E74,
0x2E93, 0x3EB2, 0x0ED1, 0x1EF0}};
u16 value = 0;
for (u32 i = 0; i < 10; i++)
value = crc16_table[(value >> 8) ^ data[i]] ^ (value << 8);
return ~(value >> 8) | (~(value) << 8);
}

View File

@ -21,9 +21,10 @@ public:
DATA_SECTOR_SIZE = 2048,
SECTOR_SYNC_SIZE = 12,
SECTOR_HEADER_SIZE = 4,
FRAMES_PER_SECOND = 75, // "sectors"
FRAMES_PER_SECOND = 75, // "sectors", or "timecode frames" (not "channel frames")
SECONDS_PER_MINUTE = 60,
FRAMES_PER_MINUTE = FRAMES_PER_SECOND * SECONDS_PER_MINUTE
FRAMES_PER_MINUTE = FRAMES_PER_SECOND * SECONDS_PER_MINUTE,
SUBCHANNEL_BYTES_PER_FRAME = 12
};
enum class ReadMode : u32
@ -33,6 +34,18 @@ public:
RawNoSync, // 2340 bytes per sector.
};
enum class TrackMode : u32
{
Audio, // 2352 bytes per sector
Mode1, // 2048 bytes per sector
Mode1Raw, // 2352 bytes per sector
Mode2, // 2336 bytes per sector
Mode2Form1, // 2048 bytes per sector
Mode2Form2, // 2324 bytes per sector
Mode2FormMix, // 2332 bytes per sector
Mode2Raw // 2352 bytes per sector
};
struct SectorHeader
{
u8 minute;
@ -70,6 +83,11 @@ public:
return ZeroExtend32(minute) * FRAMES_PER_MINUTE + ZeroExtend32(second) * FRAMES_PER_SECOND + ZeroExtend32(frame);
}
constexpr std::tuple<u8, u8, u8> ToBCD() const
{
return std::make_tuple<u8, u8, u8>(DecimalToBCD(minute), DecimalToBCD(second), DecimalToBCD(frame));
}
Position operator+(const Position& rhs) { return FromLBA(ToLBA() + rhs.ToLBA()); }
Position& operator+=(const Position& pos)
{
@ -78,7 +96,7 @@ public:
}
#define RELATIONAL_OPERATOR(op) \
bool operator op (const Position& rhs) const \
bool operator op(const Position& rhs) const \
{ \
return std::tie(minute, second, frame) op std::tie(rhs.minute, rhs.second, rhs.frame); \
}
@ -93,6 +111,43 @@ public:
#undef RELATIONAL_OPERATOR
};
union SubChannelQ
{
union Control
{
u8 bits;
BitField<u8, u8, 0, 4> adr;
BitField<u8, bool, 4, 1> audio_preemphasis;
BitField<u8, bool, 5, 1> digital_copy_permitted;
BitField<u8, bool, 6, 1> data;
BitField<u8, bool, 7, 1> four_channel_audio;
};
struct
{
Control control;
u8 track_number_bcd;
u8 index_number_bcd;
u8 relative_minute_bcd;
u8 relative_second_bcd;
u8 relative_frame_bcd;
u8 reserved;
u8 absolute_minute_bcd;
u8 absolute_second_bcd;
u8 absolute_frame_bcd;
u16 crc;
};
u8 data[SUBCHANNEL_BYTES_PER_FRAME];
u16 ComputeCRC() const;
};
static_assert(sizeof(SubChannelQ) == SUBCHANNEL_BYTES_PER_FRAME, "SubChannelQ is correct size");
// Helper functions.
static u32 GetBytesPerSector(TrackMode mode);
// Opening disc image.
static std::unique_ptr<CDImage> Open(const char* filename);
static std::unique_ptr<CDImage> OpenBinImage(const char* filename);
@ -126,6 +181,9 @@ public:
// Read a single raw sector from the current LBA.
bool ReadRawSector(void* buffer);
// Reads sub-channel Q for the current LBA.
virtual bool ReadSubChannelQ(SubChannelQ* subq);
protected:
struct Track
{
@ -133,6 +191,8 @@ protected:
LBA start_lba;
u32 first_index;
u32 length;
TrackMode mode;
SubChannelQ::Control control;
};
struct Index
@ -145,12 +205,20 @@ protected:
u32 index_number;
LBA start_lba_in_track;
u32 length;
TrackMode mode;
SubChannelQ::Control control;
bool is_pregap;
};
const Index* GetIndexForDiscPosition(LBA pos);
const Index* GetIndexForTrackPosition(u32 track_number, LBA track_pos);
/// Generates sub-channel Q given the specified position.
bool GenerateSubChannelQ(SubChannelQ* subq, LBA lba);
/// Generates sub-channel Q from the given index and index-offset.
void GenerateSubChannelQ(SubChannelQ* subq, const Index* index, u32 index_offset);
std::string m_filename;
u32 m_lba_count = 0;

View File

@ -41,14 +41,20 @@ bool CDImageBin::Open(const char* filename)
m_lba_count = file_size / track_sector_size;
SubChannelQ::Control control = {};
TrackMode mode = TrackMode::Mode2Raw;
// Two seconds default pregap.
const u32 pregap_frames = 2 * FRAMES_PER_SECOND;
Index pregap_index = {};
pregap_index.file_sector_size = track_sector_size;
pregap_index.start_lba_on_disc = 0;
pregap_index.start_lba_in_track = static_cast<LBA>(-static_cast<s32>(pregap_frames));
pregap_index.length = pregap_frames;
pregap_index.track_number = 1;
pregap_index.index_number = 0;
pregap_index.mode = mode;
pregap_index.control.bits = control.bits;
pregap_index.is_pregap = true;
m_indices.push_back(pregap_index);
@ -62,10 +68,13 @@ bool CDImageBin::Open(const char* filename)
data_index.index_number = 1;
data_index.start_lba_in_track = 0;
data_index.length = m_lba_count;
data_index.mode = mode;
data_index.control.bits = control.bits;
m_indices.push_back(data_index);
// Assume a single track.
m_tracks.push_back(Track{static_cast<u32>(1), data_index.start_lba_on_disc, static_cast<u32>(0), m_lba_count});
m_tracks.push_back(
Track{static_cast<u32>(1), data_index.start_lba_on_disc, static_cast<u32>(0), m_lba_count, mode, control});
return Seek(1, Position{0, 0, 0});
}

View File

@ -67,6 +67,9 @@ bool CDImageCueSheet::OpenAndParse(const char* filename)
u32 disc_lba = 0;
// "last track" subchannel q - used for the pregap
SubChannelQ::Control last_track_control{};
// for each track..
const int num_tracks = cd_get_ntrack(m_cd);
for (int track_num = 1; track_num <= num_tracks; track_num++)
@ -91,11 +94,20 @@ bool CDImageCueSheet::OpenAndParse(const char* filename)
it = m_files.emplace(track_filename, track_fp).first;
}
// TODO: FIXME
const u32 track_sector_size = RAW_SECTOR_SIZE;
// 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
if (track_length < 0)
{
// determine the length from the file
std::fseek(it->second, 0, SEEK_END);
long file_size = std::ftell(it->second);
std::fseek(it->second, 0, SEEK_SET);
@ -121,6 +133,8 @@ bool CDImageCueSheet::OpenAndParse(const char* filename)
pregap_index.length = pregap_frames;
pregap_index.track_number = track_num;
pregap_index.index_number = 0;
pregap_index.mode = mode;
pregap_index.control.bits = (track_num > 1) ? last_track_control.bits : control.bits;
pregap_index.is_pregap = true;
if (pregap_in_file)
{
@ -138,6 +152,7 @@ bool CDImageCueSheet::OpenAndParse(const char* filename)
// add the track itself
m_tracks.push_back(
Track{static_cast<u32>(track_num), disc_lba, static_cast<u32>(m_indices.size()), static_cast<u32>(track_length)});
last_track_control.bits = control.bits;
// how many indices in this track?
Index last_index;
@ -148,6 +163,8 @@ bool CDImageCueSheet::OpenAndParse(const char* filename)
last_index.file = it->second;
last_index.file_sector_size = track_sector_size;
last_index.file_offset = file_offset;
last_index.mode = mode;
last_index.control.bits = control.bits;
last_index.is_pregap = false;
long last_index_offset = track_start;