1
0
mirror of https://github.com/libretro/RetroArch synced 2024-07-05 09:48:42 +00:00

Make FLAC, zlib and LZMA support in libchdr optional

This commit is contained in:
twinaphex 2018-04-22 20:19:07 +02:00
parent fc169cf4fe
commit 997c24ae0c
9 changed files with 265 additions and 170 deletions

View File

@ -1495,32 +1495,27 @@ ifeq ($(HAVE_ZLIB), 1)
endif
endif
ifeq ($(HAVE_FLAC), 1)
ifeq ($(HAVE_7ZIP), 1)
ifeq ($(HAVE_ZLIB), 1)
HAVE_CHD = 1
DEFINES += -DHAVE_CHD -DWANT_SUBCODE -DWANT_RAW_DATA_SECTOR
ifeq ($(HAVE_CHD), 1)
CFLAGS += -I$(LIBRETRO_COMM_DIR)/formats/libchdr
DEFINES += -DHAVE_CHD -DWANT_SUBCODE -DWANT_RAW_DATA_SECTOR
OBJ += $(LIBRETRO_COMM_DIR)/formats/libchdr/libchdr_bitstream.o \
$(LIBRETRO_COMM_DIR)/formats/libchdr/libchdr_cdrom.o \
$(LIBRETRO_COMM_DIR)/formats/libchdr/libchdr_chd.o \
$(LIBRETRO_COMM_DIR)/formats/libchdr/libchdr_flac.o \
$(LIBRETRO_COMM_DIR)/formats/libchdr/libchdr_huffman.o \
$(LIBRETRO_COMM_DIR)/streams/chd_stream.o
endif
endif
endif
ifeq ($(HAVE_FLAC), 1)
OBJ += $(LIBRETRO_COMM_DIR)/formats/libchdr/libchdr_flac.o \
$(LIBRETRO_COMM_DIR)/formats/libchdr/libchdr_flac_codec.o
endif
ifeq ($(HAVE_CHD), 1)
ifeq ($(HAVE_7ZIP), 1)
OBJ += $(LIBRETRO_COMM_DIR)/formats/libchdr/libchdr_lzma.o
endif
endif
ifeq ($(HAVE_7ZIP), 1)
OBJ += $(LIBRETRO_COMM_DIR)/formats/libchdr/libchdr_lzma.o
endif
ifeq ($(HAVE_CHD), 1)
ifeq ($(HAVE_ZLIB), 1)
OBJ += $(LIBRETRO_COMM_DIR)/formats/libchdr/libchdr_zlib.o
endif
ifeq ($(HAVE_ZLIB), 1)
OBJ += $(LIBRETRO_COMM_DIR)/formats/libchdr/libchdr_zlib.o
endif
endif
ifeq ($(HAVE_RTGA), 1)

View File

@ -1310,7 +1310,20 @@ DEPENDENCIES
#include "../libretro-common/formats/libchdr/libchdr_bitstream.c"
#include "../libretro-common/formats/libchdr/libchdr_cdrom.c"
#include "../libretro-common/formats/libchdr/libchdr_chd.c"
#ifdef HAVE_FLAC
#include "../libretro-common/formats/libchdr/libchdr_flac.c"
#include "../libretro-common/formats/libchdr/libchdr_flac_codec.c"
#endif
#ifdef HAVE_ZLIB
#include "../libretro-common/formats/libchdr/libchdr_zlib.c"
#endif
#ifdef HAVE_7ZIP
#include "../libretro-common/formats/libchdr/libchdr_lzma.c"
#endif
#include "../libretro-common/formats/libchdr/libchdr_huffman.c"
#include "../libretro-common/streams/chd_stream.c"

View File

@ -45,11 +45,16 @@
#include <libchdr/chd.h>
#include <libchdr/minmax.h>
#include <libchdr/cdrom.h>
#include <libchdr/flac.h>
#include <libchdr/huffman.h>
#ifdef HAVE_FLAC
#include <libchdr/flac.h>
#endif
#ifdef HAVE_7ZIP
#include <libchdr/lzma.h>
#endif
#ifdef HAVE_ZLIB
#include <libchdr/libchdr_zlib.h>
#endif
@ -185,18 +190,6 @@ struct _metadata_entry
UINT8 flags; /* flag bits */
};
/* codec-private data for the CDFL codec */
typedef struct _cdfl_codec_data cdfl_codec_data;
struct _cdfl_codec_data {
/* internal state */
int swap_endian;
flac_decoder decoder;
#ifdef WANT_SUBCODE
zlib_codec_data subcode_decompressor;
#endif
uint8_t* buffer;
};
/* internal representation of an open CHD file */
struct _chd_file
{
@ -228,7 +221,9 @@ struct _chd_file
#ifdef HAVE_7ZIP
cdlz_codec_data cdlz_codec_data; /* cdlz codec data */
#endif
#ifdef HAVE_FLAC
cdfl_codec_data cdfl_codec_data; /* cdfl codec data */
#endif
#ifdef NEED_CACHE_HUNK
UINT32 maxhunk; /* maximum hunk accessed */
@ -263,118 +258,6 @@ static chd_error map_read(chd_file *chd);
/* metadata management */
static chd_error metadata_find_entry(chd_file *chd, UINT32 metatag, UINT32 metaindex, metadata_entry *metaentry);
/* cdfl compression codec */
static chd_error cdfl_codec_init(void* codec, uint32_t hunkbytes);
static void cdfl_codec_free(void* codec);
static chd_error cdfl_codec_decompress(void *codec, const uint8_t *src, uint32_t complen, uint8_t *dest, uint32_t destlen);
/***************************************************************************
* CD FLAC DECOMPRESSOR
***************************************************************************
*/
/*------------------------------------------------------
* cdfl_codec_blocksize - return the optimal block size
*------------------------------------------------------
*/
static uint32_t cdfl_codec_blocksize(uint32_t bytes)
{
/* determine FLAC block size, which must be 16-65535
* clamp to 2k since that's supposed to be the sweet spot */
uint32_t hunkbytes = bytes / 4;
while (hunkbytes > 2048)
hunkbytes /= 2;
return hunkbytes;
}
chd_error cdfl_codec_init(void *codec, uint32_t hunkbytes)
{
#ifdef WANT_SUBCODE
chd_error ret;
#endif
uint16_t native_endian = 0;
cdfl_codec_data *cdfl = (cdfl_codec_data*)codec;
/* make sure the CHD's hunk size is an even multiple of the frame size */
if (hunkbytes % CD_FRAME_SIZE != 0)
return CHDERR_CODEC_ERROR;
cdfl->buffer = (uint8_t*)malloc(sizeof(uint8_t) * hunkbytes);
if (cdfl->buffer == NULL)
return CHDERR_OUT_OF_MEMORY;
/* determine whether we want native or swapped samples */
*(uint8_t *)(&native_endian) = 1;
cdfl->swap_endian = (native_endian & 1);
#ifdef WANT_SUBCODE
/* init zlib inflater */
ret = zlib_codec_init(&cdfl->subcode_decompressor, (hunkbytes / CD_FRAME_SIZE) * CD_MAX_SECTOR_DATA);
if (ret != CHDERR_NONE)
return ret;
#endif
/* flac decoder init */
flac_decoder_init(&cdfl->decoder);
if (cdfl->decoder.decoder == NULL)
return CHDERR_OUT_OF_MEMORY;
return CHDERR_NONE;
}
void cdfl_codec_free(void *codec)
{
cdfl_codec_data *cdfl = (cdfl_codec_data*)codec;
flac_decoder_free(&cdfl->decoder);
#ifdef WANT_SUBCODE
zlib_codec_free(&cdfl->subcode_decompressor);
#endif
if (cdfl->buffer)
free(cdfl->buffer);
}
chd_error cdfl_codec_decompress(void *codec, const uint8_t *src, uint32_t complen, uint8_t *dest, uint32_t destlen)
{
uint32_t framenum;
uint8_t *buffer;
#ifdef WANT_SUBCODE
uint32_t offset;
chd_error ret;
#endif
cdfl_codec_data *cdfl = (cdfl_codec_data*)codec;
/* reset and decode */
uint32_t frames = destlen / CD_FRAME_SIZE;
if (!flac_decoder_reset(&cdfl->decoder, 44100, 2, cdfl_codec_blocksize(frames * CD_MAX_SECTOR_DATA), src, complen))
return CHDERR_DECOMPRESSION_ERROR;
buffer = &cdfl->buffer[0];
if (!flac_decoder_decode_interleaved(&cdfl->decoder, (int16_t *)(buffer), frames * CD_MAX_SECTOR_DATA/4, cdfl->swap_endian))
return CHDERR_DECOMPRESSION_ERROR;
#ifdef WANT_SUBCODE
/* inflate the subcode data */
offset = flac_decoder_finish(&cdfl->decoder);
ret = zlib_codec_decompress(&cdfl->subcode_decompressor, src + offset, complen - offset, &cdfl->buffer[frames * CD_MAX_SECTOR_DATA], frames * CD_MAX_SUBCODE_DATA);
if (ret != CHDERR_NONE)
return ret;
#else
flac_decoder_finish(&cdfl->decoder);
#endif
/* reassemble the data */
for (framenum = 0; framenum < frames; framenum++)
{
memcpy(&dest[framenum * CD_FRAME_SIZE], &cdfl->buffer[framenum * CD_MAX_SECTOR_DATA], CD_MAX_SECTOR_DATA);
#ifdef WANT_SUBCODE
memcpy(&dest[framenum * CD_FRAME_SIZE + CD_MAX_SECTOR_DATA], &cdfl->buffer[frames * CD_MAX_SECTOR_DATA + framenum * CD_MAX_SUBCODE_DATA], CD_MAX_SUBCODE_DATA);
#endif
}
return CHDERR_NONE;
}
/***************************************************************************
CODEC INTERFACES
***************************************************************************/
@ -447,6 +330,7 @@ static const codec_interface codec_interfaces[] =
},
#endif
#ifdef HAVE_FLAC
/* V5 CD flac compression */
{
CHD_CODEC_CD_FLAC,
@ -457,6 +341,7 @@ static const codec_interface codec_interfaces[] =
cdfl_codec_decompress,
NULL
},
#endif
};
/***************************************************************************
@ -967,12 +852,14 @@ chd_error chd_open_file(RFILE *file, int mode, chd_file *parent, chd_file **chd)
if (intfnum == ARRAY_SIZE(codec_interfaces))
EARLY_EXIT(err = CHDERR_UNSUPPORTED_FORMAT);
#ifdef HAVE_ZLIB
/* initialize the codec */
if (newchd->codecintf[0]->init != NULL)
{
err = (*newchd->codecintf[0]->init)(&newchd->zlib_codec_data, newchd->header.hunkbytes);
(void)err;
}
{
err = (*newchd->codecintf[0]->init)(&newchd->zlib_codec_data, newchd->header.hunkbytes);
(void)err;
}
#endif
}
else
{
@ -998,15 +885,21 @@ chd_error chd_open_file(RFILE *file, int mode, chd_file *parent, chd_file **chd)
switch (newchd->header.compression[decompnum])
{
case CHD_CODEC_CD_ZLIB:
#ifdef HAVE_ZLIB
codec = &newchd->cdzl_codec_data;
#endif
break;
case CHD_CODEC_CD_LZMA:
#ifdef HAVE_7ZIP
codec = &newchd->cdlz_codec_data;
#endif
break;
case CHD_CODEC_CD_FLAC:
#ifdef HAVE_FLAC
codec = &newchd->cdfl_codec_data;
#endif
break;
}
if (codec != NULL)
@ -1122,35 +1015,41 @@ void chd_close(chd_file *chd)
/* deinit the codec */
if (chd->header.version < 5)
{
#ifdef HAVE_ZLIB
if (chd->codecintf[0] != NULL && chd->codecintf[0]->free != NULL)
(*chd->codecintf[0]->free)(&chd->zlib_codec_data);
#endif
}
else
{
int i;
/* Free the codecs */
for (i = 0 ; i < 4 ; i++)
{
void* codec = NULL;
switch (chd->codecintf[i]->compression)
{
case CHD_CODEC_CD_LZMA:
codec = &chd->cdlz_codec_data;
break;
{
void* codec = NULL;
switch (chd->codecintf[i]->compression)
{
case CHD_CODEC_CD_LZMA:
#ifdef HAVE_7ZIP
codec = &chd->cdlz_codec_data;
#endif
break;
case CHD_CODEC_CD_ZLIB:
codec = &chd->cdzl_codec_data;
break;
case CHD_CODEC_CD_ZLIB:
#ifdef HAVE_ZLIB
codec = &chd->cdzl_codec_data;
#endif
break;
case CHD_CODEC_CD_FLAC:
codec = &chd->cdfl_codec_data;
break;
}
if (codec)
{
(*chd->codecintf[i]->free)(codec);
}
}
case CHD_CODEC_CD_FLAC:
#ifdef HAVE_FLAC
codec = &chd->cdfl_codec_data;
#endif
break;
}
if (codec)
(*chd->codecintf[i]->free)(codec);
}
/* Free the raw map */
if (chd->header.rawmap != NULL)
@ -1686,6 +1585,7 @@ static chd_error hunk_read_into_memory(chd_file *chd, UINT32 hunknum, UINT8 *des
if (bytes == NULL)
return CHDERR_READ_ERROR;
#ifdef HAVE_ZLIB
/* now decompress using the codec */
err = CHDERR_NONE;
codec = &chd->zlib_codec_data;
@ -1693,6 +1593,7 @@ static chd_error hunk_read_into_memory(chd_file *chd, UINT32 hunknum, UINT8 *des
err = (*chd->codecintf[0]->decompress)(codec, chd->compressed, entry->length, dest, chd->header.hunkbytes);
if (err != CHDERR_NONE)
return err;
#endif
}
break;
@ -1774,15 +1675,21 @@ static chd_error hunk_read_into_memory(chd_file *chd, UINT32 hunknum, UINT8 *des
switch (chd->codecintf[rawmap[0]]->compression)
{
case CHD_CODEC_CD_LZMA:
#ifdef HAVE_7ZIP
codec = &chd->cdlz_codec_data;
#endif
break;
case CHD_CODEC_CD_ZLIB:
#ifdef HAVE_ZLIB
codec = &chd->cdzl_codec_data;
#endif
break;
case CHD_CODEC_CD_FLAC:
#ifdef HAVE_FLAC
codec = &chd->cdfl_codec_data;
#endif
break;
}
if (codec==NULL)

View File

@ -0,0 +1,163 @@
/***************************************************************************
libchdr_flac_codec.c
MAME Compressed Hunks of Data file format
****************************************************************************
Copyright Aaron Giles
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name 'MAME' nor the names of its contributors may be
used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY AARON GILES ''AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL AARON GILES BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
***************************************************************************/
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libchdr/chd.h>
#include <libchdr/minmax.h>
#include <libchdr/cdrom.h>
#include <libchdr/flac.h>
#include <libchdr/huffman.h>
#include <zlib.h>
#include <retro_inline.h>
#include <streams/file_stream.h>
#define TRUE 1
#define FALSE 0
/***************************************************************************
* CD FLAC DECOMPRESSOR
***************************************************************************
*/
/*------------------------------------------------------
* cdfl_codec_blocksize - return the optimal block size
*------------------------------------------------------
*/
static uint32_t cdfl_codec_blocksize(uint32_t bytes)
{
/* determine FLAC block size, which must be 16-65535
* clamp to 2k since that's supposed to be the sweet spot */
uint32_t hunkbytes = bytes / 4;
while (hunkbytes > 2048)
hunkbytes /= 2;
return hunkbytes;
}
chd_error cdfl_codec_init(void *codec, uint32_t hunkbytes)
{
#ifdef WANT_SUBCODE
chd_error ret;
#endif
uint16_t native_endian = 0;
cdfl_codec_data *cdfl = (cdfl_codec_data*)codec;
/* make sure the CHD's hunk size is an even multiple of the frame size */
if (hunkbytes % CD_FRAME_SIZE != 0)
return CHDERR_CODEC_ERROR;
cdfl->buffer = (uint8_t*)malloc(sizeof(uint8_t) * hunkbytes);
if (cdfl->buffer == NULL)
return CHDERR_OUT_OF_MEMORY;
/* determine whether we want native or swapped samples */
*(uint8_t *)(&native_endian) = 1;
cdfl->swap_endian = (native_endian & 1);
#ifdef WANT_SUBCODE
/* init zlib inflater */
ret = zlib_codec_init(&cdfl->subcode_decompressor, (hunkbytes / CD_FRAME_SIZE) * CD_MAX_SECTOR_DATA);
if (ret != CHDERR_NONE)
return ret;
#endif
/* flac decoder init */
flac_decoder_init(&cdfl->decoder);
if (cdfl->decoder.decoder == NULL)
return CHDERR_OUT_OF_MEMORY;
return CHDERR_NONE;
}
void cdfl_codec_free(void *codec)
{
cdfl_codec_data *cdfl = (cdfl_codec_data*)codec;
flac_decoder_free(&cdfl->decoder);
#ifdef WANT_SUBCODE
zlib_codec_free(&cdfl->subcode_decompressor);
#endif
if (cdfl->buffer)
free(cdfl->buffer);
}
chd_error cdfl_codec_decompress(void *codec, const uint8_t *src, uint32_t complen, uint8_t *dest, uint32_t destlen)
{
uint32_t framenum;
uint8_t *buffer;
#ifdef WANT_SUBCODE
uint32_t offset;
chd_error ret;
#endif
cdfl_codec_data *cdfl = (cdfl_codec_data*)codec;
/* reset and decode */
uint32_t frames = destlen / CD_FRAME_SIZE;
if (!flac_decoder_reset(&cdfl->decoder, 44100, 2, cdfl_codec_blocksize(frames * CD_MAX_SECTOR_DATA), src, complen))
return CHDERR_DECOMPRESSION_ERROR;
buffer = &cdfl->buffer[0];
if (!flac_decoder_decode_interleaved(&cdfl->decoder, (int16_t *)(buffer), frames * CD_MAX_SECTOR_DATA/4, cdfl->swap_endian))
return CHDERR_DECOMPRESSION_ERROR;
#ifdef WANT_SUBCODE
/* inflate the subcode data */
offset = flac_decoder_finish(&cdfl->decoder);
ret = zlib_codec_decompress(&cdfl->subcode_decompressor, src + offset, complen - offset, &cdfl->buffer[frames * CD_MAX_SECTOR_DATA], frames * CD_MAX_SUBCODE_DATA);
if (ret != CHDERR_NONE)
return ret;
#else
flac_decoder_finish(&cdfl->decoder);
#endif
/* reassemble the data */
for (framenum = 0; framenum < frames; framenum++)
{
memcpy(&dest[framenum * CD_FRAME_SIZE], &cdfl->buffer[framenum * CD_MAX_SECTOR_DATA], CD_MAX_SECTOR_DATA);
#ifdef WANT_SUBCODE
memcpy(&dest[framenum * CD_FRAME_SIZE + CD_MAX_SECTOR_DATA], &cdfl->buffer[frames * CD_MAX_SECTOR_DATA + framenum * CD_MAX_SUBCODE_DATA], CD_MAX_SUBCODE_DATA);
#endif
}
return CHDERR_NONE;
}

View File

@ -1,6 +1,6 @@
/***************************************************************************
chd.c
libchdr_lzma_codec.c
MAME Compressed Hunks of Data file format
@ -45,7 +45,6 @@
#include <libchdr/chd.h>
#include <libchdr/minmax.h>
#include <libchdr/cdrom.h>
#include <libchdr/flac.h>
#include <libchdr/lzma.h>
#include <libchdr/huffman.h>
#include <zlib.h>

View File

@ -45,8 +45,6 @@
#include <libchdr/chd.h>
#include <libchdr/minmax.h>
#include <libchdr/cdrom.h>
#include <libchdr/flac.h>
#include <libchdr/lzma.h>
#include <libchdr/huffman.h>
#include <libchdr/libchdr_zlib.h>
#include <zlib.h>

View File

@ -14,6 +14,7 @@
#define __FLAC_H__
#include <stdint.h>
#include "libchdr_zlib.h"
#include "FLAC/ordinals.h"
#include "FLAC/stream_decoder.h"
@ -49,4 +50,21 @@ int flac_decoder_reset(flac_decoder* decoder, uint32_t sample_rate, uint8_t nu
int flac_decoder_decode_interleaved(flac_decoder* decoder, int16_t *samples, uint32_t num_samples, int swap_endian);
uint32_t flac_decoder_finish(flac_decoder* decoder);
/* codec-private data for the CDFL codec */
typedef struct _cdfl_codec_data cdfl_codec_data;
struct _cdfl_codec_data {
/* internal state */
int swap_endian;
flac_decoder decoder;
#ifdef WANT_SUBCODE
zlib_codec_data subcode_decompressor;
#endif
uint8_t* buffer;
};
/* cdfl compression codec */
chd_error cdfl_codec_init(void* codec, uint32_t hunkbytes);
void cdfl_codec_free(void* codec);
chd_error cdfl_codec_decompress(void *codec, const uint8_t *src, uint32_t complen, uint8_t *dest, uint32_t destlen);
#endif /* __FLAC_H__ */

View File

@ -17,6 +17,7 @@
#include <zlib.h>
#include "coretypes.h"
#include "chd.h"
#define MAX_ZLIB_ALLOCS 64

View File

@ -92,6 +92,7 @@ HAVE_NEON=no # ARM NEON optimizations
HAVE_SSE=no # x86 SSE optimizations (SSE, SSE2)
HAVE_FLOATHARD=no # Force hard float ABI (for ARM)
HAVE_FLOATSOFTFP=no # Force soft float ABI (for ARM)
HAVE_CHD=yes # Compile in chd support
HAVE_7ZIP=yes # Compile in 7z support
HAVE_FLAC=auto # Compile in flac support
HAVE_BUILTINFLAC=yes # Bake in flac support