Commit 69eef0ee authored by Libretro-Admin's avatar Libretro-Admin
Browse files

Update to new cdrom code

parent cb945643
......@@ -5,6 +5,7 @@ DEPS_DIR := $(CORE_DIR)/deps
LIBRETRO_DIR := $(CORE_DIR)/libretro-common
MEDNAFEN_DIR := $(CORE_DIR)/mednafen
CORE_EMU_DIR := $(MEDNAFEN_DIR)/ss
CDROM_DIR := $(MEDNAFEN_DIR)/cdrom
INCFLAGS := -I$(CORE_DIR) -I$(MEDNAFEN_DIR) -I$(MEDNAFEN_DIR)/include -I$(MEDNAFEN_DIR)/intl -I$(MEDNAFEN_DIR)/hw_sound -I$(MEDNAFEN_DIR)/hw_cpu -I$(MEDNAFEN_DIR)/hw_misc -I$(LIBRETRO_DIR)/include -I$(DEPS_DIR)/zlib
......@@ -109,22 +110,21 @@ ifeq ($(NEED_TREMOR), 1)
SOURCES_C += $(wildcard $(MEDNAFEN_DIR)/tremor/*.c)
endif
ifeq ($(NEED_CD), 1)
SOURCES_CXX += $(MEDNAFEN_DIR)/cdrom/CDAccess.cpp \
$(MEDNAFEN_DIR)/cdrom/CDAccess_Image.cpp \
$(MEDNAFEN_DIR)/cdrom/CDAccess_CCD.cpp \
$(MEDNAFEN_DIR)/cdrom/audioreader.cpp \
$(MEDNAFEN_DIR)/cdrom/misc.cpp \
$(MEDNAFEN_DIR)/cdrom/cdromif.cpp
ifneq ($(HAVE_GRIFFIN),1)
SOURCES_CXX += $(CDROM_DIR)/CDAccess.cpp \
$(CDROM_DIR)/CDAccess_Image.cpp \
$(CDROM_DIR)/CDAccess_CCD.cpp \
$(CDROM_DIR)/CDAFReader.cpp \
$(CDROM_DIR)/CDAFReader_Vorbis.cpp \
$(CDROM_DIR)/cdromif.cpp \
$(CDROM_DIR)/CDUtility.cpp \
$(CDROM_DIR)/lec.cpp \
$(CDROM_DIR)/galois.cpp \
$(CDROM_DIR)/recover-raw.cpp \
$(CDROM_DIR)/l-ec.cpp \
$(CDROM_DIR)/edc_crc32.cpp
endif
SOURCES_C += \
$(MEDNAFEN_DIR)/cdrom/CDUtility.c \
$(MEDNAFEN_DIR)/cdrom/galois.c \
$(MEDNAFEN_DIR)/cdrom/l-ec.c \
$(MEDNAFEN_DIR)/cdrom/lec.c \
$(MEDNAFEN_DIR)/cdrom/recover-raw.c \
$(MEDNAFEN_DIR)/cdrom/edc_crc32.c
FLAGS += -DNEED_CD
SOURCES_CXX += \
$(MEDNAFEN_DIR)/error.cpp \
......
......@@ -1371,7 +1371,6 @@ static bool Load(MDFNFILE* fp)
unsigned i;
if(MDFN_GetSettingS("ss.dbg_exe_cdpath") != "")
{
bool success;
RMD_Drive dr;
dr.Name = std::string("Virtual CD Drive");
......@@ -1387,8 +1386,7 @@ static bool Load(MDFNFILE* fp)
static std::vector<CDIF *> CDInterfaces;
CDInterfaces.clear();
CDInterfaces.push_back(CDIF_Open(&success, MDFN_GetSettingS("ss.dbg_exe_cdpath").c_str(), false,
false));
CDInterfaces.push_back(CDIF_Open(MDFN_GetSettingS("ss.dbg_exe_cdpath").c_str(), false));
cdifs = &CDInterfaces;
}
......@@ -2250,14 +2248,14 @@ static MDFNGI *MDFNI_LoadCD(const char *devicename)
for(unsigned i = 0; i < file_list.size(); i++)
{
bool success = true;
CDIF *image = CDIF_Open(&success, file_list[i].c_str(), false, old_cdimagecache);
CDIF *image = CDIF_Open(file_list[i].c_str(), false);
CDInterfaces.push_back(image);
}
}
else
{
bool success = true;
CDIF *image = CDIF_Open(&success, devicename, false, old_cdimagecache);
CDIF *image = CDIF_Open(devicename, false);
log_cb(RETRO_LOG_INFO, "Pushing CD image onto stack: %s.\n", devicename);
CDInterfaces.push_back(image);
}
......@@ -2272,7 +2270,6 @@ static MDFNGI *MDFNI_LoadCD(const char *devicename)
for(unsigned i = 0; i < CDInterfaces.size(); i++)
{
TOC toc;
TOC_Clear(&toc);
CDInterfaces[i]->ReadTOC(&toc);
......@@ -2296,9 +2293,8 @@ static MDFNGI *MDFNI_LoadCD(const char *devicename)
for(unsigned i = 0; i < CDInterfaces.size(); i++)
{
CD_TOC toc;
TOC toc;
TOC_Clear(&toc);
CDInterfaces[i]->ReadTOC(&toc);
layout_md5.update_u32_as_lsb(toc.first_track);
......
/******************************************************************************/
/* Mednafen - Multi-system Emulator */
/******************************************************************************/
/* CDAFReader.cpp:
** Copyright (C) 2010-2016 Mednafen Team
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation; either version 2
** of the License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software Foundation, Inc.,
** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
// CDAFR_Open(), and CDAFReader, will NOT take "ownership" of the Stream object(IE it won't ever delete it). Though it does assume it has exclusive access
// to it for as long as the CDAFReader object exists.
// Don't allow exceptions to propagate into the vorbis/musepack/etc. libraries, as it could easily leave the state of the library's decoder "object" in an
// inconsistent state, which would cause all sorts of unfun when we try to destroy it while handling the exception farther up.
#include <mednafen/mednafen.h>
#include "CDAFReader.h"
#include "CDAFReader_Vorbis.h"
#include "CDAFReader_MPC.h"
CDAFReader::CDAFReader() : LastReadPos(0)
{
}
CDAFReader::~CDAFReader()
{
}
CDAFReader *CDAFR_Open(Stream *fp)
{
static CDAFReader* (* const OpenFuncs[])(Stream* fp) =
{
#ifdef HAVE_MPC
CDAFR_MPC_Open,
#endif
CDAFR_Vorbis_Open
};
for(auto const& f : OpenFuncs)
{
return f(fp);
}
return(NULL);
}
/******************************************************************************/
/* Mednafen - Multi-system Emulator */
/******************************************************************************/
/* CDAFReader.h:
** Copyright (C) 2010-2016 Mednafen Team
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation; either version 2
** of the License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software Foundation, Inc.,
** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef __MDFN_CDAFREADER_H
#define __MDFN_CDAFREADER_H
#include <mednafen/Stream.h>
class CDAFReader
{
public:
CDAFReader();
virtual ~CDAFReader();
virtual uint64_t FrameCount(void) = 0;
INLINE uint64_t Read(uint64_t frame_offset, int16 *buffer, uint64_t frames)
{
uint64_t ret;
if(LastReadPos != frame_offset)
{
//puts("SEEK");
if(!Seek_(frame_offset))
return(0);
LastReadPos = frame_offset;
}
ret = Read_(buffer, frames);
LastReadPos += ret;
return(ret);
}
private:
virtual uint64_t Read_(int16 *buffer, uint64_t frames) = 0;
virtual bool Seek_(uint64_t frame_offset) = 0;
uint64_t LastReadPos;
};
// AR_Open(), and CDAFReader, will NOT take "ownership" of the Stream object(IE it won't ever delete it). Though it does assume it has exclusive access
// to it for as long as the CDAFReader object exists.
CDAFReader *CDAFR_Open(Stream *fp);
#endif
/******************************************************************************/
/* Mednafen - Multi-system Emulator */
/******************************************************************************/
/* CDAFReader_MPC.cpp:
** Copyright (C) 2006-2016 Mednafen Team
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation; either version 2
** of the License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software Foundation, Inc.,
** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <mednafen/mednafen.h>
#include "CDAFReader.h"
#include "CDAFReader_MPC.h"
#include <mednafen/mpcdec/mpcdec.h>
class CDAFReader_MPC final : public CDAFReader
{
public:
CDAFReader_MPC(Stream *fp);
~CDAFReader_MPC();
uint64_t Read_(int16 *buffer, uint64_t frames) override;
bool Seek_(uint64_t frame_offset) override;
uint64_t FrameCount(void) override;
private:
mpc_reader reader;
mpc_demux *demux;
mpc_streaminfo si;
MPC_SAMPLE_FORMAT MPCBuffer[MPC_DECODER_BUFFER_LENGTH];
uint32_t MPCBufferIn;
uint32_t MPCBufferOffs;
Stream *fw;
};
/// Reads size bytes of data into buffer at ptr.
static mpc_int32_t_t impc_read(mpc_reader *p_reader, void *ptr, mpc_int32_t_t size)
{
Stream *fw = (Stream*)(p_reader->data);
return fw->read(ptr, size, false);
}
/// Seeks to byte position offset.
static mpc_bool_t impc_seek(mpc_reader *p_reader, mpc_int32_t_t offset)
{
Stream *fw = (Stream*)(p_reader->data);
fw->seek(offset, SEEK_SET);
return(MPC_TRUE);
}
/// Returns the current byte offset in the stream.
static mpc_int32_t_t impc_tell(mpc_reader *p_reader)
{
Stream *fw = (Stream*)(p_reader->data);
return fw->tell();
}
/// Returns the total length of the source stream, in bytes.
static mpc_int32_t_t impc_get_size(mpc_reader *p_reader)
{
Stream *fw = (Stream*)(p_reader->data);
return fw->size();
}
/// True if the stream is a seekable stream.
static mpc_bool_t impc_canseek(mpc_reader *p_reader)
{
return(MPC_TRUE);
}
CDAFReader_MPC::CDAFReader_MPC(Stream *fp) : fw(fp)
{
demux = NULL;
memset(&si, 0, sizeof(si));
memset(MPCBuffer, 0, sizeof(MPCBuffer));
MPCBufferOffs = 0;
MPCBufferIn = 0;
memset(&reader, 0, sizeof(reader));
reader.read = impc_read;
reader.seek = impc_seek;
reader.tell = impc_tell;
reader.get_size = impc_get_size;
reader.canseek = impc_canseek;
reader.data = (void*)fp;
if(!(demux = mpc_demux_init(&reader)))
{
throw(0);
}
mpc_demux_get_info(demux, &si);
if(si.channels != 2)
{
mpc_demux_exit(demux);
demux = NULL;
throw MDFN_Error(0, _("MusePack stream has wrong number of channels(%u); the correct number is 2."), si.channels);
}
if(si.sample_freq != 44100)
{
mpc_demux_exit(demux);
demux = NULL;
throw MDFN_Error(0, _("MusePack stream has wrong samplerate(%u Hz); the correct samplerate is 44100 Hz."), si.sample_freq);
}
}
CDAFReader_MPC::~CDAFReader_MPC()
{
if(demux)
{
mpc_demux_exit(demux);
demux = NULL;
}
}
uint64_t CDAFReader_MPC::Read_(int16 *buffer, uint64_t frames)
{
mpc_status err;
int16 *cowbuf = (int16 *)buffer;
int32_t toread = frames * 2;
while(toread > 0)
{
int32_t tmplen;
if(!MPCBufferIn)
{
mpc_frame_info fi;
memset(&fi, 0, sizeof(fi));
fi.buffer = MPCBuffer;
if((err = mpc_demux_decode(demux, &fi)) < 0 || fi.bits == -1)
return(frames - toread / 2);
MPCBufferIn = fi.samples * 2;
MPCBufferOffs = 0;
}
tmplen = MPCBufferIn;
if(tmplen >= toread)
tmplen = toread;
for(int x = 0; x < tmplen; x++)
{
int32_t samp = MPCBuffer[MPCBufferOffs + x] >> MPC_FIXED_POINT_FRACTPART;
if(samp < -32768)
samp = -32768;
if(samp > 32767)
samp = 32767;
*cowbuf = (int16)samp;
cowbuf++;
}
MPCBufferOffs += tmplen;
toread -= tmplen;
MPCBufferIn -= tmplen;
}
return(frames - toread / 2);
}
bool CDAFReader_MPC::Seek_(uint64_t frame_offset)
{
MPCBufferOffs = 0;
MPCBufferIn = 0;
if(mpc_demux_seek_sample(demux, frame_offset) < 0)
return(false);
return(true);
}
uint64_t CDAFReader_MPC::FrameCount(void)
{
return(mpc_streaminfo_get_length_samples(&si));
}
CDAFReader* CDAFR_MPC_Open(Stream* fp)
{
return new CDAFReader_MPC(fp);
}
/******************************************************************************/
/* Mednafen - Multi-system Emulator */
/******************************************************************************/
/* CDAFReader_MPC.h:
** Copyright (C) 2015-2016 Mednafen Team
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation; either version 2
** of the License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software Foundation, Inc.,
** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef __MDFN_CDAFREADER_MPC_H
#define __MDFN_CDAFREADER_MPC_H
CDAFReader* CDAFR_MPC_Open(Stream* fp);
#endif
/* Mednafen - Multi-system Emulator
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
// AR_Open(), and AudioReader, will NOT take "ownership" of the Stream object(IE it won't ever delete it). Though it does assume it has exclusive access
// to it for as long as the AudioReader object exists.
// Don't allow exceptions to propagate into the vorbis/musepack/etc. libraries, as it could easily leave the state of the library's decoder "object" in an
// inconsistent state, which would cause all sorts of unfun when we try to destroy it while handling the exception farther up.
#include "../mednafen.h"
#include "audioreader.h"
#include "../tremor/ivorbisfile.h"
#include <string.h>
#include <errno.h>
#include <time.h>
#include "../general.h"
#include "../mednafen-endian.h"
AudioReader::AudioReader() : LastReadPos(0)
{
}
AudioReader::~AudioReader()
{
}
int64_t AudioReader::Read_(int16_t *buffer, int64_t frames)
{
abort();
return(false);
}
bool AudioReader::Seek_(int64_t frame_offset)
{
abort();
return(false);
}
int64_t AudioReader::FrameCount(void)
{
abort();
return(0);
}
class OggVorbisReader : public AudioReader
/******************************************************************************/
/* Mednafen - Multi-system Emulator */
/******************************************************************************/
/* CDAFReader_Vorbis.cpp:
** Copyright (C) 2010-2016 Mednafen Team
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation; either version 2
** of the License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software Foundation, Inc.,
** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <mednafen/mednafen.h>
#include "CDAFReader.h"
#include "CDAFReader_Vorbis.h"
#include <mednafen/tremor/ivorbisfile.h>
class CDAFReader_Vorbis final : public CDAFReader
{
public:
OggVorbisReader(Stream *fp);
~OggVorbisReader();
CDAFReader_Vorbis(Stream *fp);
~CDAFReader_Vorbis();
int64_t Read_(int16_t *buffer, int64_t frames);
bool Seek_(int64_t frame_offset);
int64_t FrameCount(void);
uint64_t Read_(int16_t *buffer, uint64_t frames) override;
bool Seek_(uint64_t frame_offset) override;
uint64_t FrameCount(void) override;
private:
OggVorbis_File ovfile;
Stream *fw;
};
......@@ -80,18 +45,17 @@ static size_t iov_read_func(void *ptr, size_t size, size_t nmemb, void *user_dat
{
Stream *fw = (Stream*)user_data;
if(!size || !fw)
if(!size)
return(0);
return fw->read(ptr, size * nmemb, false) / size;
}
static int iov_seek_func(void *user_data, ogg_int64_t offset, int whence)
static int iov_seek_func(void *user_data, int64_t offset, int whence)
{
Stream *fw = (Stream*)user_data;
if (fw)
fw->seek(offset, whence);
fw->seek(offset, whence);
return(0);
}
......@@ -99,22 +63,17 @@ static int iov_close_func(void *user_data)
{
Stream *fw = (Stream*)user_data;
if (fw)
fw->close();
fw->close();
return(0);
}
static long iov_tell_func(void *user_data)
{
Stream *fw = (Stream*)user_data;
if (!fw)
return -1;
return fw->tell();
}
OggVorbisReader::OggVorbisReader(Stream *fp)
CDAFReader_Vorbis::CDAFReader_Vorbis(Stream *fp) : fw(fp)
{
ov_callbacks cb;
......@@ -124,17 +83,16 @@ OggVorbisReader::OggVorbisReader(Stream *fp)
cb.close_func = iov_close_func;
cb.tell_func = iov_tell_func;
fp->seek(0, SEEK_SET);
if(ov_open_callbacks(fp, &ovfile, NULL, 0, cb))
throw(0);
}
OggVorbisReader::~OggVorbisReader()
CDAFReader_Vorbis::~CDAFReader_Vorbis()
{
ov_clear(&ovfile);