diff --git a/libretro-common/formats/libchdr/bitstream.c b/libretro-common/formats/libchdr/bitstream.c index 735b97f4b1..922d8aa323 100644 --- a/libretro-common/formats/libchdr/bitstream.c +++ b/libretro-common/formats/libchdr/bitstream.c @@ -1,5 +1,6 @@ -// license:BSD-3-Clause -// copyright-holders:Aaron Giles +/* license:BSD-3-Clause + * copyright-holders:Aaron Giles + */ /*************************************************************************** bitstream.c @@ -11,15 +12,17 @@ #include "bitstream.h" #include -//************************************************************************** -// INLINE FUNCTIONS -//************************************************************************** +/*************************************************************************** + * INLINE FUNCTIONS + *************************************************************************** + */ int bitstream_overflow(struct bitstream* bitstream) { return ((bitstream->doffset - bitstream->bits / 8) > bitstream->dlength); } -//------------------------------------------------- -// create_bitstream - constructor -//------------------------------------------------- +/*------------------------------------------------- + * create_bitstream - constructor + *------------------------------------------------- + */ struct bitstream* create_bitstream(const void *src, uint32_t srclength) { @@ -33,17 +36,18 @@ struct bitstream* create_bitstream(const void *src, uint32_t srclength) } -//----------------------------------------------------- -// bitstream_peek - fetch the requested number of bits -// but don't advance the input pointer -//----------------------------------------------------- +/*----------------------------------------------------- + * bitstream_peek - fetch the requested number of bits + * but don't advance the input pointer + *----------------------------------------------------- + */ uint32_t bitstream_peek(struct bitstream* bitstream, int numbits) { if (numbits == 0) return 0; - // fetch data if we need more + /* fetch data if we need more */ if (numbits > bitstream->bits) { while (bitstream->bits <= 24) @@ -55,15 +59,16 @@ uint32_t bitstream_peek(struct bitstream* bitstream, int numbits) } } - // return the data + /* return the data */ return bitstream->buffer >> (32 - numbits); } -//----------------------------------------------------- -// bitstream_remove - advance the input pointer by the -// specified number of bits -//----------------------------------------------------- +/*----------------------------------------------------- + * bitstream_remove - advance the input pointer by the + * specified number of bits + *----------------------------------------------------- + */ void bitstream_remove(struct bitstream* bitstream, int numbits) { @@ -72,9 +77,10 @@ void bitstream_remove(struct bitstream* bitstream, int numbits) } -//----------------------------------------------------- -// bitstream_read - fetch the requested number of bits -//----------------------------------------------------- +/*----------------------------------------------------- + * bitstream_read - fetch the requested number of bits + *----------------------------------------------------- + */ uint32_t bitstream_read(struct bitstream* bitstream, int numbits) { @@ -84,9 +90,10 @@ uint32_t bitstream_read(struct bitstream* bitstream, int numbits) } -//------------------------------------------------- -// read_offset - return the current read offset -//------------------------------------------------- +/*------------------------------------------------- + * read_offset - return the current read offset + *------------------------------------------------- + */ uint32_t bitstream_read_offset(struct bitstream* bitstream) { diff --git a/libretro-common/formats/libchdr/bitstream.h b/libretro-common/formats/libchdr/bitstream.h index 9250d3369e..a30a9f2ff3 100644 --- a/libretro-common/formats/libchdr/bitstream.h +++ b/libretro-common/formats/libchdr/bitstream.h @@ -1,42 +1,43 @@ -// license:BSD-3-Clause -// copyright-holders:Aaron Giles -/*************************************************************************** - - bitstream.h - - Helper classes for reading/writing at the bit level. - -***************************************************************************/ - -#pragma once - -#ifndef __BITSTREAM_H__ -#define __BITSTREAM_H__ - -#include - -//************************************************************************** -// TYPE DEFINITIONS -//************************************************************************** - -// helper class for reading from a bit buffer -struct bitstream -{ - uint32_t buffer; // current bit accumulator - int bits; // number of bits in the accumulator - const uint8_t * read; // read pointer - uint32_t doffset; // byte offset within the data - uint32_t dlength; // length of the data -}; - -struct bitstream* create_bitstream(const void *src, uint32_t srclength); -int bitstream_overflow(struct bitstream* bitstream); -uint32_t bitstream_read_offset(struct bitstream* bitstream); - -uint32_t bitstream_read(struct bitstream* bitstream, int numbits); -uint32_t bitstream_peek(struct bitstream* bitstream, int numbits); -void bitstream_remove(struct bitstream* bitstream, int numbits); -uint32_t bitstream_flush(struct bitstream* bitstream); - - -#endif +/* license:BSD-3-Clause + * copyright-holders:Aaron Giles + *************************************************************************** + + bitstream.h + + Helper classes for reading/writing at the bit level. + +***************************************************************************/ + +#pragma once + +#ifndef __BITSTREAM_H__ +#define __BITSTREAM_H__ + +#include + +/*************************************************************************** + * TYPE DEFINITIONS + *************************************************************************** + */ + +/* helper class for reading from a bit buffer */ +struct bitstream +{ + uint32_t buffer; /* current bit accumulator */ + int bits; /* number of bits in the accumulator */ + const uint8_t * read; /* read pointer */ + uint32_t doffset; /* byte offset within the data */ + uint32_t dlength; /* length of the data */ +}; + +struct bitstream* create_bitstream(const void *src, uint32_t srclength); +int bitstream_overflow(struct bitstream* bitstream); +uint32_t bitstream_read_offset(struct bitstream* bitstream); + +uint32_t bitstream_read(struct bitstream* bitstream, int numbits); +uint32_t bitstream_peek(struct bitstream* bitstream, int numbits); +void bitstream_remove(struct bitstream* bitstream, int numbits); +uint32_t bitstream_flush(struct bitstream* bitstream); + + +#endif diff --git a/libretro-common/formats/libchdr/cdrom.c b/libretro-common/formats/libchdr/cdrom.c index 6c120cbc2a..9c68be0794 100644 --- a/libretro-common/formats/libchdr/cdrom.c +++ b/libretro-common/formats/libchdr/cdrom.c @@ -15,6 +15,7 @@ schemes will differ after track 1! ***************************************************************************/ +#ifdef WANT_RAW_DATA_SECTOR #include #include @@ -307,7 +308,7 @@ static const uint16_t qoffsets[ECC_Q_NUM_BYTES][ECC_Q_COMP] = // particular to a mode //------------------------------------------------- -static uint8_t ecc_source_byte(const uint8_t *sector, uint32_t offset) +static inline uint8_t ecc_source_byte(const uint8_t *sector, uint32_t offset) { // in mode 2 always treat these as 0 bytes return (sector[MODE_OFFSET] == 2 && offset < 4) ? 0x00 : sector[SYNC_OFFSET + SYNC_NUM_BYTES + offset]; @@ -329,9 +330,8 @@ static uint8_t ecc_source_byte(const uint8_t *sector, uint32_t offset) void ecc_compute_bytes(const uint8_t *sector, const uint16_t *row, int rowlen, uint8_t *val1, uint8_t *val2) { - int component; *val1 = *val2 = 0; - for (component = 0; component < rowlen; component++) + for (int component = 0; component < rowlen; component++) { *val1 ^= ecc_source_byte(sector, row[component]); *val2 ^= ecc_source_byte(sector, row[component]); @@ -355,10 +355,8 @@ void ecc_compute_bytes(const uint8_t *sector, const uint16_t *row, int rowlen, u int ecc_verify(const uint8_t *sector) { - int byte; - // first verify P bytes - for (byte = 0; byte < ECC_P_NUM_BYTES; byte++) + for (int byte = 0; byte < ECC_P_NUM_BYTES; byte++) { uint8_t val1, val2; ecc_compute_bytes(sector, poffsets[byte], ECC_P_COMP, &val1, &val2); @@ -367,7 +365,7 @@ int ecc_verify(const uint8_t *sector) } // then verify Q bytes - for (byte = 0; byte < ECC_Q_NUM_BYTES; byte++) + for (int byte = 0; byte < ECC_Q_NUM_BYTES; byte++) { uint8_t val1, val2; ecc_compute_bytes(sector, qoffsets[byte], ECC_Q_COMP, &val1, &val2); @@ -390,13 +388,12 @@ int ecc_verify(const uint8_t *sector) void ecc_generate(uint8_t *sector) { - int byte; // first verify P bytes - for (byte = 0; byte < ECC_P_NUM_BYTES; byte++) + for (int byte = 0; byte < ECC_P_NUM_BYTES; byte++) ecc_compute_bytes(sector, poffsets[byte], ECC_P_COMP, §or[ECC_P_OFFSET + byte], §or[ECC_P_OFFSET + ECC_P_NUM_BYTES + byte]); // then verify Q bytes - for (byte = 0; byte < ECC_Q_NUM_BYTES; byte++) + for (int byte = 0; byte < ECC_Q_NUM_BYTES; byte++) ecc_compute_bytes(sector, qoffsets[byte], ECC_Q_COMP, §or[ECC_Q_OFFSET + byte], §or[ECC_Q_OFFSET + ECC_Q_NUM_BYTES + byte]); } @@ -415,3 +412,5 @@ void ecc_clear(uint8_t *sector) memset(§or[ECC_P_OFFSET], 0, 2 * ECC_P_NUM_BYTES); memset(§or[ECC_Q_OFFSET], 0, 2 * ECC_Q_NUM_BYTES); } + +#endif /* WANT_RAW_DATA_SECTOR */ diff --git a/libretro-common/formats/libchdr/cdrom.h b/libretro-common/formats/libchdr/cdrom.h index 8fc52bfa92..dc0a042172 100644 --- a/libretro-common/formats/libchdr/cdrom.h +++ b/libretro-common/formats/libchdr/cdrom.h @@ -1,5 +1,5 @@ -// license:BSD-3-Clause -// copyright-holders:Aaron Giles +/* license:BSD-3-Clause */ +/* copyright-holders:Aaron Giles */ /*************************************************************************** cdrom.h @@ -15,14 +15,13 @@ #include -#include /*************************************************************************** CONSTANTS ***************************************************************************/ -// tracks are padded to a multiple of this many frames -extern const uint32_t CD_TRACK_PADDING; +/* tracks are padded to a multiple of this many frames */ +#define CD_TRACK_PADDING (4) #define CD_MAX_TRACKS (99) /* AFAIK the theoretical limit */ #define CD_MAX_SECTOR_DATA (2352) @@ -61,48 +60,11 @@ enum FUNCTION PROTOTYPES ***************************************************************************/ -// ECC utilities +#ifdef WANT_RAW_DATA_SECTOR +/* ECC utilities */ int ecc_verify(const uint8_t *sector); void ecc_generate(uint8_t *sector); void ecc_clear(uint8_t *sector); +#endif - - -/*************************************************************************** - INLINE FUNCTIONS -***************************************************************************/ - -static INLINE uint32_t msf_to_lba(uint32_t msf) -{ - return ( ((msf&0x00ff0000)>>16) * 60 * 75) + (((msf&0x0000ff00)>>8) * 75) + ((msf&0x000000ff)>>0); -} - -static INLINE uint32_t lba_to_msf(uint32_t lba) -{ - uint8_t m, s, f; - - m = lba / (60 * 75); - lba -= m * (60 * 75); - s = lba / 75; - f = lba % 75; - - return ((m / 10) << 20) | ((m % 10) << 16) | - ((s / 10) << 12) | ((s % 10) << 8) | - ((f / 10) << 4) | ((f % 10) << 0); -} - -// segacd needs it like this.. investigate -// Angelo also says PCE tracks often start playing at the -// wrong address.. related? -static INLINE uint32_t lba_to_msf_alt(int lba) -{ - uint32_t ret = 0; - - ret |= ((lba / (60 * 75))&0xff)<<16; - ret |= (((lba / 75) % 60)&0xff)<<8; - ret |= ((lba % 75)&0xff)<<0; - - return ret; -} - -#endif // __CDROM_H__ +#endif /* __CDROM_H__ */ diff --git a/libretro-common/formats/libchdr/chd.c b/libretro-common/formats/libchdr/chd.c index 9adecc6627..6d4031d14f 100644 --- a/libretro-common/formats/libchdr/chd.c +++ b/libretro-common/formats/libchdr/chd.c @@ -36,28 +36,19 @@ POSSIBILITY OF SUCH DAMAGE. ***************************************************************************/ -#define DONT_SET_BYTE -typedef unsigned char Byte; +#include +#include +#include +#include #include "chd.h" #include "cdrom.h" -#include "huffman.h" #include "flac.h" - -#include "md5.h" -#include "sha1.h" +#include "huffman.h" +#include "zlib.h" #include "LzmaEnc.h" #include "LzmaDec.h" - -#include -#include - -#include -#include -#include -#include - -#include +#include "retro_inline.h" #define TRUE 1 #define FALSE 0 @@ -65,8 +56,6 @@ typedef unsigned char Byte; #define MAX(x, y) (((x) > (y)) ? (x) : (y)) #define MIN(x, y) (((x) < (y)) ? (x) : (y)) -#define SHA1_DIGEST_SIZE 20 - #define CHD_MAKE_TAG(a,b,c,d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d)) /*************************************************************************** @@ -85,7 +74,6 @@ typedef unsigned char Byte; #define MAP_ENTRY_SIZE 16 /* V3 and later */ #define OLD_MAP_ENTRY_SIZE 8 /* V1-V2 */ #define METADATA_HEADER_SIZE 16 /* metadata header size */ -#define CRCMAP_HASH_SIZE 4095 /* number of CRC hashtable entries */ #define MAP_ENTRY_FLAG_TYPE_MASK 0x0f /* what type of hunk */ #define MAP_ENTRY_FLAG_NO_CRC 0x10 /* no CRC is present */ @@ -99,53 +87,55 @@ typedef unsigned char Byte; #define NO_MATCH (~0) +#ifdef WANT_RAW_DATA_SECTOR static const uint8_t s_cd_sync_header[12] = { 0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00 }; +#endif -// V3-V4 entry types +/* V3-V4 entry types */ enum { - V34_MAP_ENTRY_TYPE_INVALID = 0, // invalid type - V34_MAP_ENTRY_TYPE_COMPRESSED = 1, // standard compression - V34_MAP_ENTRY_TYPE_UNCOMPRESSED = 2, // uncompressed data - V34_MAP_ENTRY_TYPE_MINI = 3, // mini: use offset as raw data - V34_MAP_ENTRY_TYPE_SELF_HUNK = 4, // same as another hunk in this file - V34_MAP_ENTRY_TYPE_PARENT_HUNK = 5, // same as a hunk in the parent file - V34_MAP_ENTRY_TYPE_2ND_COMPRESSED = 6 // compressed with secondary algorithm (usually FLAC CDDA) + V34_MAP_ENTRY_TYPE_INVALID = 0, /* invalid type */ + V34_MAP_ENTRY_TYPE_COMPRESSED = 1, /* standard compression */ + V34_MAP_ENTRY_TYPE_UNCOMPRESSED = 2, /* uncompressed data */ + V34_MAP_ENTRY_TYPE_MINI = 3, /* mini: use offset as raw data */ + V34_MAP_ENTRY_TYPE_SELF_HUNK = 4, /* same as another hunk in this file */ + V34_MAP_ENTRY_TYPE_PARENT_HUNK = 5, /* same as a hunk in the parent file */ + V34_MAP_ENTRY_TYPE_2ND_COMPRESSED = 6 /* compressed with secondary algorithm (usually FLAC CDDA) */ }; -// V5 compression types +/* V5 compression types */ enum { - ///< codec #0 - // these types are live when running + /* codec #0 + * these types are live when running */ COMPRESSION_TYPE_0 = 0, - ///< codec #1 + /* codec #1 */ COMPRESSION_TYPE_1 = 1, - ///< codec #2 + /* codec #2 */ COMPRESSION_TYPE_2 = 2, - ///< codec #3 + /* codec #3 */ COMPRESSION_TYPE_3 = 3, - ///< no compression; implicit length = hunkbytes + /* no compression; implicit length = hunkbytes */ COMPRESSION_NONE = 4, - ///< same as another block in this chd + /* same as another block in this chd */ COMPRESSION_SELF = 5, - ///< same as a hunk's worth of units in the parent chd + /* same as a hunk's worth of units in the parent chd */ COMPRESSION_PARENT = 6, - ///< start of small RLE run (4-bit length) - // these additional pseudo-types are used for compressed encodings: + /* start of small RLE run (4-bit length) + * these additional pseudo-types are used for compressed encodings: */ COMPRESSION_RLE_SMALL, - ///< start of large RLE run (8-bit length) + /* start of large RLE run (8-bit length) */ COMPRESSION_RLE_LARGE, - ///< same as the last COMPRESSION_SELF block + /* same as the last COMPRESSION_SELF block */ COMPRESSION_SELF_0, - ///< same as the last COMPRESSION_SELF block + 1 + /* same as the last COMPRESSION_SELF block + 1 */ COMPRESSION_SELF_1, - ///< same block in the parent + /* same block in the parent */ COMPRESSION_PARENT_SELF, - ///< same as the last COMPRESSION_PARENT block + /* same as the last COMPRESSION_PARENT block */ COMPRESSION_PARENT_0, - ///< same as the last COMPRESSION_PARENT block + 1 + /* same as the last COMPRESSION_PARENT block + 1 */ COMPRESSION_PARENT_1 }; @@ -187,15 +177,6 @@ struct _map_entry }; -/* simple linked-list of hunks used for our CRC map */ -typedef struct _crcmap_entry crcmap_entry; -struct _crcmap_entry -{ - UINT32 hunknum; /* hunk number */ - crcmap_entry * next; /* next entry in list */ -}; - - /* a single metadata entry */ typedef struct _metadata_entry metadata_entry; struct _metadata_entry @@ -245,29 +226,34 @@ struct _lzma_codec_data /* codec-private data for the CDZL codec */ typedef struct _cdzl_codec_data cdzl_codec_data; struct _cdzl_codec_data { - // internal state + /* internal state */ zlib_codec_data base_decompressor; +#ifdef WANT_SUBCODE zlib_codec_data subcode_decompressor; +#endif uint8_t* buffer; }; /* codec-private data for the CDLZ codec */ typedef struct _cdlz_codec_data cdlz_codec_data; struct _cdlz_codec_data { - // internal state + /* internal state */ lzma_codec_data base_decompressor; +#ifdef WANT_SUBCODE zlib_codec_data subcode_decompressor; +#endif uint8_t* buffer; }; /* codec-private data for the CDFL codec */ typedef struct _cdfl_codec_data cdfl_codec_data; struct _cdfl_codec_data { - // internal state + /* internal state */ int swap_endian; flac_decoder decoder; - z_stream inflater; - zlib_allocator allocator; +#ifdef WANT_SUBCODE + zlib_codec_data subcode_decompressor; +#endif uint8_t* buffer; }; @@ -284,11 +270,13 @@ struct _chd_file map_entry * map; /* array of map entries */ +#ifdef NEED_CACHE_HUNK UINT8 * cache; /* hunk cache pointer */ UINT32 cachehunk; /* index of currently cached hunk */ UINT8 * compare; /* hunk compare pointer */ UINT32 comparehunk; /* index of current compare data */ +#endif UINT8 * compressed; /* pointer to buffer for compressed data */ const codec_interface * codecintf[4]; /* interface to the codec */ @@ -298,37 +286,12 @@ struct _chd_file cdlz_codec_data cdlz_codec_data; /* cdlz codec data */ cdfl_codec_data cdfl_codec_data; /* cdfl codec data */ - crcmap_entry * crcmap; /* CRC map entries */ - crcmap_entry * crcfree; /* free list CRC entries */ - crcmap_entry ** crctable; /* table of CRC entries */ - +#ifdef NEED_CACHE_HUNK UINT32 maxhunk; /* maximum hunk accessed */ - - UINT8 compressing; /* are we compressing? */ - MD5_CTX compmd5; /* running MD5 during compression */ - SHA1_CTX compsha1; /* running SHA1 during compression */ - UINT32 comphunk; /* next hunk we will compress */ - - UINT8 verifying; /* are we verifying? */ - MD5_CTX vermd5; /* running MD5 during verification */ - SHA1_CTX versha1; /* running SHA1 during verification */ - UINT32 verhunk; /* next hunk we will verify */ - - UINT32 async_hunknum; /* hunk index for asynchronous operations */ - void * async_buffer; /* buffer pointer for asynchronous operations */ +#endif }; -/* a single metadata hash entry */ -typedef struct _metadata_hash metadata_hash; -struct _metadata_hash -{ - UINT8 tag[4]; /* tag of the metadata in big-endian */ - UINT8 sha1[CHD_SHA1_BYTES]; /* hash */ -}; - - - /*************************************************************************** GLOBAL VARIABLES ***************************************************************************/ @@ -348,7 +311,9 @@ static chd_error header_read(core_file *file, chd_header *header); /* internal hunk read/write */ +#ifdef NEED_CACHE_HUNK static chd_error hunk_read_into_cache(chd_file *chd, UINT32 hunknum); +#endif static chd_error hunk_read_into_memory(chd_file *chd, UINT32 hunknum, UINT8 *dest); /* internal map access */ @@ -385,37 +350,40 @@ 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); -//************************************************************************** -// LZMA ALLOCATOR HELPER -//************************************************************************** +/*************************************************************************** + * LZMA ALLOCATOR HELPER + *************************************************************************** + */ void *lzma_fast_alloc(void *p, size_t size); void lzma_fast_free(void *p, void *address); -//------------------------------------------------- -// lzma_allocator_init -//------------------------------------------------- +/*------------------------------------------------- + * lzma_allocator_init + *------------------------------------------------- + */ void lzma_allocator_init(void* p) { lzma_allocator *codec = (lzma_allocator *)(p); - // reset pointer list + /* reset pointer list */ memset(codec->allocptr, 0, sizeof(codec->allocptr)); codec->Alloc = lzma_fast_alloc; codec->Free = lzma_fast_free; } -//------------------------------------------------- -// lzma_allocator_free -//------------------------------------------------- +/*------------------------------------------------- + * lzma_allocator_free + *------------------------------------------------- + */ void lzma_allocator_free(void* p ) { - int i; lzma_allocator *codec = (lzma_allocator *)(p); - // free our memory + /* free our memory */ + int i; for (i = 0 ; i < MAX_LZMA_ALLOCS ; i++) { if (codec->allocptr[i] != NULL) @@ -423,33 +391,34 @@ void lzma_allocator_free(void* p ) } } -//------------------------------------------------- -// lzma_fast_alloc - fast malloc for lzma, which -// allocates and frees memory frequently -//------------------------------------------------- +/*------------------------------------------------- + * lzma_fast_alloc - fast malloc for lzma, which + * allocates and frees memory frequently + *------------------------------------------------- + */ void *lzma_fast_alloc(void *p, size_t size) { - int scan; - uint32_t *addr = NULL; + int scan; + uint32_t *addr; lzma_allocator *codec = (lzma_allocator *)(p); - // compute the size, rounding to the nearest 1k + /* compute the size, rounding to the nearest 1k */ size = (size + 0x3ff) & ~0x3ff; - // reuse a hunk if we can + /* reuse a hunk if we can */ for (scan = 0; scan < MAX_LZMA_ALLOCS; scan++) { uint32_t *ptr = codec->allocptr[scan]; if (ptr != NULL && size == *ptr) { - // set the low bit of the size so we don't match next time + /* set the low bit of the size so we don't match next time */ *ptr |= 1; return ptr + 1; } } - // alloc a new one and put it into the list + /* alloc a new one and put it into the list */ addr = (uint32_t *)malloc(sizeof(uint8_t) * (size + sizeof(uint32_t))); if (addr==NULL) return NULL; @@ -462,20 +431,21 @@ void *lzma_fast_alloc(void *p, size_t size) } } - // set the low bit of the size so we don't match next time + /* set the low bit of the size so we don't match next time */ *addr = size | 1; return addr + 1; } -//------------------------------------------------- -// lzma_fast_free - fast free for lzma, which -// allocates and frees memory frequently -//------------------------------------------------- +/*------------------------------------------------- + * lzma_fast_free - fast free for lzma, which + * allocates and frees memory frequently + *------------------------------------------------- + */ void lzma_fast_free(void *p, void *address) { - int scan; + int scan; uint32_t *ptr; lzma_allocator *codec; if (address == NULL) @@ -483,52 +453,54 @@ void lzma_fast_free(void *p, void *address) codec = (lzma_allocator *)(p); - // find the hunk + /* find the hunk */ ptr = (uint32_t *)(address) - 1; for (scan = 0; scan < MAX_LZMA_ALLOCS; scan++) { if (ptr == codec->allocptr[scan]) { - // clear the low bit of the size to allow matches + /* clear the low bit of the size to allow matches */ *ptr &= ~1; return; } } } -//************************************************************************** -// LZMA DECOMPRESSOR -//************************************************************************** +/*************************************************************************** + * LZMA DECOMPRESSOR + *************************************************************************** + */ -//------------------------------------------------- -// lzma_codec_init - constructor -//------------------------------------------------- +/*------------------------------------------------- + * lzma_codec_init - constructor + *------------------------------------------------- + */ chd_error lzma_codec_init(void* codec, uint32_t hunkbytes) { - CLzmaEncHandle enc; CLzmaEncProps encoder_props; + CLzmaEncHandle enc; Byte decoder_props[LZMA_PROPS_SIZE]; - SizeT props_size; lzma_allocator* alloc; + SizeT props_size; lzma_codec_data* lzma_codec = (lzma_codec_data*) codec; - // construct the decoder + /* construct the decoder */ LzmaDec_Construct(&lzma_codec->decoder); - // FIXME: this code is written in a way that makes it impossible to safely upgrade the LZMA SDK - // This code assumes that the current version of the encoder imposes the same requirements on the - // decoder as the encoder used to produce the file. This is not necessarily true. The format - // needs to be changed so the encoder properties are written to the file. + /* FIXME: this code is written in a way that makes it impossible to safely upgrade the LZMA SDK + * This code assumes that the current version of the encoder imposes the same requirements on the + * decoder as the encoder used to produce the file. This is not necessarily true. The format + * needs to be changed so the encoder properties are written to the file. - // configure the properties like the compressor did + * configure the properties like the compressor did */ LzmaEncProps_Init(&encoder_props); encoder_props.level = 9; encoder_props.reduceSize = hunkbytes; LzmaEncProps_Normalize(&encoder_props); - // convert to decoder properties + /* convert to decoder properties */ alloc = &lzma_codec->allocator; lzma_allocator_init(alloc); enc = LzmaEnc_Create((ISzAlloc*)alloc); @@ -547,43 +519,47 @@ chd_error lzma_codec_init(void* codec, uint32_t hunkbytes) } LzmaEnc_Destroy(enc, (ISzAlloc*)alloc, (ISzAlloc*)alloc); - // do memory allocations + /* do memory allocations */ if (LzmaDec_Allocate(&lzma_codec->decoder, decoder_props, LZMA_PROPS_SIZE, (ISzAlloc*)alloc) != SZ_OK) return CHDERR_DECOMPRESSION_ERROR; - // Okay + /* Okay */ return CHDERR_NONE; } -//------------------------------------------------- -// lzma_codec_free -//------------------------------------------------- +/*------------------------------------------------- + * lzma_codec_free + *------------------------------------------------- + */ void lzma_codec_free(void* codec) { lzma_codec_data* lzma_codec = (lzma_codec_data*) codec; + lzma_allocator* alloc = &lzma_codec->allocator; - // free memory + /* free memory */ + lzma_allocator_free(alloc); LzmaDec_Free(&lzma_codec->decoder, (ISzAlloc*)&lzma_codec->allocator); } -//------------------------------------------------- -// decompress - decompress data using the LZMA -// codec -//------------------------------------------------- +/*------------------------------------------------- + * decompress - decompress data using the LZMA + * codec + *------------------------------------------------- + */ chd_error lzma_codec_decompress(void* codec, const uint8_t *src, uint32_t complen, uint8_t *dest, uint32_t destlen) { ELzmaStatus status; SRes res; SizeT consumedlen, decodedlen; - // initialize + /* initialize */ lzma_codec_data* lzma_codec = (lzma_codec_data*) codec; LzmaDec_Init(&lzma_codec->decoder); - // decode + /* decode */ consumedlen = complen; decodedlen = destlen; res = LzmaDec_DecodeToBuf(&lzma_codec->decoder, dest, &decodedlen, src, &consumedlen, LZMA_FINISH_END, &status); @@ -592,143 +568,185 @@ chd_error lzma_codec_decompress(void* codec, const uint8_t *src, uint32_t comple return CHDERR_NONE; } -// cdlz +/* cdlz */ chd_error cdlz_codec_init(void* codec, uint32_t hunkbytes) { + chd_error ret; cdlz_codec_data* cdlz = (cdlz_codec_data*) codec; - // allocate buffer + /* allocate buffer */ cdlz->buffer = (uint8_t*)malloc(sizeof(uint8_t) * hunkbytes); + if (cdlz->buffer == NULL) + return CHDERR_OUT_OF_MEMORY; - // make sure the CHD's hunk size is an even multiple of the frame size - lzma_codec_init(&cdlz->base_decompressor, (hunkbytes / CD_FRAME_SIZE) * CD_MAX_SECTOR_DATA); - zlib_codec_init(&cdlz->subcode_decompressor, (hunkbytes / CD_FRAME_SIZE) * CD_MAX_SUBCODE_DATA); + ret = lzma_codec_init(&cdlz->base_decompressor, (hunkbytes / CD_FRAME_SIZE) * CD_MAX_SECTOR_DATA); + if (ret != CHDERR_NONE) + return ret; - if (hunkbytes % CD_FRAME_SIZE != 0) - return CHDERR_CODEC_ERROR; +#ifdef WANT_SUBCODE + ret = zlib_codec_init(&cdlz->subcode_decompressor, (hunkbytes / CD_FRAME_SIZE) * CD_MAX_SECTOR_DATA); + if (ret != CHDERR_NONE) + return ret; +#endif return CHDERR_NONE; } void cdlz_codec_free(void* codec) { - // TODO + cdlz_codec_data* cdlz = (cdlz_codec_data*) codec; + + lzma_codec_free(&cdlz->base_decompressor); +#ifdef WANT_SUBCODE + zlib_codec_free(&cdlz->subcode_decompressor); +#endif + if (cdlz->buffer) + free(cdlz->buffer); } chd_error cdlz_codec_decompress(void *codec, const uint8_t *src, uint32_t complen, uint8_t *dest, uint32_t destlen) { - uint32_t framenum; + uint32_t framenum; cdlz_codec_data* cdlz = (cdlz_codec_data*)codec; - // determine header bytes + /* determine header bytes */ uint32_t frames = destlen / CD_FRAME_SIZE; uint32_t complen_bytes = (destlen < 65536) ? 2 : 3; uint32_t ecc_bytes = (frames + 7) / 8; uint32_t header_bytes = ecc_bytes + complen_bytes; - // extract compressed length of base + /* extract compressed length of base */ uint32_t complen_base = (src[ecc_bytes + 0] << 8) | src[ecc_bytes + 1]; if (complen_bytes > 2) complen_base = (complen_base << 8) | src[ecc_bytes + 2]; - // reset and decode + /* reset and decode */ lzma_codec_decompress(&cdlz->base_decompressor, &src[header_bytes], complen_base, &cdlz->buffer[0], frames * CD_MAX_SECTOR_DATA); +#ifdef WANT_SUBCODE zlib_codec_decompress(&cdlz->subcode_decompressor, &src[header_bytes + complen_base], complen - complen_base - header_bytes, &cdlz->buffer[frames * CD_MAX_SECTOR_DATA], frames * CD_MAX_SUBCODE_DATA); +#endif - // reassemble the data + /* reassemble the data */ for (framenum = 0; framenum < frames; framenum++) { - uint8_t *sector; - memcpy(&dest[framenum * CD_FRAME_SIZE], &cdlz->buffer[framenum * CD_MAX_SECTOR_DATA], CD_MAX_SECTOR_DATA); +#ifdef WANT_SUBCODE memcpy(&dest[framenum * CD_FRAME_SIZE + CD_MAX_SECTOR_DATA], &cdlz->buffer[frames * CD_MAX_SECTOR_DATA + framenum * CD_MAX_SUBCODE_DATA], CD_MAX_SUBCODE_DATA); +#endif - // reconstitute the ECC data and sync header - sector = &dest[framenum * CD_FRAME_SIZE]; +#ifdef WANT_RAW_DATA_SECTOR + /* reconstitute the ECC data and sync header */ + uint8_t *sector = &dest[framenum * CD_FRAME_SIZE]; if ((src[framenum / 8] & (1 << (framenum % 8))) != 0) { memcpy(sector, s_cd_sync_header, sizeof(s_cd_sync_header)); ecc_generate(sector); } +#endif } return CHDERR_NONE; } -// cdzl +/* cdzl */ chd_error cdzl_codec_init(void *codec, uint32_t hunkbytes) { + chd_error ret; cdzl_codec_data* cdzl = (cdzl_codec_data*)codec; - // make sure the CHD's hunk size is an even multiple of the frame size - zlib_codec_init(&cdzl->base_decompressor, (hunkbytes / CD_FRAME_SIZE) * CD_MAX_SECTOR_DATA); - zlib_codec_init(&cdzl->subcode_decompressor, (hunkbytes / CD_FRAME_SIZE) * CD_MAX_SUBCODE_DATA); - - cdzl->buffer = (uint8_t*)malloc(sizeof(uint8_t) * hunkbytes); + /* 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; + cdzl->buffer = (uint8_t*)malloc(sizeof(uint8_t) * hunkbytes); + if (cdzl->buffer == NULL) + return CHDERR_OUT_OF_MEMORY; + + ret = zlib_codec_init(&cdzl->base_decompressor, (hunkbytes / CD_FRAME_SIZE) * CD_MAX_SECTOR_DATA); + if (ret != CHDERR_NONE) + return ret; + +#ifdef WANT_SUBCODE + ret = zlib_codec_init(&cdzl->subcode_decompressor, (hunkbytes / CD_FRAME_SIZE) * CD_MAX_SECTOR_DATA); + if (ret != CHDERR_NONE) + return ret; +#endif + return CHDERR_NONE; } void cdzl_codec_free(void *codec) { - // TODO + cdzl_codec_data* cdzl = (cdzl_codec_data*)codec; + + zlib_codec_free(&cdzl->base_decompressor); +#ifdef WANT_SUBCODE + zlib_codec_free(&cdzl->subcode_decompressor); +#endif + if (cdzl->buffer) + free(cdzl->buffer); } chd_error cdzl_codec_decompress(void *codec, const uint8_t *src, uint32_t complen, uint8_t *dest, uint32_t destlen) { - uint32_t framenum; + uint32_t framenum; cdzl_codec_data* cdzl = (cdzl_codec_data*)codec; - - // determine header bytes + + /* determine header bytes */ uint32_t frames = destlen / CD_FRAME_SIZE; uint32_t complen_bytes = (destlen < 65536) ? 2 : 3; uint32_t ecc_bytes = (frames + 7) / 8; uint32_t header_bytes = ecc_bytes + complen_bytes; - // extract compressed length of base + /* extract compressed length of base */ uint32_t complen_base = (src[ecc_bytes + 0] << 8) | src[ecc_bytes + 1]; if (complen_bytes > 2) complen_base = (complen_base << 8) | src[ecc_bytes + 2]; - // reset and decode + /* reset and decode */ zlib_codec_decompress(&cdzl->base_decompressor, &src[header_bytes], complen_base, &cdzl->buffer[0], frames * CD_MAX_SECTOR_DATA); +#ifdef WANT_SUBCODE zlib_codec_decompress(&cdzl->subcode_decompressor, &src[header_bytes + complen_base], complen - complen_base - header_bytes, &cdzl->buffer[frames * CD_MAX_SECTOR_DATA], frames * CD_MAX_SUBCODE_DATA); +#endif - // reassemble the data + /* reassemble the data */ for (framenum = 0; framenum < frames; framenum++) { - uint8_t *sector; memcpy(&dest[framenum * CD_FRAME_SIZE], &cdzl->buffer[framenum * CD_MAX_SECTOR_DATA], CD_MAX_SECTOR_DATA); +#ifdef WANT_SUBCODE memcpy(&dest[framenum * CD_FRAME_SIZE + CD_MAX_SECTOR_DATA], &cdzl->buffer[frames * CD_MAX_SECTOR_DATA + framenum * CD_MAX_SUBCODE_DATA], CD_MAX_SUBCODE_DATA); +#endif - // reconstitute the ECC data and sync header - sector = &dest[framenum * CD_FRAME_SIZE]; +#ifdef WANT_RAW_DATA_SECTOR + /* reconstitute the ECC data and sync header */ + uint8_t *sector = &dest[framenum * CD_FRAME_SIZE]; if ((src[framenum / 8] & (1 << (framenum % 8))) != 0) { memcpy(sector, s_cd_sync_header, sizeof(s_cd_sync_header)); ecc_generate(sector); } +#endif } return CHDERR_NONE; } -//************************************************************************** -// CD FLAC DECOMPRESSOR -//************************************************************************** +/*************************************************************************** + * CD FLAC DECOMPRESSOR + *************************************************************************** + */ -//------------------------------------------------------ -// cdfl_codec_blocksize - return the optimal block size -//------------------------------------------------------ +/*------------------------------------------------------ + * 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 + /* 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; @@ -737,37 +755,35 @@ static uint32_t cdfl_codec_blocksize(uint32_t bytes) chd_error cdfl_codec_init(void *codec, uint32_t hunkbytes) { - int zerr; - uint16_t native_endian = 0; +#ifdef WANT_SUBCODE + chd_error ret; +#endif + uint16_t native_endian = 0; cdfl_codec_data *cdfl = (cdfl_codec_data*)codec; - cdfl->buffer = (uint8_t*)malloc(sizeof(uint8_t) * hunkbytes); - - // make sure the CHD's hunk size is an even multiple of the frame size + /* 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; - // determine whether we want native or swapped samples + 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); - // init the inflater - cdfl->inflater.next_in = (Bytef *)cdfl; // bogus, but that's ok - cdfl->inflater.avail_in = 0; - //cdfl->allocator.install(cdfl->inflater); - cdfl->inflater.zalloc = zlib_fast_alloc; - cdfl->inflater.zfree = zlib_fast_free; - cdfl->inflater.opaque = &cdfl->allocator; - zerr = inflateInit2(&cdfl->inflater, -MAX_WBITS); +#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 - // convert errors - if (zerr == Z_MEM_ERROR) - return CHDERR_OUT_OF_MEMORY; - else if (zerr != Z_OK) - return CHDERR_CODEC_ERROR; - - // init flac decoder + /* flac decoder init */ flac_decoder_init(&cdfl->decoder); + if (cdfl->decoder.decoder == NULL) + return CHDERR_OUT_OF_MEMORY; return CHDERR_NONE; } @@ -775,48 +791,50 @@ chd_error cdfl_codec_init(void *codec, uint32_t hunkbytes) void cdfl_codec_free(void *codec) { cdfl_codec_data *cdfl = (cdfl_codec_data*)codec; - inflateEnd(&cdfl->inflater); + 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) { - int zerr; + uint32_t framenum; uint8_t *buffer; - uint32_t framenum, offset; +#ifdef WANT_SUBCODE + uint32_t offset; + chd_error ret; +#endif cdfl_codec_data *cdfl = (cdfl_codec_data*)codec; - // reset and decode + /* 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; - // inflate the subcode data +#ifdef WANT_SUBCODE + /* inflate the subcode data */ offset = flac_decoder_finish(&cdfl->decoder); - cdfl->inflater.next_in = (Bytef *)(src + offset); - cdfl->inflater.avail_in = complen - offset; - cdfl->inflater.total_in = 0; - cdfl->inflater.next_out = &cdfl->buffer[frames * CD_MAX_SECTOR_DATA]; - cdfl->inflater.avail_out = frames * CD_MAX_SUBCODE_DATA; - cdfl->inflater.total_out = 0; - zerr = inflateReset(&cdfl->inflater); - if (zerr != Z_OK) - return CHDERR_DECOMPRESSION_ERROR; + 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 - // do it - zerr = inflate(&cdfl->inflater, Z_FINISH); - if (zerr != Z_STREAM_END) - return CHDERR_DECOMPRESSION_ERROR; - if (cdfl->inflater.total_out != frames * CD_MAX_SUBCODE_DATA) - return CHDERR_DECOMPRESSION_ERROR; - - // reassemble the data + /* 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; @@ -827,7 +845,7 @@ chd_error cdfl_codec_decompress(void *codec, const uint8_t *src, uint32_t comple #define CHD_MAKE_TAG(a,b,c,d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d)) -// general codecs with CD frontend +/* general codecs with CD frontend */ #define CHD_CODEC_CD_ZLIB CHD_MAKE_TAG('c','d','z','l') #define CHD_CODEC_CD_LZMA CHD_MAKE_TAG('c','d','l','z') #define CHD_CODEC_CD_FLAC CHD_MAKE_TAG('c','d','f','l') @@ -910,7 +928,7 @@ static const codec_interface codec_interfaces[] = the data stream in bigendian order -------------------------------------------------*/ -static INLINE UINT64 get_bigendian_uint64(const UINT8 *base) +INLINE UINT64 get_bigendian_uint64(const UINT8 *base) { return ((UINT64)base[0] << 56) | ((UINT64)base[1] << 48) | ((UINT64)base[2] << 40) | ((UINT64)base[3] << 32) | ((UINT64)base[4] << 24) | ((UINT64)base[5] << 16) | ((UINT64)base[6] << 8) | (UINT64)base[7]; @@ -922,7 +940,7 @@ static INLINE UINT64 get_bigendian_uint64(const UINT8 *base) the data stream in bigendian order -------------------------------------------------*/ -static INLINE void put_bigendian_uint64(UINT8 *base, UINT64 value) +INLINE void put_bigendian_uint64(UINT8 *base, UINT64 value) { base[0] = value >> 56; base[1] = value >> 48; @@ -939,7 +957,7 @@ static INLINE void put_bigendian_uint64(UINT8 *base, UINT64 value) the data stream in bigendian order -------------------------------------------------*/ -static INLINE UINT64 get_bigendian_uint48(const UINT8 *base) +INLINE UINT64 get_bigendian_uint48(const UINT8 *base) { return ((UINT64)base[0] << 40) | ((UINT64)base[1] << 32) | ((UINT64)base[2] << 24) | ((UINT64)base[3] << 16) | ((UINT64)base[4] << 8) | (UINT64)base[5]; @@ -950,7 +968,7 @@ static INLINE UINT64 get_bigendian_uint48(const UINT8 *base) the data stream in bigendian order -------------------------------------------------*/ -static INLINE void put_bigendian_uint48(UINT8 *base, UINT64 value) +INLINE void put_bigendian_uint48(UINT8 *base, UINT64 value) { value &= 0xffffffffffff; base[0] = value >> 40; @@ -965,7 +983,7 @@ static INLINE void put_bigendian_uint48(UINT8 *base, UINT64 value) the data stream in bigendian order -------------------------------------------------*/ -static INLINE UINT32 get_bigendian_uint32(const UINT8 *base) +INLINE UINT32 get_bigendian_uint32(const UINT8 *base) { return (base[0] << 24) | (base[1] << 16) | (base[2] << 8) | base[3]; } @@ -976,7 +994,7 @@ static INLINE UINT32 get_bigendian_uint32(const UINT8 *base) the data stream in bigendian order -------------------------------------------------*/ -static INLINE void put_bigendian_uint24(UINT8 *base, UINT32 value) +INLINE void put_bigendian_uint24(UINT8 *base, UINT32 value) { value &= 0xffffff; base[0] = value >> 16; @@ -990,7 +1008,7 @@ static INLINE void put_bigendian_uint24(UINT8 *base, UINT32 value) the data stream in bigendian order -------------------------------------------------*/ -static INLINE void put_bigendian_uint32(UINT8 *base, UINT32 value) +INLINE void put_bigendian_uint32(UINT8 *base, UINT32 value) { value &= 0xffffff; base[0] = value >> 16; @@ -1003,7 +1021,7 @@ static INLINE void put_bigendian_uint32(UINT8 *base, UINT32 value) the data stream in bigendian order -------------------------------------------------*/ -static INLINE UINT32 get_bigendian_uint24(const UINT8 *base) +INLINE UINT32 get_bigendian_uint24(const UINT8 *base) { return (base[0] << 16) | (base[1] << 8) | base[2]; } @@ -1013,7 +1031,7 @@ static INLINE UINT32 get_bigendian_uint24(const UINT8 *base) the data stream in bigendian order -------------------------------------------------*/ -static INLINE UINT16 get_bigendian_uint16(const UINT8 *base) +INLINE UINT16 get_bigendian_uint16(const UINT8 *base) { return (base[0] << 8) | base[1]; } @@ -1024,7 +1042,7 @@ static INLINE UINT16 get_bigendian_uint16(const UINT8 *base) the data stream in bigendian order -------------------------------------------------*/ -static INLINE void put_bigendian_uint16(UINT8 *base, UINT16 value) +INLINE void put_bigendian_uint16(UINT8 *base, UINT16 value) { base[0] = value >> 8; base[1] = value; @@ -1036,7 +1054,7 @@ static INLINE void put_bigendian_uint16(UINT8 *base, UINT16 value) entry from the datastream -------------------------------------------------*/ -static INLINE void map_extract(const UINT8 *base, map_entry *entry) +INLINE void map_extract(const UINT8 *base, map_entry *entry) { entry->offset = get_bigendian_uint64(&base[0]); entry->crc = get_bigendian_uint32(&base[8]); @@ -1050,7 +1068,7 @@ static INLINE void map_extract(const UINT8 *base, map_entry *entry) entry to the datastream -------------------------------------------------*/ -static INLINE void map_assemble(UINT8 *base, map_entry *entry) +INLINE void map_assemble(UINT8 *base, map_entry *entry) { put_bigendian_uint64(&base[0], entry->offset); put_bigendian_uint32(&base[8], entry->crc); @@ -1062,7 +1080,7 @@ static INLINE void map_assemble(UINT8 *base, map_entry *entry) /*------------------------------------------------- map_size_v5 - calculate CHDv5 map size -------------------------------------------------*/ -static INLINE int map_size_v5(chd_header* header) +INLINE int map_size_v5(chd_header* header) { return header->hunkcount * header->mapentrybytes; } @@ -1113,7 +1131,7 @@ uint16_t crc16(const void *data, uint32_t length) const uint8_t *src = (uint8_t*)data; - // fetch the current value into a local and rip through the source data + /* fetch the current value into a local and rip through the source data */ while (length-- != 0) crc = (crc << 8) ^ s_table[(crc >> 8) ^ *src++]; return crc; @@ -1125,31 +1143,29 @@ uint16_t crc16(const void *data, uint32_t length) static chd_error decompress_v5_map(chd_file* chd, chd_header* header) { - int hunknum; - uint8_t lastcomp = 0; - int repcount = 0; - uint32_t last_self = 0; - uint64_t last_parent = 0; - struct bitstream* bitbuf; + uint8_t rawbuf[16]; + uint16_t mapcrc; uint32_t mapbytes; uint64_t firstoffs; - uint16_t mapcrc; - uint8_t lengthbits; - uint8_t selfbits; - uint8_t parentbits; - uint8_t *compressed; - uint8_t rawbuf[16]; - struct huffman_decoder* decoder; + uint32_t last_self = 0; + uint64_t last_parent = 0; + uint8_t lastcomp = 0; + int hunknum, repcount = 0; enum huffman_error err; + uint8_t lengthbits, selfbits, parentbits; + uint8_t* compressed; + struct bitstream* bitbuf; + struct huffman_decoder* decoder; uint64_t curoffset; - if (header->mapoffset == 0) { - //memset(header->rawmap, 0xff,map_size_v5(header)); +#if 0 + memset(header->rawmap, 0xff,map_size_v5(header)); +#endif return CHDERR_READ_ERROR; } - // read the reader + /* read the reader */ core_fseek(chd->file, header->mapoffset, SEEK_SET); core_fread(chd->file, rawbuf, sizeof(rawbuf)); mapbytes = get_bigendian_uint32(&rawbuf[0]); @@ -1159,18 +1175,46 @@ static chd_error decompress_v5_map(chd_file* chd, chd_header* header) selfbits = rawbuf[13]; parentbits = rawbuf[14]; - // now read the map + /* now read the map */ compressed = (uint8_t*)malloc(sizeof(uint8_t) * mapbytes); + if (compressed == NULL) + return CHDERR_OUT_OF_MEMORY; + core_fseek(chd->file, header->mapoffset + 16, SEEK_SET); core_fread(chd->file, compressed, mapbytes); bitbuf = create_bitstream(compressed, sizeof(uint8_t) * mapbytes); - header->rawmap = (uint8_t*)malloc(sizeof(uint8_t) * map_size_v5(header)); + if (bitbuf == NULL) + { + free(compressed); + return CHDERR_OUT_OF_MEMORY; + } - // first decode the compression types + header->rawmap = (uint8_t*)malloc(sizeof(uint8_t) * map_size_v5(header)); + if (header->rawmap == NULL) + { + free(compressed); + free(bitbuf); + return CHDERR_OUT_OF_MEMORY; + } + + /* first decode the compression types */ decoder = create_huffman_decoder(16, 8); + if (decoder == NULL) + { + free(compressed); + free(bitbuf); + return CHDERR_OUT_OF_MEMORY; + } + err = huffman_import_tree_rle(decoder, bitbuf); if (err != HUFFERR_NONE) + { + free(compressed); + free(bitbuf); + delete_huffman_decoder(decoder); return CHDERR_DECOMPRESSION_ERROR; + } + for (hunknum = 0; hunknum < header->hunkcount; hunknum++) { uint8_t *rawmap = header->rawmap + (hunknum * 12); @@ -1188,7 +1232,7 @@ static chd_error decompress_v5_map(chd_file* chd, chd_header* header) } } - // then iterate through the hunks and extract the needed data + /* then iterate through the hunks and extract the needed data */ curoffset = firstoffs; for (hunknum = 0; hunknum < header->hunkcount; hunknum++) { @@ -1198,7 +1242,7 @@ static chd_error decompress_v5_map(chd_file* chd, chd_header* header) uint16_t crc = 0; switch (rawmap[0]) { - // base types + /* base types */ case COMPRESSION_TYPE_0: case COMPRESSION_TYPE_1: case COMPRESSION_TYPE_2: @@ -1221,7 +1265,7 @@ static chd_error decompress_v5_map(chd_file* chd, chd_header* header) last_parent = offset; break; - // pseudo-types; convert into base types + /* pseudo-types; convert into base types */ case COMPRESSION_SELF_1: last_self++; case COMPRESSION_SELF_0: @@ -1241,17 +1285,22 @@ static chd_error decompress_v5_map(chd_file* chd, chd_header* header) offset = last_parent; break; } - // UINT24 length + /* UINT24 length */ put_bigendian_uint24(&rawmap[1], length); - // UINT48 offset + /* UINT48 offset */ put_bigendian_uint48(&rawmap[4], offset); - // crc16 + /* crc16 */ put_bigendian_uint16(&rawmap[10], crc); } - // verify the final CRC + /* free memory */ + free(compressed); + free(bitbuf); + delete_huffman_decoder(decoder); + + /* verify the final CRC */ if (crc16(&header->rawmap[0], header->hunkcount * 12) != mapcrc) return CHDERR_DECOMPRESSION_ERROR; @@ -1263,7 +1312,7 @@ static chd_error decompress_v5_map(chd_file* chd, chd_header* header) entry in old format from the datastream -------------------------------------------------*/ -static INLINE void map_extract_old(const UINT8 *base, map_entry *entry, UINT32 hunkbytes) +INLINE void map_extract_old(const UINT8 *base, map_entry *entry, UINT32 hunkbytes) { entry->offset = get_bigendian_uint64(&base[0]); entry->crc = 0; @@ -1301,10 +1350,10 @@ chd_error chd_open_file(core_file *file, int mode, chd_file *parent, chd_file ** EARLY_EXIT(err = CHDERR_INVALID_PARAMETER); /* allocate memory for the final result */ - newchd = (chd_file *)malloc(sizeof(chd_file)); + newchd = (chd_file *)malloc(sizeof(**chd)); if (newchd == NULL) EARLY_EXIT(err = CHDERR_OUT_OF_MEMORY); - memset(newchd, 0, sizeof(chd_file)); + memset(newchd, 0, sizeof(*newchd)); newchd->cookie = COOKIE_VALUE; newchd->parent = parent; newchd->file = file; @@ -1359,13 +1408,15 @@ chd_error chd_open_file(core_file *file, int mode, chd_file *parent, chd_file ** err = decompress_v5_map(newchd, &(newchd->header)); } - /* allocate and init the hunk cache */ +#ifdef NEED_CACHE_HUNK + /* allocate and init the hunk cache */ newchd->cache = (UINT8 *)malloc(newchd->header.hunkbytes); newchd->compare = (UINT8 *)malloc(newchd->header.hunkbytes); if (newchd->cache == NULL || newchd->compare == NULL) EARLY_EXIT(err = CHDERR_OUT_OF_MEMORY); newchd->cachehunk = ~0; newchd->comparehunk = ~0; +#endif /* allocate the temporary compressed buffer */ newchd->compressed = (UINT8 *)malloc(newchd->header.hunkbytes); @@ -1390,12 +1441,10 @@ chd_error chd_open_file(core_file *file, int mode, chd_file *parent, chd_file ** } else { - int decompnum; - - // verify the compression types and initialize the codecs + int i, decompnum; + /* verify the compression types and initialize the codecs */ for (decompnum = 0; decompnum < ARRAY_LENGTH(newchd->header.compression); decompnum++) { - int i; for (i = 0 ; i < ARRAY_LENGTH(codec_interfaces) ; i++) { if (codec_interfaces[i].compression == newchd->header.compression[decompnum]) @@ -1423,18 +1472,19 @@ chd_error chd_open_file(core_file *file, int mode, chd_file *parent, chd_file ** break; } if (codec != NULL) - { err = (*newchd->codecintf[decompnum]->init)(codec, newchd->header.hunkbytes); - } } + } } } } - // HACK - //if (err != CHDERR_NONE) - // EARLY_EXIT(err); +#if 0 + /* HACK */ + if (err != CHDERR_NONE) + EARLY_EXIT(err); +#endif /* all done */ *chd = newchd; @@ -1455,7 +1505,6 @@ chd_error chd_open(const char *filename, int mode, chd_file *parent, chd_file ** { chd_error err; core_file *file = NULL; - UINT32 openflags; /* choose the proper mode */ switch(mode) @@ -1504,13 +1553,13 @@ void chd_close(chd_file *chd) /* deinit the codec */ if (chd->header.version < 5) { - if (chd->codecintf[0] != NULL && chd->codecintf[0]->free != NULL) - (*chd->codecintf[0]->free)(&chd->zlib_codec_data); + if (chd->codecintf[0] != NULL && chd->codecintf[0]->free != NULL) + (*chd->codecintf[0]->free)(&chd->zlib_codec_data); } else { - int i; - // Free the codecs + int i; + /* Free the codecs */ for (i = 0 ; i < 4 ; i++) { void* codec = NULL; @@ -1527,14 +1576,14 @@ void chd_close(chd_file *chd) case CHD_CODEC_CD_FLAC: codec = &chd->cdfl_codec_data; break; - } + } if (codec) { (*chd->codecintf[i]->free)(codec); } } - // Free the raw map + /* Free the raw map */ if (chd->header.rawmap != NULL) free(chd->header.rawmap); } @@ -1543,29 +1592,25 @@ void chd_close(chd_file *chd) if (chd->compressed != NULL) free(chd->compressed); +#ifdef NEED_CACHE_HUNK /* free the hunk cache and compare data */ if (chd->compare != NULL) free(chd->compare); if (chd->cache != NULL) free(chd->cache); +#endif /* free the hunk map */ if (chd->map != NULL) free(chd->map); - /* free the CRC table */ - if (chd->crctable != NULL) - free(chd->crctable); - - /* free the CRC map */ - if (chd->crcmap != NULL) - free(chd->crcmap); - /* close the file */ if (chd->owns_file && chd->file != NULL) core_fclose(chd->file); +#ifdef NEED_CACHE_HUNK if (PRINTF_MAX_HUNK) printf("Max hunk = %d/%d\n", chd->maxhunk, chd->header.totalhunks); +#endif /* free our memory */ free(chd); @@ -1661,10 +1706,6 @@ chd_error chd_read(chd_file *chd, UINT32 hunknum, void *buffer) if (chd == NULL || chd->cookie != COOKIE_VALUE) return CHDERR_INVALID_PARAMETER; - /* if we're past the end, fail */ - if (hunknum >= chd->header.totalhunks) - return CHDERR_HUNK_OUT_OF_RANGE; - /* perform the read */ return hunk_read_into_memory(chd, hunknum, (UINT8 *)buffer); } @@ -1745,7 +1786,6 @@ chd_error chd_get_metadata(chd_file *chd, UINT32 searchtag, UINT32 searchindex, chd_error chd_codec_config(chd_file *chd, int param, void *config) { - return CHDERR_INVALID_PARAMETER; } @@ -1773,11 +1813,11 @@ const char *chd_get_codec_name(UINT32 codec) static chd_error header_validate(const chd_header *header) { int intfnum; - + /* require a valid version */ if (header->version == 0 || header->version > CHD_HEADER_VERSION) return CHDERR_UNSUPPORTED_VERSION; - + /* require a valid length */ if ((header->version == 1 && header->length != CHD_V1_HEADER_SIZE) || (header->version == 2 && header->length != CHD_V2_HEADER_SIZE) || @@ -1785,7 +1825,7 @@ static chd_error header_validate(const chd_header *header) (header->version == 4 && header->length != CHD_V4_HEADER_SIZE) || (header->version == 5 && header->length != CHD_V5_HEADER_SIZE)) return CHDERR_INVALID_PARAMETER; - + /* Do not validate v5 header */ if (header->version <= 4) { @@ -1873,7 +1913,7 @@ static chd_error header_read(core_file *file, chd_header *header) (header->version == 3 && header->length != CHD_V3_HEADER_SIZE) || (header->version == 4 && header->length != CHD_V4_HEADER_SIZE) || (header->version == 5 && header->length != CHD_V5_HEADER_SIZE)) - + return CHDERR_INVALID_DATA; /* extract the common data */ @@ -1940,10 +1980,10 @@ static chd_error header_read(core_file *file, chd_header *header) memcpy(header->parentsha1, &rawheader[104], CHD_SHA1_BYTES); memcpy(header->rawsha1, &rawheader[64], CHD_SHA1_BYTES); - // determine properties of map entries - header->mapentrybytes = 12; //TODO compressed() ? 12 : 4; + /* determine properties of map entries */ + header->mapentrybytes = 12; /*TODO compressed() ? 12 : 4; */ - // hack + /* hack */ header->totalhunks = header->hunkcount; } @@ -1962,6 +2002,7 @@ static chd_error header_read(core_file *file, chd_header *header) INTERNAL HUNK READ/WRITE ***************************************************************************/ +#ifdef NEED_CACHE_HUNK /*------------------------------------------------- hunk_read_into_cache - read a hunk into the CHD's hunk cache @@ -1989,7 +2030,7 @@ static chd_error hunk_read_into_cache(chd_file *chd, UINT32 hunknum) chd->cachehunk = hunknum; return CHDERR_NONE; } - +#endif /*------------------------------------------------- hunk_read_into_memory - read a hunk into @@ -2000,7 +2041,7 @@ static chd_error hunk_read_into_memory(chd_file *chd, UINT32 hunknum, UINT8 *des { chd_error err; - // punt if no file + /* punt if no file */ if (chd->file == NULL) return CHDERR_INVALID_FILE; @@ -2008,8 +2049,12 @@ static chd_error hunk_read_into_memory(chd_file *chd, UINT32 hunknum, UINT8 *des if (hunknum >= chd->header.totalhunks) return CHDERR_HUNK_OUT_OF_RANGE; + if (dest == NULL) + return CHDERR_INVALID_PARAMETER; + if (chd->header.version < 5) { + void* codec; map_entry *entry = &chd->map[hunknum]; UINT32 bytes; @@ -2018,22 +2063,20 @@ static chd_error hunk_read_into_memory(chd_file *chd, UINT32 hunknum, UINT8 *des { /* compressed data */ case V34_MAP_ENTRY_TYPE_COMPRESSED: - { - void* codec; - /* read it into the decompression buffer */ - core_fseek(chd->file, entry->offset, SEEK_SET); - bytes = core_fread(chd->file, chd->compressed, entry->length); - if (bytes != entry->length) - return CHDERR_READ_ERROR; - /* now decompress using the codec */ - err = CHDERR_NONE; - codec = &chd->zlib_codec_data; - if (chd->codecintf[0]->decompress != NULL) - err = (*chd->codecintf[0]->decompress)(codec, chd->compressed, entry->length, dest, chd->header.hunkbytes); - if (err != CHDERR_NONE) - return err; - } + /* read it into the decompression buffer */ + core_fseek(chd->file, entry->offset, SEEK_SET); + bytes = core_fread(chd->file, chd->compressed, entry->length); + if (bytes != entry->length) + return CHDERR_READ_ERROR; + + /* now decompress using the codec */ + err = CHDERR_NONE; + codec = &chd->zlib_codec_data; + if (chd->codecintf[0]->decompress != NULL) + err = (*chd->codecintf[0]->decompress)(codec, chd->compressed, entry->length, dest, chd->header.hunkbytes); + if (err != CHDERR_NONE) + return err; break; /* uncompressed data */ @@ -2053,8 +2096,10 @@ static chd_error hunk_read_into_memory(chd_file *chd, UINT32 hunknum, UINT8 *des /* self-referenced data */ case V34_MAP_ENTRY_TYPE_SELF_HUNK: +#ifdef NEED_CACHE_HUNK if (chd->cachehunk == entry->offset && dest == chd->cache) break; +#endif return hunk_read_into_memory(chd, entry->offset, dest); /* parent-referenced data */ @@ -2068,15 +2113,16 @@ static chd_error hunk_read_into_memory(chd_file *chd, UINT32 hunknum, UINT8 *des } else { - - // get a pointer to the map entry + void* codec = NULL; + /* get a pointer to the map entry */ uint64_t blockoffs; uint32_t blocklen; +#ifdef VERIFY_BLOCK_CRC uint16_t blockcrc; - void* codec = NULL; +#endif uint8_t *rawmap = &chd->header.rawmap[chd->header.mapentrybytes * hunknum]; - // uncompressed case + /* uncompressed case */ /* TODO if (!compressed()) { @@ -2092,10 +2138,12 @@ static chd_error hunk_read_into_memory(chd_file *chd, UINT32 hunknum, UINT8 *des return CHDERR_NONE; }*/ - // compressed case + /* compressed case */ blocklen = get_bigendian_uint24(&rawmap[1]); blockoffs = get_bigendian_uint48(&rawmap[4]); +#ifdef VERIFY_BLOCK_CRC blockcrc = get_bigendian_uint16(&rawmap[10]); +#endif switch (rawmap[0]) { case COMPRESSION_TYPE_0: @@ -2119,33 +2167,41 @@ static chd_error hunk_read_into_memory(chd_file *chd, UINT32 hunknum, UINT8 *des break; } if (codec==NULL) + return CHDERR_CODEC_ERROR; + err = (*chd->codecintf[rawmap[0]]->decompress)(codec, chd->compressed, blocklen, dest, chd->header.hunkbytes); + if (err != CHDERR_NONE) + return err; +#ifdef VERIFY_BLOCK_CRC + if (crc16(dest, chd->header.hunkbytes) != blockcrc) return CHDERR_DECOMPRESSION_ERROR; - chd->codecintf[rawmap[0]]->decompress(codec, chd->compressed, blocklen, dest, chd->header.hunkbytes); - if (dest != NULL && crc16(dest, chd->header.hunkbytes) != blockcrc) - return CHDERR_DECOMPRESSION_ERROR; +#endif return CHDERR_NONE; case COMPRESSION_NONE: core_fseek(chd->file, blockoffs, SEEK_SET); core_fread(chd->file, dest, chd->header.hunkbytes); +#ifdef VERIFY_BLOCK_CRC if (crc16(dest, chd->header.hunkbytes) != blockcrc) return CHDERR_DECOMPRESSION_ERROR; +#endif return CHDERR_NONE; case COMPRESSION_SELF: return hunk_read_into_memory(chd, blockoffs, dest); case COMPRESSION_PARENT: - // TODO - //if (m_parent_missing) - // return CHDERR_REQUIRES_PARENT; - //return m_parent->read_bytes(uint64_t(blockoffs) * uint64_t(m_parent->unit_bytes()), dest, m_hunkbytes); + /* TODO */ +#if 0 + if (m_parent_missing) + return CHDERR_REQUIRES_PARENT; + return m_parent->read_bytes(uint64_t(blockoffs) * uint64_t(m_parent->unit_bytes()), dest, m_hunkbytes); +#endif return CHDERR_DECOMPRESSION_ERROR; } return CHDERR_NONE; } - // We should not reach this code + /* We should not reach this code */ return CHDERR_DECOMPRESSION_ERROR; } @@ -2154,6 +2210,15 @@ static chd_error hunk_read_into_memory(chd_file *chd, UINT32 hunknum, UINT8 *des INTERNAL MAP ACCESS ***************************************************************************/ +static size_t core_fsize(core_file *f) +{ + long rv,p = ftell(f); + fseek(f, 0, SEEK_END); + rv = ftell(f); + fseek(f, p, SEEK_SET); + return rv; +} + /*------------------------------------------------- map_read - read the initial sector map -------------------------------------------------*/ @@ -2299,9 +2364,9 @@ static chd_error metadata_find_entry(chd_file *chd, UINT32 metatag, UINT32 metai static chd_error zlib_codec_init(void *codec, uint32_t hunkbytes) { - zlib_codec_data *data = (zlib_codec_data*)codec; - chd_error err; int zerr; + chd_error err; + zlib_codec_data *data = (zlib_codec_data*)codec; /* clear the buffers */ memset(data, 0, sizeof(zlib_codec_data)); @@ -2322,10 +2387,6 @@ static chd_error zlib_codec_init(void *codec, uint32_t hunkbytes) else err = CHDERR_NONE; - /* handle an error */ - if (err != CHDERR_NONE) - free(data); - return err; } diff --git a/libretro-common/formats/libchdr/chd.h b/libretro-common/formats/libchdr/chd.h index a3a44b840a..4fe3e6411a 100644 --- a/libretro-common/formats/libchdr/chd.h +++ b/libretro-common/formats/libchdr/chd.h @@ -310,9 +310,9 @@ struct _chd_header UINT64 unitcount; /* TODO V5 */ UINT32 hunkcount; /* TODO V5 */ - // map information - UINT32 mapentrybytes; // length of each entry in a map (V5) - UINT8* rawmap; // raw map data + /* map information */ + UINT32 mapentrybytes; /* length of each entry in a map (V5) */ + UINT8* rawmap; /* raw map data */ UINT32 obsolete_cylinders; /* obsolete field -- do not use! */ UINT32 obsolete_sectors; /* obsolete field -- do not use! */ @@ -341,12 +341,13 @@ struct _chd_verify_result /* ----- CHD file management ----- */ /* create a new CHD file fitting the given description */ -// chd_error chd_create(const char *filename, UINT64 logicalbytes, UINT32 hunkbytes, UINT32 compression, chd_file *parent); +/* chd_error chd_create(const char *filename, UINT64 logicalbytes, UINT32 hunkbytes, UINT32 compression, chd_file *parent); */ /* same as chd_create(), but accepts an already-opened core_file object */ -// chd_error chd_create_file(core_file *file, UINT64 logicalbytes, UINT32 hunkbytes, UINT32 compression, chd_file *parent); +/* chd_error chd_create_file(core_file *file, UINT64 logicalbytes, UINT32 hunkbytes, UINT32 compression, chd_file *parent); */ /* open an existing CHD file */ +chd_error chd_open_file(core_file *file, int mode, chd_file *parent, chd_file **chd); chd_error chd_open(const char *filename, int mode, chd_file *parent, chd_file **chd); diff --git a/libretro-common/formats/libchdr/coretypes.h b/libretro-common/formats/libchdr/coretypes.h index 80f161f2b6..5a769f6a86 100644 --- a/libretro-common/formats/libchdr/coretypes.h +++ b/libretro-common/formats/libchdr/coretypes.h @@ -7,14 +7,18 @@ #define ARRAY_LENGTH(x) (sizeof(x)/sizeof(x[0])) typedef uint64_t UINT64; +#ifndef OSD_CPU_H typedef uint32_t UINT32; typedef uint16_t UINT16; typedef uint8_t UINT8; +#endif typedef int64_t INT64; +#ifndef OSD_CPU_H typedef int32_t INT32; typedef int16_t INT16; typedef int8_t INT8; +#endif #define core_file FILE #define core_fopen(file) fopen(file, "rb") @@ -23,14 +27,4 @@ typedef int8_t INT8; #define core_fclose fclose #define core_ftell ftell -static size_t core_fsize(core_file* f) -{ - size_t rv; - size_t p = ftell(f); - fseek(f, 0, SEEK_END); - rv = ftell(f); - fseek(f, p, SEEK_SET); - return rv; -} - #endif diff --git a/libretro-common/formats/libchdr/flac.c b/libretro-common/formats/libchdr/flac.c index 29844b6513..dfde00a2ee 100644 --- a/libretro-common/formats/libchdr/flac.c +++ b/libretro-common/formats/libchdr/flac.c @@ -24,17 +24,10 @@ static FLAC__StreamDecoderWriteStatus flac_decoder_write_callback_static(const F FLAC__StreamDecoderWriteStatus flac_decoder_write_callback(void* client_data, const FLAC__Frame *frame, const FLAC__int32 * const buffer[]); static void flac_decoder_error_callback_static(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data); -// getters (valid after reset) -static uint32_t sample_rate(flac_decoder *decoder) { return decoder->sample_rate; } -static uint8_t channels(flac_decoder *decoder) { return decoder->channels; } -static uint8_t bits_per_sample(flac_decoder *decoder) { return decoder->bits_per_sample; } -static uint32_t total_samples(flac_decoder *decoder) { return FLAC__stream_decoder_get_total_samples(decoder->decoder); } -static FLAC__StreamDecoderState state(flac_decoder *decoder) { return FLAC__stream_decoder_get_state(decoder->decoder); } -static const char *state_string(flac_decoder *decoder) { return FLAC__stream_decoder_get_resolved_state_string(decoder->decoder); } - -//------------------------------------------------- -// flac_decoder - constructor -//------------------------------------------------- +/*------------------------------------------------- + * flac_decoder - constructor + *------------------------------------------------- + */ void flac_decoder_init(flac_decoder *decoder) { @@ -52,9 +45,10 @@ void flac_decoder_init(flac_decoder *decoder) decoder->uncompressed_swap = 0; } -//------------------------------------------------- -// flac_decoder - destructor -//------------------------------------------------- +/*------------------------------------------------- + * flac_decoder - destructor + *------------------------------------------------- + */ void flac_decoder_free(flac_decoder* decoder) { @@ -63,10 +57,11 @@ void flac_decoder_free(flac_decoder* decoder) } -//------------------------------------------------- -// reset - reset state with the original -// parameters -//------------------------------------------------- +/*------------------------------------------------- + * reset - reset state with the original + * parameters + *------------------------------------------------- + */ static int flac_decoder_internal_reset(flac_decoder* decoder) { @@ -86,30 +81,31 @@ static int flac_decoder_internal_reset(flac_decoder* decoder) -//------------------------------------------------- -// reset - reset state with new memory parameters -// and a custom-generated header -//------------------------------------------------- +/*------------------------------------------------- + * reset - reset state with new memory parameters + * and a custom-generated header + *------------------------------------------------- + */ int flac_decoder_reset(flac_decoder* decoder, uint32_t sample_rate, uint8_t num_channels, uint32_t block_size, const void *buffer, uint32_t length) { - // modify the template header with our parameters + /* modify the template header with our parameters */ static const uint8_t s_header_template[0x2a] = { - 0x66, 0x4C, 0x61, 0x43, // +00: 'fLaC' stream header - 0x80, // +04: metadata block type 0 (STREAMINFO), - // flagged as last block - 0x00, 0x00, 0x22, // +05: metadata block length = 0x22 - 0x00, 0x00, // +08: minimum block size - 0x00, 0x00, // +0A: maximum block size - 0x00, 0x00, 0x00, // +0C: minimum frame size (0 == unknown) - 0x00, 0x00, 0x00, // +0F: maximum frame size (0 == unknown) - 0x0A, 0xC4, 0x42, 0xF0, 0x00, 0x00, 0x00, 0x00, // +12: sample rate (0x0ac44 == 44100), - // numchannels (2), sample bits (16), - // samples in stream (0 == unknown) - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +1A: MD5 signature (0 == none) - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // - // +2A: start of stream data + 0x66, 0x4C, 0x61, 0x43, /* +00: 'fLaC' stream header */ + 0x80, /* +04: metadata block type 0 (STREAMINFO), */ + /* flagged as last block */ + 0x00, 0x00, 0x22, /* +05: metadata block length = 0x22 */ + 0x00, 0x00, /* +08: minimum block size */ + 0x00, 0x00, /* +0A: maximum block size */ + 0x00, 0x00, 0x00, /* +0C: minimum frame size (0 == unknown) */ + 0x00, 0x00, 0x00, /* +0F: maximum frame size (0 == unknown) */ + 0x0A, 0xC4, 0x42, 0xF0, 0x00, 0x00, 0x00, 0x00, /* +12: sample rate (0x0ac44 == 44100), */ + /* numchannels (2), sample bits (16), */ + /* samples in stream (0 == unknown) */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* +1A: MD5 signature (0 == none) */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 /* */ + /* +2A: start of stream data */ }; memcpy(decoder->custom_header, s_header_template, sizeof(s_header_template)); decoder->custom_header[0x08] = decoder->custom_header[0x0a] = block_size >> 8; @@ -118,7 +114,7 @@ int flac_decoder_reset(flac_decoder* decoder, uint32_t sample_rate, uint8_t num_ decoder->custom_header[0x13] = sample_rate >> 4; decoder->custom_header[0x14] = (sample_rate << 4) | ((num_channels - 1) << 1); - // configure the header ahead of the provided buffer + /* configure the header ahead of the provided buffer */ decoder->compressed_start = (const FLAC__byte *)(decoder->custom_header); decoder->compressed_length = sizeof(decoder->custom_header); decoder->compressed2_start = (const FLAC__byte *)(buffer); @@ -127,21 +123,22 @@ int flac_decoder_reset(flac_decoder* decoder, uint32_t sample_rate, uint8_t num_ } -//------------------------------------------------- -// decode_interleaved - decode to an interleaved -// sound stream -//------------------------------------------------- +/*------------------------------------------------- + * decode_interleaved - decode to an interleaved + * sound stream + *------------------------------------------------- + */ int flac_decoder_decode_interleaved(flac_decoder* decoder, int16_t *samples, uint32_t num_samples, int swap_endian) { - // configure the uncompressed buffer + /* configure the uncompressed buffer */ memset(decoder->uncompressed_start, 0, sizeof(decoder->uncompressed_start)); decoder->uncompressed_start[0] = samples; decoder->uncompressed_offset = 0; decoder->uncompressed_length = num_samples; decoder->uncompressed_swap = swap_endian; - // loop until we get everything we want + /* loop until we get everything we want */ while (decoder->uncompressed_offset < decoder->uncompressed_length) if (!FLAC__stream_decoder_process_single(decoder->decoder)) return 0; @@ -149,20 +146,22 @@ int flac_decoder_decode_interleaved(flac_decoder* decoder, int16_t *samples, uin } +#if 0 /* -//------------------------------------------------- -// decode - decode to an multiple independent -// data streams -//------------------------------------------------- + *------------------------------------------------- + * decode - decode to an multiple independent + * data streams + *------------------------------------------------- + */ bool flac_decoder::decode(int16_t **samples, uint32_t num_samples, bool swap_endian) { - // make sure we don't have too many channels + /* make sure we don't have too many channels */ int chans = channels(); if (chans > ARRAY_LENGTH(m_uncompressed_start)) return false; - // configure the uncompressed buffer + /* configure the uncompressed buffer */ memset(m_uncompressed_start, 0, sizeof(m_uncompressed_start)); for (int curchan = 0; curchan < chans; curchan++) m_uncompressed_start[curchan] = samples[curchan]; @@ -170,26 +169,27 @@ bool flac_decoder::decode(int16_t **samples, uint32_t num_samples, bool swap_end m_uncompressed_length = num_samples; m_uncompressed_swap = swap_endian; - // loop until we get everything we want + /* loop until we get everything we want */ while (m_uncompressed_offset < m_uncompressed_length) if (!FLAC__stream_decoder_process_single(m_decoder)) return false; return true; } -*/ +#endif -//------------------------------------------------- -// finish - finish up the decode -//------------------------------------------------- +/*------------------------------------------------- + * finish - finish up the decode + *------------------------------------------------- + */ uint32_t flac_decoder_finish(flac_decoder* decoder) { - // get the final decoding position and move forward + /* get the final decoding position and move forward */ FLAC__uint64 position = 0; FLAC__stream_decoder_get_decode_position(decoder->decoder, &position); FLAC__stream_decoder_finish(decoder->decoder); - // adjust position if we provided the header + /* adjust position if we provided the header */ if (position == 0) return 0; if (decoder->compressed_start == (const FLAC__byte *)(decoder->custom_header)) @@ -198,10 +198,11 @@ uint32_t flac_decoder_finish(flac_decoder* decoder) } -//------------------------------------------------- -// read_callback - handle reads from the input -// stream -//------------------------------------------------- +/*------------------------------------------------- + * read_callback - handle reads from the input + * stream + *------------------------------------------------- + */ #define MIN(x, y) ((x) < (y) ? (x) : (y)) @@ -216,7 +217,7 @@ FLAC__StreamDecoderReadStatus flac_decoder_read_callback(void* client_data, FLAC uint32_t expected = *bytes; - // copy from primary buffer first + /* copy from primary buffer first */ uint32_t outputpos = 0; if (outputpos < *bytes && decoder->compressed_offset < decoder->compressed_length) { @@ -226,7 +227,7 @@ FLAC__StreamDecoderReadStatus flac_decoder_read_callback(void* client_data, FLAC decoder->compressed_offset += bytes_to_copy; } - // once we're out of that, copy from the secondary buffer + /* once we're out of that, copy from the secondary buffer */ if (outputpos < *bytes && decoder->compressed_offset < decoder->compressed_length + decoder->compressed2_length) { uint32_t bytes_to_copy = MIN(*bytes - outputpos, decoder->compressed2_length - (decoder->compressed_offset - decoder->compressed_length)); @@ -236,23 +237,24 @@ FLAC__StreamDecoderReadStatus flac_decoder_read_callback(void* client_data, FLAC } *bytes = outputpos; - // return based on whether we ran out of data + /* return based on whether we ran out of data */ return (*bytes < expected) ? FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM : FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; } -//------------------------------------------------- -// metadata_callback - handle STREAMINFO metadata -//------------------------------------------------- +/*------------------------------------------------- + * metadata_callback - handle STREAMINFO metadata + *------------------------------------------------- + */ void flac_decoder_metadata_callback_static(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data) { flac_decoder *fldecoder; - // ignore all but STREAMINFO metadata + /* ignore all but STREAMINFO metadata */ if (metadata->type != FLAC__METADATA_TYPE_STREAMINFO) return; - // parse out the data we care about + /* parse out the data we care about */ fldecoder = (flac_decoder *)(client_data); fldecoder->sample_rate = metadata->data.stream_info.sample_rate; fldecoder->bits_per_sample = metadata->data.stream_info.bits_per_sample; @@ -260,10 +262,11 @@ void flac_decoder_metadata_callback_static(const FLAC__StreamDecoder *decoder, c } -//------------------------------------------------- -// tell_callback - handle requests to find out -// where in the input stream we are -//------------------------------------------------- +/*------------------------------------------------- + * tell_callback - handle requests to find out + * where in the input stream we are + *------------------------------------------------- + */ FLAC__StreamDecoderTellStatus flac_decoder_tell_callback_static(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data) { @@ -272,10 +275,11 @@ FLAC__StreamDecoderTellStatus flac_decoder_tell_callback_static(const FLAC__Stre } -//------------------------------------------------- -// write_callback - handle writes to the output -// stream -//------------------------------------------------- +/*------------------------------------------------- + * write_callback - handle writes to the output + * stream + *------------------------------------------------- + */ FLAC__StreamDecoderWriteStatus flac_decoder_write_callback_static(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data) { @@ -284,28 +288,26 @@ FLAC__StreamDecoderWriteStatus flac_decoder_write_callback_static(const FLAC__St FLAC__StreamDecoderWriteStatus flac_decoder_write_callback(void *client_data, const FLAC__Frame *frame, const FLAC__int32 * const buffer[]) { + int sampnum, chan; int shift, blocksize; flac_decoder * decoder = (flac_decoder *)client_data; - assert(frame->header.channels == channels(decoder)); + assert(frame->header.channels == decoder->channels); - // interleaved case + /* interleaved case */ shift = decoder->uncompressed_swap ? 8 : 0; blocksize = frame->header.blocksize; - if (decoder->uncompressed_start[1] == NULL) { - int sampnum, chan; int16_t *dest = decoder->uncompressed_start[0] + decoder->uncompressed_offset * frame->header.channels; for (sampnum = 0; sampnum < blocksize && decoder->uncompressed_offset < decoder->uncompressed_length; sampnum++, decoder->uncompressed_offset++) for (chan = 0; chan < frame->header.channels; chan++) *dest++ = (int16_t)((((uint16_t)buffer[chan][sampnum]) << shift) | (((uint16_t)buffer[chan][sampnum]) >> shift)); } - // non-interleaved case + /* non-interleaved case */ else { - int sampnum, chan; for (sampnum = 0; sampnum < blocksize && decoder->uncompressed_offset < decoder->uncompressed_length; sampnum++, decoder->uncompressed_offset++) for (chan = 0; chan < frame->header.channels; chan++) if (decoder->uncompressed_start[chan] != NULL) diff --git a/libretro-common/formats/libchdr/flac.h b/libretro-common/formats/libchdr/flac.h index 23e91a9ae3..dbb83584e1 100644 --- a/libretro-common/formats/libchdr/flac.h +++ b/libretro-common/formats/libchdr/flac.h @@ -14,7 +14,8 @@ #define __FLAC_H__ #include -#include "FLAC/all.h" +#include "FLAC/ordinals.h" +#include "FLAC/stream_decoder.h" //************************************************************************** // TYPE DEFINITIONS diff --git a/libretro-common/formats/libchdr/huffman.c b/libretro-common/formats/libchdr/huffman.c index d67ec19c96..861ae959db 100644 --- a/libretro-common/formats/libchdr/huffman.c +++ b/libretro-common/formats/libchdr/huffman.c @@ -1,6 +1,6 @@ -// license:BSD-3-Clause -// copyright-holders:Aaron Giles -/*************************************************************************** +/* license:BSD-3-Clause + * copyright-holders:Aaron Giles + *************************************************************************** huffman.c @@ -105,26 +105,28 @@ #define MAX(x,y) ((x) > (y) ? (x) : (y)) -//************************************************************************** -// MACROS -//************************************************************************** +/*************************************************************************** + * MACROS + *************************************************************************** + */ #define MAKE_LOOKUP(code,bits) (((code) << 5) | ((bits) & 0x1f)) -//************************************************************************** -// IMPLEMENTATION -//************************************************************************** +/*************************************************************************** + * IMPLEMENTATION + * ************************************************************************** + */ -//------------------------------------------------- -// huffman_context_base - create an encoding/ -// decoding context -//------------------------------------------------- +/*------------------------------------------------- + * huffman_context_base - create an encoding/ + * decoding context + *------------------------------------------------- + */ struct huffman_decoder* create_huffman_decoder(int numcodes, int maxbits) { struct huffman_decoder* decoder; - /* limit to 24 bits */ if (maxbits > 24) return NULL; @@ -140,10 +142,23 @@ struct huffman_decoder* create_huffman_decoder(int numcodes, int maxbits) return decoder; } -//------------------------------------------------- -// decode_one - decode a single code from the -// huffman stream -//------------------------------------------------- +void delete_huffman_decoder(struct huffman_decoder* decoder) +{ + if (decoder != NULL) + { + if (decoder->lookup != NULL) + free(decoder->lookup); + if (decoder->huffnode != NULL) + free(decoder->huffnode); + free(decoder); + } +} + +/*------------------------------------------------- + * decode_one - decode a single code from the + * huffman stream + *------------------------------------------------- + */ uint32_t huffman_decode_one(struct huffman_decoder* decoder, struct bitstream* bitbuf) { @@ -158,17 +173,18 @@ uint32_t huffman_decode_one(struct huffman_decoder* decoder, struct bitstream* b return lookup >> 5; } -//------------------------------------------------- -// import_tree_rle - import an RLE-encoded -// huffman tree from a source data stream -//------------------------------------------------- +/*------------------------------------------------- + * import_tree_rle - import an RLE-encoded + * huffman tree from a source data stream + *------------------------------------------------- + */ enum huffman_error huffman_import_tree_rle(struct huffman_decoder* decoder, struct bitstream* bitbuf) { enum huffman_error error; - int curnode; - // bits per entry depends on the maxbits + /* bits per entry depends on the maxbits */ int numbits; + int curnode; if (decoder->maxbits >= 16) numbits = 5; else if (decoder->maxbits >= 8) @@ -176,23 +192,23 @@ enum huffman_error huffman_import_tree_rle(struct huffman_decoder* decoder, stru else numbits = 3; - // loop until we read all the nodes + /* loop until we read all the nodes */ for (curnode = 0; curnode < decoder->numcodes; ) { - // a non-one value is just raw + /* a non-one value is just raw */ int nodebits = bitstream_read(bitbuf, numbits); if (nodebits != 1) decoder->huffnode[curnode++].numbits = nodebits; - // a one value is an escape code + /* a one value is an escape code */ else { - // a double 1 is just a single 1 + /* a double 1 is just a single 1 */ nodebits = bitstream_read(bitbuf, numbits); if (nodebits == 1) decoder->huffnode[curnode++].numbits = nodebits; - // otherwise, we need one for value for the repeat count + /* otherwise, we need one for value for the repeat count */ else { int repcount = bitstream_read(bitbuf, numbits) + 3; @@ -202,39 +218,39 @@ enum huffman_error huffman_import_tree_rle(struct huffman_decoder* decoder, stru } } - // make sure we ended up with the right number + /* make sure we ended up with the right number */ if (curnode != decoder->numcodes) return HUFFERR_INVALID_DATA; - // assign canonical codes for all nodes based on their code lengths + /* assign canonical codes for all nodes based on their code lengths */ error = huffman_assign_canonical_codes(decoder); if (error != HUFFERR_NONE) return error; - // build the lookup table + /* build the lookup table */ huffman_build_lookup_table(decoder); - // determine final input length and report errors + /* determine final input length and report errors */ return bitstream_overflow(bitbuf) ? HUFFERR_INPUT_BUFFER_TOO_SMALL : HUFFERR_NONE; } -//------------------------------------------------- -// import_tree_huffman - import a huffman-encoded -// huffman tree from a source data stream -//------------------------------------------------- +/*------------------------------------------------- + * import_tree_huffman - import a huffman-encoded + * huffman tree from a source data stream + *------------------------------------------------- + */ enum huffman_error huffman_import_tree_huffman(struct huffman_decoder* decoder, struct bitstream* bitbuf) { - int index; - int start; - int count = 0; - uint8_t rlefullbits = 0; int last = 0; int curcode; - enum huffman_error error; uint32_t temp; - // start by parsing the lengths for the small tree + enum huffman_error error; + uint8_t rlefullbits = 0; + int index, count = 0; + int start; + /* start by parsing the lengths for the small tree */ struct huffman_decoder* smallhuff = create_huffman_decoder(24, 6); smallhuff->huffnode[0].numbits = bitstream_read(bitbuf, 3); @@ -251,18 +267,18 @@ enum huffman_error huffman_import_tree_huffman(struct huffman_decoder* decoder, } } - // then regenerate the tree + /* then regenerate the tree */ error = huffman_assign_canonical_codes(smallhuff); if (error != HUFFERR_NONE) return error; huffman_build_lookup_table(smallhuff); - // determine the maximum length of an RLE count + /* determine the maximum length of an RLE count */ temp = decoder->numcodes - 9; while (temp != 0) temp >>= 1, rlefullbits++; - // now process the rest of the data + /* now process the rest of the data */ for (curcode = 0; curcode < decoder->numcodes; ) { int value = huffman_decode_one(smallhuff, bitbuf); @@ -278,52 +294,53 @@ enum huffman_error huffman_import_tree_huffman(struct huffman_decoder* decoder, } } - // make sure we ended up with the right number + /* make sure we ended up with the right number */ if (curcode != decoder->numcodes) return HUFFERR_INVALID_DATA; - // assign canonical codes for all nodes based on their code lengths + /* assign canonical codes for all nodes based on their code lengths */ error = huffman_assign_canonical_codes(decoder); if (error != HUFFERR_NONE) return error; - // build the lookup table + /* build the lookup table */ huffman_build_lookup_table(decoder); - // determine final input length and report errors + /* determine final input length and report errors */ return bitstream_overflow(bitbuf) ? HUFFERR_INPUT_BUFFER_TOO_SMALL : HUFFERR_NONE; } -//------------------------------------------------- -// compute_tree_from_histo - common backend for -// computing a tree based on the data histogram -//------------------------------------------------- +/*------------------------------------------------- + * compute_tree_from_histo - common backend for + * computing a tree based on the data histogram + *------------------------------------------------- + */ enum huffman_error huffman_compute_tree_from_histo(struct huffman_decoder* decoder) { - int i; + /* compute the number of data items in the histogram */ + int i; uint32_t upperweight; uint32_t lowerweight = 0; - // compute the number of data items in the histogram uint32_t sdatacount = 0; for (i = 0; i < decoder->numcodes; i++) sdatacount += decoder->datahisto[i]; - // binary search to achieve the optimum encoding + /* binary search to achieve the optimum encoding */ upperweight = sdatacount * 2; while (1) { - // build a tree using the current weight + /* build a tree using the current weight */ uint32_t curweight = (upperweight + lowerweight) / 2; int curmaxbits = huffman_build_tree(decoder, sdatacount, curweight); - // apply binary search here + /* apply binary search here */ if (curmaxbits <= decoder->maxbits) { lowerweight = curweight; - // early out if it worked with the raw weights, or if we're done searching + /* early out if it worked with the raw weights, or if we're done searching */ if (curweight == sdatacount || (upperweight - lowerweight) <= 1) break; } @@ -331,20 +348,22 @@ enum huffman_error huffman_compute_tree_from_histo(struct huffman_decoder* decod upperweight = curweight; } - // assign canonical codes for all nodes based on their code lengths + /* assign canonical codes for all nodes based on their code lengths */ return huffman_assign_canonical_codes(decoder); } -//************************************************************************** -// INTERNAL FUNCTIONS -//************************************************************************** +/*************************************************************************** + * INTERNAL FUNCTIONS + *************************************************************************** + */ -//------------------------------------------------- -// tree_node_compare - compare two tree nodes -// by weight -//------------------------------------------------- +/*------------------------------------------------- + * tree_node_compare - compare two tree nodes + * by weight + *------------------------------------------------- + */ static int huffman_tree_node_compare(const void *item1, const void *item2) { @@ -358,19 +377,19 @@ static int huffman_tree_node_compare(const void *item1, const void *item2) } -//------------------------------------------------- -// build_tree - build a huffman tree based on the -// data distribution -//------------------------------------------------- +/*------------------------------------------------- + * build_tree - build a huffman tree based on the + * data distribution + *------------------------------------------------- + */ int huffman_build_tree(struct huffman_decoder* decoder, uint32_t totaldata, uint32_t totalweight) { - int curcode; int nextalloc; int maxbits = 0; - // make a list of all non-zero nodes + /* make a list of all non-zero nodes */ struct node_t** list = (struct node_t**)malloc(sizeof(struct node_t*) * decoder->numcodes * 2); - int listitems = 0; + int curcode, listitems = 0; memset(decoder->huffnode, 0, decoder->numcodes * sizeof(decoder->huffnode[0])); for (curcode = 0; curcode < decoder->numcodes; curcode++) if (decoder->datahisto[curcode] != 0) @@ -379,7 +398,7 @@ int huffman_build_tree(struct huffman_decoder* decoder, uint32_t totaldata, uint decoder->huffnode[curcode].count = decoder->datahisto[curcode]; decoder->huffnode[curcode].bits = curcode; - // scale the weight by the current effective length, ensuring we don't go to 0 + /* scale the weight by the current effective length, ensuring we don't go to 0 */ decoder->huffnode[curcode].weight = ((uint64_t)decoder->datahisto[curcode]) * ((uint64_t)totalweight) / ((uint64_t)totaldata); if (decoder->huffnode[curcode].weight == 0) decoder->huffnode[curcode].weight = 1; @@ -390,7 +409,7 @@ int huffman_build_tree(struct huffman_decoder* decoder, uint32_t totaldata, uint fprintf(stderr, "weight: %d code: %d\n", list[i]->m_weight, list[i]->m_bits); } */ - // sort the list by weight, largest weight first + /* sort the list by weight, largest weight first */ qsort(&list[0], listitems, sizeof(list[0]), huffman_tree_node_compare); /* fprintf(stderr, "Post-sort:\n"); @@ -399,23 +418,23 @@ int huffman_build_tree(struct huffman_decoder* decoder, uint32_t totaldata, uint } fprintf(stderr, "===================\n"); */ - // now build the tree + /* now build the tree */ nextalloc = decoder->numcodes; while (listitems > 1) { int curitem; - // remove lowest two items + /* remove lowest two items */ struct node_t* node1 = &(*list[--listitems]); struct node_t* node0 = &(*list[--listitems]); - // create new node + /* create new node */ struct node_t* newnode = &decoder->huffnode[nextalloc++]; newnode->parent = NULL; node0->parent = node1->parent = newnode; newnode->weight = node0->weight + node1->weight; - // insert into list at appropriate location + /* insert into list at appropriate location */ for (curitem = 0; curitem < listitems; curitem++) if (newnode->weight > list[curitem]->weight) { @@ -426,9 +445,10 @@ int huffman_build_tree(struct huffman_decoder* decoder, uint32_t totaldata, uint listitems++; } - // compute the number of bits in each code, and fill in another histogram + /* compute the number of bits in each code, and fill in another histogram */ for (curcode = 0; curcode < decoder->numcodes; curcode++) { + struct node_t *curnode; struct node_t* node = &decoder->huffnode[curcode]; node->numbits = 0; node->bits = 0; @@ -436,14 +456,13 @@ int huffman_build_tree(struct huffman_decoder* decoder, uint32_t totaldata, uint // if we have a non-zero weight, compute the number of bits if (node->weight > 0) { - struct node_t *curnode; - // determine the number of bits for this node + /* determine the number of bits for this node */ for (curnode = node; curnode->parent != NULL; curnode = curnode->parent) node->numbits++; if (node->numbits == 0) node->numbits = 1; - // keep track of the max + /* keep track of the max */ maxbits = MAX(maxbits, ((int)node->numbits)); } } @@ -451,18 +470,18 @@ int huffman_build_tree(struct huffman_decoder* decoder, uint32_t totaldata, uint } -//------------------------------------------------- -// assign_canonical_codes - assign canonical codes -// to all the nodes based on the number of bits -// in each -//------------------------------------------------- +/*------------------------------------------------- + * assign_canonical_codes - assign canonical codes + * to all the nodes based on the number of bits + * in each + *------------------------------------------------- + */ enum huffman_error huffman_assign_canonical_codes(struct huffman_decoder* decoder) { - int curcode, codelen; uint32_t curstart = 0; - - // build up a histogram of bit lengths + /* build up a histogram of bit lengths */ + int curcode, codelen; uint32_t bithisto[33] = { 0 }; for (curcode = 0; curcode < decoder->numcodes; curcode++) { @@ -473,7 +492,7 @@ enum huffman_error huffman_assign_canonical_codes(struct huffman_decoder* decode bithisto[node->numbits]++; } - // for each code length, determine the starting code number + /* for each code length, determine the starting code number */ for (codelen = 32; codelen > 0; codelen--) { uint32_t nextstart = (curstart + bithisto[codelen]) >> 1; @@ -483,7 +502,7 @@ enum huffman_error huffman_assign_canonical_codes(struct huffman_decoder* decode curstart = nextstart; } - // now assign canonical codes + /* now assign canonical codes */ for (curcode = 0; curcode < decoder->numcodes; curcode++) { struct node_t* node = &decoder->huffnode[curcode]; @@ -494,33 +513,32 @@ enum huffman_error huffman_assign_canonical_codes(struct huffman_decoder* decode } -//------------------------------------------------- -// build_lookup_table - build a lookup table for -// fast decoding -//------------------------------------------------- +/*------------------------------------------------- + * build_lookup_table - build a lookup table for + * fast decoding + *------------------------------------------------- + */ void huffman_build_lookup_table(struct huffman_decoder* decoder) { - int curcode; - // iterate over all codes + /* iterate over all codes */ + int curcode; for (curcode = 0; curcode < decoder->numcodes; curcode++) { - // process all nodes which have non-zero bits + /* process all nodes which have non-zero bits */ struct node_t* node = &decoder->huffnode[curcode]; if (node->numbits > 0) { int shift; lookup_value *dest; lookup_value *destend; - - // set up the entry + /* set up the entry */ lookup_value value = MAKE_LOOKUP(curcode, node->numbits); - // fill all matching entries - shift = decoder->maxbits - node->numbits; - dest = &decoder->lookup[node->bits << shift]; + /* fill all matching entries */ + shift = decoder->maxbits - node->numbits; + dest = &decoder->lookup[node->bits << shift]; destend = &decoder->lookup[((node->bits + 1) << shift) - 1]; - while (dest <= destend) *dest++ = value; } diff --git a/libretro-common/formats/libchdr/huffman.h b/libretro-common/formats/libchdr/huffman.h index 71de399971..67467d5323 100644 --- a/libretro-common/formats/libchdr/huffman.h +++ b/libretro-common/formats/libchdr/huffman.h @@ -71,6 +71,7 @@ struct huffman_decoder // ======================> huffman_decoder struct huffman_decoder* create_huffman_decoder(int numcodes, int maxbits); +void delete_huffman_decoder(struct huffman_decoder* decoder); // single item operations uint32_t huffman_decode_one(struct huffman_decoder* decoder, struct bitstream* bitbuf);