Previous 15994 Revisions Next

r14636 Sunday 26th February, 2012 at 08:04:59 UTC by Miodrag Milanović
Sync with mame r15531 (nw)
[src/lib/util]cdrom.h chdcodec.c chdcodec.h
[src/tools]chdman.c

src/tools/chdman.c
r14635r14636
481481
482482
483483// default compressors
484static const chd_codec_type s_default_raw_compression[4] = { CHD_CODEC_LZMA, CHD_CODEC_ZLIB, CHD_CODEC_HUFFMAN };
485static const chd_codec_type s_default_hd_compression[4] = { CHD_CODEC_LZMA, CHD_CODEC_ZLIB, CHD_CODEC_HUFFMAN };
484static const chd_codec_type s_default_raw_compression[4] = { CHD_CODEC_LZMA, CHD_CODEC_ZLIB, CHD_CODEC_HUFFMAN, CHD_CODEC_FLAC };
485static const chd_codec_type s_default_hd_compression[4] = { CHD_CODEC_LZMA, CHD_CODEC_ZLIB, CHD_CODEC_HUFFMAN, CHD_CODEC_FLAC };
486486static const chd_codec_type s_default_cd_compression[4] = { CHD_CODEC_LZMA, CHD_CODEC_ZLIB, CHD_CODEC_HUFFMAN, CHD_CODEC_CD_FLAC };
487487static const chd_codec_type s_default_ld_compression[4] = { CHD_CODEC_AVHUFF };
488488
src/lib/util/cdrom.h
r14635r14636
5959#define CD_MAX_SUBCODE_DATA      (96)
6060
6161#define CD_FRAME_SIZE         (CD_MAX_SECTOR_DATA + CD_MAX_SUBCODE_DATA)
62#define CD_FRAMES_PER_HUNK      (4) // should be 8 for v5 CDs, with a 4-frame pad at the end to maintain SHA1 compatibility
62#define CD_FRAMES_PER_HUNK      (8)
6363
6464#define CD_METADATA_WORDS      (1+(CD_MAX_TRACKS * 6))
6565
src/lib/util/chdcodec.c
r14635r14636
225225{
226226public:
227227   // construction/destruction
228   chd_flac_compressor(chd_file &chd, bool lossy, bool bigendian);
228   chd_flac_compressor(chd_file &chd, bool lossy);
229229
230230   // core functionality
231231   virtual UINT32 compress(const UINT8 *src, UINT32 srclen, UINT8 *dest);
232232
233   // static helpers
234   static UINT32 blocksize(UINT32 bytes);
235
233236private:
234237   // internal state
235   bool         m_swap_endian;
238   bool         m_big_endian;
236239   flac_encoder   m_encoder;
237240};
238241
239// big-endian variant
240class chd_flac_compressor_be : public chd_flac_compressor
241{
242public:
243   // construction/destruction
244   chd_flac_compressor_be(chd_file &chd, bool lossy)
245      : chd_flac_compressor(chd, lossy, true) { }
246};
247242
248// little-endian variant
249class chd_flac_compressor_le : public chd_flac_compressor
250{
251public:
252   // construction/destruction
253   chd_flac_compressor_le(chd_file &chd, bool lossy)
254      : chd_flac_compressor(chd, lossy, false) { }
255};
256
257
258243// ======================> chd_flac_decompressor
259244
260245// FLAC decompressor
r14635r14636
262247{
263248public:
264249   // construction/destruction
265   chd_flac_decompressor(chd_file &chd, bool lossy, bool bigendian);
250   chd_flac_decompressor(chd_file &chd, bool lossy);
266251
267252   // core functionality
268253   virtual void decompress(const UINT8 *src, UINT32 complen, UINT8 *dest, UINT32 destlen);
269254
270255private:
271256   // internal state
272   bool         m_swap_endian;
257   bool         m_big_endian;
273258   flac_decoder   m_decoder;
274259};
275260
276// big-endian variant
277class chd_flac_decompressor_be : public chd_flac_decompressor
278{
279public:
280   // construction/destruction
281   chd_flac_decompressor_be(chd_file &chd, bool lossy)
282      : chd_flac_decompressor(chd, lossy, true) { }
283};
284261
285// little-endian variant
286class chd_flac_decompressor_le : public chd_flac_decompressor
287{
288public:
289   // construction/destruction
290   chd_flac_decompressor_le(chd_file &chd, bool lossy)
291      : chd_flac_decompressor(chd, lossy, false) { }
292};
293
294
295262// ======================> chd_cd_flac_compressor
296263
297264// CD/FLAC compressor
r14635r14636
305272   // core functionality
306273   virtual UINT32 compress(const UINT8 *src, UINT32 srclen, UINT8 *dest);
307274
275   // static helpers
276   static UINT32 blocksize(UINT32 bytes);
277
308278private:
309279   // internal state
310280   bool            m_swap_endian;
r14635r14636
390360   { CHD_CODEC_ZLIB,      false,   "Deflate",            &chd_codec_list::construct_compressor<chd_zlib_compressor>,      &chd_codec_list::construct_decompressor<chd_zlib_decompressor> },
391361   { CHD_CODEC_LZMA,      false,   "LZMA",               &chd_codec_list::construct_compressor<chd_lzma_compressor>,      &chd_codec_list::construct_decompressor<chd_lzma_decompressor> },
392362   { CHD_CODEC_HUFFMAN,   false,   "Huffman",            &chd_codec_list::construct_compressor<chd_huffman_compressor>,   &chd_codec_list::construct_decompressor<chd_huffman_decompressor> },
393   { CHD_CODEC_FLAC_BE,   false,   "FLAC, big-endian",      &chd_codec_list::construct_compressor<chd_flac_compressor_be>,   &chd_codec_list::construct_decompressor<chd_flac_decompressor_be> },
394   { CHD_CODEC_FLAC_LE,   false,   "FLAC, little-endian",   &chd_codec_list::construct_compressor<chd_flac_compressor_le>,   &chd_codec_list::construct_decompressor<chd_flac_decompressor_le> },
363   { CHD_CODEC_FLAC,      false,   "FLAC",               &chd_codec_list::construct_compressor<chd_flac_compressor>,      &chd_codec_list::construct_decompressor<chd_flac_decompressor> },
395364   { CHD_CODEC_CD_FLAC,   false,   "CD FLAC",            &chd_codec_list::construct_compressor<chd_cd_flac_compressor>,   &chd_codec_list::construct_decompressor<chd_cd_flac_decompressor> },
396365   { CHD_CODEC_AVHUFF,      false,   "A/V Huffman",         &chd_codec_list::construct_compressor<chd_avhuff_compressor>,   &chd_codec_list::construct_decompressor<chd_avhuff_decompressor> },
397366};
r14635r14636
11571126//  chd_flac_compressor - constructor
11581127//-------------------------------------------------
11591128
1160chd_flac_compressor::chd_flac_compressor(chd_file &chd, bool lossy, bool bigendian)
1129chd_flac_compressor::chd_flac_compressor(chd_file &chd, bool lossy)
11611130   : chd_compressor(chd, lossy)
11621131{
11631132   // determine whether we want native or swapped samples
11641133   UINT16 native_endian = 0;
11651134   *reinterpret_cast<UINT8 *>(&native_endian) = 1;
1166   if (native_endian == 1)
1167      m_swap_endian = bigendian;
1168   else
1169      m_swap_endian = !bigendian;
1170
1135   m_big_endian = (native_endian == 0x100);
1136     
11711137   // configure the encoder
11721138   m_encoder.set_sample_rate(44100);
11731139   m_encoder.set_num_channels(2);
1174   m_encoder.set_block_size(chd.hunk_bytes() / 4);
1140   m_encoder.set_block_size(blocksize(chd.hunk_bytes()));
11751141   m_encoder.set_strip_metadata(true);
11761142}
11771143
r14635r14636
11821148
11831149UINT32 chd_flac_compressor::compress(const UINT8 *src, UINT32 srclen, UINT8 *dest)
11841150{
1185   // reset and encode
1186   m_encoder.reset(dest, chd().hunk_bytes());
1187   if (!m_encoder.encode_interleaved(reinterpret_cast<const INT16 *>(src), srclen / 4, m_swap_endian))
1151   // reset and encode big-endian
1152   m_encoder.reset(dest + 1, chd().hunk_bytes() - 1);
1153   if (!m_encoder.encode_interleaved(reinterpret_cast<const INT16 *>(src), srclen / 4, !m_big_endian))
11881154      throw CHDERR_COMPRESSION_ERROR;
1189
1190   // finish up
1191   UINT32 complen = m_encoder.finish();
1192   if (complen >= chd().hunk_bytes())
1155   UINT32 complen_be = m_encoder.finish();
1156   
1157   // reset and encode little-endian
1158   m_encoder.reset(dest + 1, chd().hunk_bytes() - 1);
1159   if (!m_encoder.encode_interleaved(reinterpret_cast<const INT16 *>(src), srclen / 4, m_big_endian))
11931160      throw CHDERR_COMPRESSION_ERROR;
1194   return complen;
1161   UINT32 complen_le = m_encoder.finish();
1162   
1163   // pick the best one and add a byte
1164   UINT32 complen = MIN(complen_le, complen_be);
1165   if (complen + 1 >= chd().hunk_bytes())
1166      throw CHDERR_COMPRESSION_ERROR;
1167   
1168   // if big-endian was better, re-do it
1169   dest[0] = 'L';
1170   if (complen != complen_le)
1171   {
1172      dest[0] = 'B';
1173      m_encoder.reset(dest + 1, chd().hunk_bytes() - 1);
1174      if (!m_encoder.encode_interleaved(reinterpret_cast<const INT16 *>(src), srclen / 4, !m_big_endian))
1175         throw CHDERR_COMPRESSION_ERROR;
1176      m_encoder.finish();
1177   }
1178   return complen + 1;
11951179}
11961180
11971181
1182//-------------------------------------------------
1183//  blocksize - return the optimal block size
1184//-------------------------------------------------
11981185
1186UINT32 chd_flac_compressor::blocksize(UINT32 bytes)
1187{
1188   // determine FLAC block size, which must be 16-65535
1189   // clamp to 2k since that's supposed to be the sweet spot
1190   UINT32 blocksize = bytes / 4;
1191   while (blocksize > 2048)
1192      blocksize /= 2;
1193   return blocksize;
1194}
1195
1196
1197
11991198//**************************************************************************
12001199//  FLAC DECOMPRESSOR
12011200//**************************************************************************
r14635r14636
12041203//  chd_flac_decompressor - constructor
12051204//-------------------------------------------------
12061205
1207chd_flac_decompressor::chd_flac_decompressor(chd_file &chd, bool lossy, bool bigendian)
1206chd_flac_decompressor::chd_flac_decompressor(chd_file &chd, bool lossy)
12081207   : chd_decompressor(chd, lossy)
12091208{
12101209   // determine whether we want native or swapped samples
12111210   UINT16 native_endian = 0;
12121211   *reinterpret_cast<UINT8 *>(&native_endian) = 1;
1213   if (native_endian == 1)
1214      m_swap_endian = bigendian;
1215   else
1216      m_swap_endian = !bigendian;
1212   m_big_endian = (native_endian == 0x100);
12171213}
12181214
12191215
r14635r14636
12241220
12251221void chd_flac_decompressor::decompress(const UINT8 *src, UINT32 complen, UINT8 *dest, UINT32 destlen)
12261222{
1223   // determine the endianness
1224   bool swap_endian;
1225   if (src[0] == 'L')
1226      swap_endian = m_big_endian;
1227   else if (src[0] == 'B')
1228      swap_endian = !m_big_endian;
1229   else
1230      throw CHDERR_DECOMPRESSION_ERROR;
1231
12271232   // reset and decode
1228   if (!m_decoder.reset(44100, 2, chd().hunk_bytes() / 4, src, complen))
1233   if (!m_decoder.reset(44100, 2, chd_flac_compressor::blocksize(destlen), src + 1, complen - 1))
12291234      throw CHDERR_DECOMPRESSION_ERROR;
1230   if (!m_decoder.decode_interleaved(reinterpret_cast<INT16 *>(dest), destlen / 4, m_swap_endian))
1235   if (!m_decoder.decode_interleaved(reinterpret_cast<INT16 *>(dest), destlen / 4, swap_endian))
12311236      throw CHDERR_DECOMPRESSION_ERROR;
12321237
12331238   // finish up
r14635r14636
12601265   // configure the encoder
12611266   m_encoder.set_sample_rate(44100);
12621267   m_encoder.set_num_channels(2);
1263   m_encoder.set_block_size((chd.hunk_bytes() / CD_FRAME_SIZE) * (CD_MAX_SECTOR_DATA/4));
1268   m_encoder.set_block_size(blocksize((chd.hunk_bytes() / CD_FRAME_SIZE) * CD_MAX_SECTOR_DATA));
12641269   m_encoder.set_strip_metadata(true);
12651270
12661271   // initialize the deflater
r14635r14636
13331338}
13341339
13351340
1341//-------------------------------------------------
1342//  blocksize - return the optimal block size
1343//-------------------------------------------------
13361344
1345UINT32 chd_cd_flac_compressor::blocksize(UINT32 bytes)
1346{
1347   // for CDs it seems that CD_MAX_SECTOR_DATA is the right target
1348   UINT32 blocksize = bytes / 4;
1349   while (blocksize > CD_MAX_SECTOR_DATA)
1350      blocksize /= 2;
1351   return blocksize;
1352}
1353
1354
1355
13371356//**************************************************************************
13381357//  CD FLAC DECOMPRESSOR
13391358//**************************************************************************
r14635r14636
13871406void chd_cd_flac_decompressor::decompress(const UINT8 *src, UINT32 complen, UINT8 *dest, UINT32 destlen)
13881407{
13891408   // reset and decode
1390   UINT32 frames = chd().hunk_bytes() / CD_FRAME_SIZE;
1391   if (!m_decoder.reset(44100, 2, frames * CD_MAX_SECTOR_DATA/4, src, complen))
1409   UINT32 frames = destlen / CD_FRAME_SIZE;
1410   if (!m_decoder.reset(44100, 2, chd_cd_flac_compressor::blocksize(frames * CD_MAX_SECTOR_DATA), src, complen))
13921411      throw CHDERR_DECOMPRESSION_ERROR;
13931412   UINT8 *buffer = m_buffer;
13941413   if (!m_decoder.decode_interleaved(reinterpret_cast<INT16 *>(buffer), frames * CD_MAX_SECTOR_DATA/4, m_swap_endian))
src/lib/util/chdcodec.h
r14635r14636
198198const chd_codec_type CHD_CODEC_ZLIB    = CHD_MAKE_TAG('z','l','i','b');
199199const chd_codec_type CHD_CODEC_LZMA    = CHD_MAKE_TAG('l','z','m','a');
200200const chd_codec_type CHD_CODEC_HUFFMAN   = CHD_MAKE_TAG('h','u','f','f');
201const chd_codec_type CHD_CODEC_FLAC_BE   = CHD_MAKE_TAG('f','l','c','b');
202const chd_codec_type CHD_CODEC_FLAC_LE   = CHD_MAKE_TAG('f','l','c','l');
201const chd_codec_type CHD_CODEC_FLAC      = CHD_MAKE_TAG('f','l','a','c');
203202const chd_codec_type CHD_CODEC_CD_FLAC   = CHD_MAKE_TAG('c','d','f','l');
204203const chd_codec_type CHD_CODEC_AVHUFF   = CHD_MAKE_TAG('a','v','h','u');
205204

Previous 15994 Revisions Next


© 1998-2018 The MESS Team