Previous 15994 Revisions Next

r14498 Friday 17th February, 2012 at 16:23:56 UTC by Miodrag Milanović
Sync with MAME r15416 (no whatsnew)
[src/lib/util]chdcodec.c chdcodec.h flac.c flac.h
[src/tools]chdman.c

src/tools/chdman.c
r14497r14498
478478// default compressors
479479static const chd_codec_type s_default_raw_compression[4] = { CHD_CODEC_LZMA, CHD_CODEC_ZLIB, CHD_CODEC_HUFFMAN };
480480static const chd_codec_type s_default_hd_compression[4] = { CHD_CODEC_LZMA, CHD_CODEC_ZLIB, CHD_CODEC_HUFFMAN };
481static const chd_codec_type s_default_cd_compression[4] = { CHD_CODEC_LZMA, CHD_CODEC_ZLIB, CHD_CODEC_HUFFMAN, CHD_CODEC_FLAC_BE };
481static const chd_codec_type s_default_cd_compression[4] = { CHD_CODEC_LZMA, CHD_CODEC_ZLIB, CHD_CODEC_HUFFMAN, CHD_CODEC_CD_FLAC };
482482static const chd_codec_type s_default_ld_compression[4] = { CHD_CODEC_AVHUFF };
483483
484484
r14497r14498
634634         REQUIRED OPTION_OUTPUT,
635635         OPTION_OUTPUT_BIN,
636636         OPTION_OUTPUT_FORCE,
637         REQUIRED OPTION_INPUT
637         REQUIRED OPTION_INPUT,
638638         OPTION_INPUT_PARENT,
639639      }
640640   },
src/lib/util/flac.c
r14497r14498
367367bool flac_decoder::reset()
368368{
369369   m_compressed_offset = 0;
370   if (FLAC__stream_decoder_init_stream(m_decoder, &flac_decoder::read_callback_static, NULL, NULL, NULL, NULL, &flac_decoder::write_callback_static, NULL, &flac_decoder::error_callback_static, this) != FLAC__STREAM_DECODER_INIT_STATUS_OK)
370   if (FLAC__stream_decoder_init_stream(m_decoder, &flac_decoder::read_callback_static, NULL, &flac_decoder::tell_callback_static, NULL, NULL, &flac_decoder::write_callback_static, NULL, &flac_decoder::error_callback_static, this) != FLAC__STREAM_DECODER_INIT_STATUS_OK)
371371      return false;
372372   return FLAC__stream_decoder_process_until_end_of_metadata(m_decoder);
373373}
r14497r14498
499499//  finish - finish up the decode
500500//-------------------------------------------------
501501
502void flac_decoder::finish()
502UINT32 flac_decoder::finish()
503503{
504   // get the final decoding position and move forward
505   FLAC__uint64 position = 0;
506   FLAC__stream_decoder_get_decode_position(m_decoder, &position);
504507   FLAC__stream_decoder_finish(m_decoder);
508   
509   // adjust position if we provided the header
510   if (position == 0)
511      return 0;
512   if (m_compressed_start == reinterpret_cast<const FLAC__byte *>(m_custom_header))
513      position -= m_compressed_length;
514   return position;
505515}
506516
507517
r14497r14498
553563
554564
555565//-------------------------------------------------
566//  tell_callback - handle requests to find out
567//  where in the input stream we are
568//-------------------------------------------------
569
570FLAC__StreamDecoderTellStatus flac_decoder::tell_callback_static(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
571{
572   *absolute_byte_offset = reinterpret_cast<flac_decoder *>(client_data)->m_compressed_offset;
573   return FLAC__STREAM_DECODER_TELL_STATUS_OK;
574}
575
576
577//-------------------------------------------------
556578//  write_callback - handle writes to the output
557579//  stream
558580//-------------------------------------------------
src/lib/util/flac.h
r14497r14498
138138   bool decode(INT16 **samples, UINT32 num_samples, bool swap_endian = false);
139139
140140   // finish up
141   void finish();
141   UINT32 finish();
142142
143143private:
144144   // internal helpers
145145   static FLAC__StreamDecoderReadStatus read_callback_static(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
146146   FLAC__StreamDecoderReadStatus read_callback(FLAC__byte buffer[], size_t *bytes);
147   static FLAC__StreamDecoderTellStatus tell_callback_static(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
147148   static FLAC__StreamDecoderWriteStatus write_callback_static(const FLAC__StreamDecoder *decoder, const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
148149   FLAC__StreamDecoderWriteStatus write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[]);
149150   static void error_callback_static(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
src/lib/util/chdcodec.c
r14497r14498
4141#include "hashing.h"
4242#include "avhuff.h"
4343#include "flac.h"
44#include "cdrom.h"
4445#include <zlib.h>
4546#include "lib7z/LzmaEnc.h"
4647#include "lib7z/LzmaDec.h"
r14497r14498
291292};
292293
293294
295// ======================> chd_cd_flac_compressor
296
297// CD/FLAC compressor
298class chd_cd_flac_compressor : public chd_compressor
299{
300public:
301   // construction/destruction
302   chd_cd_flac_compressor(chd_file &chd, bool lossy);
303   ~chd_cd_flac_compressor();
304
305   // core functionality
306   virtual UINT32 compress(const UINT8 *src, UINT32 srclen, UINT8 *dest);
307
308private:
309   // internal state
310   bool            m_swap_endian;
311   flac_encoder      m_encoder;
312   z_stream         m_deflater;
313   chd_zlib_allocator   m_allocator;
314   dynamic_buffer      m_buffer;
315};
316
317
318// ======================> chd_cd_flac_decompressor
319
320// FLAC decompressor
321class chd_cd_flac_decompressor : public chd_decompressor
322{
323public:
324   // construction/destruction
325   chd_cd_flac_decompressor(chd_file &chd, bool lossy);
326   ~chd_cd_flac_decompressor();
327
328   // core functionality
329   virtual void decompress(const UINT8 *src, UINT32 complen, UINT8 *dest, UINT32 destlen);
330
331private:
332   // internal state
333   bool            m_swap_endian;
334   flac_decoder      m_decoder;
335   z_stream         m_inflater;
336   chd_zlib_allocator   m_allocator;
337   dynamic_buffer      m_buffer;
338};
339
340
294341// ======================> chd_avhuff_compressor
295342
296343// A/V compressor
r14497r14498
345392   { CHD_CODEC_HUFFMAN,   false,   "Huffman",            &chd_codec_list::construct_compressor<chd_huffman_compressor>,   &chd_codec_list::construct_decompressor<chd_huffman_decompressor> },
346393   { 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> },
347394   { 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> },
395   { 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> },
348396   { CHD_CODEC_AVHUFF,      false,   "A/V Huffman",         &chd_codec_list::construct_compressor<chd_avhuff_compressor>,   &chd_codec_list::construct_decompressor<chd_avhuff_decompressor> },
349397};
350398
r14497r14498
759807chd_zlib_decompressor::chd_zlib_decompressor(chd_file &chd, bool lossy)
760808   : chd_decompressor(chd, lossy)
761809{
762   // init the inflater first
810   // init the inflater
763811   m_inflater.next_in = (Bytef *)this;   // bogus, but that's ok
764812   m_inflater.avail_in = 0;
765813   m_allocator.install(m_inflater);
r14497r14498
11491197
11501198
11511199//**************************************************************************
1152//  FLAC COMPRESSOR
1200//  FLAC DECOMPRESSOR
11531201//**************************************************************************
11541202
11551203//-------------------------------------------------
r14497r14498
11891237
11901238
11911239//**************************************************************************
1240//  CD FLAC COMPRESSOR
1241//**************************************************************************
1242
1243//-------------------------------------------------
1244//  chd_cd_flac_compressor - constructor
1245//-------------------------------------------------
1246
1247chd_cd_flac_compressor::chd_cd_flac_compressor(chd_file &chd, bool lossy)
1248   : chd_compressor(chd, lossy),
1249     m_buffer(chd.hunk_bytes())
1250{
1251   // determine whether we want native or swapped samples
1252   UINT16 native_endian = 0;
1253   *reinterpret_cast<UINT8 *>(&native_endian) = 1;
1254   m_swap_endian = (native_endian == 1);
1255   
1256   // configure the encoder
1257   m_encoder.set_sample_rate(44100);
1258   m_encoder.set_num_channels(2);
1259   m_encoder.set_block_size((chd.hunk_bytes() / CD_FRAME_SIZE) * (CD_MAX_SECTOR_DATA/4));
1260   m_encoder.set_strip_metadata(true);
1261
1262   // initialize the deflater
1263   m_deflater.next_in = (Bytef *)this;   // bogus, but that's ok
1264   m_deflater.avail_in = 0;
1265   m_allocator.install(m_deflater);
1266   int zerr = deflateInit2(&m_deflater, Z_BEST_COMPRESSION, Z_DEFLATED, -MAX_WBITS, 8, Z_DEFAULT_STRATEGY);
1267
1268   // convert errors
1269   if (zerr == Z_MEM_ERROR)
1270      throw std::bad_alloc();
1271   else if (zerr != Z_OK)
1272      throw CHDERR_CODEC_ERROR;
1273}
1274
1275
1276//-------------------------------------------------
1277//  ~chd_cd_flac_compressor - destructor
1278//-------------------------------------------------
1279
1280chd_cd_flac_compressor::~chd_cd_flac_compressor()
1281{
1282   deflateEnd(&m_deflater);
1283}
1284
1285
1286//-------------------------------------------------
1287//  compress - compress data using the FLAC codec,
1288//  and use zlib on the subcode data
1289//-------------------------------------------------
1290
1291UINT32 chd_cd_flac_compressor::compress(const UINT8 *src, UINT32 srclen, UINT8 *dest)
1292{
1293   // copy audio data followed by subcode data
1294   UINT32 frames = chd().hunk_bytes() / CD_FRAME_SIZE;
1295   for (UINT32 framenum = 0; framenum < frames; framenum++)
1296   {
1297      memcpy(&m_buffer[framenum * CD_MAX_SECTOR_DATA], &src[framenum * CD_FRAME_SIZE], CD_MAX_SECTOR_DATA);
1298      memcpy(&m_buffer[frames * CD_MAX_SECTOR_DATA + framenum * CD_MAX_SUBCODE_DATA], &src[framenum * CD_FRAME_SIZE + CD_MAX_SECTOR_DATA], CD_MAX_SUBCODE_DATA);
1299   }
1300
1301   // reset and encode the audio portion
1302   m_encoder.reset(dest, chd().hunk_bytes());
1303   UINT8 *buffer = m_buffer;
1304   if (!m_encoder.encode_interleaved(reinterpret_cast<INT16 *>(buffer), frames * CD_MAX_SECTOR_DATA/4, m_swap_endian))
1305      throw CHDERR_COMPRESSION_ERROR;
1306
1307   // finish up
1308   UINT32 complen = m_encoder.finish();
1309   
1310   // deflate the subcode data
1311   m_deflater.next_in = const_cast<Bytef *>(&m_buffer[frames * CD_MAX_SECTOR_DATA]);
1312   m_deflater.avail_in = frames * CD_MAX_SUBCODE_DATA;
1313   m_deflater.total_in = 0;
1314   m_deflater.next_out = &dest[complen];
1315   m_deflater.avail_out = chd().hunk_bytes() - complen;
1316   m_deflater.total_out = 0;
1317   int zerr = deflateReset(&m_deflater);
1318   if (zerr != Z_OK)
1319      throw CHDERR_COMPRESSION_ERROR;
1320
1321   // do it
1322   zerr = deflate(&m_deflater, Z_FINISH);
1323
1324   // if we ended up with more data than we started with, return an error
1325   complen += m_deflater.total_out;
1326   if (zerr != Z_STREAM_END || complen >= srclen)
1327      throw CHDERR_COMPRESSION_ERROR;
1328   return complen;
1329}
1330
1331
1332
1333//**************************************************************************
1334//  CD FLAC DECOMPRESSOR
1335//**************************************************************************
1336
1337//-------------------------------------------------
1338//  chd_cd_flac_decompressor - constructor
1339//-------------------------------------------------
1340
1341chd_cd_flac_decompressor::chd_cd_flac_decompressor(chd_file &chd, bool lossy)
1342   : chd_decompressor(chd, lossy),
1343     m_buffer(chd.hunk_bytes())
1344{
1345   // determine whether we want native or swapped samples
1346   UINT16 native_endian = 0;
1347   *reinterpret_cast<UINT8 *>(&native_endian) = 1;
1348   m_swap_endian = (native_endian == 1);
1349
1350   // init the inflater
1351   m_inflater.next_in = (Bytef *)this;   // bogus, but that's ok
1352   m_inflater.avail_in = 0;
1353   m_allocator.install(m_inflater);
1354   int zerr = inflateInit2(&m_inflater, -MAX_WBITS);
1355
1356   // convert errors
1357   if (zerr == Z_MEM_ERROR)
1358      throw std::bad_alloc();
1359   else if (zerr != Z_OK)
1360      throw CHDERR_CODEC_ERROR;
1361}
1362
1363
1364//-------------------------------------------------
1365//  ~chd_cd_flac_decompressor - destructor
1366//-------------------------------------------------
1367
1368chd_cd_flac_decompressor::~chd_cd_flac_decompressor()
1369{
1370   inflateEnd(&m_inflater);
1371}
1372
1373
1374//-------------------------------------------------
1375//  decompress - decompress data using the FLAC
1376//  codec
1377//-------------------------------------------------
1378
1379void chd_cd_flac_decompressor::decompress(const UINT8 *src, UINT32 complen, UINT8 *dest, UINT32 destlen)
1380{
1381   // reset and decode
1382   UINT32 frames = chd().hunk_bytes() / CD_FRAME_SIZE;
1383   if (!m_decoder.reset(44100, 2, frames * CD_MAX_SECTOR_DATA/4, src, complen))
1384      throw CHDERR_DECOMPRESSION_ERROR;
1385   UINT8 *buffer = m_buffer;
1386   if (!m_decoder.decode_interleaved(reinterpret_cast<INT16 *>(buffer), frames * CD_MAX_SECTOR_DATA/4, m_swap_endian))
1387      throw CHDERR_DECOMPRESSION_ERROR;
1388
1389   // inflate the subcode data
1390   UINT32 offset = m_decoder.finish();
1391   m_inflater.next_in = const_cast<Bytef *>(src + offset);
1392   m_inflater.avail_in = complen - offset;
1393   m_inflater.total_in = 0;
1394   m_inflater.next_out = &m_buffer[frames * CD_MAX_SECTOR_DATA];
1395   m_inflater.avail_out = frames * CD_MAX_SUBCODE_DATA;
1396   m_inflater.total_out = 0;
1397   int zerr = inflateReset(&m_inflater);
1398   if (zerr != Z_OK)
1399      throw CHDERR_DECOMPRESSION_ERROR;
1400
1401   // do it
1402   zerr = inflate(&m_inflater, Z_FINISH);
1403   if (m_inflater.total_out != frames * CD_MAX_SUBCODE_DATA)
1404      throw CHDERR_DECOMPRESSION_ERROR;
1405   
1406   // reassemble the data
1407   for (UINT32 framenum = 0; framenum < frames; framenum++)
1408   {
1409      memcpy(&dest[framenum * CD_FRAME_SIZE], &m_buffer[framenum * CD_MAX_SECTOR_DATA], CD_MAX_SECTOR_DATA);
1410      memcpy(&dest[framenum * CD_FRAME_SIZE + CD_MAX_SECTOR_DATA], &m_buffer[frames * CD_MAX_SECTOR_DATA + framenum * CD_MAX_SUBCODE_DATA], CD_MAX_SUBCODE_DATA);
1411   }
1412}
1413
1414
1415
1416//**************************************************************************
11921417//  AVHUFF COMPRESSOR
11931418//**************************************************************************
11941419
src/lib/util/chdcodec.h
r14497r14498
200200const chd_codec_type CHD_CODEC_HUFFMAN    = CHD_MAKE_TAG('h','u','f','f');
201201const chd_codec_type CHD_CODEC_FLAC_BE    = CHD_MAKE_TAG('f','l','c','b');
202202const chd_codec_type CHD_CODEC_FLAC_LE    = CHD_MAKE_TAG('f','l','c','l');
203const chd_codec_type CHD_CODEC_CD_FLAC    = CHD_MAKE_TAG('c','d','f','l');
203204const chd_codec_type CHD_CODEC_AVHUFF   = CHD_MAKE_TAG('a','v','h','u');
204205
205206// A/V codec configuration parameters

Previous 15994 Revisions Next


© 1998-2018 The MESS Team