src/lib/util/flac.c
| r14497 | r14498 | |
| 367 | 367 | bool flac_decoder::reset() |
| 368 | 368 | { |
| 369 | 369 | 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) |
| 371 | 371 | return false; |
| 372 | 372 | return FLAC__stream_decoder_process_until_end_of_metadata(m_decoder); |
| 373 | 373 | } |
| r14497 | r14498 | |
| 499 | 499 | // finish - finish up the decode |
| 500 | 500 | //------------------------------------------------- |
| 501 | 501 | |
| 502 | | void flac_decoder::finish() |
| 502 | UINT32 flac_decoder::finish() |
| 503 | 503 | { |
| 504 | // get the final decoding position and move forward |
| 505 | FLAC__uint64 position = 0; |
| 506 | FLAC__stream_decoder_get_decode_position(m_decoder, &position); |
| 504 | 507 | 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; |
| 505 | 515 | } |
| 506 | 516 | |
| 507 | 517 | |
| r14497 | r14498 | |
| 553 | 563 | |
| 554 | 564 | |
| 555 | 565 | //------------------------------------------------- |
| 566 | // tell_callback - handle requests to find out |
| 567 | // where in the input stream we are |
| 568 | //------------------------------------------------- |
| 569 | |
| 570 | FLAC__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 | //------------------------------------------------- |
| 556 | 578 | // write_callback - handle writes to the output |
| 557 | 579 | // stream |
| 558 | 580 | //------------------------------------------------- |
src/lib/util/chdcodec.c
| r14497 | r14498 | |
| 41 | 41 | #include "hashing.h" |
| 42 | 42 | #include "avhuff.h" |
| 43 | 43 | #include "flac.h" |
| 44 | #include "cdrom.h" |
| 44 | 45 | #include <zlib.h> |
| 45 | 46 | #include "lib7z/LzmaEnc.h" |
| 46 | 47 | #include "lib7z/LzmaDec.h" |
| r14497 | r14498 | |
| 291 | 292 | }; |
| 292 | 293 | |
| 293 | 294 | |
| 295 | // ======================> chd_cd_flac_compressor |
| 296 | |
| 297 | // CD/FLAC compressor |
| 298 | class chd_cd_flac_compressor : public chd_compressor |
| 299 | { |
| 300 | public: |
| 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 | |
| 308 | private: |
| 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 |
| 321 | class chd_cd_flac_decompressor : public chd_decompressor |
| 322 | { |
| 323 | public: |
| 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 | |
| 331 | private: |
| 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 | |
| 294 | 341 | // ======================> chd_avhuff_compressor |
| 295 | 342 | |
| 296 | 343 | // A/V compressor |
| r14497 | r14498 | |
| 345 | 392 | { CHD_CODEC_HUFFMAN, false, "Huffman", &chd_codec_list::construct_compressor<chd_huffman_compressor>, &chd_codec_list::construct_decompressor<chd_huffman_decompressor> }, |
| 346 | 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> }, |
| 347 | 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> }, |
| 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> }, |
| 348 | 396 | { CHD_CODEC_AVHUFF, false, "A/V Huffman", &chd_codec_list::construct_compressor<chd_avhuff_compressor>, &chd_codec_list::construct_decompressor<chd_avhuff_decompressor> }, |
| 349 | 397 | }; |
| 350 | 398 | |
| r14497 | r14498 | |
| 759 | 807 | chd_zlib_decompressor::chd_zlib_decompressor(chd_file &chd, bool lossy) |
| 760 | 808 | : chd_decompressor(chd, lossy) |
| 761 | 809 | { |
| 762 | | // init the inflater first |
| 810 | // init the inflater |
| 763 | 811 | m_inflater.next_in = (Bytef *)this; // bogus, but that's ok |
| 764 | 812 | m_inflater.avail_in = 0; |
| 765 | 813 | m_allocator.install(m_inflater); |
| r14497 | r14498 | |
| 1149 | 1197 | |
| 1150 | 1198 | |
| 1151 | 1199 | //************************************************************************** |
| 1152 | | // FLAC COMPRESSOR |
| 1200 | // FLAC DECOMPRESSOR |
| 1153 | 1201 | //************************************************************************** |
| 1154 | 1202 | |
| 1155 | 1203 | //------------------------------------------------- |
| r14497 | r14498 | |
| 1189 | 1237 | |
| 1190 | 1238 | |
| 1191 | 1239 | //************************************************************************** |
| 1240 | // CD FLAC COMPRESSOR |
| 1241 | //************************************************************************** |
| 1242 | |
| 1243 | //------------------------------------------------- |
| 1244 | // chd_cd_flac_compressor - constructor |
| 1245 | //------------------------------------------------- |
| 1246 | |
| 1247 | chd_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 | |
| 1280 | chd_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 | |
| 1291 | UINT32 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 | |
| 1341 | chd_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 | |
| 1368 | chd_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 | |
| 1379 | void 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 | //************************************************************************** |
| 1192 | 1417 | // AVHUFF COMPRESSOR |
| 1193 | 1418 | //************************************************************************** |
| 1194 | 1419 | |