From 2b721975dde0f7e8890fda07ba7956af4ef067ff Mon Sep 17 00:00:00 2001 From: Randy Heit Date: Sun, 9 Mar 2008 03:13:49 +0000 Subject: [PATCH] VERY IMPORTANT NOTE FOR ANYBODY BUILDING FROM THE TRUNK: This commit adds support for FMOD Ex while at the same time removing support for FMOD 3. Be sure to update your SDKs. GCC users, be sure to do a "make cleandep && make clean" before building, or you will likely get inexplicable errors. - Fixed: If you wanted to make cleandep with MinGW, you had to specifically specify Makefile.mingw as the makefile to use. - Added a normalizer to the OPL synth. It helped bring up the volume a little, but not nearly as much as I would have liked. - Removed MIDI Mapper references. It doesn't work with the stream API, and it doesn't really exist on NT kernels, either. - Reworked music volume: Except for MIDI, all music volume is controlled through GSnd and not at the individual song level. - Removed the mididevice global variable. - Removed snd_midivolume. Now that all music uses a linear volume scale, there's no need for two separate music volume controls. - Increased snd_samplerate default up to 48000. - Added snd_format, defaulting to "PCM-16". - Added snd_speakermode, defaulting to "Auto". - Replaced snd_fpu with snd_resampler, defaulting to "Linear". - Bumped the snd_channels default up from a pitiful 12 to 32. - Changed snd_3d default to true. The new cvar snd_hw3d determines if hardware 3D support is used and default to false. - Removed the libFLAC source, since FMOD Ex has native FLAC support. - Removed the altsound code, since it was terribly gimped in comparison to the FMOD code. It's original purpose was to have been as a springboard for writing a non-FMOD sound system for Unix-y systems, but that never happened. - Finished preliminary FMOD Ex support. SVN r789 (trunk) --- FLAC/FLAC++/decoder.h | 245 --- FLAC/FLAC++/export.h | 80 - FLAC/FLAC.vcproj | 690 ------ FLAC/FLAC/assert.h | 45 - FLAC/FLAC/export.h | 91 - FLAC/FLAC/format.h | 1010 --------- FLAC/FLAC/ordinals.h | 80 - FLAC/FLAC/stream_decoder.h | 1559 ------------- FLAC/Makefile.mgw | 79 - FLAC/ReadMe.txt | 7 - FLAC/bitmath.c | 149 -- FLAC/bitreader.c | 1376 ------------ FLAC/cpu.c | 418 ---- FLAC/crc.c | 142 -- FLAC/fixed.c | 435 ---- FLAC/format.c | 593 ----- FLAC/ia32/bitreader_asm.nasm | 568 ----- FLAC/ia32/cpu_asm.nasm | 121 -- FLAC/ia32/fixed_asm.nasm | 312 --- FLAC/ia32/lpc_asm.nasm | 1511 ------------- FLAC/ia32/nasm.h | 75 - FLAC/ia32/stream_encoder_asm.nasm | 159 -- FLAC/lpc.c | 1377 ------------ FLAC/md5.c | 426 ---- FLAC/memory.c | 221 -- FLAC/private/bitmath.h | 42 - FLAC/private/bitreader.h | 99 - FLAC/private/cpu.h | 88 - FLAC/private/crc.h | 61 - FLAC/private/fixed.h | 97 - FLAC/private/float.h | 97 - FLAC/private/format.h | 44 - FLAC/private/lpc.h | 214 -- FLAC/private/md5.h | 44 - FLAC/private/memory.h | 56 - FLAC/protected/stream_decoder.h | 58 - FLAC/share/alloc.h | 214 -- FLAC/stream_decoder.c | 3388 ----------------------------- FLAC/stream_decoder_pp.cpp | 379 ---- Makefile | 22 +- Makefile.linux | 2 +- Makefile.mgw | 109 +- Makefile.mingw | 8 +- docs/rh-log.txt | 31 + src/m_options.cpp | 6 +- src/oplsynth/opl_mus_player.cpp | 18 +- src/oplsynth/opl_mus_player.h | 1 + src/s_environment.cpp | 55 +- src/s_sound.cpp | 78 +- src/s_sound.h | 7 +- src/sound/altsound.cpp | 1422 ------------ src/sound/altsound.h | 106 - src/sound/altsoundmixer.cpp | 156 -- src/sound/fmod_wrap.h | 568 +++++ src/sound/fmodsound.cpp | 1504 +++++++------ src/sound/fmodsound.h | 36 +- src/sound/i_music.cpp | 48 +- src/sound/i_musicinterns.h | 55 +- src/sound/i_sound.cpp | 30 +- src/sound/i_sound.h | 27 +- src/sound/music_flac.cpp | 323 --- src/sound/music_midi_base.cpp | 34 +- src/sound/music_midi_midiout.cpp | 3 - src/sound/music_midi_timidity.cpp | 13 +- src/sound/music_midistream.cpp | 25 +- src/sound/music_mod.cpp | 91 - src/sound/music_mus_midiout.cpp | 3 - src/sound/music_mus_opl.cpp | 2 +- src/sound/music_spc.cpp | 2 +- src/sound/music_stream.cpp | 17 +- src/sound/sample_flac.cpp | 300 --- src/sound/sample_flac.h | 34 - zdoom.sln | 11 - zdoom.vcproj | 30 +- 74 files changed, 1673 insertions(+), 20154 deletions(-) delete mode 100644 FLAC/FLAC++/decoder.h delete mode 100644 FLAC/FLAC++/export.h delete mode 100644 FLAC/FLAC.vcproj delete mode 100644 FLAC/FLAC/assert.h delete mode 100644 FLAC/FLAC/export.h delete mode 100644 FLAC/FLAC/format.h delete mode 100644 FLAC/FLAC/ordinals.h delete mode 100644 FLAC/FLAC/stream_decoder.h delete mode 100644 FLAC/Makefile.mgw delete mode 100644 FLAC/ReadMe.txt delete mode 100644 FLAC/bitmath.c delete mode 100644 FLAC/bitreader.c delete mode 100644 FLAC/cpu.c delete mode 100644 FLAC/crc.c delete mode 100644 FLAC/fixed.c delete mode 100644 FLAC/format.c delete mode 100644 FLAC/ia32/bitreader_asm.nasm delete mode 100644 FLAC/ia32/cpu_asm.nasm delete mode 100644 FLAC/ia32/fixed_asm.nasm delete mode 100644 FLAC/ia32/lpc_asm.nasm delete mode 100644 FLAC/ia32/nasm.h delete mode 100644 FLAC/ia32/stream_encoder_asm.nasm delete mode 100644 FLAC/lpc.c delete mode 100644 FLAC/md5.c delete mode 100644 FLAC/memory.c delete mode 100644 FLAC/private/bitmath.h delete mode 100644 FLAC/private/bitreader.h delete mode 100644 FLAC/private/cpu.h delete mode 100644 FLAC/private/crc.h delete mode 100644 FLAC/private/fixed.h delete mode 100644 FLAC/private/float.h delete mode 100644 FLAC/private/format.h delete mode 100644 FLAC/private/lpc.h delete mode 100644 FLAC/private/md5.h delete mode 100644 FLAC/private/memory.h delete mode 100644 FLAC/protected/stream_decoder.h delete mode 100644 FLAC/share/alloc.h delete mode 100644 FLAC/stream_decoder.c delete mode 100644 FLAC/stream_decoder_pp.cpp delete mode 100644 src/sound/altsound.cpp delete mode 100644 src/sound/altsound.h delete mode 100644 src/sound/altsoundmixer.cpp create mode 100644 src/sound/fmod_wrap.h delete mode 100644 src/sound/music_flac.cpp delete mode 100644 src/sound/music_mod.cpp delete mode 100644 src/sound/sample_flac.cpp delete mode 100644 src/sound/sample_flac.h diff --git a/FLAC/FLAC++/decoder.h b/FLAC/FLAC++/decoder.h deleted file mode 100644 index e074223d8..000000000 --- a/FLAC/FLAC++/decoder.h +++ /dev/null @@ -1,245 +0,0 @@ -/* libFLAC++ - Free Lossless Audio Codec library - * Copyright (C) 2002,2003,2004,2005,2006,2007 Josh Coalson - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * - Neither the name of the Xiph.org Foundation nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef FLACPP__DECODER_H -#define FLACPP__DECODER_H - -#include "export.h" - -// [RH] ZDoom doesn't need these -//#include "FLAC/file_decoder.h" -//#include "FLAC/seekable_stream_decoder.h" -#include "FLAC/stream_decoder.h" - - -/** \file include/FLAC++/decoder.h - * - * \brief - * This module contains the classes which implement the various - * decoders. - * - * See the detailed documentation in the - * \link flacpp_decoder decoder \endlink module. - */ - -/** \defgroup flacpp_decoder FLAC++/decoder.h: decoder classes - * \ingroup flacpp - * - * \brief - * This module describes the decoder layers provided by libFLAC++. - * - * The libFLAC++ decoder classes are object wrappers around their - * counterparts in libFLAC. All decoding layers available in - * libFLAC are also provided here. The interface is very similar; - * make sure to read the \link flac_decoder libFLAC decoder module \endlink. - * - * There are only two significant differences here. First, instead of - * passing in C function pointers for callbacks, you inherit from the - * decoder class and provide implementations for the callbacks in your - * derived class; because of this there is no need for a 'client_data' - * property. - * - * Second, there are two stream decoder classes. FLAC::Decoder::Stream - * is used for the same cases that FLAC__stream_decoder_init_stream() / - * FLAC__stream_decoder_init_ogg_stream() are used, and FLAC::Decoder::File - * is used for the same cases that - * FLAC__stream_decoder_init_FILE() and FLAC__stream_decoder_init_file() / - * FLAC__stream_decoder_init_ogg_FILE() and FLAC__stream_decoder_init_ogg_file() - * are used. - */ - -namespace FLAC { - namespace Decoder { - - /** \ingroup flacpp_decoder - * \brief - * This class wraps the ::FLAC__StreamDecoder. If you are - * decoding from a file, FLAC::Decoder::File may be more - * convenient. - * - * The usage of this class is similar to FLAC__StreamDecoder, - * except instead of providing callbacks to - * FLAC__stream_decoder_init*_stream(), you will inherit from this - * class and override the virtual callback functions with your - * own implementations, then call init() or init_ogg(). The rest - * of the calls work the same as in the C layer. - * - * Only the read, write, and error callbacks are mandatory. The - * others are optional; this class provides default - * implementations that do nothing. In order for seeking to work - * you must overide seek_callback(), tell_callback(), - * length_callback(), and eof_callback(). - */ - class FLACPP_API Stream { - public: - /** This class is a wrapper around FLAC__StreamDecoderState. - */ - class FLACPP_API State { - public: - inline State(::FLAC__StreamDecoderState state): state_(state) { } - inline operator ::FLAC__StreamDecoderState() const { return state_; } - inline const char *as_cstring() const { return ::FLAC__StreamDecoderStateString[state_]; } - inline const char *resolved_as_cstring(const Stream &decoder) const { return ::FLAC__stream_decoder_get_resolved_state_string(decoder.decoder_); } - protected: - ::FLAC__StreamDecoderState state_; - }; - - Stream(); - virtual ~Stream(); - - //@{ - /** Call after construction to check the that the object was created - * successfully. If not, use get_state() to find out why not. - */ - virtual bool is_valid() const; - inline operator bool() const { return is_valid(); } ///< See is_valid() - //@} - - virtual bool set_ogg_serial_number(long value); ///< See FLAC__stream_decoder_set_ogg_serial_number() - virtual bool set_md5_checking(bool value); ///< See FLAC__stream_decoder_set_md5_checking() - virtual bool set_metadata_respond(::FLAC__MetadataType type); ///< See FLAC__stream_decoder_set_metadata_respond() - virtual bool set_metadata_respond_application(const FLAC__byte id[4]); ///< See FLAC__stream_decoder_set_metadata_respond_application() - virtual bool set_metadata_respond_all(); ///< See FLAC__stream_decoder_set_metadata_respond_all() - virtual bool set_metadata_ignore(::FLAC__MetadataType type); ///< See FLAC__stream_decoder_set_metadata_ignore() - virtual bool set_metadata_ignore_application(const FLAC__byte id[4]); ///< See FLAC__stream_decoder_set_metadata_ignore_application() - virtual bool set_metadata_ignore_all(); ///< See FLAC__stream_decoder_set_metadata_ignore_all() - - /* get_state() is not virtual since we want subclasses to be able to return their own state */ - State get_state() const; ///< See FLAC__stream_decoder_get_state() - virtual bool get_md5_checking() const; ///< See FLAC__stream_decoder_get_md5_checking() - virtual FLAC__uint64 get_total_samples() const; ///< See FLAC__stream_decoder_get_total_samples() - virtual unsigned get_channels() const; ///< See FLAC__stream_decoder_get_channels() - virtual ::FLAC__ChannelAssignment get_channel_assignment() const; ///< See FLAC__stream_decoder_get_channel_assignment() - virtual unsigned get_bits_per_sample() const; ///< See FLAC__stream_decoder_get_bits_per_sample() - virtual unsigned get_sample_rate() const; ///< See FLAC__stream_decoder_get_sample_rate() - virtual unsigned get_blocksize() const; ///< See FLAC__stream_decoder_get_blocksize() - virtual bool get_decode_position(FLAC__uint64 *position) const; ///< See FLAC__stream_decoder_get_decode_position() - - virtual ::FLAC__StreamDecoderInitStatus init(); ///< Seek FLAC__stream_decoder_init_stream() - virtual ::FLAC__StreamDecoderInitStatus init_ogg(); ///< Seek FLAC__stream_decoder_init_ogg_stream() - - virtual bool finish(); ///< See FLAC__stream_decoder_finish() - - virtual bool flush(); ///< See FLAC__stream_decoder_flush() - virtual bool reset(); ///< See FLAC__stream_decoder_reset() - - virtual bool process_single(); ///< See FLAC__stream_decoder_process_single() - virtual bool process_until_end_of_metadata(); ///< See FLAC__stream_decoder_process_until_end_of_metadata() - virtual bool process_until_end_of_stream(); ///< See FLAC__stream_decoder_process_until_end_of_stream() - virtual bool skip_single_frame(); ///< See FLAC__stream_decoder_skip_single_frame() - - virtual bool seek_absolute(FLAC__uint64 sample); ///< See FLAC__stream_decoder_seek_absolute() - protected: - /// see FLAC__StreamDecoderReadCallback - virtual ::FLAC__StreamDecoderReadStatus read_callback(FLAC__byte buffer[], size_t *bytes) = 0; - - /// see FLAC__StreamDecoderSeekCallback - virtual ::FLAC__StreamDecoderSeekStatus seek_callback(FLAC__uint64 absolute_byte_offset); - - /// see FLAC__StreamDecoderTellCallback - virtual ::FLAC__StreamDecoderTellStatus tell_callback(FLAC__uint64 *absolute_byte_offset); - - /// see FLAC__StreamDecoderLengthCallback - virtual ::FLAC__StreamDecoderLengthStatus length_callback(FLAC__uint64 *stream_length); - - /// see FLAC__StreamDecoderEofCallback - virtual bool eof_callback(); - - /// see FLAC__StreamDecoderWriteCallback - virtual ::FLAC__StreamDecoderWriteStatus write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[]) = 0; - - /// see FLAC__StreamDecoderMetadataCallback - virtual void metadata_callback(const ::FLAC__StreamMetadata *metadata); - - /// see FLAC__StreamDecoderErrorCallback - virtual void error_callback(::FLAC__StreamDecoderErrorStatus status) = 0; - -#if (defined _MSC_VER) || (defined __BORLANDC__) || (defined __GNUG__ && (__GNUG__ < 2 || (__GNUG__ == 2 && __GNUC_MINOR__ < 96))) || (defined __SUNPRO_CC) - // lame hack: some MSVC/GCC versions can't see a protected decoder_ from nested State::resolved_as_cstring() - friend State; -#endif - ::FLAC__StreamDecoder *decoder_; - - static ::FLAC__StreamDecoderReadStatus read_callback_(const ::FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data); - static ::FLAC__StreamDecoderSeekStatus seek_callback_(const ::FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data); - static ::FLAC__StreamDecoderTellStatus tell_callback_(const ::FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data); - static ::FLAC__StreamDecoderLengthStatus length_callback_(const ::FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data); - static FLAC__bool eof_callback_(const ::FLAC__StreamDecoder *decoder, void *client_data); - static ::FLAC__StreamDecoderWriteStatus write_callback_(const ::FLAC__StreamDecoder *decoder, const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data); - static void metadata_callback_(const ::FLAC__StreamDecoder *decoder, const ::FLAC__StreamMetadata *metadata, void *client_data); - static void error_callback_(const ::FLAC__StreamDecoder *decoder, ::FLAC__StreamDecoderErrorStatus status, void *client_data); - private: - // Private and undefined so you can't use them: - Stream(const Stream &); - void operator=(const Stream &); - }; - - /** \ingroup flacpp_decoder - * \brief - * This class wraps the ::FLAC__StreamDecoder. If you are - * not decoding from a file, you may need to use - * FLAC::Decoder::Stream. - * - * The usage of this class is similar to FLAC__StreamDecoder, - * except instead of providing callbacks to - * FLAC__stream_decoder_init*_FILE() or - * FLAC__stream_decoder_init*_file(), you will inherit from this - * class and override the virtual callback functions with your - * own implementations, then call init() or init_off(). The rest - * of the calls work the same as in the C layer. - * - * Only the write, and error callbacks from FLAC::Decoder::Stream - * are mandatory. The others are optional; this class provides - * full working implementations for all other callbacks and - * supports seeking. - */ - class FLACPP_API File: public Stream { - public: - File(); - virtual ~File(); - - virtual ::FLAC__StreamDecoderInitStatus init(FILE *file); ///< See FLAC__stream_decoder_init_FILE() - virtual ::FLAC__StreamDecoderInitStatus init(const char *filename); ///< See FLAC__stream_decoder_init_file() - virtual ::FLAC__StreamDecoderInitStatus init_ogg(FILE *file); ///< See FLAC__stream_decoder_init_ogg_FILE() - virtual ::FLAC__StreamDecoderInitStatus init_ogg(const char *filename); ///< See FLAC__stream_decoder_init_ogg_file() - protected: - // this is a dummy implementation to satisfy the pure virtual in Stream that is actually supplied internally by the C layer - virtual ::FLAC__StreamDecoderReadStatus read_callback(FLAC__byte buffer[], size_t *bytes); - private: - // Private and undefined so you can't use them: - File(const File &); - void operator=(const File &); - }; - - } -} - -#endif diff --git a/FLAC/FLAC++/export.h b/FLAC/FLAC++/export.h deleted file mode 100644 index 61c9f0853..000000000 --- a/FLAC/FLAC++/export.h +++ /dev/null @@ -1,80 +0,0 @@ -/* libFLAC++ - Free Lossless Audio Codec library - * Copyright (C) 2002,2003,2004,2005,2006,2007 Josh Coalson - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * - Neither the name of the Xiph.org Foundation nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef FLACPP__EXPORT_H -#define FLACPP__EXPORT_H - -/** \file include/FLAC++/export.h - * - * \brief - * This module contains #defines and symbols for exporting function - * calls, and providing version information and compiled-in features. - * - * See the \link flacpp_export export \endlink module. - */ - -/** \defgroup flacpp_export FLAC++/export.h: export symbols - * \ingroup flacpp - * - * \brief - * This module contains #defines and symbols for exporting function - * calls, and providing version information and compiled-in features. - * - * If you are compiling with MSVC and will link to the static library - * (libFLAC++.lib) you should define FLAC__NO_DLL in your project to - * make sure the symbols are exported properly. - * - * \{ - */ - -#if defined(FLAC__NO_DLL) || !defined(_MSC_VER) -#define FLACPP_API - -#else - -#ifdef FLACPP_API_EXPORTS -#define FLACPP_API _declspec(dllexport) -#else -#define FLACPP_API _declspec(dllimport) - -#endif -#endif - -/* These #defines will mirror the libtool-based library version number, see - * http://www.gnu.org/software/libtool/manual.html#Libtool-versioning - */ -#define FLACPP_API_VERSION_CURRENT 8 -#define FLACPP_API_VERSION_REVISION 0 -#define FLACPP_API_VERSION_AGE 2 - -/* \} */ - -#endif diff --git a/FLAC/FLAC.vcproj b/FLAC/FLAC.vcproj deleted file mode 100644 index 23a31f6b0..000000000 --- a/FLAC/FLAC.vcproj +++ /dev/null @@ -1,690 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/FLAC/FLAC/assert.h b/FLAC/FLAC/assert.h deleted file mode 100644 index 5c2d5048b..000000000 --- a/FLAC/FLAC/assert.h +++ /dev/null @@ -1,45 +0,0 @@ -/* libFLAC - Free Lossless Audio Codec library - * Copyright (C) 2001,2002,2003,2004,2005,2006,2007 Josh Coalson - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * - Neither the name of the Xiph.org Foundation nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef FLAC__ASSERT_H -#define FLAC__ASSERT_H - -/* we need this since some compilers (like MSVC) leave assert()s on release code (and we don't want to use their ASSERT) */ -#ifdef DEBUG -#include -#define FLAC__ASSERT(x) assert(x) -#define FLAC__ASSERT_DECLARATION(x) x -#else -#define FLAC__ASSERT(x) -#define FLAC__ASSERT_DECLARATION(x) -#endif - -#endif diff --git a/FLAC/FLAC/export.h b/FLAC/FLAC/export.h deleted file mode 100644 index 5a5654c0f..000000000 --- a/FLAC/FLAC/export.h +++ /dev/null @@ -1,91 +0,0 @@ -/* libFLAC - Free Lossless Audio Codec library - * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 Josh Coalson - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * - Neither the name of the Xiph.org Foundation nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef FLAC__EXPORT_H -#define FLAC__EXPORT_H - -/** \file include/FLAC/export.h - * - * \brief - * This module contains #defines and symbols for exporting function - * calls, and providing version information and compiled-in features. - * - * See the \link flac_export export \endlink module. - */ - -/** \defgroup flac_export FLAC/export.h: export symbols - * \ingroup flac - * - * \brief - * This module contains #defines and symbols for exporting function - * calls, and providing version information and compiled-in features. - * - * If you are compiling with MSVC and will link to the static library - * (libFLAC.lib) you should define FLAC__NO_DLL in your project to - * make sure the symbols are exported properly. - * - * \{ - */ - -#if defined(FLAC__NO_DLL) || !defined(_MSC_VER) -#define FLAC_API - -#else - -#ifdef FLAC_API_EXPORTS -#define FLAC_API _declspec(dllexport) -#else -#define FLAC_API _declspec(dllimport) - -#endif -#endif - -/** These #defines will mirror the libtool-based library version number, see - * http://www.gnu.org/software/libtool/manual.html#Libtool-versioning - */ -#define FLAC_API_VERSION_CURRENT 10 -#define FLAC_API_VERSION_REVISION 0 /**< see above */ -#define FLAC_API_VERSION_AGE 2 /**< see above */ - -#ifdef __cplusplus -extern "C" { -#endif - -/** \c 1 if the library has been compiled with support for Ogg FLAC, else \c 0. */ -extern FLAC_API int FLAC_API_SUPPORTS_OGG_FLAC; - -#ifdef __cplusplus -} -#endif - -/* \} */ - -#endif diff --git a/FLAC/FLAC/format.h b/FLAC/FLAC/format.h deleted file mode 100644 index 75c06c1bb..000000000 --- a/FLAC/FLAC/format.h +++ /dev/null @@ -1,1010 +0,0 @@ -/* libFLAC - Free Lossless Audio Codec library - * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 Josh Coalson - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * - Neither the name of the Xiph.org Foundation nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef FLAC__FORMAT_H -#define FLAC__FORMAT_H - -#include "export.h" -#include "ordinals.h" - -#ifdef __cplusplus -extern "C" { -#endif - -/** \file include/FLAC/format.h - * - * \brief - * This module contains structure definitions for the representation - * of FLAC format components in memory. These are the basic - * structures used by the rest of the interfaces. - * - * See the detailed documentation in the - * \link flac_format format \endlink module. - */ - -/** \defgroup flac_format FLAC/format.h: format components - * \ingroup flac - * - * \brief - * This module contains structure definitions for the representation - * of FLAC format components in memory. These are the basic - * structures used by the rest of the interfaces. - * - * First, you should be familiar with the - * FLAC format. Many of the values here - * follow directly from the specification. As a user of libFLAC, the - * interesting parts really are the structures that describe the frame - * header and metadata blocks. - * - * The format structures here are very primitive, designed to store - * information in an efficient way. Reading information from the - * structures is easy but creating or modifying them directly is - * more complex. For the most part, as a user of a library, editing - * is not necessary; however, for metadata blocks it is, so there are - * convenience functions provided in the \link flac_metadata metadata - * module \endlink to simplify the manipulation of metadata blocks. - * - * \note - * It's not the best convention, but symbols ending in _LEN are in bits - * and _LENGTH are in bytes. _LENGTH symbols are \#defines instead of - * global variables because they are usually used when declaring byte - * arrays and some compilers require compile-time knowledge of array - * sizes when declared on the stack. - * - * \{ - */ - - -/* - Most of the values described in this file are defined by the FLAC - format specification. There is nothing to tune here. -*/ - -/** The largest legal metadata type code. */ -#define FLAC__MAX_METADATA_TYPE_CODE (126u) - -/** The minimum block size, in samples, permitted by the format. */ -#define FLAC__MIN_BLOCK_SIZE (16u) - -/** The maximum block size, in samples, permitted by the format. */ -#define FLAC__MAX_BLOCK_SIZE (65535u) - -/** The maximum block size, in samples, permitted by the FLAC subset for - * sample rates up to 48kHz. */ -#define FLAC__SUBSET_MAX_BLOCK_SIZE_48000HZ (4608u) - -/** The maximum number of channels permitted by the format. */ -#define FLAC__MAX_CHANNELS (8u) - -/** The minimum sample resolution permitted by the format. */ -#define FLAC__MIN_BITS_PER_SAMPLE (4u) - -/** The maximum sample resolution permitted by the format. */ -#define FLAC__MAX_BITS_PER_SAMPLE (32u) - -/** The maximum sample resolution permitted by libFLAC. - * - * \warning - * FLAC__MAX_BITS_PER_SAMPLE is the limit of the FLAC format. However, - * the reference encoder/decoder is currently limited to 24 bits because - * of prevalent 32-bit math, so make sure and use this value when - * appropriate. - */ -#define FLAC__REFERENCE_CODEC_MAX_BITS_PER_SAMPLE (24u) - -/** The maximum sample rate permitted by the format. The value is - * ((2 ^ 16) - 1) * 10; see FLAC format - * as to why. - */ -#define FLAC__MAX_SAMPLE_RATE (655350u) - -/** The maximum LPC order permitted by the format. */ -#define FLAC__MAX_LPC_ORDER (32u) - -/** The maximum LPC order permitted by the FLAC subset for sample rates - * up to 48kHz. */ -#define FLAC__SUBSET_MAX_LPC_ORDER_48000HZ (12u) - -/** The minimum quantized linear predictor coefficient precision - * permitted by the format. - */ -#define FLAC__MIN_QLP_COEFF_PRECISION (5u) - -/** The maximum quantized linear predictor coefficient precision - * permitted by the format. - */ -#define FLAC__MAX_QLP_COEFF_PRECISION (15u) - -/** The maximum order of the fixed predictors permitted by the format. */ -#define FLAC__MAX_FIXED_ORDER (4u) - -/** The maximum Rice partition order permitted by the format. */ -#define FLAC__MAX_RICE_PARTITION_ORDER (15u) - -/** The maximum Rice partition order permitted by the FLAC Subset. */ -#define FLAC__SUBSET_MAX_RICE_PARTITION_ORDER (8u) - -/** The version string of the release, stamped onto the libraries and binaries. - * - * \note - * This does not correspond to the shared library version number, which - * is used to determine binary compatibility. - */ -extern FLAC_API const char *FLAC__VERSION_STRING; - -/** The vendor string inserted by the encoder into the VORBIS_COMMENT block. - * This is a NUL-terminated ASCII string; when inserted into the - * VORBIS_COMMENT the trailing null is stripped. - */ -extern FLAC_API const char *FLAC__VENDOR_STRING; - -/** The byte string representation of the beginning of a FLAC stream. */ -extern FLAC_API const FLAC__byte FLAC__STREAM_SYNC_STRING[4]; /* = "fLaC" */ - -/** The 32-bit integer big-endian representation of the beginning of - * a FLAC stream. - */ -extern FLAC_API const unsigned FLAC__STREAM_SYNC; /* = 0x664C6143 */ - -/** The length of the FLAC signature in bits. */ -extern FLAC_API const unsigned FLAC__STREAM_SYNC_LEN; /* = 32 bits */ - -/** The length of the FLAC signature in bytes. */ -#define FLAC__STREAM_SYNC_LENGTH (4u) - - -/***************************************************************************** - * - * Subframe structures - * - *****************************************************************************/ - -/*****************************************************************************/ - -/** An enumeration of the available entropy coding methods. */ -typedef enum { - FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE = 0, - /**< Residual is coded by partitioning into contexts, each with it's own - * 4-bit Rice parameter. */ - - FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2 = 1 - /**< Residual is coded by partitioning into contexts, each with it's own - * 5-bit Rice parameter. */ -} FLAC__EntropyCodingMethodType; - -/** Maps a FLAC__EntropyCodingMethodType to a C string. - * - * Using a FLAC__EntropyCodingMethodType as the index to this array will - * give the string equivalent. The contents should not be modified. - */ -extern FLAC_API const char * const FLAC__EntropyCodingMethodTypeString[]; - - -/** Contents of a Rice partitioned residual - */ -typedef struct { - - unsigned *parameters; - /**< The Rice parameters for each context. */ - - unsigned *raw_bits; - /**< Widths for escape-coded partitions. Will be non-zero for escaped - * partitions and zero for unescaped partitions. - */ - - unsigned capacity_by_order; - /**< The capacity of the \a parameters and \a raw_bits arrays - * specified as an order, i.e. the number of array elements - * allocated is 2 ^ \a capacity_by_order. - */ -} FLAC__EntropyCodingMethod_PartitionedRiceContents; - -/** Header for a Rice partitioned residual. (c.f. format specification) - */ -typedef struct { - - unsigned order; - /**< The partition order, i.e. # of contexts = 2 ^ \a order. */ - - const FLAC__EntropyCodingMethod_PartitionedRiceContents *contents; - /**< The context's Rice parameters and/or raw bits. */ - -} FLAC__EntropyCodingMethod_PartitionedRice; - -extern FLAC_API const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN; /**< == 4 (bits) */ -extern FLAC_API const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN; /**< == 4 (bits) */ -extern FLAC_API const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN; /**< == 5 (bits) */ -extern FLAC_API const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN; /**< == 5 (bits) */ - -extern FLAC_API const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER; -/**< == (1<format specification) - */ -typedef struct { - FLAC__EntropyCodingMethodType type; - union { - FLAC__EntropyCodingMethod_PartitionedRice partitioned_rice; - } data; -} FLAC__EntropyCodingMethod; - -extern FLAC_API const unsigned FLAC__ENTROPY_CODING_METHOD_TYPE_LEN; /**< == 2 (bits) */ - -/*****************************************************************************/ - -/** An enumeration of the available subframe types. */ -typedef enum { - FLAC__SUBFRAME_TYPE_CONSTANT = 0, /**< constant signal */ - FLAC__SUBFRAME_TYPE_VERBATIM = 1, /**< uncompressed signal */ - FLAC__SUBFRAME_TYPE_FIXED = 2, /**< fixed polynomial prediction */ - FLAC__SUBFRAME_TYPE_LPC = 3 /**< linear prediction */ -} FLAC__SubframeType; - -/** Maps a FLAC__SubframeType to a C string. - * - * Using a FLAC__SubframeType as the index to this array will - * give the string equivalent. The contents should not be modified. - */ -extern FLAC_API const char * const FLAC__SubframeTypeString[]; - - -/** CONSTANT subframe. (c.f. format specification) - */ -typedef struct { - FLAC__int32 value; /**< The constant signal value. */ -} FLAC__Subframe_Constant; - - -/** VERBATIM subframe. (c.f. format specification) - */ -typedef struct { - const FLAC__int32 *data; /**< A pointer to verbatim signal. */ -} FLAC__Subframe_Verbatim; - - -/** FIXED subframe. (c.f. format specification) - */ -typedef struct { - FLAC__EntropyCodingMethod entropy_coding_method; - /**< The residual coding method. */ - - unsigned order; - /**< The polynomial order. */ - - FLAC__int32 warmup[FLAC__MAX_FIXED_ORDER]; - /**< Warmup samples to prime the predictor, length == order. */ - - const FLAC__int32 *residual; - /**< The residual signal, length == (blocksize minus order) samples. */ -} FLAC__Subframe_Fixed; - - -/** LPC subframe. (c.f. format specification) - */ -typedef struct { - FLAC__EntropyCodingMethod entropy_coding_method; - /**< The residual coding method. */ - - unsigned order; - /**< The FIR order. */ - - unsigned qlp_coeff_precision; - /**< Quantized FIR filter coefficient precision in bits. */ - - int quantization_level; - /**< The qlp coeff shift needed. */ - - FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER]; - /**< FIR filter coefficients. */ - - FLAC__int32 warmup[FLAC__MAX_LPC_ORDER]; - /**< Warmup samples to prime the predictor, length == order. */ - - const FLAC__int32 *residual; - /**< The residual signal, length == (blocksize minus order) samples. */ -} FLAC__Subframe_LPC; - -extern FLAC_API const unsigned FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN; /**< == 4 (bits) */ -extern FLAC_API const unsigned FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN; /**< == 5 (bits) */ - - -/** FLAC subframe structure. (c.f. format specification) - */ -typedef struct { - FLAC__SubframeType type; - union { - FLAC__Subframe_Constant constant; - FLAC__Subframe_Fixed fixed; - FLAC__Subframe_LPC lpc; - FLAC__Subframe_Verbatim verbatim; - } data; - unsigned wasted_bits; -} FLAC__Subframe; - -/** == 1 (bit) - * - * This used to be a zero-padding bit (hence the name - * FLAC__SUBFRAME_ZERO_PAD_LEN) but is now a reserved bit. It still has a - * mandatory value of \c 0 but in the future may take on the value \c 0 or \c 1 - * to mean something else. - */ -extern FLAC_API const unsigned FLAC__SUBFRAME_ZERO_PAD_LEN; -extern FLAC_API const unsigned FLAC__SUBFRAME_TYPE_LEN; /**< == 6 (bits) */ -extern FLAC_API const unsigned FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN; /**< == 1 (bit) */ - -extern FLAC_API const unsigned FLAC__SUBFRAME_TYPE_CONSTANT_BYTE_ALIGNED_MASK; /**< = 0x00 */ -extern FLAC_API const unsigned FLAC__SUBFRAME_TYPE_VERBATIM_BYTE_ALIGNED_MASK; /**< = 0x02 */ -extern FLAC_API const unsigned FLAC__SUBFRAME_TYPE_FIXED_BYTE_ALIGNED_MASK; /**< = 0x10 */ -extern FLAC_API const unsigned FLAC__SUBFRAME_TYPE_LPC_BYTE_ALIGNED_MASK; /**< = 0x40 */ - -/*****************************************************************************/ - - -/***************************************************************************** - * - * Frame structures - * - *****************************************************************************/ - -/** An enumeration of the available channel assignments. */ -typedef enum { - FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT = 0, /**< independent channels */ - FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE = 1, /**< left+side stereo */ - FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE = 2, /**< right+side stereo */ - FLAC__CHANNEL_ASSIGNMENT_MID_SIDE = 3 /**< mid+side stereo */ -} FLAC__ChannelAssignment; - -/** Maps a FLAC__ChannelAssignment to a C string. - * - * Using a FLAC__ChannelAssignment as the index to this array will - * give the string equivalent. The contents should not be modified. - */ -extern FLAC_API const char * const FLAC__ChannelAssignmentString[]; - -/** An enumeration of the possible frame numbering methods. */ -typedef enum { - FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER, /**< number contains the frame number */ - FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER /**< number contains the sample number of first sample in frame */ -} FLAC__FrameNumberType; - -/** Maps a FLAC__FrameNumberType to a C string. - * - * Using a FLAC__FrameNumberType as the index to this array will - * give the string equivalent. The contents should not be modified. - */ -extern FLAC_API const char * const FLAC__FrameNumberTypeString[]; - - -/** FLAC frame header structure. (c.f. format specification) - */ -typedef struct { - unsigned blocksize; - /**< The number of samples per subframe. */ - - unsigned sample_rate; - /**< The sample rate in Hz. */ - - unsigned channels; - /**< The number of channels (== number of subframes). */ - - FLAC__ChannelAssignment channel_assignment; - /**< The channel assignment for the frame. */ - - unsigned bits_per_sample; - /**< The sample resolution. */ - - FLAC__FrameNumberType number_type; - /**< The numbering scheme used for the frame. As a convenience, the - * decoder will always convert a frame number to a sample number because - * the rules are complex. */ - - union { - FLAC__uint32 frame_number; - FLAC__uint64 sample_number; - } number; - /**< The frame number or sample number of first sample in frame; - * use the \a number_type value to determine which to use. */ - - FLAC__uint8 crc; - /**< CRC-8 (polynomial = x^8 + x^2 + x^1 + x^0, initialized with 0) - * of the raw frame header bytes, meaning everything before the CRC byte - * including the sync code. - */ -} FLAC__FrameHeader; - -extern FLAC_API const unsigned FLAC__FRAME_HEADER_SYNC; /**< == 0x3ffe; the frame header sync code */ -extern FLAC_API const unsigned FLAC__FRAME_HEADER_SYNC_LEN; /**< == 14 (bits) */ -extern FLAC_API const unsigned FLAC__FRAME_HEADER_RESERVED_LEN; /**< == 1 (bits) */ -extern FLAC_API const unsigned FLAC__FRAME_HEADER_BLOCKING_STRATEGY_LEN; /**< == 1 (bits) */ -extern FLAC_API const unsigned FLAC__FRAME_HEADER_BLOCK_SIZE_LEN; /**< == 4 (bits) */ -extern FLAC_API const unsigned FLAC__FRAME_HEADER_SAMPLE_RATE_LEN; /**< == 4 (bits) */ -extern FLAC_API const unsigned FLAC__FRAME_HEADER_CHANNEL_ASSIGNMENT_LEN; /**< == 4 (bits) */ -extern FLAC_API const unsigned FLAC__FRAME_HEADER_BITS_PER_SAMPLE_LEN; /**< == 3 (bits) */ -extern FLAC_API const unsigned FLAC__FRAME_HEADER_ZERO_PAD_LEN; /**< == 1 (bit) */ -extern FLAC_API const unsigned FLAC__FRAME_HEADER_CRC_LEN; /**< == 8 (bits) */ - - -/** FLAC frame footer structure. (c.f. format specification) - */ -typedef struct { - FLAC__uint16 crc; - /**< CRC-16 (polynomial = x^16 + x^15 + x^2 + x^0, initialized with - * 0) of the bytes before the crc, back to and including the frame header - * sync code. - */ -} FLAC__FrameFooter; - -extern FLAC_API const unsigned FLAC__FRAME_FOOTER_CRC_LEN; /**< == 16 (bits) */ - - -/** FLAC frame structure. (c.f. format specification) - */ -typedef struct { - FLAC__FrameHeader header; - FLAC__Subframe subframes[FLAC__MAX_CHANNELS]; - FLAC__FrameFooter footer; -} FLAC__Frame; - -/*****************************************************************************/ - - -/***************************************************************************** - * - * Meta-data structures - * - *****************************************************************************/ - -/** An enumeration of the available metadata block types. */ -typedef enum { - - FLAC__METADATA_TYPE_STREAMINFO = 0, - /**< STREAMINFO block */ - - FLAC__METADATA_TYPE_PADDING = 1, - /**< PADDING block */ - - FLAC__METADATA_TYPE_APPLICATION = 2, - /**< APPLICATION block */ - - FLAC__METADATA_TYPE_SEEKTABLE = 3, - /**< SEEKTABLE block */ - - FLAC__METADATA_TYPE_VORBIS_COMMENT = 4, - /**< VORBISCOMMENT block (a.k.a. FLAC tags) */ - - FLAC__METADATA_TYPE_CUESHEET = 5, - /**< CUESHEET block */ - - FLAC__METADATA_TYPE_PICTURE = 6, - /**< PICTURE block */ - - FLAC__METADATA_TYPE_UNDEFINED = 7 - /**< marker to denote beginning of undefined type range; this number will increase as new metadata types are added */ - -} FLAC__MetadataType; - -/** Maps a FLAC__MetadataType to a C string. - * - * Using a FLAC__MetadataType as the index to this array will - * give the string equivalent. The contents should not be modified. - */ -extern FLAC_API const char * const FLAC__MetadataTypeString[]; - - -/** FLAC STREAMINFO structure. (c.f. format specification) - */ -typedef struct { - unsigned min_blocksize, max_blocksize; - unsigned min_framesize, max_framesize; - unsigned sample_rate; - unsigned channels; - unsigned bits_per_sample; - FLAC__uint64 total_samples; - FLAC__byte md5sum[16]; -} FLAC__StreamMetadata_StreamInfo; - -extern FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN; /**< == 16 (bits) */ -extern FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN; /**< == 16 (bits) */ -extern FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN; /**< == 24 (bits) */ -extern FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN; /**< == 24 (bits) */ -extern FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN; /**< == 20 (bits) */ -extern FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN; /**< == 3 (bits) */ -extern FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN; /**< == 5 (bits) */ -extern FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN; /**< == 36 (bits) */ -extern FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_MD5SUM_LEN; /**< == 128 (bits) */ - -/** The total stream length of the STREAMINFO block in bytes. */ -#define FLAC__STREAM_METADATA_STREAMINFO_LENGTH (34u) - -/** FLAC PADDING structure. (c.f. format specification) - */ -typedef struct { - int dummy; - /**< Conceptually this is an empty struct since we don't store the - * padding bytes. Empty structs are not allowed by some C compilers, - * hence the dummy. - */ -} FLAC__StreamMetadata_Padding; - - -/** FLAC APPLICATION structure. (c.f. format specification) - */ -typedef struct { - FLAC__byte id[4]; - FLAC__byte *data; -} FLAC__StreamMetadata_Application; - -extern FLAC_API const unsigned FLAC__STREAM_METADATA_APPLICATION_ID_LEN; /**< == 32 (bits) */ - -/** SeekPoint structure used in SEEKTABLE blocks. (c.f. format specification) - */ -typedef struct { - FLAC__uint64 sample_number; - /**< The sample number of the target frame. */ - - FLAC__uint64 stream_offset; - /**< The offset, in bytes, of the target frame with respect to - * beginning of the first frame. */ - - unsigned frame_samples; - /**< The number of samples in the target frame. */ -} FLAC__StreamMetadata_SeekPoint; - -extern FLAC_API const unsigned FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN; /**< == 64 (bits) */ -extern FLAC_API const unsigned FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN; /**< == 64 (bits) */ -extern FLAC_API const unsigned FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN; /**< == 16 (bits) */ - -/** The total stream length of a seek point in bytes. */ -#define FLAC__STREAM_METADATA_SEEKPOINT_LENGTH (18u) - -/** The value used in the \a sample_number field of - * FLAC__StreamMetadataSeekPoint used to indicate a placeholder - * point (== 0xffffffffffffffff). - */ -extern FLAC_API const FLAC__uint64 FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER; - - -/** FLAC SEEKTABLE structure. (c.f. format specification) - * - * \note From the format specification: - * - The seek points must be sorted by ascending sample number. - * - Each seek point's sample number must be the first sample of the - * target frame. - * - Each seek point's sample number must be unique within the table. - * - Existence of a SEEKTABLE block implies a correct setting of - * total_samples in the stream_info block. - * - Behavior is undefined when more than one SEEKTABLE block is - * present in a stream. - */ -typedef struct { - unsigned num_points; - FLAC__StreamMetadata_SeekPoint *points; -} FLAC__StreamMetadata_SeekTable; - - -/** Vorbis comment entry structure used in VORBIS_COMMENT blocks. (c.f. format specification) - * - * For convenience, the APIs maintain a trailing NUL character at the end of - * \a entry which is not counted toward \a length, i.e. - * \code strlen(entry) == length \endcode - */ -typedef struct { - FLAC__uint32 length; - FLAC__byte *entry; -} FLAC__StreamMetadata_VorbisComment_Entry; - -extern FLAC_API const unsigned FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN; /**< == 32 (bits) */ - - -/** FLAC VORBIS_COMMENT structure. (c.f. format specification) - */ -typedef struct { - FLAC__StreamMetadata_VorbisComment_Entry vendor_string; - FLAC__uint32 num_comments; - FLAC__StreamMetadata_VorbisComment_Entry *comments; -} FLAC__StreamMetadata_VorbisComment; - -extern FLAC_API const unsigned FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN; /**< == 32 (bits) */ - - -/** FLAC CUESHEET track index structure. (See the - * format specification for - * the full description of each field.) - */ -typedef struct { - FLAC__uint64 offset; - /**< Offset in samples, relative to the track offset, of the index - * point. - */ - - FLAC__byte number; - /**< The index point number. */ -} FLAC__StreamMetadata_CueSheet_Index; - -extern FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN; /**< == 64 (bits) */ -extern FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN; /**< == 8 (bits) */ -extern FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN; /**< == 3*8 (bits) */ - - -/** FLAC CUESHEET track structure. (See the - * format specification for - * the full description of each field.) - */ -typedef struct { - FLAC__uint64 offset; - /**< Track offset in samples, relative to the beginning of the FLAC audio stream. */ - - FLAC__byte number; - /**< The track number. */ - - char isrc[13]; - /**< Track ISRC. This is a 12-digit alphanumeric code plus a trailing \c NUL byte */ - - unsigned type:1; - /**< The track type: 0 for audio, 1 for non-audio. */ - - unsigned pre_emphasis:1; - /**< The pre-emphasis flag: 0 for no pre-emphasis, 1 for pre-emphasis. */ - - FLAC__byte num_indices; - /**< The number of track index points. */ - - FLAC__StreamMetadata_CueSheet_Index *indices; - /**< NULL if num_indices == 0, else pointer to array of index points. */ - -} FLAC__StreamMetadata_CueSheet_Track; - -extern FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN; /**< == 64 (bits) */ -extern FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN; /**< == 8 (bits) */ -extern FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN; /**< == 12*8 (bits) */ -extern FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN; /**< == 1 (bit) */ -extern FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN; /**< == 1 (bit) */ -extern FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN; /**< == 6+13*8 (bits) */ -extern FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN; /**< == 8 (bits) */ - - -/** FLAC CUESHEET structure. (See the - * format specification - * for the full description of each field.) - */ -typedef struct { - char media_catalog_number[129]; - /**< Media catalog number, in ASCII printable characters 0x20-0x7e. In - * general, the media catalog number may be 0 to 128 bytes long; any - * unused characters should be right-padded with NUL characters. - */ - - FLAC__uint64 lead_in; - /**< The number of lead-in samples. */ - - FLAC__bool is_cd; - /**< \c true if CUESHEET corresponds to a Compact Disc, else \c false. */ - - unsigned num_tracks; - /**< The number of tracks. */ - - FLAC__StreamMetadata_CueSheet_Track *tracks; - /**< NULL if num_tracks == 0, else pointer to array of tracks. */ - -} FLAC__StreamMetadata_CueSheet; - -extern FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN; /**< == 128*8 (bits) */ -extern FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN; /**< == 64 (bits) */ -extern FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN; /**< == 1 (bit) */ -extern FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN; /**< == 7+258*8 (bits) */ -extern FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN; /**< == 8 (bits) */ - - -/** An enumeration of the PICTURE types (see FLAC__StreamMetadataPicture and id3 v2.4 APIC tag). */ -typedef enum { - FLAC__STREAM_METADATA_PICTURE_TYPE_OTHER = 0, /**< Other */ - FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD = 1, /**< 32x32 pixels 'file icon' (PNG only) */ - FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON = 2, /**< Other file icon */ - FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER = 3, /**< Cover (front) */ - FLAC__STREAM_METADATA_PICTURE_TYPE_BACK_COVER = 4, /**< Cover (back) */ - FLAC__STREAM_METADATA_PICTURE_TYPE_LEAFLET_PAGE = 5, /**< Leaflet page */ - FLAC__STREAM_METADATA_PICTURE_TYPE_MEDIA = 6, /**< Media (e.g. label side of CD) */ - FLAC__STREAM_METADATA_PICTURE_TYPE_LEAD_ARTIST = 7, /**< Lead artist/lead performer/soloist */ - FLAC__STREAM_METADATA_PICTURE_TYPE_ARTIST = 8, /**< Artist/performer */ - FLAC__STREAM_METADATA_PICTURE_TYPE_CONDUCTOR = 9, /**< Conductor */ - FLAC__STREAM_METADATA_PICTURE_TYPE_BAND = 10, /**< Band/Orchestra */ - FLAC__STREAM_METADATA_PICTURE_TYPE_COMPOSER = 11, /**< Composer */ - FLAC__STREAM_METADATA_PICTURE_TYPE_LYRICIST = 12, /**< Lyricist/text writer */ - FLAC__STREAM_METADATA_PICTURE_TYPE_RECORDING_LOCATION = 13, /**< Recording Location */ - FLAC__STREAM_METADATA_PICTURE_TYPE_DURING_RECORDING = 14, /**< During recording */ - FLAC__STREAM_METADATA_PICTURE_TYPE_DURING_PERFORMANCE = 15, /**< During performance */ - FLAC__STREAM_METADATA_PICTURE_TYPE_VIDEO_SCREEN_CAPTURE = 16, /**< Movie/video screen capture */ - FLAC__STREAM_METADATA_PICTURE_TYPE_FISH = 17, /**< A bright coloured fish */ - FLAC__STREAM_METADATA_PICTURE_TYPE_ILLUSTRATION = 18, /**< Illustration */ - FLAC__STREAM_METADATA_PICTURE_TYPE_BAND_LOGOTYPE = 19, /**< Band/artist logotype */ - FLAC__STREAM_METADATA_PICTURE_TYPE_PUBLISHER_LOGOTYPE = 20, /**< Publisher/Studio logotype */ - FLAC__STREAM_METADATA_PICTURE_TYPE_UNDEFINED -} FLAC__StreamMetadata_Picture_Type; - -/** Maps a FLAC__StreamMetadata_Picture_Type to a C string. - * - * Using a FLAC__StreamMetadata_Picture_Type as the index to this array - * will give the string equivalent. The contents should not be - * modified. - */ -extern FLAC_API const char * const FLAC__StreamMetadata_Picture_TypeString[]; - -/** FLAC PICTURE structure. (See the - * format specification - * for the full description of each field.) - */ -typedef struct { - FLAC__StreamMetadata_Picture_Type type; - /**< The kind of picture stored. */ - - char *mime_type; - /**< Picture data's MIME type, in ASCII printable characters - * 0x20-0x7e, NUL terminated. For best compatibility with players, - * use picture data of MIME type \c image/jpeg or \c image/png. A - * MIME type of '-->' is also allowed, in which case the picture - * data should be a complete URL. In file storage, the MIME type is - * stored as a 32-bit length followed by the ASCII string with no NUL - * terminator, but is converted to a plain C string in this structure - * for convenience. - */ - - FLAC__byte *description; - /**< Picture's description in UTF-8, NUL terminated. In file storage, - * the description is stored as a 32-bit length followed by the UTF-8 - * string with no NUL terminator, but is converted to a plain C string - * in this structure for convenience. - */ - - FLAC__uint32 width; - /**< Picture's width in pixels. */ - - FLAC__uint32 height; - /**< Picture's height in pixels. */ - - FLAC__uint32 depth; - /**< Picture's color depth in bits-per-pixel. */ - - FLAC__uint32 colors; - /**< For indexed palettes (like GIF), picture's number of colors (the - * number of palette entries), or \c 0 for non-indexed (i.e. 2^depth). - */ - - FLAC__uint32 data_length; - /**< Length of binary picture data in bytes. */ - - FLAC__byte *data; - /**< Binary picture data. */ - -} FLAC__StreamMetadata_Picture; - -extern FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_TYPE_LEN; /**< == 32 (bits) */ -extern FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN; /**< == 32 (bits) */ -extern FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN; /**< == 32 (bits) */ -extern FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN; /**< == 32 (bits) */ -extern FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN; /**< == 32 (bits) */ -extern FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN; /**< == 32 (bits) */ -extern FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_COLORS_LEN; /**< == 32 (bits) */ -extern FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN; /**< == 32 (bits) */ - - -/** Structure that is used when a metadata block of unknown type is loaded. - * The contents are opaque. The structure is used only internally to - * correctly handle unknown metadata. - */ -typedef struct { - FLAC__byte *data; -} FLAC__StreamMetadata_Unknown; - - -/** FLAC metadata block structure. (c.f. format specification) - */ -typedef struct { - FLAC__MetadataType type; - /**< The type of the metadata block; used determine which member of the - * \a data union to dereference. If type >= FLAC__METADATA_TYPE_UNDEFINED - * then \a data.unknown must be used. */ - - FLAC__bool is_last; - /**< \c true if this metadata block is the last, else \a false */ - - unsigned length; - /**< Length, in bytes, of the block data as it appears in the stream. */ - - union { - FLAC__StreamMetadata_StreamInfo stream_info; - FLAC__StreamMetadata_Padding padding; - FLAC__StreamMetadata_Application application; - FLAC__StreamMetadata_SeekTable seek_table; - FLAC__StreamMetadata_VorbisComment vorbis_comment; - FLAC__StreamMetadata_CueSheet cue_sheet; - FLAC__StreamMetadata_Picture picture; - FLAC__StreamMetadata_Unknown unknown; - } data; - /**< Polymorphic block data; use the \a type value to determine which - * to use. */ -} FLAC__StreamMetadata; - -extern FLAC_API const unsigned FLAC__STREAM_METADATA_IS_LAST_LEN; /**< == 1 (bit) */ -extern FLAC_API const unsigned FLAC__STREAM_METADATA_TYPE_LEN; /**< == 7 (bits) */ -extern FLAC_API const unsigned FLAC__STREAM_METADATA_LENGTH_LEN; /**< == 24 (bits) */ - -/** The total stream length of a metadata block header in bytes. */ -#define FLAC__STREAM_METADATA_HEADER_LENGTH (4u) - -/*****************************************************************************/ - - -/***************************************************************************** - * - * Utility functions - * - *****************************************************************************/ - -/** Tests that a sample rate is valid for FLAC. - * - * \param sample_rate The sample rate to test for compliance. - * \retval FLAC__bool - * \c true if the given sample rate conforms to the specification, else - * \c false. - */ -FLAC_API FLAC__bool FLAC__format_sample_rate_is_valid(unsigned sample_rate); - -/** Tests that a sample rate is valid for the FLAC subset. The subset rules - * for valid sample rates are slightly more complex since the rate has to - * be expressible completely in the frame header. - * - * \param sample_rate The sample rate to test for compliance. - * \retval FLAC__bool - * \c true if the given sample rate conforms to the specification for the - * subset, else \c false. - */ -FLAC_API FLAC__bool FLAC__format_sample_rate_is_subset(unsigned sample_rate); - -/** Check a Vorbis comment entry name to see if it conforms to the Vorbis - * comment specification. - * - * Vorbis comment names must be composed only of characters from - * [0x20-0x3C,0x3E-0x7D]. - * - * \param name A NUL-terminated string to be checked. - * \assert - * \code name != NULL \endcode - * \retval FLAC__bool - * \c false if entry name is illegal, else \c true. - */ -FLAC_API FLAC__bool FLAC__format_vorbiscomment_entry_name_is_legal(const char *name); - -/** Check a Vorbis comment entry value to see if it conforms to the Vorbis - * comment specification. - * - * Vorbis comment values must be valid UTF-8 sequences. - * - * \param value A string to be checked. - * \param length A the length of \a value in bytes. May be - * \c (unsigned)(-1) to indicate that \a value is a plain - * UTF-8 NUL-terminated string. - * \assert - * \code value != NULL \endcode - * \retval FLAC__bool - * \c false if entry name is illegal, else \c true. - */ -FLAC_API FLAC__bool FLAC__format_vorbiscomment_entry_value_is_legal(const FLAC__byte *value, unsigned length); - -/** Check a Vorbis comment entry to see if it conforms to the Vorbis - * comment specification. - * - * Vorbis comment entries must be of the form 'name=value', and 'name' and - * 'value' must be legal according to - * FLAC__format_vorbiscomment_entry_name_is_legal() and - * FLAC__format_vorbiscomment_entry_value_is_legal() respectively. - * - * \param entry An entry to be checked. - * \param length The length of \a entry in bytes. - * \assert - * \code value != NULL \endcode - * \retval FLAC__bool - * \c false if entry name is illegal, else \c true. - */ -FLAC_API FLAC__bool FLAC__format_vorbiscomment_entry_is_legal(const FLAC__byte *entry, unsigned length); - -/** Check a seek table to see if it conforms to the FLAC specification. - * See the format specification for limits on the contents of the - * seek table. - * - * \param seek_table A pointer to a seek table to be checked. - * \assert - * \code seek_table != NULL \endcode - * \retval FLAC__bool - * \c false if seek table is illegal, else \c true. - */ -FLAC_API FLAC__bool FLAC__format_seektable_is_legal(const FLAC__StreamMetadata_SeekTable *seek_table); - -/** Sort a seek table's seek points according to the format specification. - * This includes a "unique-ification" step to remove duplicates, i.e. - * seek points with identical \a sample_number values. Duplicate seek - * points are converted into placeholder points and sorted to the end of - * the table. - * - * \param seek_table A pointer to a seek table to be sorted. - * \assert - * \code seek_table != NULL \endcode - * \retval unsigned - * The number of duplicate seek points converted into placeholders. - */ -FLAC_API unsigned FLAC__format_seektable_sort(FLAC__StreamMetadata_SeekTable *seek_table); - -/** Check a cue sheet to see if it conforms to the FLAC specification. - * See the format specification for limits on the contents of the - * cue sheet. - * - * \param cue_sheet A pointer to an existing cue sheet to be checked. - * \param check_cd_da_subset If \c true, check CUESHEET against more - * stringent requirements for a CD-DA (audio) disc. - * \param violation Address of a pointer to a string. If there is a - * violation, a pointer to a string explanation of the - * violation will be returned here. \a violation may be - * \c NULL if you don't need the returned string. Do not - * free the returned string; it will always point to static - * data. - * \assert - * \code cue_sheet != NULL \endcode - * \retval FLAC__bool - * \c false if cue sheet is illegal, else \c true. - */ -FLAC_API FLAC__bool FLAC__format_cuesheet_is_legal(const FLAC__StreamMetadata_CueSheet *cue_sheet, FLAC__bool check_cd_da_subset, const char **violation); - -/** Check picture data to see if it conforms to the FLAC specification. - * See the format specification for limits on the contents of the - * PICTURE block. - * - * \param picture A pointer to existing picture data to be checked. - * \param violation Address of a pointer to a string. If there is a - * violation, a pointer to a string explanation of the - * violation will be returned here. \a violation may be - * \c NULL if you don't need the returned string. Do not - * free the returned string; it will always point to static - * data. - * \assert - * \code picture != NULL \endcode - * \retval FLAC__bool - * \c false if picture data is illegal, else \c true. - */ -FLAC_API FLAC__bool FLAC__format_picture_is_legal(const FLAC__StreamMetadata_Picture *picture, const char **violation); - -/* \} */ - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/FLAC/FLAC/ordinals.h b/FLAC/FLAC/ordinals.h deleted file mode 100644 index b3ea5928c..000000000 --- a/FLAC/FLAC/ordinals.h +++ /dev/null @@ -1,80 +0,0 @@ -/* libFLAC - Free Lossless Audio Codec library - * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 Josh Coalson - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * - Neither the name of the Xiph.org Foundation nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef FLAC__ORDINALS_H -#define FLAC__ORDINALS_H - -#if !(defined(_MSC_VER) || defined(__BORLANDC__) || defined(__EMX__)) -#include -#endif - -typedef signed char FLAC__int8; -typedef unsigned char FLAC__uint8; - -#if defined(_MSC_VER) || defined(__BORLANDC__) -typedef __int16 FLAC__int16; -typedef __int32 FLAC__int32; -typedef __int64 FLAC__int64; -typedef unsigned __int16 FLAC__uint16; -typedef unsigned __int32 FLAC__uint32; -typedef unsigned __int64 FLAC__uint64; -#elif defined(__EMX__) -typedef short FLAC__int16; -typedef long FLAC__int32; -typedef long long FLAC__int64; -typedef unsigned short FLAC__uint16; -typedef unsigned long FLAC__uint32; -typedef unsigned long long FLAC__uint64; -#else -typedef int16_t FLAC__int16; -typedef int32_t FLAC__int32; -typedef int64_t FLAC__int64; -typedef uint16_t FLAC__uint16; -typedef uint32_t FLAC__uint32; -typedef uint64_t FLAC__uint64; -#endif - -typedef int FLAC__bool; - -typedef FLAC__uint8 FLAC__byte; - -#ifdef true -#undef true -#endif -#ifdef false -#undef false -#endif -#ifndef __cplusplus -#define true 1 -#define false 0 -#endif - -#endif diff --git a/FLAC/FLAC/stream_decoder.h b/FLAC/FLAC/stream_decoder.h deleted file mode 100644 index 5818fdd33..000000000 --- a/FLAC/FLAC/stream_decoder.h +++ /dev/null @@ -1,1559 +0,0 @@ -/* libFLAC - Free Lossless Audio Codec library - * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 Josh Coalson - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * - Neither the name of the Xiph.org Foundation nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef FLAC__STREAM_DECODER_H -#define FLAC__STREAM_DECODER_H - -#include /* for FILE */ -#include "export.h" -#include "format.h" - -#ifdef __cplusplus -extern "C" { -#endif - - -/** \file include/FLAC/stream_decoder.h - * - * \brief - * This module contains the functions which implement the stream - * decoder. - * - * See the detailed documentation in the - * \link flac_stream_decoder stream decoder \endlink module. - */ - -/** \defgroup flac_decoder FLAC/ \*_decoder.h: decoder interfaces - * \ingroup flac - * - * \brief - * This module describes the decoder layers provided by libFLAC. - * - * The stream decoder can be used to decode complete streams either from - * the client via callbacks, or directly from a file, depending on how - * it is initialized. When decoding via callbacks, the client provides - * callbacks for reading FLAC data and writing decoded samples, and - * handling metadata and errors. If the client also supplies seek-related - * callback, the decoder function for sample-accurate seeking within the - * FLAC input is also available. When decoding from a file, the client - * needs only supply a filename or open \c FILE* and write/metadata/error - * callbacks; the rest of the callbacks are supplied internally. For more - * info see the \link flac_stream_decoder stream decoder \endlink module. - */ - -/** \defgroup flac_stream_decoder FLAC/stream_decoder.h: stream decoder interface - * \ingroup flac_decoder - * - * \brief - * This module contains the functions which implement the stream - * decoder. - * - * The stream decoder can decode native FLAC, and optionally Ogg FLAC - * (check FLAC_API_SUPPORTS_OGG_FLAC) streams and files. - * - * The basic usage of this decoder is as follows: - * - The program creates an instance of a decoder using - * FLAC__stream_decoder_new(). - * - The program overrides the default settings using - * FLAC__stream_decoder_set_*() functions. - * - The program initializes the instance to validate the settings and - * prepare for decoding using - * - FLAC__stream_decoder_init_stream() or FLAC__stream_decoder_init_FILE() - * or FLAC__stream_decoder_init_file() for native FLAC, - * - FLAC__stream_decoder_init_ogg_stream() or FLAC__stream_decoder_init_ogg_FILE() - * or FLAC__stream_decoder_init_ogg_file() for Ogg FLAC - * - The program calls the FLAC__stream_decoder_process_*() functions - * to decode data, which subsequently calls the callbacks. - * - The program finishes the decoding with FLAC__stream_decoder_finish(), - * which flushes the input and output and resets the decoder to the - * uninitialized state. - * - The instance may be used again or deleted with - * FLAC__stream_decoder_delete(). - * - * In more detail, the program will create a new instance by calling - * FLAC__stream_decoder_new(), then call FLAC__stream_decoder_set_*() - * functions to override the default decoder options, and call - * one of the FLAC__stream_decoder_init_*() functions. - * - * There are three initialization functions for native FLAC, one for - * setting up the decoder to decode FLAC data from the client via - * callbacks, and two for decoding directly from a FLAC file. - * - * For decoding via callbacks, use FLAC__stream_decoder_init_stream(). - * You must also supply several callbacks for handling I/O. Some (like - * seeking) are optional, depending on the capabilities of the input. - * - * For decoding directly from a file, use FLAC__stream_decoder_init_FILE() - * or FLAC__stream_decoder_init_file(). Then you must only supply an open - * \c FILE* or filename and fewer callbacks; the decoder will handle - * the other callbacks internally. - * - * There are three similarly-named init functions for decoding from Ogg - * FLAC streams. Check \c FLAC_API_SUPPORTS_OGG_FLAC to find out if the - * library has been built with Ogg support. - * - * Once the decoder is initialized, your program will call one of several - * functions to start the decoding process: - * - * - FLAC__stream_decoder_process_single() - Tells the decoder to process at - * most one metadata block or audio frame and return, calling either the - * metadata callback or write callback, respectively, once. If the decoder - * loses sync it will return with only the error callback being called. - * - FLAC__stream_decoder_process_until_end_of_metadata() - Tells the decoder - * to process the stream from the current location and stop upon reaching - * the first audio frame. The client will get one metadata, write, or error - * callback per metadata block, audio frame, or sync error, respectively. - * - FLAC__stream_decoder_process_until_end_of_stream() - Tells the decoder - * to process the stream from the current location until the read callback - * returns FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM or - * FLAC__STREAM_DECODER_READ_STATUS_ABORT. The client will get one metadata, - * write, or error callback per metadata block, audio frame, or sync error, - * respectively. - * - * When the decoder has finished decoding (normally or through an abort), - * the instance is finished by calling FLAC__stream_decoder_finish(), which - * ensures the decoder is in the correct state and frees memory. Then the - * instance may be deleted with FLAC__stream_decoder_delete() or initialized - * again to decode another stream. - * - * Seeking is exposed through the FLAC__stream_decoder_seek_absolute() method. - * At any point after the stream decoder has been initialized, the client can - * call this function to seek to an exact sample within the stream. - * Subsequently, the first time the write callback is called it will be - * passed a (possibly partial) block starting at that sample. - * - * If the client cannot seek via the callback interface provided, but still - * has another way of seeking, it can flush the decoder using - * FLAC__stream_decoder_flush() and start feeding data from the new position - * through the read callback. - * - * The stream decoder also provides MD5 signature checking. If this is - * turned on before initialization, FLAC__stream_decoder_finish() will - * report when the decoded MD5 signature does not match the one stored - * in the STREAMINFO block. MD5 checking is automatically turned off - * (until the next FLAC__stream_decoder_reset()) if there is no signature - * in the STREAMINFO block or when a seek is attempted. - * - * The FLAC__stream_decoder_set_metadata_*() functions deserve special - * attention. By default, the decoder only calls the metadata_callback for - * the STREAMINFO block. These functions allow you to tell the decoder - * explicitly which blocks to parse and return via the metadata_callback - * and/or which to skip. Use a FLAC__stream_decoder_set_metadata_respond_all(), - * FLAC__stream_decoder_set_metadata_ignore() ... or FLAC__stream_decoder_set_metadata_ignore_all(), - * FLAC__stream_decoder_set_metadata_respond() ... sequence to exactly specify - * which blocks to return. Remember that metadata blocks can potentially - * be big (for example, cover art) so filtering out the ones you don't - * use can reduce the memory requirements of the decoder. Also note the - * special forms FLAC__stream_decoder_set_metadata_respond_application(id) - * and FLAC__stream_decoder_set_metadata_ignore_application(id) for - * filtering APPLICATION blocks based on the application ID. - * - * STREAMINFO and SEEKTABLE blocks are always parsed and used internally, but - * they still can legally be filtered from the metadata_callback. - * - * \note - * The "set" functions may only be called when the decoder is in the - * state FLAC__STREAM_DECODER_UNINITIALIZED, i.e. after - * FLAC__stream_decoder_new() or FLAC__stream_decoder_finish(), but - * before FLAC__stream_decoder_init_*(). If this is the case they will - * return \c true, otherwise \c false. - * - * \note - * FLAC__stream_decoder_finish() resets all settings to the constructor - * defaults, including the callbacks. - * - * \{ - */ - - -/** State values for a FLAC__StreamDecoder - * - * The decoder's state can be obtained by calling FLAC__stream_decoder_get_state(). - */ -typedef enum { - - FLAC__STREAM_DECODER_SEARCH_FOR_METADATA = 0, - /**< The decoder is ready to search for metadata. */ - - FLAC__STREAM_DECODER_READ_METADATA, - /**< The decoder is ready to or is in the process of reading metadata. */ - - FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC, - /**< The decoder is ready to or is in the process of searching for the - * frame sync code. - */ - - FLAC__STREAM_DECODER_READ_FRAME, - /**< The decoder is ready to or is in the process of reading a frame. */ - - FLAC__STREAM_DECODER_END_OF_STREAM, - /**< The decoder has reached the end of the stream. */ - - FLAC__STREAM_DECODER_OGG_ERROR, - /**< An error occurred in the underlying Ogg layer. */ - - FLAC__STREAM_DECODER_SEEK_ERROR, - /**< An error occurred while seeking. The decoder must be flushed - * with FLAC__stream_decoder_flush() or reset with - * FLAC__stream_decoder_reset() before decoding can continue. - */ - - FLAC__STREAM_DECODER_ABORTED, - /**< The decoder was aborted by the read callback. */ - - FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR, - /**< An error occurred allocating memory. The decoder is in an invalid - * state and can no longer be used. - */ - - FLAC__STREAM_DECODER_UNINITIALIZED - /**< The decoder is in the uninitialized state; one of the - * FLAC__stream_decoder_init_*() functions must be called before samples - * can be processed. - */ - -} FLAC__StreamDecoderState; - -/** Maps a FLAC__StreamDecoderState to a C string. - * - * Using a FLAC__StreamDecoderState as the index to this array - * will give the string equivalent. The contents should not be modified. - */ -extern FLAC_API const char * const FLAC__StreamDecoderStateString[]; - - -/** Possible return values for the FLAC__stream_decoder_init_*() functions. - */ -typedef enum { - - FLAC__STREAM_DECODER_INIT_STATUS_OK = 0, - /**< Initialization was successful. */ - - FLAC__STREAM_DECODER_INIT_STATUS_UNSUPPORTED_CONTAINER, - /**< The library was not compiled with support for the given container - * format. - */ - - FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS, - /**< A required callback was not supplied. */ - - FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR, - /**< An error occurred allocating memory. */ - - FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE, - /**< fopen() failed in FLAC__stream_decoder_init_file() or - * FLAC__stream_decoder_init_ogg_file(). */ - - FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED - /**< FLAC__stream_decoder_init_*() was called when the decoder was - * already initialized, usually because - * FLAC__stream_decoder_finish() was not called. - */ - -} FLAC__StreamDecoderInitStatus; - -/** Maps a FLAC__StreamDecoderInitStatus to a C string. - * - * Using a FLAC__StreamDecoderInitStatus as the index to this array - * will give the string equivalent. The contents should not be modified. - */ -extern FLAC_API const char * const FLAC__StreamDecoderInitStatusString[]; - - -/** Return values for the FLAC__StreamDecoder read callback. - */ -typedef enum { - - FLAC__STREAM_DECODER_READ_STATUS_CONTINUE, - /**< The read was OK and decoding can continue. */ - - FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM, - /**< The read was attempted while at the end of the stream. Note that - * the client must only return this value when the read callback was - * called when already at the end of the stream. Otherwise, if the read - * itself moves to the end of the stream, the client should still return - * the data and \c FLAC__STREAM_DECODER_READ_STATUS_CONTINUE, and then on - * the next read callback it should return - * \c FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM with a byte count - * of \c 0. - */ - - FLAC__STREAM_DECODER_READ_STATUS_ABORT - /**< An unrecoverable error occurred. The decoder will return from the process call. */ - -} FLAC__StreamDecoderReadStatus; - -/** Maps a FLAC__StreamDecoderReadStatus to a C string. - * - * Using a FLAC__StreamDecoderReadStatus as the index to this array - * will give the string equivalent. The contents should not be modified. - */ -extern FLAC_API const char * const FLAC__StreamDecoderReadStatusString[]; - - -/** Return values for the FLAC__StreamDecoder seek callback. - */ -typedef enum { - - FLAC__STREAM_DECODER_SEEK_STATUS_OK, - /**< The seek was OK and decoding can continue. */ - - FLAC__STREAM_DECODER_SEEK_STATUS_ERROR, - /**< An unrecoverable error occurred. The decoder will return from the process call. */ - - FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED - /**< Client does not support seeking. */ - -} FLAC__StreamDecoderSeekStatus; - -/** Maps a FLAC__StreamDecoderSeekStatus to a C string. - * - * Using a FLAC__StreamDecoderSeekStatus as the index to this array - * will give the string equivalent. The contents should not be modified. - */ -extern FLAC_API const char * const FLAC__StreamDecoderSeekStatusString[]; - - -/** Return values for the FLAC__StreamDecoder tell callback. - */ -typedef enum { - - FLAC__STREAM_DECODER_TELL_STATUS_OK, - /**< The tell was OK and decoding can continue. */ - - FLAC__STREAM_DECODER_TELL_STATUS_ERROR, - /**< An unrecoverable error occurred. The decoder will return from the process call. */ - - FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED - /**< Client does not support telling the position. */ - -} FLAC__StreamDecoderTellStatus; - -/** Maps a FLAC__StreamDecoderTellStatus to a C string. - * - * Using a FLAC__StreamDecoderTellStatus as the index to this array - * will give the string equivalent. The contents should not be modified. - */ -extern FLAC_API const char * const FLAC__StreamDecoderTellStatusString[]; - - -/** Return values for the FLAC__StreamDecoder length callback. - */ -typedef enum { - - FLAC__STREAM_DECODER_LENGTH_STATUS_OK, - /**< The length call was OK and decoding can continue. */ - - FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR, - /**< An unrecoverable error occurred. The decoder will return from the process call. */ - - FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED - /**< Client does not support reporting the length. */ - -} FLAC__StreamDecoderLengthStatus; - -/** Maps a FLAC__StreamDecoderLengthStatus to a C string. - * - * Using a FLAC__StreamDecoderLengthStatus as the index to this array - * will give the string equivalent. The contents should not be modified. - */ -extern FLAC_API const char * const FLAC__StreamDecoderLengthStatusString[]; - - -/** Return values for the FLAC__StreamDecoder write callback. - */ -typedef enum { - - FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE, - /**< The write was OK and decoding can continue. */ - - FLAC__STREAM_DECODER_WRITE_STATUS_ABORT - /**< An unrecoverable error occurred. The decoder will return from the process call. */ - -} FLAC__StreamDecoderWriteStatus; - -/** Maps a FLAC__StreamDecoderWriteStatus to a C string. - * - * Using a FLAC__StreamDecoderWriteStatus as the index to this array - * will give the string equivalent. The contents should not be modified. - */ -extern FLAC_API const char * const FLAC__StreamDecoderWriteStatusString[]; - - -/** Possible values passed back to the FLAC__StreamDecoder error callback. - * \c FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC is the generic catch- - * all. The rest could be caused by bad sync (false synchronization on - * data that is not the start of a frame) or corrupted data. The error - * itself is the decoder's best guess at what happened assuming a correct - * sync. For example \c FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER - * could be caused by a correct sync on the start of a frame, but some - * data in the frame header was corrupted. Or it could be the result of - * syncing on a point the stream that looked like the starting of a frame - * but was not. \c FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM - * could be because the decoder encountered a valid frame made by a future - * version of the encoder which it cannot parse, or because of a false - * sync making it appear as though an encountered frame was generated by - * a future encoder. - */ -typedef enum { - - FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, - /**< An error in the stream caused the decoder to lose synchronization. */ - - FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, - /**< The decoder encountered a corrupted frame header. */ - - FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH, - /**< The frame's data did not match the CRC in the footer. */ - - FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM - /**< The decoder encountered reserved fields in use in the stream. */ - -} FLAC__StreamDecoderErrorStatus; - -/** Maps a FLAC__StreamDecoderErrorStatus to a C string. - * - * Using a FLAC__StreamDecoderErrorStatus as the index to this array - * will give the string equivalent. The contents should not be modified. - */ -extern FLAC_API const char * const FLAC__StreamDecoderErrorStatusString[]; - - -/*********************************************************************** - * - * class FLAC__StreamDecoder - * - ***********************************************************************/ - -struct FLAC__StreamDecoderProtected; -struct FLAC__StreamDecoderPrivate; -/** The opaque structure definition for the stream decoder type. - * See the \link flac_stream_decoder stream decoder module \endlink - * for a detailed description. - */ -typedef struct { - struct FLAC__StreamDecoderProtected *protected_; /* avoid the C++ keyword 'protected' */ - struct FLAC__StreamDecoderPrivate *private_; /* avoid the C++ keyword 'private' */ -} FLAC__StreamDecoder; - -/** Signature for the read callback. - * - * A function pointer matching this signature must be passed to - * FLAC__stream_decoder_init*_stream(). The supplied function will be - * called when the decoder needs more input data. The address of the - * buffer to be filled is supplied, along with the number of bytes the - * buffer can hold. The callback may choose to supply less data and - * modify the byte count but must be careful not to overflow the buffer. - * The callback then returns a status code chosen from - * FLAC__StreamDecoderReadStatus. - * - * Here is an example of a read callback for stdio streams: - * \code - * FLAC__StreamDecoderReadStatus read_cb(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data) - * { - * FILE *file = ((MyClientData*)client_data)->file; - * if(*bytes > 0) { - * *bytes = fread(buffer, sizeof(FLAC__byte), *bytes, file); - * if(ferror(file)) - * return FLAC__STREAM_DECODER_READ_STATUS_ABORT; - * else if(*bytes == 0) - * return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM; - * else - * return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; - * } - * else - * return FLAC__STREAM_DECODER_READ_STATUS_ABORT; - * } - * \endcode - * - * \note In general, FLAC__StreamDecoder functions which change the - * state should not be called on the \a decoder while in the callback. - * - * \param decoder The decoder instance calling the callback. - * \param buffer A pointer to a location for the callee to store - * data to be decoded. - * \param bytes A pointer to the size of the buffer. On entry - * to the callback, it contains the maximum number - * of bytes that may be stored in \a buffer. The - * callee must set it to the actual number of bytes - * stored (0 in case of error or end-of-stream) before - * returning. - * \param client_data The callee's client data set through - * FLAC__stream_decoder_init_*(). - * \retval FLAC__StreamDecoderReadStatus - * The callee's return status. Note that the callback should return - * \c FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM if and only if - * zero bytes were read and there is no more data to be read. - */ -typedef FLAC__StreamDecoderReadStatus (*FLAC__StreamDecoderReadCallback)(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data); - -/** Signature for the seek callback. - * - * A function pointer matching this signature may be passed to - * FLAC__stream_decoder_init*_stream(). The supplied function will be - * called when the decoder needs to seek the input stream. The decoder - * will pass the absolute byte offset to seek to, 0 meaning the - * beginning of the stream. - * - * Here is an example of a seek callback for stdio streams: - * \code - * FLAC__StreamDecoderSeekStatus seek_cb(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data) - * { - * FILE *file = ((MyClientData*)client_data)->file; - * if(file == stdin) - * return FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED; - * else if(fseeko(file, (off_t)absolute_byte_offset, SEEK_SET) < 0) - * return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR; - * else - * return FLAC__STREAM_DECODER_SEEK_STATUS_OK; - * } - * \endcode - * - * \note In general, FLAC__StreamDecoder functions which change the - * state should not be called on the \a decoder while in the callback. - * - * \param decoder The decoder instance calling the callback. - * \param absolute_byte_offset The offset from the beginning of the stream - * to seek to. - * \param client_data The callee's client data set through - * FLAC__stream_decoder_init_*(). - * \retval FLAC__StreamDecoderSeekStatus - * The callee's return status. - */ -typedef FLAC__StreamDecoderSeekStatus (*FLAC__StreamDecoderSeekCallback)(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data); - -/** Signature for the tell callback. - * - * A function pointer matching this signature may be passed to - * FLAC__stream_decoder_init*_stream(). The supplied function will be - * called when the decoder wants to know the current position of the - * stream. The callback should return the byte offset from the - * beginning of the stream. - * - * Here is an example of a tell callback for stdio streams: - * \code - * FLAC__StreamDecoderTellStatus tell_cb(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data) - * { - * FILE *file = ((MyClientData*)client_data)->file; - * off_t pos; - * if(file == stdin) - * return FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED; - * else if((pos = ftello(file)) < 0) - * return FLAC__STREAM_DECODER_TELL_STATUS_ERROR; - * else { - * *absolute_byte_offset = (FLAC__uint64)pos; - * return FLAC__STREAM_DECODER_TELL_STATUS_OK; - * } - * } - * \endcode - * - * \note In general, FLAC__StreamDecoder functions which change the - * state should not be called on the \a decoder while in the callback. - * - * \param decoder The decoder instance calling the callback. - * \param absolute_byte_offset A pointer to storage for the current offset - * from the beginning of the stream. - * \param client_data The callee's client data set through - * FLAC__stream_decoder_init_*(). - * \retval FLAC__StreamDecoderTellStatus - * The callee's return status. - */ -typedef FLAC__StreamDecoderTellStatus (*FLAC__StreamDecoderTellCallback)(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data); - -/** Signature for the length callback. - * - * A function pointer matching this signature may be passed to - * FLAC__stream_decoder_init*_stream(). The supplied function will be - * called when the decoder wants to know the total length of the stream - * in bytes. - * - * Here is an example of a length callback for stdio streams: - * \code - * FLAC__StreamDecoderLengthStatus length_cb(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data) - * { - * FILE *file = ((MyClientData*)client_data)->file; - * struct stat filestats; - * - * if(file == stdin) - * return FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED; - * else if(fstat(fileno(file), &filestats) != 0) - * return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR; - * else { - * *stream_length = (FLAC__uint64)filestats.st_size; - * return FLAC__STREAM_DECODER_LENGTH_STATUS_OK; - * } - * } - * \endcode - * - * \note In general, FLAC__StreamDecoder functions which change the - * state should not be called on the \a decoder while in the callback. - * - * \param decoder The decoder instance calling the callback. - * \param stream_length A pointer to storage for the length of the stream - * in bytes. - * \param client_data The callee's client data set through - * FLAC__stream_decoder_init_*(). - * \retval FLAC__StreamDecoderLengthStatus - * The callee's return status. - */ -typedef FLAC__StreamDecoderLengthStatus (*FLAC__StreamDecoderLengthCallback)(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data); - -/** Signature for the EOF callback. - * - * A function pointer matching this signature may be passed to - * FLAC__stream_decoder_init*_stream(). The supplied function will be - * called when the decoder needs to know if the end of the stream has - * been reached. - * - * Here is an example of a EOF callback for stdio streams: - * FLAC__bool eof_cb(const FLAC__StreamDecoder *decoder, void *client_data) - * \code - * { - * FILE *file = ((MyClientData*)client_data)->file; - * return feof(file)? true : false; - * } - * \endcode - * - * \note In general, FLAC__StreamDecoder functions which change the - * state should not be called on the \a decoder while in the callback. - * - * \param decoder The decoder instance calling the callback. - * \param client_data The callee's client data set through - * FLAC__stream_decoder_init_*(). - * \retval FLAC__bool - * \c true if the currently at the end of the stream, else \c false. - */ -typedef FLAC__bool (*FLAC__StreamDecoderEofCallback)(const FLAC__StreamDecoder *decoder, void *client_data); - -/** Signature for the write callback. - * - * A function pointer matching this signature must be passed to one of - * the FLAC__stream_decoder_init_*() functions. - * The supplied function will be called when the decoder has decoded a - * single audio frame. The decoder will pass the frame metadata as well - * as an array of pointers (one for each channel) pointing to the - * decoded audio. - * - * \note In general, FLAC__StreamDecoder functions which change the - * state should not be called on the \a decoder while in the callback. - * - * \param decoder The decoder instance calling the callback. - * \param frame The description of the decoded frame. See - * FLAC__Frame. - * \param buffer An array of pointers to decoded channels of data. - * Each pointer will point to an array of signed - * samples of length \a frame->header.blocksize. - * Channels will be ordered according to the FLAC - * specification; see the documentation for the - * frame header. - * \param client_data The callee's client data set through - * FLAC__stream_decoder_init_*(). - * \retval FLAC__StreamDecoderWriteStatus - * The callee's return status. - */ -typedef FLAC__StreamDecoderWriteStatus (*FLAC__StreamDecoderWriteCallback)(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data); - -/** Signature for the metadata callback. - * - * A function pointer matching this signature must be passed to one of - * the FLAC__stream_decoder_init_*() functions. - * The supplied function will be called when the decoder has decoded a - * metadata block. In a valid FLAC file there will always be one - * \c STREAMINFO block, followed by zero or more other metadata blocks. - * These will be supplied by the decoder in the same order as they - * appear in the stream and always before the first audio frame (i.e. - * write callback). The metadata block that is passed in must not be - * modified, and it doesn't live beyond the callback, so you should make - * a copy of it with FLAC__metadata_object_clone() if you will need it - * elsewhere. Since metadata blocks can potentially be large, by - * default the decoder only calls the metadata callback for the - * \c STREAMINFO block; you can instruct the decoder to pass or filter - * other blocks with FLAC__stream_decoder_set_metadata_*() calls. - * - * \note In general, FLAC__StreamDecoder functions which change the - * state should not be called on the \a decoder while in the callback. - * - * \param decoder The decoder instance calling the callback. - * \param metadata The decoded metadata block. - * \param client_data The callee's client data set through - * FLAC__stream_decoder_init_*(). - */ -typedef void (*FLAC__StreamDecoderMetadataCallback)(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data); - -/** Signature for the error callback. - * - * A function pointer matching this signature must be passed to one of - * the FLAC__stream_decoder_init_*() functions. - * The supplied function will be called whenever an error occurs during - * decoding. - * - * \note In general, FLAC__StreamDecoder functions which change the - * state should not be called on the \a decoder while in the callback. - * - * \param decoder The decoder instance calling the callback. - * \param status The error encountered by the decoder. - * \param client_data The callee's client data set through - * FLAC__stream_decoder_init_*(). - */ -typedef void (*FLAC__StreamDecoderErrorCallback)(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data); - - -/*********************************************************************** - * - * Class constructor/destructor - * - ***********************************************************************/ - -/** Create a new stream decoder instance. The instance is created with - * default settings; see the individual FLAC__stream_decoder_set_*() - * functions for each setting's default. - * - * \retval FLAC__StreamDecoder* - * \c NULL if there was an error allocating memory, else the new instance. - */ -FLAC_API FLAC__StreamDecoder *FLAC__stream_decoder_new(void); - -/** Free a decoder instance. Deletes the object pointed to by \a decoder. - * - * \param decoder A pointer to an existing decoder. - * \assert - * \code decoder != NULL \endcode - */ -FLAC_API void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder); - - -/*********************************************************************** - * - * Public class method prototypes - * - ***********************************************************************/ - -/** Set the serial number for the FLAC stream within the Ogg container. - * The default behavior is to use the serial number of the first Ogg - * page. Setting a serial number here will explicitly specify which - * stream is to be decoded. - * - * \note - * This does not need to be set for native FLAC decoding. - * - * \default \c use serial number of first page - * \param decoder A decoder instance to set. - * \param serial_number See above. - * \assert - * \code decoder != NULL \endcode - * \retval FLAC__bool - * \c false if the decoder is already initialized, else \c true. - */ -FLAC_API FLAC__bool FLAC__stream_decoder_set_ogg_serial_number(FLAC__StreamDecoder *decoder, long serial_number); - -/** Set the "MD5 signature checking" flag. If \c true, the decoder will - * compute the MD5 signature of the unencoded audio data while decoding - * and compare it to the signature from the STREAMINFO block, if it - * exists, during FLAC__stream_decoder_finish(). - * - * MD5 signature checking will be turned off (until the next - * FLAC__stream_decoder_reset()) if there is no signature in the - * STREAMINFO block or when a seek is attempted. - * - * Clients that do not use the MD5 check should leave this off to speed - * up decoding. - * - * \default \c false - * \param decoder A decoder instance to set. - * \param value Flag value (see above). - * \assert - * \code decoder != NULL \endcode - * \retval FLAC__bool - * \c false if the decoder is already initialized, else \c true. - */ -FLAC_API FLAC__bool FLAC__stream_decoder_set_md5_checking(FLAC__StreamDecoder *decoder, FLAC__bool value); - -/** Direct the decoder to pass on all metadata blocks of type \a type. - * - * \default By default, only the \c STREAMINFO block is returned via the - * metadata callback. - * \param decoder A decoder instance to set. - * \param type See above. - * \assert - * \code decoder != NULL \endcode - * \a type is valid - * \retval FLAC__bool - * \c false if the decoder is already initialized, else \c true. - */ -FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond(FLAC__StreamDecoder *decoder, FLAC__MetadataType type); - -/** Direct the decoder to pass on all APPLICATION metadata blocks of the - * given \a id. - * - * \default By default, only the \c STREAMINFO block is returned via the - * metadata callback. - * \param decoder A decoder instance to set. - * \param id See above. - * \assert - * \code decoder != NULL \endcode - * \code id != NULL \endcode - * \retval FLAC__bool - * \c false if the decoder is already initialized, else \c true. - */ -FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4]); - -/** Direct the decoder to pass on all metadata blocks of any type. - * - * \default By default, only the \c STREAMINFO block is returned via the - * metadata callback. - * \param decoder A decoder instance to set. - * \assert - * \code decoder != NULL \endcode - * \retval FLAC__bool - * \c false if the decoder is already initialized, else \c true. - */ -FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_all(FLAC__StreamDecoder *decoder); - -/** Direct the decoder to filter out all metadata blocks of type \a type. - * - * \default By default, only the \c STREAMINFO block is returned via the - * metadata callback. - * \param decoder A decoder instance to set. - * \param type See above. - * \assert - * \code decoder != NULL \endcode - * \a type is valid - * \retval FLAC__bool - * \c false if the decoder is already initialized, else \c true. - */ -FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore(FLAC__StreamDecoder *decoder, FLAC__MetadataType type); - -/** Direct the decoder to filter out all APPLICATION metadata blocks of - * the given \a id. - * - * \default By default, only the \c STREAMINFO block is returned via the - * metadata callback. - * \param decoder A decoder instance to set. - * \param id See above. - * \assert - * \code decoder != NULL \endcode - * \code id != NULL \endcode - * \retval FLAC__bool - * \c false if the decoder is already initialized, else \c true. - */ -FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4]); - -/** Direct the decoder to filter out all metadata blocks of any type. - * - * \default By default, only the \c STREAMINFO block is returned via the - * metadata callback. - * \param decoder A decoder instance to set. - * \assert - * \code decoder != NULL \endcode - * \retval FLAC__bool - * \c false if the decoder is already initialized, else \c true. - */ -FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_all(FLAC__StreamDecoder *decoder); - -/** Get the current decoder state. - * - * \param decoder A decoder instance to query. - * \assert - * \code decoder != NULL \endcode - * \retval FLAC__StreamDecoderState - * The current decoder state. - */ -FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder); - -/** Get the current decoder state as a C string. - * - * \param decoder A decoder instance to query. - * \assert - * \code decoder != NULL \endcode - * \retval const char * - * The decoder state as a C string. Do not modify the contents. - */ -FLAC_API const char *FLAC__stream_decoder_get_resolved_state_string(const FLAC__StreamDecoder *decoder); - -/** Get the "MD5 signature checking" flag. - * This is the value of the setting, not whether or not the decoder is - * currently checking the MD5 (remember, it can be turned off automatically - * by a seek). When the decoder is reset the flag will be restored to the - * value returned by this function. - * - * \param decoder A decoder instance to query. - * \assert - * \code decoder != NULL \endcode - * \retval FLAC__bool - * See above. - */ -FLAC_API FLAC__bool FLAC__stream_decoder_get_md5_checking(const FLAC__StreamDecoder *decoder); - -/** Get the total number of samples in the stream being decoded. - * Will only be valid after decoding has started and will contain the - * value from the \c STREAMINFO block. A value of \c 0 means "unknown". - * - * \param decoder A decoder instance to query. - * \assert - * \code decoder != NULL \endcode - * \retval unsigned - * See above. - */ -FLAC_API FLAC__uint64 FLAC__stream_decoder_get_total_samples(const FLAC__StreamDecoder *decoder); - -/** Get the current number of channels in the stream being decoded. - * Will only be valid after decoding has started and will contain the - * value from the most recently decoded frame header. - * - * \param decoder A decoder instance to query. - * \assert - * \code decoder != NULL \endcode - * \retval unsigned - * See above. - */ -FLAC_API unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder); - -/** Get the current channel assignment in the stream being decoded. - * Will only be valid after decoding has started and will contain the - * value from the most recently decoded frame header. - * - * \param decoder A decoder instance to query. - * \assert - * \code decoder != NULL \endcode - * \retval FLAC__ChannelAssignment - * See above. - */ -FLAC_API FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder); - -/** Get the current sample resolution in the stream being decoded. - * Will only be valid after decoding has started and will contain the - * value from the most recently decoded frame header. - * - * \param decoder A decoder instance to query. - * \assert - * \code decoder != NULL \endcode - * \retval unsigned - * See above. - */ -FLAC_API unsigned FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder); - -/** Get the current sample rate in Hz of the stream being decoded. - * Will only be valid after decoding has started and will contain the - * value from the most recently decoded frame header. - * - * \param decoder A decoder instance to query. - * \assert - * \code decoder != NULL \endcode - * \retval unsigned - * See above. - */ -FLAC_API unsigned FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder); - -/** Get the current blocksize of the stream being decoded. - * Will only be valid after decoding has started and will contain the - * value from the most recently decoded frame header. - * - * \param decoder A decoder instance to query. - * \assert - * \code decoder != NULL \endcode - * \retval unsigned - * See above. - */ -FLAC_API unsigned FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder); - -/** Returns the decoder's current read position within the stream. - * The position is the byte offset from the start of the stream. - * Bytes before this position have been fully decoded. Note that - * there may still be undecoded bytes in the decoder's read FIFO. - * The returned position is correct even after a seek. - * - * \warning This function currently only works for native FLAC, - * not Ogg FLAC streams. - * - * \param decoder A decoder instance to query. - * \param position Address at which to return the desired position. - * \assert - * \code decoder != NULL \endcode - * \code position != NULL \endcode - * \retval FLAC__bool - * \c true if successful, \c false if the stream is not native FLAC, - * or there was an error from the 'tell' callback or it returned - * \c FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED. - */ -FLAC_API FLAC__bool FLAC__stream_decoder_get_decode_position(const FLAC__StreamDecoder *decoder, FLAC__uint64 *position); - -/** Initialize the decoder instance to decode native FLAC streams. - * - * This flavor of initialization sets up the decoder to decode from a - * native FLAC stream. I/O is performed via callbacks to the client. - * For decoding from a plain file via filename or open FILE*, - * FLAC__stream_decoder_init_file() and FLAC__stream_decoder_init_FILE() - * provide a simpler interface. - * - * This function should be called after FLAC__stream_decoder_new() and - * FLAC__stream_decoder_set_*() but before any of the - * FLAC__stream_decoder_process_*() functions. Will set and return the - * decoder state, which will be FLAC__STREAM_DECODER_SEARCH_FOR_METADATA - * if initialization succeeded. - * - * \param decoder An uninitialized decoder instance. - * \param read_callback See FLAC__StreamDecoderReadCallback. This - * pointer must not be \c NULL. - * \param seek_callback See FLAC__StreamDecoderSeekCallback. This - * pointer may be \c NULL if seeking is not - * supported. If \a seek_callback is not \c NULL then a - * \a tell_callback, \a length_callback, and \a eof_callback must also be supplied. - * Alternatively, a dummy seek callback that just - * returns \c FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED - * may also be supplied, all though this is slightly - * less efficient for the decoder. - * \param tell_callback See FLAC__StreamDecoderTellCallback. This - * pointer may be \c NULL if not supported by the client. If - * \a seek_callback is not \c NULL then a - * \a tell_callback must also be supplied. - * Alternatively, a dummy tell callback that just - * returns \c FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED - * may also be supplied, all though this is slightly - * less efficient for the decoder. - * \param length_callback See FLAC__StreamDecoderLengthCallback. This - * pointer may be \c NULL if not supported by the client. If - * \a seek_callback is not \c NULL then a - * \a length_callback must also be supplied. - * Alternatively, a dummy length callback that just - * returns \c FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED - * may also be supplied, all though this is slightly - * less efficient for the decoder. - * \param eof_callback See FLAC__StreamDecoderEofCallback. This - * pointer may be \c NULL if not supported by the client. If - * \a seek_callback is not \c NULL then a - * \a eof_callback must also be supplied. - * Alternatively, a dummy length callback that just - * returns \c false - * may also be supplied, all though this is slightly - * less efficient for the decoder. - * \param write_callback See FLAC__StreamDecoderWriteCallback. This - * pointer must not be \c NULL. - * \param metadata_callback See FLAC__StreamDecoderMetadataCallback. This - * pointer may be \c NULL if the callback is not - * desired. - * \param error_callback See FLAC__StreamDecoderErrorCallback. This - * pointer must not be \c NULL. - * \param client_data This value will be supplied to callbacks in their - * \a client_data argument. - * \assert - * \code decoder != NULL \endcode - * \retval FLAC__StreamDecoderInitStatus - * \c FLAC__STREAM_DECODER_INIT_STATUS_OK if initialization was successful; - * see FLAC__StreamDecoderInitStatus for the meanings of other return values. - */ -FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_stream( - FLAC__StreamDecoder *decoder, - FLAC__StreamDecoderReadCallback read_callback, - FLAC__StreamDecoderSeekCallback seek_callback, - FLAC__StreamDecoderTellCallback tell_callback, - FLAC__StreamDecoderLengthCallback length_callback, - FLAC__StreamDecoderEofCallback eof_callback, - FLAC__StreamDecoderWriteCallback write_callback, - FLAC__StreamDecoderMetadataCallback metadata_callback, - FLAC__StreamDecoderErrorCallback error_callback, - void *client_data -); - -/** Initialize the decoder instance to decode Ogg FLAC streams. - * - * This flavor of initialization sets up the decoder to decode from a - * FLAC stream in an Ogg container. I/O is performed via callbacks to the - * client. For decoding from a plain file via filename or open FILE*, - * FLAC__stream_decoder_init_ogg_file() and FLAC__stream_decoder_init_ogg_FILE() - * provide a simpler interface. - * - * This function should be called after FLAC__stream_decoder_new() and - * FLAC__stream_decoder_set_*() but before any of the - * FLAC__stream_decoder_process_*() functions. Will set and return the - * decoder state, which will be FLAC__STREAM_DECODER_SEARCH_FOR_METADATA - * if initialization succeeded. - * - * \note Support for Ogg FLAC in the library is optional. If this - * library has been built without support for Ogg FLAC, this function - * will return \c FLAC__STREAM_DECODER_INIT_STATUS_UNSUPPORTED_CONTAINER. - * - * \param decoder An uninitialized decoder instance. - * \param read_callback See FLAC__StreamDecoderReadCallback. This - * pointer must not be \c NULL. - * \param seek_callback See FLAC__StreamDecoderSeekCallback. This - * pointer may be \c NULL if seeking is not - * supported. If \a seek_callback is not \c NULL then a - * \a tell_callback, \a length_callback, and \a eof_callback must also be supplied. - * Alternatively, a dummy seek callback that just - * returns \c FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED - * may also be supplied, all though this is slightly - * less efficient for the decoder. - * \param tell_callback See FLAC__StreamDecoderTellCallback. This - * pointer may be \c NULL if not supported by the client. If - * \a seek_callback is not \c NULL then a - * \a tell_callback must also be supplied. - * Alternatively, a dummy tell callback that just - * returns \c FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED - * may also be supplied, all though this is slightly - * less efficient for the decoder. - * \param length_callback See FLAC__StreamDecoderLengthCallback. This - * pointer may be \c NULL if not supported by the client. If - * \a seek_callback is not \c NULL then a - * \a length_callback must also be supplied. - * Alternatively, a dummy length callback that just - * returns \c FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED - * may also be supplied, all though this is slightly - * less efficient for the decoder. - * \param eof_callback See FLAC__StreamDecoderEofCallback. This - * pointer may be \c NULL if not supported by the client. If - * \a seek_callback is not \c NULL then a - * \a eof_callback must also be supplied. - * Alternatively, a dummy length callback that just - * returns \c false - * may also be supplied, all though this is slightly - * less efficient for the decoder. - * \param write_callback See FLAC__StreamDecoderWriteCallback. This - * pointer must not be \c NULL. - * \param metadata_callback See FLAC__StreamDecoderMetadataCallback. This - * pointer may be \c NULL if the callback is not - * desired. - * \param error_callback See FLAC__StreamDecoderErrorCallback. This - * pointer must not be \c NULL. - * \param client_data This value will be supplied to callbacks in their - * \a client_data argument. - * \assert - * \code decoder != NULL \endcode - * \retval FLAC__StreamDecoderInitStatus - * \c FLAC__STREAM_DECODER_INIT_STATUS_OK if initialization was successful; - * see FLAC__StreamDecoderInitStatus for the meanings of other return values. - */ -FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_stream( - FLAC__StreamDecoder *decoder, - FLAC__StreamDecoderReadCallback read_callback, - FLAC__StreamDecoderSeekCallback seek_callback, - FLAC__StreamDecoderTellCallback tell_callback, - FLAC__StreamDecoderLengthCallback length_callback, - FLAC__StreamDecoderEofCallback eof_callback, - FLAC__StreamDecoderWriteCallback write_callback, - FLAC__StreamDecoderMetadataCallback metadata_callback, - FLAC__StreamDecoderErrorCallback error_callback, - void *client_data -); - -/** Initialize the decoder instance to decode native FLAC files. - * - * This flavor of initialization sets up the decoder to decode from a - * plain native FLAC file. For non-stdio streams, you must use - * FLAC__stream_decoder_init_stream() and provide callbacks for the I/O. - * - * This function should be called after FLAC__stream_decoder_new() and - * FLAC__stream_decoder_set_*() but before any of the - * FLAC__stream_decoder_process_*() functions. Will set and return the - * decoder state, which will be FLAC__STREAM_DECODER_SEARCH_FOR_METADATA - * if initialization succeeded. - * - * \param decoder An uninitialized decoder instance. - * \param file An open FLAC file. The file should have been - * opened with mode \c "rb" and rewound. The file - * becomes owned by the decoder and should not be - * manipulated by the client while decoding. - * Unless \a file is \c stdin, it will be closed - * when FLAC__stream_decoder_finish() is called. - * Note however that seeking will not work when - * decoding from \c stdout since it is not seekable. - * \param write_callback See FLAC__StreamDecoderWriteCallback. This - * pointer must not be \c NULL. - * \param metadata_callback See FLAC__StreamDecoderMetadataCallback. This - * pointer may be \c NULL if the callback is not - * desired. - * \param error_callback See FLAC__StreamDecoderErrorCallback. This - * pointer must not be \c NULL. - * \param client_data This value will be supplied to callbacks in their - * \a client_data argument. - * \assert - * \code decoder != NULL \endcode - * \code file != NULL \endcode - * \retval FLAC__StreamDecoderInitStatus - * \c FLAC__STREAM_DECODER_INIT_STATUS_OK if initialization was successful; - * see FLAC__StreamDecoderInitStatus for the meanings of other return values. - */ -FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_FILE( - FLAC__StreamDecoder *decoder, - FILE *file, - FLAC__StreamDecoderWriteCallback write_callback, - FLAC__StreamDecoderMetadataCallback metadata_callback, - FLAC__StreamDecoderErrorCallback error_callback, - void *client_data -); - -/** Initialize the decoder instance to decode Ogg FLAC files. - * - * This flavor of initialization sets up the decoder to decode from a - * plain Ogg FLAC file. For non-stdio streams, you must use - * FLAC__stream_decoder_init_ogg_stream() and provide callbacks for the I/O. - * - * This function should be called after FLAC__stream_decoder_new() and - * FLAC__stream_decoder_set_*() but before any of the - * FLAC__stream_decoder_process_*() functions. Will set and return the - * decoder state, which will be FLAC__STREAM_DECODER_SEARCH_FOR_METADATA - * if initialization succeeded. - * - * \note Support for Ogg FLAC in the library is optional. If this - * library has been built without support for Ogg FLAC, this function - * will return \c FLAC__STREAM_DECODER_INIT_STATUS_UNSUPPORTED_CONTAINER. - * - * \param decoder An uninitialized decoder instance. - * \param file An open FLAC file. The file should have been - * opened with mode \c "rb" and rewound. The file - * becomes owned by the decoder and should not be - * manipulated by the client while decoding. - * Unless \a file is \c stdin, it will be closed - * when FLAC__stream_decoder_finish() is called. - * Note however that seeking will not work when - * decoding from \c stdout since it is not seekable. - * \param write_callback See FLAC__StreamDecoderWriteCallback. This - * pointer must not be \c NULL. - * \param metadata_callback See FLAC__StreamDecoderMetadataCallback. This - * pointer may be \c NULL if the callback is not - * desired. - * \param error_callback See FLAC__StreamDecoderErrorCallback. This - * pointer must not be \c NULL. - * \param client_data This value will be supplied to callbacks in their - * \a client_data argument. - * \assert - * \code decoder != NULL \endcode - * \code file != NULL \endcode - * \retval FLAC__StreamDecoderInitStatus - * \c FLAC__STREAM_DECODER_INIT_STATUS_OK if initialization was successful; - * see FLAC__StreamDecoderInitStatus for the meanings of other return values. - */ -FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_FILE( - FLAC__StreamDecoder *decoder, - FILE *file, - FLAC__StreamDecoderWriteCallback write_callback, - FLAC__StreamDecoderMetadataCallback metadata_callback, - FLAC__StreamDecoderErrorCallback error_callback, - void *client_data -); - -/** Initialize the decoder instance to decode native FLAC files. - * - * This flavor of initialization sets up the decoder to decode from a plain - * native FLAC file. If POSIX fopen() semantics are not sufficient, (for - * example, with Unicode filenames on Windows), you must use - * FLAC__stream_decoder_init_FILE(), or FLAC__stream_decoder_init_stream() - * and provide callbacks for the I/O. - * - * This function should be called after FLAC__stream_decoder_new() and - * FLAC__stream_decoder_set_*() but before any of the - * FLAC__stream_decoder_process_*() functions. Will set and return the - * decoder state, which will be FLAC__STREAM_DECODER_SEARCH_FOR_METADATA - * if initialization succeeded. - * - * \param decoder An uninitialized decoder instance. - * \param filename The name of the file to decode from. The file will - * be opened with fopen(). Use \c NULL to decode from - * \c stdin. Note that \c stdin is not seekable. - * \param write_callback See FLAC__StreamDecoderWriteCallback. This - * pointer must not be \c NULL. - * \param metadata_callback See FLAC__StreamDecoderMetadataCallback. This - * pointer may be \c NULL if the callback is not - * desired. - * \param error_callback See FLAC__StreamDecoderErrorCallback. This - * pointer must not be \c NULL. - * \param client_data This value will be supplied to callbacks in their - * \a client_data argument. - * \assert - * \code decoder != NULL \endcode - * \retval FLAC__StreamDecoderInitStatus - * \c FLAC__STREAM_DECODER_INIT_STATUS_OK if initialization was successful; - * see FLAC__StreamDecoderInitStatus for the meanings of other return values. - */ -FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_file( - FLAC__StreamDecoder *decoder, - const char *filename, - FLAC__StreamDecoderWriteCallback write_callback, - FLAC__StreamDecoderMetadataCallback metadata_callback, - FLAC__StreamDecoderErrorCallback error_callback, - void *client_data -); - -/** Initialize the decoder instance to decode Ogg FLAC files. - * - * This flavor of initialization sets up the decoder to decode from a plain - * Ogg FLAC file. If POSIX fopen() semantics are not sufficient, (for - * example, with Unicode filenames on Windows), you must use - * FLAC__stream_decoder_init_ogg_FILE(), or FLAC__stream_decoder_init_ogg_stream() - * and provide callbacks for the I/O. - * - * This function should be called after FLAC__stream_decoder_new() and - * FLAC__stream_decoder_set_*() but before any of the - * FLAC__stream_decoder_process_*() functions. Will set and return the - * decoder state, which will be FLAC__STREAM_DECODER_SEARCH_FOR_METADATA - * if initialization succeeded. - * - * \note Support for Ogg FLAC in the library is optional. If this - * library has been built without support for Ogg FLAC, this function - * will return \c FLAC__STREAM_DECODER_INIT_STATUS_UNSUPPORTED_CONTAINER. - * - * \param decoder An uninitialized decoder instance. - * \param filename The name of the file to decode from. The file will - * be opened with fopen(). Use \c NULL to decode from - * \c stdin. Note that \c stdin is not seekable. - * \param write_callback See FLAC__StreamDecoderWriteCallback. This - * pointer must not be \c NULL. - * \param metadata_callback See FLAC__StreamDecoderMetadataCallback. This - * pointer may be \c NULL if the callback is not - * desired. - * \param error_callback See FLAC__StreamDecoderErrorCallback. This - * pointer must not be \c NULL. - * \param client_data This value will be supplied to callbacks in their - * \a client_data argument. - * \assert - * \code decoder != NULL \endcode - * \retval FLAC__StreamDecoderInitStatus - * \c FLAC__STREAM_DECODER_INIT_STATUS_OK if initialization was successful; - * see FLAC__StreamDecoderInitStatus for the meanings of other return values. - */ -FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_file( - FLAC__StreamDecoder *decoder, - const char *filename, - FLAC__StreamDecoderWriteCallback write_callback, - FLAC__StreamDecoderMetadataCallback metadata_callback, - FLAC__StreamDecoderErrorCallback error_callback, - void *client_data -); - -/** Finish the decoding process. - * Flushes the decoding buffer, releases resources, resets the decoder - * settings to their defaults, and returns the decoder state to - * FLAC__STREAM_DECODER_UNINITIALIZED. - * - * In the event of a prematurely-terminated decode, it is not strictly - * necessary to call this immediately before FLAC__stream_decoder_delete() - * but it is good practice to match every FLAC__stream_decoder_init_*() - * with a FLAC__stream_decoder_finish(). - * - * \param decoder An uninitialized decoder instance. - * \assert - * \code decoder != NULL \endcode - * \retval FLAC__bool - * \c false if MD5 checking is on AND a STREAMINFO block was available - * AND the MD5 signature in the STREAMINFO block was non-zero AND the - * signature does not match the one computed by the decoder; else - * \c true. - */ -FLAC_API FLAC__bool FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder); - -/** Flush the stream input. - * The decoder's input buffer will be cleared and the state set to - * \c FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC. This will also turn - * off MD5 checking. - * - * \param decoder A decoder instance. - * \assert - * \code decoder != NULL \endcode - * \retval FLAC__bool - * \c true if successful, else \c false if a memory allocation - * error occurs (in which case the state will be set to - * \c FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR). - */ -FLAC_API FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder); - -/** Reset the decoding process. - * The decoder's input buffer will be cleared and the state set to - * \c FLAC__STREAM_DECODER_SEARCH_FOR_METADATA. This is similar to - * FLAC__stream_decoder_finish() except that the settings are - * preserved; there is no need to call FLAC__stream_decoder_init_*() - * before decoding again. MD5 checking will be restored to its original - * setting. - * - * If the decoder is seekable, or was initialized with - * FLAC__stream_decoder_init*_FILE() or FLAC__stream_decoder_init*_file(), - * the decoder will also attempt to seek to the beginning of the file. - * If this rewind fails, this function will return \c false. It follows - * that FLAC__stream_decoder_reset() cannot be used when decoding from - * \c stdin. - * - * If the decoder was initialized with FLAC__stream_encoder_init*_stream() - * and is not seekable (i.e. no seek callback was provided or the seek - * callback returns \c FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED), it - * is the duty of the client to start feeding data from the beginning of - * the stream on the next FLAC__stream_decoder_process() or - * FLAC__stream_decoder_process_interleaved() call. - * - * \param decoder A decoder instance. - * \assert - * \code decoder != NULL \endcode - * \retval FLAC__bool - * \c true if successful, else \c false if a memory allocation occurs - * (in which case the state will be set to - * \c FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR) or a seek error - * occurs (the state will be unchanged). - */ -FLAC_API FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder); - -/** Decode one metadata block or audio frame. - * This version instructs the decoder to decode a either a single metadata - * block or a single frame and stop, unless the callbacks return a fatal - * error or the read callback returns - * \c FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM. - * - * As the decoder needs more input it will call the read callback. - * Depending on what was decoded, the metadata or write callback will be - * called with the decoded metadata block or audio frame. - * - * Unless there is a fatal read error or end of stream, this function - * will return once one whole frame is decoded. In other words, if the - * stream is not synchronized or points to a corrupt frame header, the - * decoder will continue to try and resync until it gets to a valid - * frame, then decode one frame, then return. If the decoder points to - * a frame whose frame CRC in the frame footer does not match the - * computed frame CRC, this function will issue a - * FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH error to the - * error callback, and return, having decoded one complete, although - * corrupt, frame. (Such corrupted frames are sent as silence of the - * correct length to the write callback.) - * - * \param decoder An initialized decoder instance. - * \assert - * \code decoder != NULL \endcode - * \retval FLAC__bool - * \c false if any fatal read, write, or memory allocation error - * occurred (meaning decoding must stop), else \c true; for more - * information about the decoder, check the decoder state with - * FLAC__stream_decoder_get_state(). - */ -FLAC_API FLAC__bool FLAC__stream_decoder_process_single(FLAC__StreamDecoder *decoder); - -/** Decode until the end of the metadata. - * This version instructs the decoder to decode from the current position - * and continue until all the metadata has been read, or until the - * callbacks return a fatal error or the read callback returns - * \c FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM. - * - * As the decoder needs more input it will call the read callback. - * As each metadata block is decoded, the metadata callback will be called - * with the decoded metadata. - * - * \param decoder An initialized decoder instance. - * \assert - * \code decoder != NULL \endcode - * \retval FLAC__bool - * \c false if any fatal read, write, or memory allocation error - * occurred (meaning decoding must stop), else \c true; for more - * information about the decoder, check the decoder state with - * FLAC__stream_decoder_get_state(). - */ -FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_metadata(FLAC__StreamDecoder *decoder); - -/** Decode until the end of the stream. - * This version instructs the decoder to decode from the current position - * and continue until the end of stream (the read callback returns - * \c FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM), or until the - * callbacks return a fatal error. - * - * As the decoder needs more input it will call the read callback. - * As each metadata block and frame is decoded, the metadata or write - * callback will be called with the decoded metadata or frame. - * - * \param decoder An initialized decoder instance. - * \assert - * \code decoder != NULL \endcode - * \retval FLAC__bool - * \c false if any fatal read, write, or memory allocation error - * occurred (meaning decoding must stop), else \c true; for more - * information about the decoder, check the decoder state with - * FLAC__stream_decoder_get_state(). - */ -FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_stream(FLAC__StreamDecoder *decoder); - -/** Skip one audio frame. - * This version instructs the decoder to 'skip' a single frame and stop, - * unless the callbacks return a fatal error or the read callback returns - * \c FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM. - * - * The decoding flow is the same as what occurs when - * FLAC__stream_decoder_process_single() is called to process an audio - * frame, except that this function does not decode the parsed data into - * PCM or call the write callback. The integrity of the frame is still - * checked the same way as in the other process functions. - * - * This function will return once one whole frame is skipped, in the - * same way that FLAC__stream_decoder_process_single() will return once - * one whole frame is decoded. - * - * This function can be used in more quickly determining FLAC frame - * boundaries when decoding of the actual data is not needed, for - * example when an application is separating a FLAC stream into frames - * for editing or storing in a container. To do this, the application - * can use FLAC__stream_decoder_skip_single_frame() to quickly advance - * to the next frame, then use - * FLAC__stream_decoder_get_decode_position() to find the new frame - * boundary. - * - * This function should only be called when the stream has advanced - * past all the metadata, otherwise it will return \c false. - * - * \param decoder An initialized decoder instance not in a metadata - * state. - * \assert - * \code decoder != NULL \endcode - * \retval FLAC__bool - * \c false if any fatal read, write, or memory allocation error - * occurred (meaning decoding must stop), or if the decoder - * is in the FLAC__STREAM_DECODER_SEARCH_FOR_METADATA or - * FLAC__STREAM_DECODER_READ_METADATA state, else \c true; for more - * information about the decoder, check the decoder state with - * FLAC__stream_decoder_get_state(). - */ -FLAC_API FLAC__bool FLAC__stream_decoder_skip_single_frame(FLAC__StreamDecoder *decoder); - -/** Flush the input and seek to an absolute sample. - * Decoding will resume at the given sample. Note that because of - * this, the next write callback may contain a partial block. The - * client must support seeking the input or this function will fail - * and return \c false. Furthermore, if the decoder state is - * \c FLAC__STREAM_DECODER_SEEK_ERROR, then the decoder must be flushed - * with FLAC__stream_decoder_flush() or reset with - * FLAC__stream_decoder_reset() before decoding can continue. - * - * \param decoder A decoder instance. - * \param sample The target sample number to seek to. - * \assert - * \code decoder != NULL \endcode - * \retval FLAC__bool - * \c true if successful, else \c false. - */ -FLAC_API FLAC__bool FLAC__stream_decoder_seek_absolute(FLAC__StreamDecoder *decoder, FLAC__uint64 sample); - -/* \} */ - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/FLAC/Makefile.mgw b/FLAC/Makefile.mgw deleted file mode 100644 index d1a8bc3b4..000000000 --- a/FLAC/Makefile.mgw +++ /dev/null @@ -1,79 +0,0 @@ -# Makefile for combined FLAC, FLAC++, derived from zlib's Makefile.mgw, -# which was itself derived from zlib's Makefile.dj2. -# Modified for mingw32 by C. Spieler, 6/16/98. -# Updated for zlib 1.2.x by Christian Spieler and Cosmin Truta, Mar-2003. -# Last updated: 1-Aug-2003. - -# Copyright (C) 1995-2003 Jean-loup Gailly. -# For conditions of distribution and use, see copyright notice in zlib.h - -ifeq (Windows_NT,$(OS)) - WIN=1 - WINCMD=1 -endif -ifeq ($(findstring msys,$(shell sh --version 2>nul)),msys) - WIN=1 - WINCMD=0 -endif - -STATICLIB = libflac.a - -#LOC = -DASMV -#LOC = -DDEBUG -g - -DEFINES = -D__MINW32__ -DWIN32 -DNDEBUG -D_LIB -DFLAC__CPU_IA32 -DFLAC_HAS_NASM -DFLAC__SSE_OS -DFLAC__USE_3DNOW -DFLAC__NO_DLL -I. - -CCDV = @../ccdv -CC = gcc -CXX = g++ -CFLAGS = $(LOC) $(DEFINES) -O2 -Wall -Wno-unused-function -fomit-frame-pointer -CXXFLAGS = $(LOC) $(DEFINES) -O2 -Wall - -NASM = nasm -NASMFLAGS = -d OBJ_FORMAT_win32 -f win32 - -AR = ar -ARFLAGS = rcs - -OBJS = cpu_asm.o fixed_asm.o lpc_asm.o \ - bitmath.o bitreader.o cpu.o crc.o fixed.o format.o lpc.o md5.o memory.o stream_decoder.o stream_decoder_pp.o - -all: $(STATICLIB) - -.c.o: - $(CCDV) $(CC) $(CFLAGS) -c -o $@ $< - -.cpp.o: - $(CCDV) $(CXX) $(CXXFLAGS) -c -o $@ $< - -$(STATICLIB): $(OBJS) - $(CCDV) $(AR) $(ARFLAGS) $@ $(OBJS) - - -.PHONY: clean - -clean: -ifeq (1,$(WINCMD)) - -del /q /f $(STATICLIB) 2>nul - -del /q /f *.o 2>nul -else - rm -f $(STATICLIB) - rm -f *.o -endif - -cpu_asm.o: ia32/cpu_asm.nasm - $(CCDV) $(NASM) -o $@ $(NASMFLAGS) $< -fixed_asm.o: ia32/fixed_asm.nasm - $(CCDV) $(NASM) -o $@ $(NASMFLAGS) $< -lpc_asm.o: ia32/lpc_asm.nasm - $(CCDV) $(NASM) -o $@ $(NASMFLAGS) $< -bitbuffer.o: bitbuffer.c -bitmath.o: bitmath.c -cpu.o: cpu.c -crc.o: crc.c -fixed.o: fixed.c -format.o: format.c -lpc.o: lpc.c -memory.o: memory.c -stream_decoder.o: stream_decoder.c -stream_decoder_pp.o: stream_decoder_pp.cpp diff --git a/FLAC/ReadMe.txt b/FLAC/ReadMe.txt deleted file mode 100644 index db5d2e9b0..000000000 --- a/FLAC/ReadMe.txt +++ /dev/null @@ -1,7 +0,0 @@ -This is not the complete FLAC distribution. It contains only what -ZDoom needs to decode compressed FLAC streams. For the complete -distribution, please visit . The -version here is currently version 1.2.1. - -Of course, under Linux, you are encouraged to ignore this source -here and dynamically link to the full libraries. \ No newline at end of file diff --git a/FLAC/bitmath.c b/FLAC/bitmath.c deleted file mode 100644 index 8cf3c17b1..000000000 --- a/FLAC/bitmath.c +++ /dev/null @@ -1,149 +0,0 @@ -/* libFLAC - Free Lossless Audio Codec library - * Copyright (C) 2001,2002,2003,2004,2005,2006,2007 Josh Coalson - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * - Neither the name of the Xiph.org Foundation nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#if HAVE_CONFIG_H -# include -#endif - -#include "private/bitmath.h" -#include "FLAC/assert.h" - -/* An example of what FLAC__bitmath_ilog2() computes: - * - * ilog2( 0) = assertion failure - * ilog2( 1) = 0 - * ilog2( 2) = 1 - * ilog2( 3) = 1 - * ilog2( 4) = 2 - * ilog2( 5) = 2 - * ilog2( 6) = 2 - * ilog2( 7) = 2 - * ilog2( 8) = 3 - * ilog2( 9) = 3 - * ilog2(10) = 3 - * ilog2(11) = 3 - * ilog2(12) = 3 - * ilog2(13) = 3 - * ilog2(14) = 3 - * ilog2(15) = 3 - * ilog2(16) = 4 - * ilog2(17) = 4 - * ilog2(18) = 4 - */ -unsigned FLAC__bitmath_ilog2(FLAC__uint32 v) -{ - unsigned l = 0; - FLAC__ASSERT(v > 0); - while(v >>= 1) - l++; - return l; -} - -unsigned FLAC__bitmath_ilog2_wide(FLAC__uint64 v) -{ - unsigned l = 0; - FLAC__ASSERT(v > 0); - while(v >>= 1) - l++; - return l; -} - -/* An example of what FLAC__bitmath_silog2() computes: - * - * silog2(-10) = 5 - * silog2(- 9) = 5 - * silog2(- 8) = 4 - * silog2(- 7) = 4 - * silog2(- 6) = 4 - * silog2(- 5) = 4 - * silog2(- 4) = 3 - * silog2(- 3) = 3 - * silog2(- 2) = 2 - * silog2(- 1) = 2 - * silog2( 0) = 0 - * silog2( 1) = 2 - * silog2( 2) = 3 - * silog2( 3) = 3 - * silog2( 4) = 4 - * silog2( 5) = 4 - * silog2( 6) = 4 - * silog2( 7) = 4 - * silog2( 8) = 5 - * silog2( 9) = 5 - * silog2( 10) = 5 - */ -unsigned FLAC__bitmath_silog2(int v) -{ - while(1) { - if(v == 0) { - return 0; - } - else if(v > 0) { - unsigned l = 0; - while(v) { - l++; - v >>= 1; - } - return l+1; - } - else if(v == -1) { - return 2; - } - else { - v++; - v = -v; - } - } -} - -unsigned FLAC__bitmath_silog2_wide(FLAC__int64 v) -{ - while(1) { - if(v == 0) { - return 0; - } - else if(v > 0) { - unsigned l = 0; - while(v) { - l++; - v >>= 1; - } - return l+1; - } - else if(v == -1) { - return 2; - } - else { - v++; - v = -v; - } - } -} diff --git a/FLAC/bitreader.c b/FLAC/bitreader.c deleted file mode 100644 index 7d63e526e..000000000 --- a/FLAC/bitreader.c +++ /dev/null @@ -1,1376 +0,0 @@ -/* libFLAC - Free Lossless Audio Codec library - * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 Josh Coalson - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * - Neither the name of the Xiph.org Foundation nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#if HAVE_CONFIG_H -# include -#endif - -#include /* for malloc() */ -#include /* for memcpy(), memset() */ -#ifdef _MSC_VER -#include /* for ntohl() */ -#elif defined FLAC__SYS_DARWIN -#include /* for ntohl() */ -#elif defined __MINGW32__ -#include /* for ntohl() */ -#else -#include /* for ntohl() */ -#endif -#include "private/bitmath.h" -#include "private/bitreader.h" -#include "private/crc.h" -#include "FLAC/assert.h" - -/* Things should be fastest when this matches the machine word size */ -/* WATCHOUT: if you change this you must also change the following #defines down to COUNT_ZERO_MSBS below to match */ -/* WATCHOUT: there are a few places where the code will not work unless brword is >= 32 bits wide */ -/* also, some sections currently only have fast versions for 4 or 8 bytes per word */ -typedef FLAC__uint32 brword; -#define FLAC__BYTES_PER_WORD 4 -#define FLAC__BITS_PER_WORD 32 -#define FLAC__WORD_ALL_ONES ((FLAC__uint32)0xffffffff) -/* SWAP_BE_WORD_TO_HOST swaps bytes in a brword (which is always big-endian) if necessary to match host byte order */ -#if WORDS_BIGENDIAN -#define SWAP_BE_WORD_TO_HOST(x) (x) -#else -#ifdef _MSC_VER -#define SWAP_BE_WORD_TO_HOST(x) local_swap32_(x) -#else -#define SWAP_BE_WORD_TO_HOST(x) ntohl(x) -#endif -#endif -/* counts the # of zero MSBs in a word */ -#define COUNT_ZERO_MSBS(word) ( \ - (word) <= 0xffff ? \ - ( (word) <= 0xff? byte_to_unary_table[word] + 24 : byte_to_unary_table[(word) >> 8] + 16 ) : \ - ( (word) <= 0xffffff? byte_to_unary_table[word >> 16] + 8 : byte_to_unary_table[(word) >> 24] ) \ -) -/* this alternate might be slightly faster on some systems/compilers: */ -#define COUNT_ZERO_MSBS2(word) ( (word) <= 0xff ? byte_to_unary_table[word] + 24 : ((word) <= 0xffff ? byte_to_unary_table[(word) >> 8] + 16 : ((word) <= 0xffffff ? byte_to_unary_table[(word) >> 16] + 8 : byte_to_unary_table[(word) >> 24])) ) - - -/* - * This should be at least twice as large as the largest number of words - * required to represent any 'number' (in any encoding) you are going to - * read. With FLAC this is on the order of maybe a few hundred bits. - * If the buffer is smaller than that, the decoder won't be able to read - * in a whole number that is in a variable length encoding (e.g. Rice). - * But to be practical it should be at least 1K bytes. - * - * Increase this number to decrease the number of read callbacks, at the - * expense of using more memory. Or decrease for the reverse effect, - * keeping in mind the limit from the first paragraph. The optimal size - * also depends on the CPU cache size and other factors; some twiddling - * may be necessary to squeeze out the best performance. - */ -static const unsigned FLAC__BITREADER_DEFAULT_CAPACITY = 65536u / FLAC__BITS_PER_WORD; /* in words */ - -static const unsigned char byte_to_unary_table[] = { - 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 -}; - -#ifdef min -#undef min -#endif -#define min(x,y) ((x)<(y)?(x):(y)) -#ifdef max -#undef max -#endif -#define max(x,y) ((x)>(y)?(x):(y)) - -/* adjust for compilers that can't understand using LLU suffix for uint64_t literals */ -#ifdef _MSC_VER -#define FLAC__U64L(x) x -#else -#define FLAC__U64L(x) x##LLU -#endif - -#ifndef FLaC__INLINE -#define FLaC__INLINE -#endif - -/* WATCHOUT: assembly routines rely on the order in which these fields are declared */ -struct FLAC__BitReader { - /* any partially-consumed word at the head will stay right-justified as bits are consumed from the left */ - /* any incomplete word at the tail will be left-justified, and bytes from the read callback are added on the right */ - brword *buffer; - unsigned capacity; /* in words */ - unsigned words; /* # of completed words in buffer */ - unsigned bytes; /* # of bytes in incomplete word at buffer[words] */ - unsigned consumed_words; /* #words ... */ - unsigned consumed_bits; /* ... + (#bits of head word) already consumed from the front of buffer */ - unsigned read_crc16; /* the running frame CRC */ - unsigned crc16_align; /* the number of bits in the current consumed word that should not be CRC'd */ - FLAC__BitReaderReadCallback read_callback; - void *client_data; - FLAC__CPUInfo cpu_info; -}; - -#ifdef _MSC_VER -/* OPT: an MSVC built-in would be better */ -static _inline FLAC__uint32 local_swap32_(FLAC__uint32 x) -{ - x = ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF); - return (x>>16) | (x<<16); -} -static void local_swap32_block_(FLAC__uint32 *start, FLAC__uint32 len) -{ - __asm { - mov edx, start - mov ecx, len - test ecx, ecx -loop1: - jz done1 - mov eax, [edx] - bswap eax - mov [edx], eax - add edx, 4 - dec ecx - jmp short loop1 -done1: - } -} -#endif - -static FLaC__INLINE void crc16_update_word_(FLAC__BitReader *br, brword word) -{ - register unsigned crc = br->read_crc16; -#if FLAC__BYTES_PER_WORD == 4 - switch(br->crc16_align) { - case 0: crc = FLAC__CRC16_UPDATE((unsigned)(word >> 24), crc); - case 8: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 16) & 0xff), crc); - case 16: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 8) & 0xff), crc); - case 24: br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)(word & 0xff), crc); - } -#elif FLAC__BYTES_PER_WORD == 8 - switch(br->crc16_align) { - case 0: crc = FLAC__CRC16_UPDATE((unsigned)(word >> 56), crc); - case 8: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 48) & 0xff), crc); - case 16: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 40) & 0xff), crc); - case 24: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 32) & 0xff), crc); - case 32: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 24) & 0xff), crc); - case 40: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 16) & 0xff), crc); - case 48: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 8) & 0xff), crc); - case 56: br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)(word & 0xff), crc); - } -#else - for( ; br->crc16_align < FLAC__BITS_PER_WORD; br->crc16_align += 8) - crc = FLAC__CRC16_UPDATE((unsigned)((word >> (FLAC__BITS_PER_WORD-8-br->crc16_align)) & 0xff), crc); - br->read_crc16 = crc; -#endif - br->crc16_align = 0; -} - -/* would be static except it needs to be called by asm routines */ -FLAC__bool bitreader_read_from_client_(FLAC__BitReader *br) -{ - unsigned start, end; - size_t bytes; - FLAC__byte *target; - - /* first shift the unconsumed buffer data toward the front as much as possible */ - if(br->consumed_words > 0) { - start = br->consumed_words; - end = br->words + (br->bytes? 1:0); - memmove(br->buffer, br->buffer+start, FLAC__BYTES_PER_WORD * (end - start)); - - br->words -= start; - br->consumed_words = 0; - } - - /* - * set the target for reading, taking into account word alignment and endianness - */ - bytes = (br->capacity - br->words) * FLAC__BYTES_PER_WORD - br->bytes; - if(bytes == 0) - return false; /* no space left, buffer is too small; see note for FLAC__BITREADER_DEFAULT_CAPACITY */ - target = ((FLAC__byte*)(br->buffer+br->words)) + br->bytes; - - /* before reading, if the existing reader looks like this (say brword is 32 bits wide) - * bitstream : 11 22 33 44 55 br->words=1 br->bytes=1 (partial tail word is left-justified) - * buffer[BE]: 11 22 33 44 55 ?? ?? ?? (shown layed out as bytes sequentially in memory) - * buffer[LE]: 44 33 22 11 ?? ?? ?? 55 (?? being don't-care) - * ^^-------target, bytes=3 - * on LE machines, have to byteswap the odd tail word so nothing is - * overwritten: - */ -#if WORDS_BIGENDIAN -#else - if(br->bytes) - br->buffer[br->words] = SWAP_BE_WORD_TO_HOST(br->buffer[br->words]); -#endif - - /* now it looks like: - * bitstream : 11 22 33 44 55 br->words=1 br->bytes=1 - * buffer[BE]: 11 22 33 44 55 ?? ?? ?? - * buffer[LE]: 44 33 22 11 55 ?? ?? ?? - * ^^-------target, bytes=3 - */ - - /* read in the data; note that the callback may return a smaller number of bytes */ - if(!br->read_callback(target, &bytes, br->client_data)) - return false; - - /* after reading bytes 66 77 88 99 AA BB CC DD EE FF from the client: - * bitstream : 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF - * buffer[BE]: 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF ?? - * buffer[LE]: 44 33 22 11 55 66 77 88 99 AA BB CC DD EE FF ?? - * now have to byteswap on LE machines: - */ -#if WORDS_BIGENDIAN -#else - end = (br->words*FLAC__BYTES_PER_WORD + br->bytes + bytes + (FLAC__BYTES_PER_WORD-1)) / FLAC__BYTES_PER_WORD; -# if defined(_MSC_VER) && (FLAC__BYTES_PER_WORD == 4) - if(br->cpu_info.type == FLAC__CPUINFO_TYPE_IA32 && br->cpu_info.data.ia32.bswap) { - start = br->words; - local_swap32_block_(br->buffer + start, end - start); - } - else -# endif - for(start = br->words; start < end; start++) - br->buffer[start] = SWAP_BE_WORD_TO_HOST(br->buffer[start]); -#endif - - /* now it looks like: - * bitstream : 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF - * buffer[BE]: 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF ?? - * buffer[LE]: 44 33 22 11 88 77 66 55 CC BB AA 99 ?? FF EE DD - * finally we'll update the reader values: - */ - end = br->words*FLAC__BYTES_PER_WORD + br->bytes + bytes; - br->words = end / FLAC__BYTES_PER_WORD; - br->bytes = end % FLAC__BYTES_PER_WORD; - - return true; -} - -/*********************************************************************** - * - * Class constructor/destructor - * - ***********************************************************************/ - -FLAC__BitReader *FLAC__bitreader_new(void) -{ - FLAC__BitReader *br = (FLAC__BitReader*)calloc(1, sizeof(FLAC__BitReader)); - - /* calloc() implies: - memset(br, 0, sizeof(FLAC__BitReader)); - br->buffer = 0; - br->capacity = 0; - br->words = br->bytes = 0; - br->consumed_words = br->consumed_bits = 0; - br->read_callback = 0; - br->client_data = 0; - */ - return br; -} - -void FLAC__bitreader_delete(FLAC__BitReader *br) -{ - FLAC__ASSERT(0 != br); - - FLAC__bitreader_free(br); - free(br); -} - -/*********************************************************************** - * - * Public class methods - * - ***********************************************************************/ - -FLAC__bool FLAC__bitreader_init(FLAC__BitReader *br, FLAC__CPUInfo cpu, FLAC__BitReaderReadCallback rcb, void *cd) -{ - FLAC__ASSERT(0 != br); - - br->words = br->bytes = 0; - br->consumed_words = br->consumed_bits = 0; - br->capacity = FLAC__BITREADER_DEFAULT_CAPACITY; - br->buffer = (brword*)malloc(sizeof(brword) * br->capacity); - if(br->buffer == 0) - return false; - br->read_callback = rcb; - br->client_data = cd; - br->cpu_info = cpu; - - return true; -} - -void FLAC__bitreader_free(FLAC__BitReader *br) -{ - FLAC__ASSERT(0 != br); - - if(0 != br->buffer) - free(br->buffer); - br->buffer = 0; - br->capacity = 0; - br->words = br->bytes = 0; - br->consumed_words = br->consumed_bits = 0; - br->read_callback = 0; - br->client_data = 0; -} - -FLAC__bool FLAC__bitreader_clear(FLAC__BitReader *br) -{ - br->words = br->bytes = 0; - br->consumed_words = br->consumed_bits = 0; - return true; -} - -void FLAC__bitreader_dump(const FLAC__BitReader *br, FILE *out) -{ - unsigned i, j; - if(br == 0) { - fprintf(out, "bitreader is NULL\n"); - } - else { - fprintf(out, "bitreader: capacity=%u words=%u bytes=%u consumed: words=%u, bits=%u\n", br->capacity, br->words, br->bytes, br->consumed_words, br->consumed_bits); - - for(i = 0; i < br->words; i++) { - fprintf(out, "%08X: ", i); - for(j = 0; j < FLAC__BITS_PER_WORD; j++) - if(i < br->consumed_words || (i == br->consumed_words && j < br->consumed_bits)) - fprintf(out, "."); - else - fprintf(out, "%01u", br->buffer[i] & (1 << (FLAC__BITS_PER_WORD-j-1)) ? 1:0); - fprintf(out, "\n"); - } - if(br->bytes > 0) { - fprintf(out, "%08X: ", i); - for(j = 0; j < br->bytes*8; j++) - if(i < br->consumed_words || (i == br->consumed_words && j < br->consumed_bits)) - fprintf(out, "."); - else - fprintf(out, "%01u", br->buffer[i] & (1 << (br->bytes*8-j-1)) ? 1:0); - fprintf(out, "\n"); - } - } -} - -void FLAC__bitreader_reset_read_crc16(FLAC__BitReader *br, FLAC__uint16 seed) -{ - FLAC__ASSERT(0 != br); - FLAC__ASSERT(0 != br->buffer); - FLAC__ASSERT((br->consumed_bits & 7) == 0); - - br->read_crc16 = (unsigned)seed; - br->crc16_align = br->consumed_bits; -} - -FLAC__uint16 FLAC__bitreader_get_read_crc16(FLAC__BitReader *br) -{ - FLAC__ASSERT(0 != br); - FLAC__ASSERT(0 != br->buffer); - FLAC__ASSERT((br->consumed_bits & 7) == 0); - FLAC__ASSERT(br->crc16_align <= br->consumed_bits); - - /* CRC any tail bytes in a partially-consumed word */ - if(br->consumed_bits) { - const brword tail = br->buffer[br->consumed_words]; - for( ; br->crc16_align < br->consumed_bits; br->crc16_align += 8) - br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)((tail >> (FLAC__BITS_PER_WORD-8-br->crc16_align)) & 0xff), br->read_crc16); - } - return br->read_crc16; -} - -FLaC__INLINE FLAC__bool FLAC__bitreader_is_consumed_byte_aligned(const FLAC__BitReader *br) -{ - return ((br->consumed_bits & 7) == 0); -} - -FLaC__INLINE unsigned FLAC__bitreader_bits_left_for_byte_alignment(const FLAC__BitReader *br) -{ - return 8 - (br->consumed_bits & 7); -} - -FLaC__INLINE unsigned FLAC__bitreader_get_input_bits_unconsumed(const FLAC__BitReader *br) -{ - return (br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 - br->consumed_bits; -} - -FLaC__INLINE FLAC__bool FLAC__bitreader_read_raw_uint32(FLAC__BitReader *br, FLAC__uint32 *val, unsigned bits) -{ - FLAC__ASSERT(0 != br); - FLAC__ASSERT(0 != br->buffer); - - FLAC__ASSERT(bits <= 32); - FLAC__ASSERT((br->capacity*FLAC__BITS_PER_WORD) * 2 >= bits); - FLAC__ASSERT(br->consumed_words <= br->words); - - /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */ - FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32); - - if(bits == 0) { /* OPT: investigate if this can ever happen, maybe change to assertion */ - *val = 0; - return true; - } - - while((br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 - br->consumed_bits < bits) { - if(!bitreader_read_from_client_(br)) - return false; - } - if(br->consumed_words < br->words) { /* if we've not consumed up to a partial tail word... */ - /* OPT: taking out the consumed_bits==0 "else" case below might make things faster if less code allows the compiler to inline this function */ - if(br->consumed_bits) { - /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */ - const unsigned n = FLAC__BITS_PER_WORD - br->consumed_bits; - const brword word = br->buffer[br->consumed_words]; - if(bits < n) { - *val = (word & (FLAC__WORD_ALL_ONES >> br->consumed_bits)) >> (n-bits); - br->consumed_bits += bits; - return true; - } - *val = word & (FLAC__WORD_ALL_ONES >> br->consumed_bits); - bits -= n; - crc16_update_word_(br, word); - br->consumed_words++; - br->consumed_bits = 0; - if(bits) { /* if there are still bits left to read, there have to be less than 32 so they will all be in the next word */ - *val <<= bits; - *val |= (br->buffer[br->consumed_words] >> (FLAC__BITS_PER_WORD-bits)); - br->consumed_bits = bits; - } - return true; - } - else { - const brword word = br->buffer[br->consumed_words]; - if(bits < FLAC__BITS_PER_WORD) { - *val = word >> (FLAC__BITS_PER_WORD-bits); - br->consumed_bits = bits; - return true; - } - /* at this point 'bits' must be == FLAC__BITS_PER_WORD; because of previous assertions, it can't be larger */ - *val = word; - crc16_update_word_(br, word); - br->consumed_words++; - return true; - } - } - else { - /* in this case we're starting our read at a partial tail word; - * the reader has guaranteed that we have at least 'bits' bits - * available to read, which makes this case simpler. - */ - /* OPT: taking out the consumed_bits==0 "else" case below might make things faster if less code allows the compiler to inline this function */ - if(br->consumed_bits) { - /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */ - FLAC__ASSERT(br->consumed_bits + bits <= br->bytes*8); - *val = (br->buffer[br->consumed_words] & (FLAC__WORD_ALL_ONES >> br->consumed_bits)) >> (FLAC__BITS_PER_WORD-br->consumed_bits-bits); - br->consumed_bits += bits; - return true; - } - else { - *val = br->buffer[br->consumed_words] >> (FLAC__BITS_PER_WORD-bits); - br->consumed_bits += bits; - return true; - } - } -} - -FLAC__bool FLAC__bitreader_read_raw_int32(FLAC__BitReader *br, FLAC__int32 *val, unsigned bits) -{ - /* OPT: inline raw uint32 code here, or make into a macro if possible in the .h file */ - if(!FLAC__bitreader_read_raw_uint32(br, (FLAC__uint32*)val, bits)) - return false; - /* sign-extend: */ - *val <<= (32-bits); - *val >>= (32-bits); - return true; -} - -FLAC__bool FLAC__bitreader_read_raw_uint64(FLAC__BitReader *br, FLAC__uint64 *val, unsigned bits) -{ - FLAC__uint32 hi, lo; - - if(bits > 32) { - if(!FLAC__bitreader_read_raw_uint32(br, &hi, bits-32)) - return false; - if(!FLAC__bitreader_read_raw_uint32(br, &lo, 32)) - return false; - *val = hi; - *val <<= 32; - *val |= lo; - } - else { - if(!FLAC__bitreader_read_raw_uint32(br, &lo, bits)) - return false; - *val = lo; - } - return true; -} - -FLaC__INLINE FLAC__bool FLAC__bitreader_read_uint32_little_endian(FLAC__BitReader *br, FLAC__uint32 *val) -{ - FLAC__uint32 x8, x32 = 0; - - /* this doesn't need to be that fast as currently it is only used for vorbis comments */ - - if(!FLAC__bitreader_read_raw_uint32(br, &x32, 8)) - return false; - - if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8)) - return false; - x32 |= (x8 << 8); - - if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8)) - return false; - x32 |= (x8 << 16); - - if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8)) - return false; - x32 |= (x8 << 24); - - *val = x32; - return true; -} - -FLAC__bool FLAC__bitreader_skip_bits_no_crc(FLAC__BitReader *br, unsigned bits) -{ - /* - * OPT: a faster implementation is possible but probably not that useful - * since this is only called a couple of times in the metadata readers. - */ - FLAC__ASSERT(0 != br); - FLAC__ASSERT(0 != br->buffer); - - if(bits > 0) { - const unsigned n = br->consumed_bits & 7; - unsigned m; - FLAC__uint32 x; - - if(n != 0) { - m = min(8-n, bits); - if(!FLAC__bitreader_read_raw_uint32(br, &x, m)) - return false; - bits -= m; - } - m = bits / 8; - if(m > 0) { - if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(br, m)) - return false; - bits %= 8; - } - if(bits > 0) { - if(!FLAC__bitreader_read_raw_uint32(br, &x, bits)) - return false; - } - } - - return true; -} - -FLAC__bool FLAC__bitreader_skip_byte_block_aligned_no_crc(FLAC__BitReader *br, unsigned nvals) -{ - FLAC__uint32 x; - - FLAC__ASSERT(0 != br); - FLAC__ASSERT(0 != br->buffer); - FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(br)); - - /* step 1: skip over partial head word to get word aligned */ - while(nvals && br->consumed_bits) { /* i.e. run until we read 'nvals' bytes or we hit the end of the head word */ - if(!FLAC__bitreader_read_raw_uint32(br, &x, 8)) - return false; - nvals--; - } - if(0 == nvals) - return true; - /* step 2: skip whole words in chunks */ - while(nvals >= FLAC__BYTES_PER_WORD) { - if(br->consumed_words < br->words) { - br->consumed_words++; - nvals -= FLAC__BYTES_PER_WORD; - } - else if(!bitreader_read_from_client_(br)) - return false; - } - /* step 3: skip any remainder from partial tail bytes */ - while(nvals) { - if(!FLAC__bitreader_read_raw_uint32(br, &x, 8)) - return false; - nvals--; - } - - return true; -} - -FLAC__bool FLAC__bitreader_read_byte_block_aligned_no_crc(FLAC__BitReader *br, FLAC__byte *val, unsigned nvals) -{ - FLAC__uint32 x; - - FLAC__ASSERT(0 != br); - FLAC__ASSERT(0 != br->buffer); - FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(br)); - - /* step 1: read from partial head word to get word aligned */ - while(nvals && br->consumed_bits) { /* i.e. run until we read 'nvals' bytes or we hit the end of the head word */ - if(!FLAC__bitreader_read_raw_uint32(br, &x, 8)) - return false; - *val++ = (FLAC__byte)x; - nvals--; - } - if(0 == nvals) - return true; - /* step 2: read whole words in chunks */ - while(nvals >= FLAC__BYTES_PER_WORD) { - if(br->consumed_words < br->words) { - const brword word = br->buffer[br->consumed_words++]; -#if FLAC__BYTES_PER_WORD == 4 - val[0] = (FLAC__byte)(word >> 24); - val[1] = (FLAC__byte)(word >> 16); - val[2] = (FLAC__byte)(word >> 8); - val[3] = (FLAC__byte)word; -#elif FLAC__BYTES_PER_WORD == 8 - val[0] = (FLAC__byte)(word >> 56); - val[1] = (FLAC__byte)(word >> 48); - val[2] = (FLAC__byte)(word >> 40); - val[3] = (FLAC__byte)(word >> 32); - val[4] = (FLAC__byte)(word >> 24); - val[5] = (FLAC__byte)(word >> 16); - val[6] = (FLAC__byte)(word >> 8); - val[7] = (FLAC__byte)word; -#else - for(x = 0; x < FLAC__BYTES_PER_WORD; x++) - val[x] = (FLAC__byte)(word >> (8*(FLAC__BYTES_PER_WORD-x-1))); -#endif - val += FLAC__BYTES_PER_WORD; - nvals -= FLAC__BYTES_PER_WORD; - } - else if(!bitreader_read_from_client_(br)) - return false; - } - /* step 3: read any remainder from partial tail bytes */ - while(nvals) { - if(!FLAC__bitreader_read_raw_uint32(br, &x, 8)) - return false; - *val++ = (FLAC__byte)x; - nvals--; - } - - return true; -} - -FLaC__INLINE FLAC__bool FLAC__bitreader_read_unary_unsigned(FLAC__BitReader *br, unsigned *val) -#if 0 /* slow but readable version */ -{ - unsigned bit; - - FLAC__ASSERT(0 != br); - FLAC__ASSERT(0 != br->buffer); - - *val = 0; - while(1) { - if(!FLAC__bitreader_read_bit(br, &bit)) - return false; - if(bit) - break; - else - *val++; - } - return true; -} -#else -{ - unsigned i; - - FLAC__ASSERT(0 != br); - FLAC__ASSERT(0 != br->buffer); - - *val = 0; - while(1) { - while(br->consumed_words < br->words) { /* if we've not consumed up to a partial tail word... */ - brword b = br->buffer[br->consumed_words] << br->consumed_bits; - if(b) { - i = COUNT_ZERO_MSBS(b); - *val += i; - i++; - br->consumed_bits += i; - if(br->consumed_bits >= FLAC__BITS_PER_WORD) { /* faster way of testing if(br->consumed_bits == FLAC__BITS_PER_WORD) */ - crc16_update_word_(br, br->buffer[br->consumed_words]); - br->consumed_words++; - br->consumed_bits = 0; - } - return true; - } - else { - *val += FLAC__BITS_PER_WORD - br->consumed_bits; - crc16_update_word_(br, br->buffer[br->consumed_words]); - br->consumed_words++; - br->consumed_bits = 0; - /* didn't find stop bit yet, have to keep going... */ - } - } - /* at this point we've eaten up all the whole words; have to try - * reading through any tail bytes before calling the read callback. - * this is a repeat of the above logic adjusted for the fact we - * don't have a whole word. note though if the client is feeding - * us data a byte at a time (unlikely), br->consumed_bits may not - * be zero. - */ - if(br->bytes) { - const unsigned end = br->bytes * 8; - brword b = (br->buffer[br->consumed_words] & (FLAC__WORD_ALL_ONES << (FLAC__BITS_PER_WORD-end))) << br->consumed_bits; - if(b) { - i = COUNT_ZERO_MSBS(b); - *val += i; - i++; - br->consumed_bits += i; - FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_WORD); - return true; - } - else { - *val += end - br->consumed_bits; - br->consumed_bits += end; - FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_WORD); - /* didn't find stop bit yet, have to keep going... */ - } - } - if(!bitreader_read_from_client_(br)) - return false; - } -} -#endif - -FLAC__bool FLAC__bitreader_read_rice_signed(FLAC__BitReader *br, int *val, unsigned parameter) -{ - FLAC__uint32 lsbs = 0, msbs = 0; - unsigned uval; - - FLAC__ASSERT(0 != br); - FLAC__ASSERT(0 != br->buffer); - FLAC__ASSERT(parameter <= 31); - - /* read the unary MSBs and end bit */ - if(!FLAC__bitreader_read_unary_unsigned(br, &msbs)) - return false; - - /* read the binary LSBs */ - if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, parameter)) - return false; - - /* compose the value */ - uval = (msbs << parameter) | lsbs; - if(uval & 1) - *val = -((int)(uval >> 1)) - 1; - else - *val = (int)(uval >> 1); - - return true; -} - -/* this is by far the most heavily used reader call. it ain't pretty but it's fast */ -/* a lot of the logic is copied, then adapted, from FLAC__bitreader_read_unary_unsigned() and FLAC__bitreader_read_raw_uint32() */ -FLAC__bool FLAC__bitreader_read_rice_signed_block(FLAC__BitReader *br, int vals[], unsigned nvals, unsigned parameter) -/* OPT: possibly faster version for use with MSVC */ -#ifdef _MSC_VER -{ - unsigned i; - unsigned uval = 0; - unsigned bits; /* the # of binary LSBs left to read to finish a rice codeword */ - - /* try and get br->consumed_words and br->consumed_bits into register; - * must remember to flush them back to *br before calling other - * bitwriter functions that use them, and before returning */ - register unsigned cwords; - register unsigned cbits; - - FLAC__ASSERT(0 != br); - FLAC__ASSERT(0 != br->buffer); - /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */ - FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32); - FLAC__ASSERT(parameter < 32); - /* the above two asserts also guarantee that the binary part never straddles more that 2 words, so we don't have to loop to read it */ - - if(nvals == 0) - return true; - - cbits = br->consumed_bits; - cwords = br->consumed_words; - - while(1) { - - /* read unary part */ - while(1) { - while(cwords < br->words) { /* if we've not consumed up to a partial tail word... */ - brword b = br->buffer[cwords] << cbits; - if(b) { -#if 0 /* slower, probably due to bad register allocation... */ && defined FLAC__CPU_IA32 && !defined FLAC__NO_ASM && FLAC__BITS_PER_WORD == 32 - __asm { - bsr eax, b - not eax - and eax, 31 - mov i, eax - } -#else - i = COUNT_ZERO_MSBS(b); -#endif - uval += i; - bits = parameter; - i++; - cbits += i; - if(cbits == FLAC__BITS_PER_WORD) { - crc16_update_word_(br, br->buffer[cwords]); - cwords++; - cbits = 0; - } - goto break1; - } - else { - uval += FLAC__BITS_PER_WORD - cbits; - crc16_update_word_(br, br->buffer[cwords]); - cwords++; - cbits = 0; - /* didn't find stop bit yet, have to keep going... */ - } - } - /* at this point we've eaten up all the whole words; have to try - * reading through any tail bytes before calling the read callback. - * this is a repeat of the above logic adjusted for the fact we - * don't have a whole word. note though if the client is feeding - * us data a byte at a time (unlikely), br->consumed_bits may not - * be zero. - */ - if(br->bytes) { - const unsigned end = br->bytes * 8; - brword b = (br->buffer[cwords] & (FLAC__WORD_ALL_ONES << (FLAC__BITS_PER_WORD-end))) << cbits; - if(b) { - i = COUNT_ZERO_MSBS(b); - uval += i; - bits = parameter; - i++; - cbits += i; - FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD); - goto break1; - } - else { - uval += end - cbits; - cbits += end; - FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD); - /* didn't find stop bit yet, have to keep going... */ - } - } - /* flush registers and read; bitreader_read_from_client_() does - * not touch br->consumed_bits at all but we still need to set - * it in case it fails and we have to return false. - */ - br->consumed_bits = cbits; - br->consumed_words = cwords; - if(!bitreader_read_from_client_(br)) - return false; - cwords = br->consumed_words; - } -break1: - /* read binary part */ - FLAC__ASSERT(cwords <= br->words); - - if(bits) { - while((br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits < bits) { - /* flush registers and read; bitreader_read_from_client_() does - * not touch br->consumed_bits at all but we still need to set - * it in case it fails and we have to return false. - */ - br->consumed_bits = cbits; - br->consumed_words = cwords; - if(!bitreader_read_from_client_(br)) - return false; - cwords = br->consumed_words; - } - if(cwords < br->words) { /* if we've not consumed up to a partial tail word... */ - if(cbits) { - /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */ - const unsigned n = FLAC__BITS_PER_WORD - cbits; - const brword word = br->buffer[cwords]; - if(bits < n) { - uval <<= bits; - uval |= (word & (FLAC__WORD_ALL_ONES >> cbits)) >> (n-bits); - cbits += bits; - goto break2; - } - uval <<= n; - uval |= word & (FLAC__WORD_ALL_ONES >> cbits); - bits -= n; - crc16_update_word_(br, word); - cwords++; - cbits = 0; - if(bits) { /* if there are still bits left to read, there have to be less than 32 so they will all be in the next word */ - uval <<= bits; - uval |= (br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits)); - cbits = bits; - } - goto break2; - } - else { - FLAC__ASSERT(bits < FLAC__BITS_PER_WORD); - uval <<= bits; - uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits); - cbits = bits; - goto break2; - } - } - else { - /* in this case we're starting our read at a partial tail word; - * the reader has guaranteed that we have at least 'bits' bits - * available to read, which makes this case simpler. - */ - uval <<= bits; - if(cbits) { - /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */ - FLAC__ASSERT(cbits + bits <= br->bytes*8); - uval |= (br->buffer[cwords] & (FLAC__WORD_ALL_ONES >> cbits)) >> (FLAC__BITS_PER_WORD-cbits-bits); - cbits += bits; - goto break2; - } - else { - uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits); - cbits += bits; - goto break2; - } - } - } -break2: - /* compose the value */ - *vals = (int)(uval >> 1 ^ -(int)(uval & 1)); - - /* are we done? */ - --nvals; - if(nvals == 0) { - br->consumed_bits = cbits; - br->consumed_words = cwords; - return true; - } - - uval = 0; - ++vals; - - } -} -#else -{ - unsigned i; - unsigned uval = 0; - - /* try and get br->consumed_words and br->consumed_bits into register; - * must remember to flush them back to *br before calling other - * bitwriter functions that use them, and before returning */ - register unsigned cwords; - register unsigned cbits; - unsigned ucbits; /* keep track of the number of unconsumed bits in the buffer */ - - FLAC__ASSERT(0 != br); - FLAC__ASSERT(0 != br->buffer); - /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */ - FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32); - FLAC__ASSERT(parameter < 32); - /* the above two asserts also guarantee that the binary part never straddles more than 2 words, so we don't have to loop to read it */ - - if(nvals == 0) - return true; - - cbits = br->consumed_bits; - cwords = br->consumed_words; - ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits; - - while(1) { - - /* read unary part */ - while(1) { - while(cwords < br->words) { /* if we've not consumed up to a partial tail word... */ - brword b = br->buffer[cwords] << cbits; - if(b) { -#if 0 /* is not discernably faster... */ && defined FLAC__CPU_IA32 && !defined FLAC__NO_ASM && FLAC__BITS_PER_WORD == 32 && defined __GNUC__ - asm volatile ( - "bsrl %1, %0;" - "notl %0;" - "andl $31, %0;" - : "=r"(i) - : "r"(b) - ); -#else - i = COUNT_ZERO_MSBS(b); -#endif - uval += i; - cbits += i; - cbits++; /* skip over stop bit */ - if(cbits >= FLAC__BITS_PER_WORD) { /* faster way of testing if(cbits == FLAC__BITS_PER_WORD) */ - crc16_update_word_(br, br->buffer[cwords]); - cwords++; - cbits = 0; - } - goto break1; - } - else { - uval += FLAC__BITS_PER_WORD - cbits; - crc16_update_word_(br, br->buffer[cwords]); - cwords++; - cbits = 0; - /* didn't find stop bit yet, have to keep going... */ - } - } - /* at this point we've eaten up all the whole words; have to try - * reading through any tail bytes before calling the read callback. - * this is a repeat of the above logic adjusted for the fact we - * don't have a whole word. note though if the client is feeding - * us data a byte at a time (unlikely), br->consumed_bits may not - * be zero. - */ - if(br->bytes) { - const unsigned end = br->bytes * 8; - brword b = (br->buffer[cwords] & ~(FLAC__WORD_ALL_ONES >> end)) << cbits; - if(b) { - i = COUNT_ZERO_MSBS(b); - uval += i; - cbits += i; - cbits++; /* skip over stop bit */ - FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD); - goto break1; - } - else { - uval += end - cbits; - cbits += end; - FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD); - /* didn't find stop bit yet, have to keep going... */ - } - } - /* flush registers and read; bitreader_read_from_client_() does - * not touch br->consumed_bits at all but we still need to set - * it in case it fails and we have to return false. - */ - br->consumed_bits = cbits; - br->consumed_words = cwords; - if(!bitreader_read_from_client_(br)) - return false; - cwords = br->consumed_words; - ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits + uval; - /* + uval to offset our count by the # of unary bits already - * consumed before the read, because we will add these back - * in all at once at break1 - */ - } -break1: - ucbits -= uval; - ucbits--; /* account for stop bit */ - - /* read binary part */ - FLAC__ASSERT(cwords <= br->words); - - if(parameter) { - while(ucbits < parameter) { - /* flush registers and read; bitreader_read_from_client_() does - * not touch br->consumed_bits at all but we still need to set - * it in case it fails and we have to return false. - */ - br->consumed_bits = cbits; - br->consumed_words = cwords; - if(!bitreader_read_from_client_(br)) - return false; - cwords = br->consumed_words; - ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits; - } - if(cwords < br->words) { /* if we've not consumed up to a partial tail word... */ - if(cbits) { - /* this also works when consumed_bits==0, it's just slower than necessary for that case */ - const unsigned n = FLAC__BITS_PER_WORD - cbits; - const brword word = br->buffer[cwords]; - if(parameter < n) { - uval <<= parameter; - uval |= (word & (FLAC__WORD_ALL_ONES >> cbits)) >> (n-parameter); - cbits += parameter; - } - else { - uval <<= n; - uval |= word & (FLAC__WORD_ALL_ONES >> cbits); - crc16_update_word_(br, word); - cwords++; - cbits = parameter - n; - if(cbits) { /* parameter > n, i.e. if there are still bits left to read, there have to be less than 32 so they will all be in the next word */ - uval <<= cbits; - uval |= (br->buffer[cwords] >> (FLAC__BITS_PER_WORD-cbits)); - } - } - } - else { - cbits = parameter; - uval <<= parameter; - uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-cbits); - } - } - else { - /* in this case we're starting our read at a partial tail word; - * the reader has guaranteed that we have at least 'parameter' - * bits available to read, which makes this case simpler. - */ - uval <<= parameter; - if(cbits) { - /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */ - FLAC__ASSERT(cbits + parameter <= br->bytes*8); - uval |= (br->buffer[cwords] & (FLAC__WORD_ALL_ONES >> cbits)) >> (FLAC__BITS_PER_WORD-cbits-parameter); - cbits += parameter; - } - else { - cbits = parameter; - uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-cbits); - } - } - } - - ucbits -= parameter; - - /* compose the value */ - *vals = (int)(uval >> 1 ^ -(int)(uval & 1)); - - /* are we done? */ - --nvals; - if(nvals == 0) { - br->consumed_bits = cbits; - br->consumed_words = cwords; - return true; - } - - uval = 0; - ++vals; - - } -} -#endif - -#if 0 /* UNUSED */ -FLAC__bool FLAC__bitreader_read_golomb_signed(FLAC__BitReader *br, int *val, unsigned parameter) -{ - FLAC__uint32 lsbs = 0, msbs = 0; - unsigned bit, uval, k; - - FLAC__ASSERT(0 != br); - FLAC__ASSERT(0 != br->buffer); - - k = FLAC__bitmath_ilog2(parameter); - - /* read the unary MSBs and end bit */ - if(!FLAC__bitreader_read_unary_unsigned(br, &msbs)) - return false; - - /* read the binary LSBs */ - if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, k)) - return false; - - if(parameter == 1u<= d) { - if(!FLAC__bitreader_read_bit(br, &bit)) - return false; - lsbs <<= 1; - lsbs |= bit; - lsbs -= d; - } - /* compose the value */ - uval = msbs * parameter + lsbs; - } - - /* unfold unsigned to signed */ - if(uval & 1) - *val = -((int)(uval >> 1)) - 1; - else - *val = (int)(uval >> 1); - - return true; -} - -FLAC__bool FLAC__bitreader_read_golomb_unsigned(FLAC__BitReader *br, unsigned *val, unsigned parameter) -{ - FLAC__uint32 lsbs, msbs = 0; - unsigned bit, k; - - FLAC__ASSERT(0 != br); - FLAC__ASSERT(0 != br->buffer); - - k = FLAC__bitmath_ilog2(parameter); - - /* read the unary MSBs and end bit */ - if(!FLAC__bitreader_read_unary_unsigned(br, &msbs)) - return false; - - /* read the binary LSBs */ - if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, k)) - return false; - - if(parameter == 1u<= d) { - if(!FLAC__bitreader_read_bit(br, &bit)) - return false; - lsbs <<= 1; - lsbs |= bit; - lsbs -= d; - } - /* compose the value */ - *val = msbs * parameter + lsbs; - } - - return true; -} -#endif /* UNUSED */ - -/* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */ -FLAC__bool FLAC__bitreader_read_utf8_uint32(FLAC__BitReader *br, FLAC__uint32 *val, FLAC__byte *raw, unsigned *rawlen) -{ - FLAC__uint32 v = 0; - FLAC__uint32 x; - unsigned i; - - if(!FLAC__bitreader_read_raw_uint32(br, &x, 8)) - return false; - if(raw) - raw[(*rawlen)++] = (FLAC__byte)x; - if(!(x & 0x80)) { /* 0xxxxxxx */ - v = x; - i = 0; - } - else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */ - v = x & 0x1F; - i = 1; - } - else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */ - v = x & 0x0F; - i = 2; - } - else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */ - v = x & 0x07; - i = 3; - } - else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */ - v = x & 0x03; - i = 4; - } - else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */ - v = x & 0x01; - i = 5; - } - else { - *val = 0xffffffff; - return true; - } - for( ; i; i--) { - if(!FLAC__bitreader_read_raw_uint32(br, &x, 8)) - return false; - if(raw) - raw[(*rawlen)++] = (FLAC__byte)x; - if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */ - *val = 0xffffffff; - return true; - } - v <<= 6; - v |= (x & 0x3F); - } - *val = v; - return true; -} - -/* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */ -FLAC__bool FLAC__bitreader_read_utf8_uint64(FLAC__BitReader *br, FLAC__uint64 *val, FLAC__byte *raw, unsigned *rawlen) -{ - FLAC__uint64 v = 0; - FLAC__uint32 x; - unsigned i; - - if(!FLAC__bitreader_read_raw_uint32(br, &x, 8)) - return false; - if(raw) - raw[(*rawlen)++] = (FLAC__byte)x; - if(!(x & 0x80)) { /* 0xxxxxxx */ - v = x; - i = 0; - } - else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */ - v = x & 0x1F; - i = 1; - } - else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */ - v = x & 0x0F; - i = 2; - } - else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */ - v = x & 0x07; - i = 3; - } - else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */ - v = x & 0x03; - i = 4; - } - else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */ - v = x & 0x01; - i = 5; - } - else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */ - v = 0; - i = 6; - } - else { - *val = FLAC__U64L(0xffffffffffffffff); - return true; - } - for( ; i; i--) { - if(!FLAC__bitreader_read_raw_uint32(br, &x, 8)) - return false; - if(raw) - raw[(*rawlen)++] = (FLAC__byte)x; - if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */ - *val = FLAC__U64L(0xffffffffffffffff); - return true; - } - v <<= 6; - v |= (x & 0x3F); - } - *val = v; - return true; -} diff --git a/FLAC/cpu.c b/FLAC/cpu.c deleted file mode 100644 index 9cc759b9e..000000000 --- a/FLAC/cpu.c +++ /dev/null @@ -1,418 +0,0 @@ -/* libFLAC - Free Lossless Audio Codec library - * Copyright (C) 2001,2002,2003,2004,2005,2006,2007 Josh Coalson - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * - Neither the name of the Xiph.org Foundation nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#if HAVE_CONFIG_H -# include -#endif - -#include "private/cpu.h" -#include -#include - -#if defined FLAC__CPU_IA32 -# include -#elif defined FLAC__CPU_PPC -# if !defined FLAC__NO_ASM -# if defined FLAC__SYS_DARWIN -# include -# include -# include -# include -# include -# ifndef CPU_SUBTYPE_POWERPC_970 -# define CPU_SUBTYPE_POWERPC_970 ((cpu_subtype_t) 100) -# endif -# else /* FLAC__SYS_DARWIN */ - -# include -# include - -static sigjmp_buf jmpbuf; -static volatile sig_atomic_t canjump = 0; - -static void sigill_handler (int sig) -{ - if (!canjump) { - signal (sig, SIG_DFL); - raise (sig); - } - canjump = 0; - siglongjmp (jmpbuf, 1); -} -# endif /* FLAC__SYS_DARWIN */ -# endif /* FLAC__NO_ASM */ -#endif /* FLAC__CPU_PPC */ - -#if defined (__NetBSD__) || defined(__OpenBSD__) -#include -#include -#include -#endif - -#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) -#include -#include -#endif - -#if defined(__APPLE__) -/* how to get sysctlbyname()? */ -#endif - -/* these are flags in EDX of CPUID AX=00000001 */ -static const unsigned FLAC__CPUINFO_IA32_CPUID_CMOV = 0x00008000; -static const unsigned FLAC__CPUINFO_IA32_CPUID_MMX = 0x00800000; -static const unsigned FLAC__CPUINFO_IA32_CPUID_FXSR = 0x01000000; -static const unsigned FLAC__CPUINFO_IA32_CPUID_SSE = 0x02000000; -static const unsigned FLAC__CPUINFO_IA32_CPUID_SSE2 = 0x04000000; -/* these are flags in ECX of CPUID AX=00000001 */ -static const unsigned FLAC__CPUINFO_IA32_CPUID_SSE3 = 0x00000001; -static const unsigned FLAC__CPUINFO_IA32_CPUID_SSSE3 = 0x00000200; -/* these are flags in EDX of CPUID AX=80000001 */ -static const unsigned FLAC__CPUINFO_IA32_CPUID_EXTENDED_AMD_3DNOW = 0x80000000; -static const unsigned FLAC__CPUINFO_IA32_CPUID_EXTENDED_AMD_EXT3DNOW = 0x40000000; -static const unsigned FLAC__CPUINFO_IA32_CPUID_EXTENDED_AMD_EXTMMX = 0x00400000; - - -/* - * Extra stuff needed for detection of OS support for SSE on IA-32 - */ -#if defined(FLAC__CPU_IA32) && !defined FLAC__NO_ASM && defined FLAC__HAS_NASM && !defined FLAC__NO_SSE_OS && !defined FLAC__SSE_OS -# if defined(__linux__) -/* - * If the OS doesn't support SSE, we will get here with a SIGILL. We - * modify the return address to jump over the offending SSE instruction - * and also the operation following it that indicates the instruction - * executed successfully. In this way we use no global variables and - * stay thread-safe. - * - * 3 + 3 + 6: - * 3 bytes for "xorps xmm0,xmm0" - * 3 bytes for estimate of how long the follwing "inc var" instruction is - * 6 bytes extra in case our estimate is wrong - * 12 bytes puts us in the NOP "landing zone" - */ -# undef USE_OBSOLETE_SIGCONTEXT_FLAVOR /* #define this to use the older signal handler method */ -# ifdef USE_OBSOLETE_SIGCONTEXT_FLAVOR - static void sigill_handler_sse_os(int signal, struct sigcontext sc) - { - (void)signal; - sc.eip += 3 + 3 + 6; - } -# else -# include - static void sigill_handler_sse_os(int signal, siginfo_t *si, void *uc) - { - (void)signal, (void)si; - ((ucontext_t*)uc)->uc_mcontext.gregs[14/*REG_EIP*/] += 3 + 3 + 6; - } -# endif -# elif defined(_MSC_VER) -# include -# undef USE_TRY_CATCH_FLAVOR /* #define this to use the try/catch method for catching illegal opcode exception */ -# ifdef USE_TRY_CATCH_FLAVOR -# else - LONG CALLBACK sigill_handler_sse_os(EXCEPTION_POINTERS *ep) - { - if(ep->ExceptionRecord->ExceptionCode == EXCEPTION_ILLEGAL_INSTRUCTION) { - ep->ContextRecord->Eip += 3 + 3 + 6; - return EXCEPTION_CONTINUE_EXECUTION; - } - return EXCEPTION_CONTINUE_SEARCH; - } -# endif -# endif -#endif - - -void FLAC__cpu_info(FLAC__CPUInfo *info) -{ -/* - * IA32-specific - */ -#ifdef FLAC__CPU_IA32 - info->type = FLAC__CPUINFO_TYPE_IA32; -#if !defined FLAC__NO_ASM && defined FLAC__HAS_NASM - info->use_asm = true; /* we assume a minimum of 80386 with FLAC__CPU_IA32 */ - info->data.ia32.cpuid = FLAC__cpu_have_cpuid_asm_ia32()? true : false; - info->data.ia32.bswap = info->data.ia32.cpuid; /* CPUID => BSWAP since it came after */ - info->data.ia32.cmov = false; - info->data.ia32.mmx = false; - info->data.ia32.fxsr = false; - info->data.ia32.sse = false; - info->data.ia32.sse2 = false; - info->data.ia32.sse3 = false; - info->data.ia32.ssse3 = false; - info->data.ia32._3dnow = false; - info->data.ia32.ext3dnow = false; - info->data.ia32.extmmx = false; - if(info->data.ia32.cpuid) { - /* http://www.sandpile.org/ia32/cpuid.htm */ - FLAC__uint32 flags_edx, flags_ecx; - FLAC__cpu_info_asm_ia32(&flags_edx, &flags_ecx); - info->data.ia32.cmov = (flags_edx & FLAC__CPUINFO_IA32_CPUID_CMOV )? true : false; - info->data.ia32.mmx = (flags_edx & FLAC__CPUINFO_IA32_CPUID_MMX )? true : false; - info->data.ia32.fxsr = (flags_edx & FLAC__CPUINFO_IA32_CPUID_FXSR )? true : false; - info->data.ia32.sse = (flags_edx & FLAC__CPUINFO_IA32_CPUID_SSE )? true : false; - info->data.ia32.sse2 = (flags_edx & FLAC__CPUINFO_IA32_CPUID_SSE2 )? true : false; - info->data.ia32.sse3 = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_SSE3 )? true : false; - info->data.ia32.ssse3 = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_SSSE3)? true : false; - -#ifdef FLAC__USE_3DNOW - flags_edx = FLAC__cpu_info_extended_amd_asm_ia32(); - info->data.ia32._3dnow = (flags_edx & FLAC__CPUINFO_IA32_CPUID_EXTENDED_AMD_3DNOW )? true : false; - info->data.ia32.ext3dnow = (flags_edx & FLAC__CPUINFO_IA32_CPUID_EXTENDED_AMD_EXT3DNOW)? true : false; - info->data.ia32.extmmx = (flags_edx & FLAC__CPUINFO_IA32_CPUID_EXTENDED_AMD_EXTMMX )? true : false; -#else - info->data.ia32._3dnow = info->data.ia32.ext3dnow = info->data.ia32.extmmx = false; -#endif - -#ifdef DEBUG - fprintf(stderr, "CPU info (IA-32):\n"); - fprintf(stderr, " CPUID ...... %c\n", info->data.ia32.cpuid ? 'Y' : 'n'); - fprintf(stderr, " BSWAP ...... %c\n", info->data.ia32.bswap ? 'Y' : 'n'); - fprintf(stderr, " CMOV ....... %c\n", info->data.ia32.cmov ? 'Y' : 'n'); - fprintf(stderr, " MMX ........ %c\n", info->data.ia32.mmx ? 'Y' : 'n'); - fprintf(stderr, " FXSR ....... %c\n", info->data.ia32.fxsr ? 'Y' : 'n'); - fprintf(stderr, " SSE ........ %c\n", info->data.ia32.sse ? 'Y' : 'n'); - fprintf(stderr, " SSE2 ....... %c\n", info->data.ia32.sse2 ? 'Y' : 'n'); - fprintf(stderr, " SSE3 ....... %c\n", info->data.ia32.sse3 ? 'Y' : 'n'); - fprintf(stderr, " SSSE3 ...... %c\n", info->data.ia32.ssse3 ? 'Y' : 'n'); - fprintf(stderr, " 3DNow! ..... %c\n", info->data.ia32._3dnow ? 'Y' : 'n'); - fprintf(stderr, " 3DNow!-ext . %c\n", info->data.ia32.ext3dnow? 'Y' : 'n'); - fprintf(stderr, " 3DNow!-MMX . %c\n", info->data.ia32.extmmx ? 'Y' : 'n'); -#endif - - /* - * now have to check for OS support of SSE/SSE2 - */ - if(info->data.ia32.fxsr || info->data.ia32.sse || info->data.ia32.sse2) { -#if defined FLAC__NO_SSE_OS - /* assume user knows better than us; turn it off */ - info->data.ia32.fxsr = info->data.ia32.sse = info->data.ia32.sse2 = info->data.ia32.sse3 = info->data.ia32.ssse3 = false; -#elif defined FLAC__SSE_OS - /* assume user knows better than us; leave as detected above */ -#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__APPLE__) - int sse = 0; - size_t len; - /* at least one of these must work: */ - len = sizeof(sse); sse = sse || (sysctlbyname("hw.instruction_sse", &sse, &len, NULL, 0) == 0 && sse); - len = sizeof(sse); sse = sse || (sysctlbyname("hw.optional.sse" , &sse, &len, NULL, 0) == 0 && sse); /* __APPLE__ ? */ - if(!sse) - info->data.ia32.fxsr = info->data.ia32.sse = info->data.ia32.sse2 = info->data.ia32.sse3 = info->data.ia32.ssse3 = false; -#elif defined(__NetBSD__) || defined (__OpenBSD__) -# if __NetBSD_Version__ >= 105250000 || (defined __OpenBSD__) - int val = 0, mib[2] = { CTL_MACHDEP, CPU_SSE }; - size_t len = sizeof(val); - if(sysctl(mib, 2, &val, &len, NULL, 0) < 0 || !val) - info->data.ia32.fxsr = info->data.ia32.sse = info->data.ia32.sse2 = info->data.ia32.sse3 = info->data.ia32.ssse3 = false; - else { /* double-check SSE2 */ - mib[1] = CPU_SSE2; - len = sizeof(val); - if(sysctl(mib, 2, &val, &len, NULL, 0) < 0 || !val) - info->data.ia32.sse2 = info->data.ia32.sse3 = info->data.ia32.ssse3 = false; - } -# else - info->data.ia32.fxsr = info->data.ia32.sse = info->data.ia32.sse2 = info->data.ia32.sse3 = info->data.ia32.ssse3 = false; -# endif -#elif defined(__linux__) - int sse = 0; - struct sigaction sigill_save; -#ifdef USE_OBSOLETE_SIGCONTEXT_FLAVOR - if(0 == sigaction(SIGILL, NULL, &sigill_save) && signal(SIGILL, (void (*)(int))sigill_handler_sse_os) != SIG_ERR) -#else - struct sigaction sigill_sse; - sigill_sse.sa_sigaction = sigill_handler_sse_os; - __sigemptyset(&sigill_sse.sa_mask); - sigill_sse.sa_flags = SA_SIGINFO | SA_RESETHAND; /* SA_RESETHAND just in case our SIGILL return jump breaks, so we don't get stuck in a loop */ - if(0 == sigaction(SIGILL, &sigill_sse, &sigill_save)) -#endif - { - /* http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html */ - /* see sigill_handler_sse_os() for an explanation of the following: */ - asm volatile ( - "xorl %0,%0\n\t" /* for some reason, still need to do this to clear 'sse' var */ - "xorps %%xmm0,%%xmm0\n\t" /* will cause SIGILL if unsupported by OS */ - "incl %0\n\t" /* SIGILL handler will jump over this */ - /* landing zone */ - "nop\n\t" /* SIGILL jump lands here if "inc" is 9 bytes */ - "nop\n\t" - "nop\n\t" - "nop\n\t" - "nop\n\t" - "nop\n\t" - "nop\n\t" /* SIGILL jump lands here if "inc" is 3 bytes (expected) */ - "nop\n\t" - "nop" /* SIGILL jump lands here if "inc" is 1 byte */ - : "=r"(sse) - : "r"(sse) - ); - - sigaction(SIGILL, &sigill_save, NULL); - } - - if(!sse) - info->data.ia32.fxsr = info->data.ia32.sse = info->data.ia32.sse2 = info->data.ia32.sse3 = info->data.ia32.ssse3 = false; -#elif defined(_MSC_VER) -# ifdef USE_TRY_CATCH_FLAVOR - _try { - __asm { -# if _MSC_VER <= 1200 - /* VC6 assembler doesn't know SSE, have to emit bytecode instead */ - _emit 0x0F - _emit 0x57 - _emit 0xC0 -# else - xorps xmm0,xmm0 -# endif - } - } - _except(EXCEPTION_EXECUTE_HANDLER) { - if (_exception_code() == STATUS_ILLEGAL_INSTRUCTION) - info->data.ia32.fxsr = info->data.ia32.sse = info->data.ia32.sse2 = info->data.ia32.sse3 = info->data.ia32.ssse3 = false; - } -# else - int sse = 0; - LPTOP_LEVEL_EXCEPTION_FILTER save = SetUnhandledExceptionFilter(sigill_handler_sse_os); - /* see GCC version above for explanation */ - /* http://msdn2.microsoft.com/en-us/library/4ks26t93.aspx */ - /* http://www.codeproject.com/cpp/gccasm.asp */ - /* http://www.hick.org/~mmiller/msvc_inline_asm.html */ - __asm { -# if _MSC_VER <= 1200 - /* VC6 assembler doesn't know SSE, have to emit bytecode instead */ - _emit 0x0F - _emit 0x57 - _emit 0xC0 -# else - xorps xmm0,xmm0 -# endif - inc sse - nop - nop - nop - nop - nop - nop - nop - nop - nop - } - SetUnhandledExceptionFilter(save); - if(!sse) - info->data.ia32.fxsr = info->data.ia32.sse = info->data.ia32.sse2 = info->data.ia32.sse3 = info->data.ia32.ssse3 = false; -# endif -#else - /* no way to test, disable to be safe */ - info->data.ia32.fxsr = info->data.ia32.sse = info->data.ia32.sse2 = info->data.ia32.sse3 = info->data.ia32.ssse3 = false; -#endif -#ifdef DEBUG - fprintf(stderr, " SSE OS sup . %c\n", info->data.ia32.sse ? 'Y' : 'n'); -#endif - - } - } -#else - info->use_asm = false; -#endif - -/* - * PPC-specific - */ -#elif defined FLAC__CPU_PPC - info->type = FLAC__CPUINFO_TYPE_PPC; -# if !defined FLAC__NO_ASM - info->use_asm = true; -# ifdef FLAC__USE_ALTIVEC -# if defined FLAC__SYS_DARWIN - { - int val = 0, mib[2] = { CTL_HW, HW_VECTORUNIT }; - size_t len = sizeof(val); - info->data.ppc.altivec = !(sysctl(mib, 2, &val, &len, NULL, 0) || !val); - } - { - host_basic_info_data_t hostInfo; - mach_msg_type_number_t infoCount; - - infoCount = HOST_BASIC_INFO_COUNT; - host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)&hostInfo, &infoCount); - - info->data.ppc.ppc64 = (hostInfo.cpu_type == CPU_TYPE_POWERPC) && (hostInfo.cpu_subtype == CPU_SUBTYPE_POWERPC_970); - } -# else /* FLAC__USE_ALTIVEC && !FLAC__SYS_DARWIN */ - { - /* no Darwin, do it the brute-force way */ - /* @@@@@@ this is not thread-safe; replace with SSE OS method above or remove */ - info->data.ppc.altivec = 0; - info->data.ppc.ppc64 = 0; - - signal (SIGILL, sigill_handler); - canjump = 0; - if (!sigsetjmp (jmpbuf, 1)) { - canjump = 1; - - asm volatile ( - "mtspr 256, %0\n\t" - "vand %%v0, %%v0, %%v0" - : - : "r" (-1) - ); - - info->data.ppc.altivec = 1; - } - canjump = 0; - if (!sigsetjmp (jmpbuf, 1)) { - int x = 0; - canjump = 1; - - /* PPC64 hardware implements the cntlzd instruction */ - asm volatile ("cntlzd %0, %1" : "=r" (x) : "r" (x) ); - - info->data.ppc.ppc64 = 1; - } - signal (SIGILL, SIG_DFL); /*@@@@@@ should save and restore old signal */ - } -# endif -# else /* !FLAC__USE_ALTIVEC */ - info->data.ppc.altivec = 0; - info->data.ppc.ppc64 = 0; -# endif -# else - info->use_asm = false; -# endif - -/* - * unknown CPI - */ -#else - info->type = FLAC__CPUINFO_TYPE_UNKNOWN; - info->use_asm = false; -#endif -} diff --git a/FLAC/crc.c b/FLAC/crc.c deleted file mode 100644 index 88fbc2e74..000000000 --- a/FLAC/crc.c +++ /dev/null @@ -1,142 +0,0 @@ -/* libFLAC - Free Lossless Audio Codec library - * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 Josh Coalson - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * - Neither the name of the Xiph.org Foundation nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#if HAVE_CONFIG_H -# include -#endif - -#include "private/crc.h" - -/* CRC-8, poly = x^8 + x^2 + x^1 + x^0, init = 0 */ - -FLAC__byte const FLAC__crc8_table[256] = { - 0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B, 0x12, 0x15, - 0x38, 0x3F, 0x36, 0x31, 0x24, 0x23, 0x2A, 0x2D, - 0x70, 0x77, 0x7E, 0x79, 0x6C, 0x6B, 0x62, 0x65, - 0x48, 0x4F, 0x46, 0x41, 0x54, 0x53, 0x5A, 0x5D, - 0xE0, 0xE7, 0xEE, 0xE9, 0xFC, 0xFB, 0xF2, 0xF5, - 0xD8, 0xDF, 0xD6, 0xD1, 0xC4, 0xC3, 0xCA, 0xCD, - 0x90, 0x97, 0x9E, 0x99, 0x8C, 0x8B, 0x82, 0x85, - 0xA8, 0xAF, 0xA6, 0xA1, 0xB4, 0xB3, 0xBA, 0xBD, - 0xC7, 0xC0, 0xC9, 0xCE, 0xDB, 0xDC, 0xD5, 0xD2, - 0xFF, 0xF8, 0xF1, 0xF6, 0xE3, 0xE4, 0xED, 0xEA, - 0xB7, 0xB0, 0xB9, 0xBE, 0xAB, 0xAC, 0xA5, 0xA2, - 0x8F, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9D, 0x9A, - 0x27, 0x20, 0x29, 0x2E, 0x3B, 0x3C, 0x35, 0x32, - 0x1F, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0D, 0x0A, - 0x57, 0x50, 0x59, 0x5E, 0x4B, 0x4C, 0x45, 0x42, - 0x6F, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7D, 0x7A, - 0x89, 0x8E, 0x87, 0x80, 0x95, 0x92, 0x9B, 0x9C, - 0xB1, 0xB6, 0xBF, 0xB8, 0xAD, 0xAA, 0xA3, 0xA4, - 0xF9, 0xFE, 0xF7, 0xF0, 0xE5, 0xE2, 0xEB, 0xEC, - 0xC1, 0xC6, 0xCF, 0xC8, 0xDD, 0xDA, 0xD3, 0xD4, - 0x69, 0x6E, 0x67, 0x60, 0x75, 0x72, 0x7B, 0x7C, - 0x51, 0x56, 0x5F, 0x58, 0x4D, 0x4A, 0x43, 0x44, - 0x19, 0x1E, 0x17, 0x10, 0x05, 0x02, 0x0B, 0x0C, - 0x21, 0x26, 0x2F, 0x28, 0x3D, 0x3A, 0x33, 0x34, - 0x4E, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5C, 0x5B, - 0x76, 0x71, 0x78, 0x7F, 0x6A, 0x6D, 0x64, 0x63, - 0x3E, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2C, 0x2B, - 0x06, 0x01, 0x08, 0x0F, 0x1A, 0x1D, 0x14, 0x13, - 0xAE, 0xA9, 0xA0, 0xA7, 0xB2, 0xB5, 0xBC, 0xBB, - 0x96, 0x91, 0x98, 0x9F, 0x8A, 0x8D, 0x84, 0x83, - 0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC, 0xCB, - 0xE6, 0xE1, 0xE8, 0xEF, 0xFA, 0xFD, 0xF4, 0xF3 -}; - -/* CRC-16, poly = x^16 + x^15 + x^2 + x^0, init = 0 */ - -unsigned FLAC__crc16_table[256] = { - 0x0000, 0x8005, 0x800f, 0x000a, 0x801b, 0x001e, 0x0014, 0x8011, - 0x8033, 0x0036, 0x003c, 0x8039, 0x0028, 0x802d, 0x8027, 0x0022, - 0x8063, 0x0066, 0x006c, 0x8069, 0x0078, 0x807d, 0x8077, 0x0072, - 0x0050, 0x8055, 0x805f, 0x005a, 0x804b, 0x004e, 0x0044, 0x8041, - 0x80c3, 0x00c6, 0x00cc, 0x80c9, 0x00d8, 0x80dd, 0x80d7, 0x00d2, - 0x00f0, 0x80f5, 0x80ff, 0x00fa, 0x80eb, 0x00ee, 0x00e4, 0x80e1, - 0x00a0, 0x80a5, 0x80af, 0x00aa, 0x80bb, 0x00be, 0x00b4, 0x80b1, - 0x8093, 0x0096, 0x009c, 0x8099, 0x0088, 0x808d, 0x8087, 0x0082, - 0x8183, 0x0186, 0x018c, 0x8189, 0x0198, 0x819d, 0x8197, 0x0192, - 0x01b0, 0x81b5, 0x81bf, 0x01ba, 0x81ab, 0x01ae, 0x01a4, 0x81a1, - 0x01e0, 0x81e5, 0x81ef, 0x01ea, 0x81fb, 0x01fe, 0x01f4, 0x81f1, - 0x81d3, 0x01d6, 0x01dc, 0x81d9, 0x01c8, 0x81cd, 0x81c7, 0x01c2, - 0x0140, 0x8145, 0x814f, 0x014a, 0x815b, 0x015e, 0x0154, 0x8151, - 0x8173, 0x0176, 0x017c, 0x8179, 0x0168, 0x816d, 0x8167, 0x0162, - 0x8123, 0x0126, 0x012c, 0x8129, 0x0138, 0x813d, 0x8137, 0x0132, - 0x0110, 0x8115, 0x811f, 0x011a, 0x810b, 0x010e, 0x0104, 0x8101, - 0x8303, 0x0306, 0x030c, 0x8309, 0x0318, 0x831d, 0x8317, 0x0312, - 0x0330, 0x8335, 0x833f, 0x033a, 0x832b, 0x032e, 0x0324, 0x8321, - 0x0360, 0x8365, 0x836f, 0x036a, 0x837b, 0x037e, 0x0374, 0x8371, - 0x8353, 0x0356, 0x035c, 0x8359, 0x0348, 0x834d, 0x8347, 0x0342, - 0x03c0, 0x83c5, 0x83cf, 0x03ca, 0x83db, 0x03de, 0x03d4, 0x83d1, - 0x83f3, 0x03f6, 0x03fc, 0x83f9, 0x03e8, 0x83ed, 0x83e7, 0x03e2, - 0x83a3, 0x03a6, 0x03ac, 0x83a9, 0x03b8, 0x83bd, 0x83b7, 0x03b2, - 0x0390, 0x8395, 0x839f, 0x039a, 0x838b, 0x038e, 0x0384, 0x8381, - 0x0280, 0x8285, 0x828f, 0x028a, 0x829b, 0x029e, 0x0294, 0x8291, - 0x82b3, 0x02b6, 0x02bc, 0x82b9, 0x02a8, 0x82ad, 0x82a7, 0x02a2, - 0x82e3, 0x02e6, 0x02ec, 0x82e9, 0x02f8, 0x82fd, 0x82f7, 0x02f2, - 0x02d0, 0x82d5, 0x82df, 0x02da, 0x82cb, 0x02ce, 0x02c4, 0x82c1, - 0x8243, 0x0246, 0x024c, 0x8249, 0x0258, 0x825d, 0x8257, 0x0252, - 0x0270, 0x8275, 0x827f, 0x027a, 0x826b, 0x026e, 0x0264, 0x8261, - 0x0220, 0x8225, 0x822f, 0x022a, 0x823b, 0x023e, 0x0234, 0x8231, - 0x8213, 0x0216, 0x021c, 0x8219, 0x0208, 0x820d, 0x8207, 0x0202 -}; - - -void FLAC__crc8_update(const FLAC__byte data, FLAC__uint8 *crc) -{ - *crc = FLAC__crc8_table[*crc ^ data]; -} - -void FLAC__crc8_update_block(const FLAC__byte *data, unsigned len, FLAC__uint8 *crc) -{ - while(len--) - *crc = FLAC__crc8_table[*crc ^ *data++]; -} - -FLAC__uint8 FLAC__crc8(const FLAC__byte *data, unsigned len) -{ - FLAC__uint8 crc = 0; - - while(len--) - crc = FLAC__crc8_table[crc ^ *data++]; - - return crc; -} - -unsigned FLAC__crc16(const FLAC__byte *data, unsigned len) -{ - unsigned crc = 0; - - while(len--) - crc = ((crc<<8) ^ FLAC__crc16_table[(crc>>8) ^ *data++]) & 0xffff; - - return crc; -} diff --git a/FLAC/fixed.c b/FLAC/fixed.c deleted file mode 100644 index 2f9dee369..000000000 --- a/FLAC/fixed.c +++ /dev/null @@ -1,435 +0,0 @@ -/* libFLAC - Free Lossless Audio Codec library - * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 Josh Coalson - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * - Neither the name of the Xiph.org Foundation nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#if HAVE_CONFIG_H -# include -#endif - -#include -#include -#include "private/bitmath.h" -#include "private/fixed.h" -#include "FLAC/assert.h" - -#ifndef M_LN2 -/* math.h in VC++ doesn't seem to have this (how Microsoft is that?) */ -#define M_LN2 0.69314718055994530942 -#endif - -#ifdef min -#undef min -#endif -#define min(x,y) ((x) < (y)? (x) : (y)) - -#ifdef local_abs -#undef local_abs -#endif -#define local_abs(x) ((unsigned)((x)<0? -(x) : (x))) - -#ifdef FLAC__INTEGER_ONLY_LIBRARY -/* rbps stands for residual bits per sample - * - * (ln(2) * err) - * rbps = log (-----------) - * 2 ( n ) - */ -static FLAC__fixedpoint local__compute_rbps_integerized(FLAC__uint32 err, FLAC__uint32 n) -{ - FLAC__uint32 rbps; - unsigned bits; /* the number of bits required to represent a number */ - int fracbits; /* the number of bits of rbps that comprise the fractional part */ - - FLAC__ASSERT(sizeof(rbps) == sizeof(FLAC__fixedpoint)); - FLAC__ASSERT(err > 0); - FLAC__ASSERT(n > 0); - - FLAC__ASSERT(n <= FLAC__MAX_BLOCK_SIZE); - if(err <= n) - return 0; - /* - * The above two things tell us 1) n fits in 16 bits; 2) err/n > 1. - * These allow us later to know we won't lose too much precision in the - * fixed-point division (err< 0); - bits = FLAC__bitmath_ilog2(err)+1; - if(bits > 16) { - err >>= (bits-16); - fracbits -= (bits-16); - } - rbps = (FLAC__uint32)err; - - /* Multiply by fixed-point version of ln(2), with 16 fractional bits */ - rbps *= FLAC__FP_LN2; - fracbits += 16; - FLAC__ASSERT(fracbits >= 0); - - /* FLAC__fixedpoint_log2 requires fracbits%4 to be 0 */ - { - const int f = fracbits & 3; - if(f) { - rbps >>= f; - fracbits -= f; - } - } - - rbps = FLAC__fixedpoint_log2(rbps, fracbits, (unsigned)(-1)); - - if(rbps == 0) - return 0; - - /* - * The return value must have 16 fractional bits. Since the whole part - * of the base-2 log of a 32 bit number must fit in 5 bits, and fracbits - * must be >= -3, these assertion allows us to be able to shift rbps - * left if necessary to get 16 fracbits without losing any bits of the - * whole part of rbps. - * - * There is a slight chance due to accumulated error that the whole part - * will require 6 bits, so we use 6 in the assertion. Really though as - * long as it fits in 13 bits (32 - (16 - (-3))) we are fine. - */ - FLAC__ASSERT((int)FLAC__bitmath_ilog2(rbps)+1 <= fracbits + 6); - FLAC__ASSERT(fracbits >= -3); - - /* now shift the decimal point into place */ - if(fracbits < 16) - return rbps << (16-fracbits); - else if(fracbits > 16) - return rbps >> (fracbits-16); - else - return rbps; -} - -static FLAC__fixedpoint local__compute_rbps_wide_integerized(FLAC__uint64 err, FLAC__uint32 n) -{ - FLAC__uint32 rbps; - unsigned bits; /* the number of bits required to represent a number */ - int fracbits; /* the number of bits of rbps that comprise the fractional part */ - - FLAC__ASSERT(sizeof(rbps) == sizeof(FLAC__fixedpoint)); - FLAC__ASSERT(err > 0); - FLAC__ASSERT(n > 0); - - FLAC__ASSERT(n <= FLAC__MAX_BLOCK_SIZE); - if(err <= n) - return 0; - /* - * The above two things tell us 1) n fits in 16 bits; 2) err/n > 1. - * These allow us later to know we won't lose too much precision in the - * fixed-point division (err< 0); - bits = FLAC__bitmath_ilog2_wide(err)+1; - if(bits > 16) { - err >>= (bits-16); - fracbits -= (bits-16); - } - rbps = (FLAC__uint32)err; - - /* Multiply by fixed-point version of ln(2), with 16 fractional bits */ - rbps *= FLAC__FP_LN2; - fracbits += 16; - FLAC__ASSERT(fracbits >= 0); - - /* FLAC__fixedpoint_log2 requires fracbits%4 to be 0 */ - { - const int f = fracbits & 3; - if(f) { - rbps >>= f; - fracbits -= f; - } - } - - rbps = FLAC__fixedpoint_log2(rbps, fracbits, (unsigned)(-1)); - - if(rbps == 0) - return 0; - - /* - * The return value must have 16 fractional bits. Since the whole part - * of the base-2 log of a 32 bit number must fit in 5 bits, and fracbits - * must be >= -3, these assertion allows us to be able to shift rbps - * left if necessary to get 16 fracbits without losing any bits of the - * whole part of rbps. - * - * There is a slight chance due to accumulated error that the whole part - * will require 6 bits, so we use 6 in the assertion. Really though as - * long as it fits in 13 bits (32 - (16 - (-3))) we are fine. - */ - FLAC__ASSERT((int)FLAC__bitmath_ilog2(rbps)+1 <= fracbits + 6); - FLAC__ASSERT(fracbits >= -3); - - /* now shift the decimal point into place */ - if(fracbits < 16) - return rbps << (16-fracbits); - else if(fracbits > 16) - return rbps >> (fracbits-16); - else - return rbps; -} -#endif - -#ifndef FLAC__INTEGER_ONLY_LIBRARY -unsigned FLAC__fixed_compute_best_predictor(const FLAC__int32 data[], unsigned data_len, FLAC__float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]) -#else -unsigned FLAC__fixed_compute_best_predictor(const FLAC__int32 data[], unsigned data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]) -#endif -{ - FLAC__int32 last_error_0 = data[-1]; - FLAC__int32 last_error_1 = data[-1] - data[-2]; - FLAC__int32 last_error_2 = last_error_1 - (data[-2] - data[-3]); - FLAC__int32 last_error_3 = last_error_2 - (data[-2] - 2*data[-3] + data[-4]); - FLAC__int32 error, save; - FLAC__uint32 total_error_0 = 0, total_error_1 = 0, total_error_2 = 0, total_error_3 = 0, total_error_4 = 0; - unsigned i, order; - - for(i = 0; i < data_len; i++) { - error = data[i] ; total_error_0 += local_abs(error); save = error; - error -= last_error_0; total_error_1 += local_abs(error); last_error_0 = save; save = error; - error -= last_error_1; total_error_2 += local_abs(error); last_error_1 = save; save = error; - error -= last_error_2; total_error_3 += local_abs(error); last_error_2 = save; save = error; - error -= last_error_3; total_error_4 += local_abs(error); last_error_3 = save; - } - - if(total_error_0 < min(min(min(total_error_1, total_error_2), total_error_3), total_error_4)) - order = 0; - else if(total_error_1 < min(min(total_error_2, total_error_3), total_error_4)) - order = 1; - else if(total_error_2 < min(total_error_3, total_error_4)) - order = 2; - else if(total_error_3 < total_error_4) - order = 3; - else - order = 4; - - /* Estimate the expected number of bits per residual signal sample. */ - /* 'total_error*' is linearly related to the variance of the residual */ - /* signal, so we use it directly to compute E(|x|) */ - FLAC__ASSERT(data_len > 0 || total_error_0 == 0); - FLAC__ASSERT(data_len > 0 || total_error_1 == 0); - FLAC__ASSERT(data_len > 0 || total_error_2 == 0); - FLAC__ASSERT(data_len > 0 || total_error_3 == 0); - FLAC__ASSERT(data_len > 0 || total_error_4 == 0); -#ifndef FLAC__INTEGER_ONLY_LIBRARY - residual_bits_per_sample[0] = (FLAC__float)((total_error_0 > 0) ? log(M_LN2 * (FLAC__double)total_error_0 / (FLAC__double)data_len) / M_LN2 : 0.0); - residual_bits_per_sample[1] = (FLAC__float)((total_error_1 > 0) ? log(M_LN2 * (FLAC__double)total_error_1 / (FLAC__double)data_len) / M_LN2 : 0.0); - residual_bits_per_sample[2] = (FLAC__float)((total_error_2 > 0) ? log(M_LN2 * (FLAC__double)total_error_2 / (FLAC__double)data_len) / M_LN2 : 0.0); - residual_bits_per_sample[3] = (FLAC__float)((total_error_3 > 0) ? log(M_LN2 * (FLAC__double)total_error_3 / (FLAC__double)data_len) / M_LN2 : 0.0); - residual_bits_per_sample[4] = (FLAC__float)((total_error_4 > 0) ? log(M_LN2 * (FLAC__double)total_error_4 / (FLAC__double)data_len) / M_LN2 : 0.0); -#else - residual_bits_per_sample[0] = (total_error_0 > 0) ? local__compute_rbps_integerized(total_error_0, data_len) : 0; - residual_bits_per_sample[1] = (total_error_1 > 0) ? local__compute_rbps_integerized(total_error_1, data_len) : 0; - residual_bits_per_sample[2] = (total_error_2 > 0) ? local__compute_rbps_integerized(total_error_2, data_len) : 0; - residual_bits_per_sample[3] = (total_error_3 > 0) ? local__compute_rbps_integerized(total_error_3, data_len) : 0; - residual_bits_per_sample[4] = (total_error_4 > 0) ? local__compute_rbps_integerized(total_error_4, data_len) : 0; -#endif - - return order; -} - -#ifndef FLAC__INTEGER_ONLY_LIBRARY -unsigned FLAC__fixed_compute_best_predictor_wide(const FLAC__int32 data[], unsigned data_len, FLAC__float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]) -#else -unsigned FLAC__fixed_compute_best_predictor_wide(const FLAC__int32 data[], unsigned data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]) -#endif -{ - FLAC__int32 last_error_0 = data[-1]; - FLAC__int32 last_error_1 = data[-1] - data[-2]; - FLAC__int32 last_error_2 = last_error_1 - (data[-2] - data[-3]); - FLAC__int32 last_error_3 = last_error_2 - (data[-2] - 2*data[-3] + data[-4]); - FLAC__int32 error, save; - /* total_error_* are 64-bits to avoid overflow when encoding - * erratic signals when the bits-per-sample and blocksize are - * large. - */ - FLAC__uint64 total_error_0 = 0, total_error_1 = 0, total_error_2 = 0, total_error_3 = 0, total_error_4 = 0; - unsigned i, order; - - for(i = 0; i < data_len; i++) { - error = data[i] ; total_error_0 += local_abs(error); save = error; - error -= last_error_0; total_error_1 += local_abs(error); last_error_0 = save; save = error; - error -= last_error_1; total_error_2 += local_abs(error); last_error_1 = save; save = error; - error -= last_error_2; total_error_3 += local_abs(error); last_error_2 = save; save = error; - error -= last_error_3; total_error_4 += local_abs(error); last_error_3 = save; - } - - if(total_error_0 < min(min(min(total_error_1, total_error_2), total_error_3), total_error_4)) - order = 0; - else if(total_error_1 < min(min(total_error_2, total_error_3), total_error_4)) - order = 1; - else if(total_error_2 < min(total_error_3, total_error_4)) - order = 2; - else if(total_error_3 < total_error_4) - order = 3; - else - order = 4; - - /* Estimate the expected number of bits per residual signal sample. */ - /* 'total_error*' is linearly related to the variance of the residual */ - /* signal, so we use it directly to compute E(|x|) */ - FLAC__ASSERT(data_len > 0 || total_error_0 == 0); - FLAC__ASSERT(data_len > 0 || total_error_1 == 0); - FLAC__ASSERT(data_len > 0 || total_error_2 == 0); - FLAC__ASSERT(data_len > 0 || total_error_3 == 0); - FLAC__ASSERT(data_len > 0 || total_error_4 == 0); -#ifndef FLAC__INTEGER_ONLY_LIBRARY -#if defined _MSC_VER || defined __MINGW32__ - /* with MSVC you have to spoon feed it the casting */ - residual_bits_per_sample[0] = (FLAC__float)((total_error_0 > 0) ? log(M_LN2 * (FLAC__double)(FLAC__int64)total_error_0 / (FLAC__double)data_len) / M_LN2 : 0.0); - residual_bits_per_sample[1] = (FLAC__float)((total_error_1 > 0) ? log(M_LN2 * (FLAC__double)(FLAC__int64)total_error_1 / (FLAC__double)data_len) / M_LN2 : 0.0); - residual_bits_per_sample[2] = (FLAC__float)((total_error_2 > 0) ? log(M_LN2 * (FLAC__double)(FLAC__int64)total_error_2 / (FLAC__double)data_len) / M_LN2 : 0.0); - residual_bits_per_sample[3] = (FLAC__float)((total_error_3 > 0) ? log(M_LN2 * (FLAC__double)(FLAC__int64)total_error_3 / (FLAC__double)data_len) / M_LN2 : 0.0); - residual_bits_per_sample[4] = (FLAC__float)((total_error_4 > 0) ? log(M_LN2 * (FLAC__double)(FLAC__int64)total_error_4 / (FLAC__double)data_len) / M_LN2 : 0.0); -#else - residual_bits_per_sample[0] = (FLAC__float)((total_error_0 > 0) ? log(M_LN2 * (FLAC__double)total_error_0 / (FLAC__double)data_len) / M_LN2 : 0.0); - residual_bits_per_sample[1] = (FLAC__float)((total_error_1 > 0) ? log(M_LN2 * (FLAC__double)total_error_1 / (FLAC__double)data_len) / M_LN2 : 0.0); - residual_bits_per_sample[2] = (FLAC__float)((total_error_2 > 0) ? log(M_LN2 * (FLAC__double)total_error_2 / (FLAC__double)data_len) / M_LN2 : 0.0); - residual_bits_per_sample[3] = (FLAC__float)((total_error_3 > 0) ? log(M_LN2 * (FLAC__double)total_error_3 / (FLAC__double)data_len) / M_LN2 : 0.0); - residual_bits_per_sample[4] = (FLAC__float)((total_error_4 > 0) ? log(M_LN2 * (FLAC__double)total_error_4 / (FLAC__double)data_len) / M_LN2 : 0.0); -#endif -#else - residual_bits_per_sample[0] = (total_error_0 > 0) ? local__compute_rbps_wide_integerized(total_error_0, data_len) : 0; - residual_bits_per_sample[1] = (total_error_1 > 0) ? local__compute_rbps_wide_integerized(total_error_1, data_len) : 0; - residual_bits_per_sample[2] = (total_error_2 > 0) ? local__compute_rbps_wide_integerized(total_error_2, data_len) : 0; - residual_bits_per_sample[3] = (total_error_3 > 0) ? local__compute_rbps_wide_integerized(total_error_3, data_len) : 0; - residual_bits_per_sample[4] = (total_error_4 > 0) ? local__compute_rbps_wide_integerized(total_error_4, data_len) : 0; -#endif - - return order; -} - -void FLAC__fixed_compute_residual(const FLAC__int32 data[], unsigned data_len, unsigned order, FLAC__int32 residual[]) -{ - const int idata_len = (int)data_len; - int i; - - switch(order) { - case 0: - FLAC__ASSERT(sizeof(residual[0]) == sizeof(data[0])); - memcpy(residual, data, sizeof(residual[0])*data_len); - break; - case 1: - for(i = 0; i < idata_len; i++) - residual[i] = data[i] - data[i-1]; - break; - case 2: - for(i = 0; i < idata_len; i++) -#if 1 /* OPT: may be faster with some compilers on some systems */ - residual[i] = data[i] - (data[i-1] << 1) + data[i-2]; -#else - residual[i] = data[i] - 2*data[i-1] + data[i-2]; -#endif - break; - case 3: - for(i = 0; i < idata_len; i++) -#if 1 /* OPT: may be faster with some compilers on some systems */ - residual[i] = data[i] - (((data[i-1]-data[i-2])<<1) + (data[i-1]-data[i-2])) - data[i-3]; -#else - residual[i] = data[i] - 3*data[i-1] + 3*data[i-2] - data[i-3]; -#endif - break; - case 4: - for(i = 0; i < idata_len; i++) -#if 1 /* OPT: may be faster with some compilers on some systems */ - residual[i] = data[i] - ((data[i-1]+data[i-3])<<2) + ((data[i-2]<<2) + (data[i-2]<<1)) + data[i-4]; -#else - residual[i] = data[i] - 4*data[i-1] + 6*data[i-2] - 4*data[i-3] + data[i-4]; -#endif - break; - default: - FLAC__ASSERT(0); - } -} - -void FLAC__fixed_restore_signal(const FLAC__int32 residual[], unsigned data_len, unsigned order, FLAC__int32 data[]) -{ - int i, idata_len = (int)data_len; - - switch(order) { - case 0: - FLAC__ASSERT(sizeof(residual[0]) == sizeof(data[0])); - memcpy(data, residual, sizeof(residual[0])*data_len); - break; - case 1: - for(i = 0; i < idata_len; i++) - data[i] = residual[i] + data[i-1]; - break; - case 2: - for(i = 0; i < idata_len; i++) -#if 1 /* OPT: may be faster with some compilers on some systems */ - data[i] = residual[i] + (data[i-1]<<1) - data[i-2]; -#else - data[i] = residual[i] + 2*data[i-1] - data[i-2]; -#endif - break; - case 3: - for(i = 0; i < idata_len; i++) -#if 1 /* OPT: may be faster with some compilers on some systems */ - data[i] = residual[i] + (((data[i-1]-data[i-2])<<1) + (data[i-1]-data[i-2])) + data[i-3]; -#else - data[i] = residual[i] + 3*data[i-1] - 3*data[i-2] + data[i-3]; -#endif - break; - case 4: - for(i = 0; i < idata_len; i++) -#if 1 /* OPT: may be faster with some compilers on some systems */ - data[i] = residual[i] + ((data[i-1]+data[i-3])<<2) - ((data[i-2]<<2) + (data[i-2]<<1)) - data[i-4]; -#else - data[i] = residual[i] + 4*data[i-1] - 6*data[i-2] + 4*data[i-3] - data[i-4]; -#endif - break; - default: - FLAC__ASSERT(0); - } -} diff --git a/FLAC/format.c b/FLAC/format.c deleted file mode 100644 index 0fdbb61e8..000000000 --- a/FLAC/format.c +++ /dev/null @@ -1,593 +0,0 @@ -/* libFLAC - Free Lossless Audio Codec library - * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 Josh Coalson - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * - Neither the name of the Xiph.org Foundation nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#if HAVE_CONFIG_H -# include -#endif - -#include -#include /* for qsort() */ -#include /* for memset() */ -#include "FLAC/assert.h" -#include "FLAC/format.h" -#include "private/format.h" - -#ifndef FLaC__INLINE -#define FLaC__INLINE -#endif - -#ifdef min -#undef min -#endif -#define min(a,b) ((a)<(b)?(a):(b)) - -/* adjust for compilers that can't understand using LLU suffix for uint64_t literals */ -#ifdef _MSC_VER -#define FLAC__U64L(x) x -#else -#define FLAC__U64L(x) x##LLU -#endif - -/* VERSION should come from configure */ -FLAC_API const char *FLAC__VERSION_STRING = "1.2.1"; - -#if defined _MSC_VER || defined __BORLANDC__ || defined __MINW32__ -/* yet one more hack because of MSVC6: */ -FLAC_API const char *FLAC__VENDOR_STRING = "reference libFLAC 1.1.2 20070917"; -#else -FLAC_API const char *FLAC__VENDOR_STRING = "reference libFLAC " VERSION " 20070917"; -#endif - -FLAC_API const FLAC__byte FLAC__STREAM_SYNC_STRING[4] = { 'f','L','a','C' }; -FLAC_API const unsigned FLAC__STREAM_SYNC = 0x664C6143; -FLAC_API const unsigned FLAC__STREAM_SYNC_LEN = 32; /* bits */ - -FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN = 16; /* bits */ -FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN = 16; /* bits */ -FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN = 24; /* bits */ -FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN = 24; /* bits */ -FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN = 20; /* bits */ -FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN = 3; /* bits */ -FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN = 5; /* bits */ -FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN = 36; /* bits */ -FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_MD5SUM_LEN = 128; /* bits */ - -FLAC_API const unsigned FLAC__STREAM_METADATA_APPLICATION_ID_LEN = 32; /* bits */ - -FLAC_API const unsigned FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN = 64; /* bits */ -FLAC_API const unsigned FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN = 64; /* bits */ -FLAC_API const unsigned FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN = 16; /* bits */ - -FLAC_API const FLAC__uint64 FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER = FLAC__U64L(0xffffffffffffffff); - -FLAC_API const unsigned FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN = 32; /* bits */ -FLAC_API const unsigned FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN = 32; /* bits */ - -FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN = 64; /* bits */ -FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN = 8; /* bits */ -FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN = 3*8; /* bits */ - -FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN = 64; /* bits */ -FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN = 8; /* bits */ -FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN = 12*8; /* bits */ -FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN = 1; /* bit */ -FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN = 1; /* bit */ -FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN = 6+13*8; /* bits */ -FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN = 8; /* bits */ - -FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN = 128*8; /* bits */ -FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN = 64; /* bits */ -FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN = 1; /* bit */ -FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN = 7+258*8; /* bits */ -FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN = 8; /* bits */ - -FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_TYPE_LEN = 32; /* bits */ -FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN = 32; /* bits */ -FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN = 32; /* bits */ -FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN = 32; /* bits */ -FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN = 32; /* bits */ -FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN = 32; /* bits */ -FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_COLORS_LEN = 32; /* bits */ -FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN = 32; /* bits */ - -FLAC_API const unsigned FLAC__STREAM_METADATA_IS_LAST_LEN = 1; /* bits */ -FLAC_API const unsigned FLAC__STREAM_METADATA_TYPE_LEN = 7; /* bits */ -FLAC_API const unsigned FLAC__STREAM_METADATA_LENGTH_LEN = 24; /* bits */ - -FLAC_API const unsigned FLAC__FRAME_HEADER_SYNC = 0x3ffe; -FLAC_API const unsigned FLAC__FRAME_HEADER_SYNC_LEN = 14; /* bits */ -FLAC_API const unsigned FLAC__FRAME_HEADER_RESERVED_LEN = 1; /* bits */ -FLAC_API const unsigned FLAC__FRAME_HEADER_BLOCKING_STRATEGY_LEN = 1; /* bits */ -FLAC_API const unsigned FLAC__FRAME_HEADER_BLOCK_SIZE_LEN = 4; /* bits */ -FLAC_API const unsigned FLAC__FRAME_HEADER_SAMPLE_RATE_LEN = 4; /* bits */ -FLAC_API const unsigned FLAC__FRAME_HEADER_CHANNEL_ASSIGNMENT_LEN = 4; /* bits */ -FLAC_API const unsigned FLAC__FRAME_HEADER_BITS_PER_SAMPLE_LEN = 3; /* bits */ -FLAC_API const unsigned FLAC__FRAME_HEADER_ZERO_PAD_LEN = 1; /* bits */ -FLAC_API const unsigned FLAC__FRAME_HEADER_CRC_LEN = 8; /* bits */ - -FLAC_API const unsigned FLAC__FRAME_FOOTER_CRC_LEN = 16; /* bits */ - -FLAC_API const unsigned FLAC__ENTROPY_CODING_METHOD_TYPE_LEN = 2; /* bits */ -FLAC_API const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN = 4; /* bits */ -FLAC_API const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN = 4; /* bits */ -FLAC_API const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN = 5; /* bits */ -FLAC_API const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN = 5; /* bits */ - -FLAC_API const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER = 15; /* == (1< FLAC__MAX_SAMPLE_RATE) { - return false; - } - else - return true; -} - -FLAC_API FLAC__bool FLAC__format_sample_rate_is_subset(unsigned sample_rate) -{ - if( - !FLAC__format_sample_rate_is_valid(sample_rate) || - ( - sample_rate >= (1u << 16) && - !(sample_rate % 1000 == 0 || sample_rate % 10 == 0) - ) - ) { - return false; - } - else - return true; -} - -/* @@@@ add to unit tests; it is already indirectly tested by the metadata_object tests */ -FLAC_API FLAC__bool FLAC__format_seektable_is_legal(const FLAC__StreamMetadata_SeekTable *seek_table) -{ - unsigned i; - FLAC__uint64 prev_sample_number = 0; - FLAC__bool got_prev = false; - - FLAC__ASSERT(0 != seek_table); - - for(i = 0; i < seek_table->num_points; i++) { - if(got_prev) { - if( - seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER && - seek_table->points[i].sample_number <= prev_sample_number - ) - return false; - } - prev_sample_number = seek_table->points[i].sample_number; - got_prev = true; - } - - return true; -} - -/* used as the sort predicate for qsort() */ -static int seekpoint_compare_(const FLAC__StreamMetadata_SeekPoint *l, const FLAC__StreamMetadata_SeekPoint *r) -{ - /* we don't just 'return l->sample_number - r->sample_number' since the result (FLAC__int64) might overflow an 'int' */ - if(l->sample_number == r->sample_number) - return 0; - else if(l->sample_number < r->sample_number) - return -1; - else - return 1; -} - -/* @@@@ add to unit tests; it is already indirectly tested by the metadata_object tests */ -FLAC_API unsigned FLAC__format_seektable_sort(FLAC__StreamMetadata_SeekTable *seek_table) -{ - unsigned i, j; - FLAC__bool first; - - FLAC__ASSERT(0 != seek_table); - - /* sort the seekpoints */ - qsort(seek_table->points, seek_table->num_points, sizeof(FLAC__StreamMetadata_SeekPoint), (int (*)(const void *, const void *))seekpoint_compare_); - - /* uniquify the seekpoints */ - first = true; - for(i = j = 0; i < seek_table->num_points; i++) { - if(seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER) { - if(!first) { - if(seek_table->points[i].sample_number == seek_table->points[j-1].sample_number) - continue; - } - } - first = false; - seek_table->points[j++] = seek_table->points[i]; - } - - for(i = j; i < seek_table->num_points; i++) { - seek_table->points[i].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER; - seek_table->points[i].stream_offset = 0; - seek_table->points[i].frame_samples = 0; - } - - return j; -} - -/* - * also disallows non-shortest-form encodings, c.f. - * http://www.unicode.org/versions/corrigendum1.html - * and a more clear explanation at the end of this section: - * http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 - */ -static FLaC__INLINE unsigned utf8len_(const FLAC__byte *utf8) -{ - FLAC__ASSERT(0 != utf8); - if ((utf8[0] & 0x80) == 0) { - return 1; - } - else if ((utf8[0] & 0xE0) == 0xC0 && (utf8[1] & 0xC0) == 0x80) { - if ((utf8[0] & 0xFE) == 0xC0) /* overlong sequence check */ - return 0; - return 2; - } - else if ((utf8[0] & 0xF0) == 0xE0 && (utf8[1] & 0xC0) == 0x80 && (utf8[2] & 0xC0) == 0x80) { - if (utf8[0] == 0xE0 && (utf8[1] & 0xE0) == 0x80) /* overlong sequence check */ - return 0; - /* illegal surrogates check (U+D800...U+DFFF and U+FFFE...U+FFFF) */ - if (utf8[0] == 0xED && (utf8[1] & 0xE0) == 0xA0) /* D800-DFFF */ - return 0; - if (utf8[0] == 0xEF && utf8[1] == 0xBF && (utf8[2] & 0xFE) == 0xBE) /* FFFE-FFFF */ - return 0; - return 3; - } - else if ((utf8[0] & 0xF8) == 0xF0 && (utf8[1] & 0xC0) == 0x80 && (utf8[2] & 0xC0) == 0x80 && (utf8[3] & 0xC0) == 0x80) { - if (utf8[0] == 0xF0 && (utf8[1] & 0xF0) == 0x80) /* overlong sequence check */ - return 0; - return 4; - } - else if ((utf8[0] & 0xFC) == 0xF8 && (utf8[1] & 0xC0) == 0x80 && (utf8[2] & 0xC0) == 0x80 && (utf8[3] & 0xC0) == 0x80 && (utf8[4] & 0xC0) == 0x80) { - if (utf8[0] == 0xF8 && (utf8[1] & 0xF8) == 0x80) /* overlong sequence check */ - return 0; - return 5; - } - else if ((utf8[0] & 0xFE) == 0xFC && (utf8[1] & 0xC0) == 0x80 && (utf8[2] & 0xC0) == 0x80 && (utf8[3] & 0xC0) == 0x80 && (utf8[4] & 0xC0) == 0x80 && (utf8[5] & 0xC0) == 0x80) { - if (utf8[0] == 0xFC && (utf8[1] & 0xFC) == 0x80) /* overlong sequence check */ - return 0; - return 6; - } - else { - return 0; - } -} - -FLAC_API FLAC__bool FLAC__format_vorbiscomment_entry_name_is_legal(const char *name) -{ - char c; - for(c = *name; c; c = *(++name)) - if(c < 0x20 || c == 0x3d || c > 0x7d) - return false; - return true; -} - -FLAC_API FLAC__bool FLAC__format_vorbiscomment_entry_value_is_legal(const FLAC__byte *value, unsigned length) -{ - if(length == (unsigned)(-1)) { - while(*value) { - unsigned n = utf8len_(value); - if(n == 0) - return false; - value += n; - } - } - else { - const FLAC__byte *end = value + length; - while(value < end) { - unsigned n = utf8len_(value); - if(n == 0) - return false; - value += n; - } - if(value != end) - return false; - } - return true; -} - -FLAC_API FLAC__bool FLAC__format_vorbiscomment_entry_is_legal(const FLAC__byte *entry, unsigned length) -{ - const FLAC__byte *s, *end; - - for(s = entry, end = s + length; s < end && *s != '='; s++) { - if(*s < 0x20 || *s > 0x7D) - return false; - } - if(s == end) - return false; - - s++; /* skip '=' */ - - while(s < end) { - unsigned n = utf8len_(s); - if(n == 0) - return false; - s += n; - } - if(s != end) - return false; - - return true; -} - -/* @@@@ add to unit tests; it is already indirectly tested by the metadata_object tests */ -FLAC_API FLAC__bool FLAC__format_cuesheet_is_legal(const FLAC__StreamMetadata_CueSheet *cue_sheet, FLAC__bool check_cd_da_subset, const char **violation) -{ - unsigned i, j; - - if(check_cd_da_subset) { - if(cue_sheet->lead_in < 2 * 44100) { - if(violation) *violation = "CD-DA cue sheet must have a lead-in length of at least 2 seconds"; - return false; - } - if(cue_sheet->lead_in % 588 != 0) { - if(violation) *violation = "CD-DA cue sheet lead-in length must be evenly divisible by 588 samples"; - return false; - } - } - - if(cue_sheet->num_tracks == 0) { - if(violation) *violation = "cue sheet must have at least one track (the lead-out)"; - return false; - } - - if(check_cd_da_subset && cue_sheet->tracks[cue_sheet->num_tracks-1].number != 170) { - if(violation) *violation = "CD-DA cue sheet must have a lead-out track number 170 (0xAA)"; - return false; - } - - for(i = 0; i < cue_sheet->num_tracks; i++) { - if(cue_sheet->tracks[i].number == 0) { - if(violation) *violation = "cue sheet may not have a track number 0"; - return false; - } - - if(check_cd_da_subset) { - if(!((cue_sheet->tracks[i].number >= 1 && cue_sheet->tracks[i].number <= 99) || cue_sheet->tracks[i].number == 170)) { - if(violation) *violation = "CD-DA cue sheet track number must be 1-99 or 170"; - return false; - } - } - - if(check_cd_da_subset && cue_sheet->tracks[i].offset % 588 != 0) { - if(violation) { - if(i == cue_sheet->num_tracks-1) /* the lead-out track... */ - *violation = "CD-DA cue sheet lead-out offset must be evenly divisible by 588 samples"; - else - *violation = "CD-DA cue sheet track offset must be evenly divisible by 588 samples"; - } - return false; - } - - if(i < cue_sheet->num_tracks - 1) { - if(cue_sheet->tracks[i].num_indices == 0) { - if(violation) *violation = "cue sheet track must have at least one index point"; - return false; - } - - if(cue_sheet->tracks[i].indices[0].number > 1) { - if(violation) *violation = "cue sheet track's first index number must be 0 or 1"; - return false; - } - } - - for(j = 0; j < cue_sheet->tracks[i].num_indices; j++) { - if(check_cd_da_subset && cue_sheet->tracks[i].indices[j].offset % 588 != 0) { - if(violation) *violation = "CD-DA cue sheet track index offset must be evenly divisible by 588 samples"; - return false; - } - - if(j > 0) { - if(cue_sheet->tracks[i].indices[j].number != cue_sheet->tracks[i].indices[j-1].number + 1) { - if(violation) *violation = "cue sheet track index numbers must increase by 1"; - return false; - } - } - } - } - - return true; -} - -/* @@@@ add to unit tests; it is already indirectly tested by the metadata_object tests */ -FLAC_API FLAC__bool FLAC__format_picture_is_legal(const FLAC__StreamMetadata_Picture *picture, const char **violation) -{ - char *p; - FLAC__byte *b; - - for(p = picture->mime_type; *p; p++) { - if(*p < 0x20 || *p > 0x7e) { - if(violation) *violation = "MIME type string must contain only printable ASCII characters (0x20-0x7e)"; - return false; - } - } - - for(b = picture->description; *b; ) { - unsigned n = utf8len_(b); - if(n == 0) { - if(violation) *violation = "description string must be valid UTF-8"; - return false; - } - b += n; - } - - return true; -} - -/* - * These routines are private to libFLAC - */ -unsigned FLAC__format_get_max_rice_partition_order(unsigned blocksize, unsigned predictor_order) -{ - return - FLAC__format_get_max_rice_partition_order_from_blocksize_limited_max_and_predictor_order( - FLAC__format_get_max_rice_partition_order_from_blocksize(blocksize), - blocksize, - predictor_order - ); -} - -unsigned FLAC__format_get_max_rice_partition_order_from_blocksize(unsigned blocksize) -{ - unsigned max_rice_partition_order = 0; - while(!(blocksize & 1)) { - max_rice_partition_order++; - blocksize >>= 1; - } - return min(FLAC__MAX_RICE_PARTITION_ORDER, max_rice_partition_order); -} - -unsigned FLAC__format_get_max_rice_partition_order_from_blocksize_limited_max_and_predictor_order(unsigned limit, unsigned blocksize, unsigned predictor_order) -{ - unsigned max_rice_partition_order = limit; - - while(max_rice_partition_order > 0 && (blocksize >> max_rice_partition_order) <= predictor_order) - max_rice_partition_order--; - - FLAC__ASSERT( - (max_rice_partition_order == 0 && blocksize >= predictor_order) || - (max_rice_partition_order > 0 && blocksize >> max_rice_partition_order > predictor_order) - ); - - return max_rice_partition_order; -} - -void FLAC__format_entropy_coding_method_partitioned_rice_contents_init(FLAC__EntropyCodingMethod_PartitionedRiceContents *object) -{ - FLAC__ASSERT(0 != object); - - object->parameters = 0; - object->raw_bits = 0; - object->capacity_by_order = 0; -} - -void FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(FLAC__EntropyCodingMethod_PartitionedRiceContents *object) -{ - FLAC__ASSERT(0 != object); - - if(0 != object->parameters) - free(object->parameters); - if(0 != object->raw_bits) - free(object->raw_bits); - FLAC__format_entropy_coding_method_partitioned_rice_contents_init(object); -} - -FLAC__bool FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(FLAC__EntropyCodingMethod_PartitionedRiceContents *object, unsigned max_partition_order) -{ - FLAC__ASSERT(0 != object); - - FLAC__ASSERT(object->capacity_by_order > 0 || (0 == object->parameters && 0 == object->raw_bits)); - - if(object->capacity_by_order < max_partition_order) { - if(0 == (object->parameters = (unsigned*)realloc(object->parameters, sizeof(unsigned) << max_partition_order))) - return false; - if(0 == (object->raw_bits = (unsigned*)realloc(object->raw_bits, sizeof(unsigned) << max_partition_order))) - return false; - memset(object->raw_bits, 0, sizeof(unsigned)*(1 << max_partition_order)); - object->capacity_by_order = max_partition_order; - } - - return true; -} diff --git a/FLAC/ia32/bitreader_asm.nasm b/FLAC/ia32/bitreader_asm.nasm deleted file mode 100644 index f2b5464ff..000000000 --- a/FLAC/ia32/bitreader_asm.nasm +++ /dev/null @@ -1,568 +0,0 @@ -; vim:filetype=nasm ts=8 - -; libFLAC - Free Lossless Audio Codec library -; Copyright (C) 2001,2002,2003,2004,2005,2006,2007 Josh Coalson -; -; Redistribution and use in source and binary forms, with or without -; modification, are permitted provided that the following conditions -; are met: -; -; - Redistributions of source code must retain the above copyright -; notice, this list of conditions and the following disclaimer. -; -; - Redistributions in binary form must reproduce the above copyright -; notice, this list of conditions and the following disclaimer in the -; documentation and/or other materials provided with the distribution. -; -; - Neither the name of the Xiph.org Foundation nor the names of its -; contributors may be used to endorse or promote products derived from -; this software without specific prior written permission. -; -; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -; ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR -; CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, -; EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR -; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF -; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING -; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -%include "ia32/nasm.h" - - data_section - -cextern FLAC__crc16_table ; unsigned FLAC__crc16_table[256]; -cextern bitreader_read_from_client_ ; FLAC__bool bitreader_read_from_client_(FLAC__BitReader *br); - -cglobal FLAC__bitreader_read_rice_signed_block_asm_ia32_bswap - - code_section - - -; ********************************************************************** -; -; void FLAC__bool FLAC__bitreader_read_rice_signed_block(FLAC__BitReader *br, int vals[], unsigned nvals, unsigned parameter) -; -; Some details like assertions and other checking is performed by the caller. - ALIGN 16 -cident FLAC__bitreader_read_rice_signed_block_asm_ia32_bswap - - ;ASSERT(0 != br); - ;ASSERT(0 != br->buffer); - ; WATCHOUT: code only works if sizeof(brword)==32; we can make things much faster with this assertion - ;ASSERT(FLAC__BITS_PER_WORD == 32); - ;ASSERT(parameter < 32); - ; the above two asserts also guarantee that the binary part never straddles more than 2 words, so we don't have to loop to read it - - ;; peppered throughout the code at major checkpoints are keys like this as to where things are at that point in time - ;; [esp + 16] unsigned parameter - ;; [esp + 12] unsigned nvals - ;; [esp + 8] int vals[] - ;; [esp + 4] FLAC__BitReader *br - mov eax, [esp + 12] ; if(nvals == 0) - test eax, eax - ja .nvals_gt_0 - mov eax, 1 ; return true; - ret - -.nvals_gt_0: - push ebp - push ebx - push esi - push edi - sub esp, 4 - ;; [esp + 36] unsigned parameter - ;; [esp + 32] unsigned nvals - ;; [esp + 28] int vals[] - ;; [esp + 24] FLAC__BitReader *br - ;; [esp] ucbits - mov ebp, [esp + 24] ; ebp <- br == br->buffer - mov esi, [ebp + 16] ; esi <- br->consumed_words (aka 'cwords' in the C version) - mov ecx, [ebp + 20] ; ecx <- br->consumed_bits (aka 'cbits' in the C version) - xor edi, edi ; edi <- 0 'uval' - ;; ecx cbits - ;; esi cwords - ;; edi uval - ;; ebp br - ;; [ebp] br->buffer - ;; [ebp + 8] br->words - ;; [ebp + 12] br->bytes - ;; [ebp + 16] br->consumed_words - ;; [ebp + 20] br->consumed_bits - ;; [ebp + 24] br->read_crc - ;; [ebp + 28] br->crc16_align - - ; ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits; - mov eax, [ebp + 8] ; eax <- br->words - sub eax, esi ; eax <- br->words-cwords - shl eax, 2 ; eax <- (br->words-cwords)*FLAC__BYTES_PER_WORD - add eax, [ebp + 12] ; eax <- (br->words-cwords)*FLAC__BYTES_PER_WORD + br->bytes - shl eax, 3 ; eax <- (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - sub eax, ecx ; eax <- (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits - mov [esp], eax ; ucbits <- eax - - ALIGN 16 -.val_loop: ; while(1) { - - ; - ; read unary part - ; -.unary_loop: ; while(1) { - ;; ecx cbits - ;; esi cwords - ;; edi uval - ;; ebp br - cmp esi, [ebp + 8] ; while(cwords < br->words) /* if we've not consumed up to a partial tail word... */ - jae near .c1_next1 -.c1_loop: ; { - mov ebx, [ebp] - mov eax, [ebx + 4*esi] ; b = br->buffer[cwords] - mov edx, eax ; edx = br->buffer[cwords] (saved for later use) - shl eax, cl ; b = br->buffer[cwords] << cbits - test eax, eax ; (still have to test since cbits may be 0, thus ZF not updated for shl eax,0) - jz near .c1_next2 ; if(b) { - bsr ebx, eax - not ebx - and ebx, 31 ; ebx = 'i' = # of leading 0 bits in 'b' (eax) - add ecx, ebx ; cbits += i; - add edi, ebx ; uval += i; - add ecx, byte 1 ; cbits++; /* skip over stop bit */ - test ecx, ~31 - jz near .break1 ; if(cbits >= FLAC__BITS_PER_WORD) { /* faster way of testing if(cbits == FLAC__BITS_PER_WORD) */ - ; crc16_update_word_(br, br->buffer[cwords]); - push edi ; [need more registers] - bswap edx ; edx = br->buffer[cwords] swapped; now we can CRC the bytes from LSByte to MSByte which makes things much easier - mov ecx, [ebp + 28] ; ecx <- br->crc16_align - mov eax, [ebp + 24] ; ax <- br->read_crc (a.k.a. crc) -%ifdef FLAC__PUBLIC_NEEDS_UNDERSCORE - mov edi, _FLAC__crc16_table -%else - mov edi, FLAC__crc16_table -%endif - ;; eax (ax) crc a.k.a. br->read_crc - ;; ebx (bl) intermediate result index into FLAC__crc16_table[] - ;; ecx br->crc16_align - ;; edx byteswapped brword to CRC - ;; esi cwords - ;; edi unsigned FLAC__crc16_table[] - ;; ebp br - test ecx, ecx ; switch(br->crc16_align) ... - jnz .c0b4 ; [br->crc16_align is 0 the vast majority of the time so we optimize the common case] -.c0b0: xor dl, ah ; dl <- (crc>>8)^(word>>24) - movzx ebx, dl - mov ecx, [ebx*4 + edi] ; cx <- FLAC__crc16_table[(crc>>8)^(word>>24)] - shl eax, 8 ; ax <- (crc<<8) - xor eax, ecx ; crc <- ax <- (crc<<8) ^ FLAC__crc16_table[(crc>>8)^(word>>24)] -.c0b1: xor dh, ah ; dh <- (crc>>8)^((word>>16)&0xff)) - movzx ebx, dh - mov ecx, [ebx*4 + edi] ; cx <- FLAC__crc16_table[(crc>>8)^((word>>16)&0xff))] - shl eax, 8 ; ax <- (crc<<8) - xor eax, ecx ; crc <- ax <- (crc<<8) ^ FLAC__crc16_table[(crc>>8)^((word>>16)&0xff))] - shr edx, 16 -.c0b2: xor dl, ah ; dl <- (crc>>8)^((word>>8)&0xff)) - movzx ebx, dl - mov ecx, [ebx*4 + edi] ; cx <- FLAC__crc16_table[(crc>>8)^((word>>8)&0xff))] - shl eax, 8 ; ax <- (crc<<8) - xor eax, ecx ; crc <- ax <- (crc<<8) ^ FLAC__crc16_table[(crc>>8)^((word>>8)&0xff))] -.c0b3: xor dh, ah ; dh <- (crc>>8)^(word&0xff) - movzx ebx, dh - mov ecx, [ebx*4 + edi] ; cx <- FLAC__crc16_table[(crc>>8)^(word&0xff)] - shl eax, 8 ; ax <- (crc<<8) - xor eax, ecx ; crc <- ax <- (crc<<8) ^ FLAC__crc16_table[(crc>>8)^(word&0xff)] - movzx eax, ax - mov [ebp + 24], eax ; br->read_crc <- crc - pop edi - - add esi, byte 1 ; cwords++; - xor ecx, ecx ; cbits = 0; - ; } - jmp near .break1 ; goto break1; - ;; this section relocated out of the way for performance -.c0b4: - mov [ebp + 28], dword 0 ; br->crc16_align <- 0 - cmp ecx, 8 - je .c0b1 - shr edx, 16 - cmp ecx, 16 - je .c0b2 - jmp .c0b3 - - ;; this section relocated out of the way for performance -.c1b4: - mov [ebp + 28], dword 0 ; br->crc16_align <- 0 - cmp ecx, 8 - je .c1b1 - shr edx, 16 - cmp ecx, 16 - je .c1b2 - jmp .c1b3 - -.c1_next2: ; } else { - ;; ecx cbits - ;; edx current brword 'b' - ;; esi cwords - ;; edi uval - ;; ebp br - add edi, 32 - sub edi, ecx ; uval += FLAC__BITS_PER_WORD - cbits; - ; crc16_update_word_(br, br->buffer[cwords]); - push edi ; [need more registers] - bswap edx ; edx = br->buffer[cwords] swapped; now we can CRC the bytes from LSByte to MSByte which makes things much easier - mov ecx, [ebp + 28] ; ecx <- br->crc16_align - mov eax, [ebp + 24] ; ax <- br->read_crc (a.k.a. crc) -%ifdef FLAC__PUBLIC_NEEDS_UNDERSCORE - mov edi, _FLAC__crc16_table -%else - mov edi, FLAC__crc16_table -%endif - ;; eax (ax) crc a.k.a. br->read_crc - ;; ebx (bl) intermediate result index into FLAC__crc16_table[] - ;; ecx br->crc16_align - ;; edx byteswapped brword to CRC - ;; esi cwords - ;; edi unsigned FLAC__crc16_table[] - ;; ebp br - test ecx, ecx ; switch(br->crc16_align) ... - jnz .c1b4 ; [br->crc16_align is 0 the vast majority of the time so we optimize the common case] -.c1b0: xor dl, ah ; dl <- (crc>>8)^(word>>24) - movzx ebx, dl - mov ecx, [ebx*4 + edi] ; cx <- FLAC__crc16_table[(crc>>8)^(word>>24)] - shl eax, 8 ; ax <- (crc<<8) - xor eax, ecx ; crc <- ax <- (crc<<8) ^ FLAC__crc16_table[(crc>>8)^(word>>24)] -.c1b1: xor dh, ah ; dh <- (crc>>8)^((word>>16)&0xff)) - movzx ebx, dh - mov ecx, [ebx*4 + edi] ; cx <- FLAC__crc16_table[(crc>>8)^((word>>16)&0xff))] - shl eax, 8 ; ax <- (crc<<8) - xor eax, ecx ; crc <- ax <- (crc<<8) ^ FLAC__crc16_table[(crc>>8)^((word>>16)&0xff))] - shr edx, 16 -.c1b2: xor dl, ah ; dl <- (crc>>8)^((word>>8)&0xff)) - movzx ebx, dl - mov ecx, [ebx*4 + edi] ; cx <- FLAC__crc16_table[(crc>>8)^((word>>8)&0xff))] - shl eax, 8 ; ax <- (crc<<8) - xor eax, ecx ; crc <- ax <- (crc<<8) ^ FLAC__crc16_table[(crc>>8)^((word>>8)&0xff))] -.c1b3: xor dh, ah ; dh <- (crc>>8)^(word&0xff) - movzx ebx, dh - mov ecx, [ebx*4 + edi] ; cx <- FLAC__crc16_table[(crc>>8)^(word&0xff)] - shl eax, 8 ; ax <- (crc<<8) - xor eax, ecx ; crc <- ax <- (crc<<8) ^ FLAC__crc16_table[(crc>>8)^(word&0xff)] - movzx eax, ax - mov [ebp + 24], eax ; br->read_crc <- crc - pop edi - - add esi, byte 1 ; cwords++; - xor ecx, ecx ; cbits = 0; - ; /* didn't find stop bit yet, have to keep going... */ - ; } - - cmp esi, [ebp + 8] ; } while(cwords < br->words) /* if we've not consumed up to a partial tail word... */ - jb near .c1_loop - -.c1_next1: - ; at this point we've eaten up all the whole words; have to try - ; reading through any tail bytes before calling the read callback. - ; this is a repeat of the above logic adjusted for the fact we - ; don't have a whole word. note though if the client is feeding - ; us data a byte at a time (unlikely), br->consumed_bits may not - ; be zero. - ;; ecx cbits - ;; esi cwords - ;; edi uval - ;; ebp br - mov edx, [ebp + 12] ; edx <- br->bytes - test edx, edx - jz .read1 ; if(br->bytes) { [NOTE: this case is rare so it doesn't have to be all that fast ] - mov ebx, [ebp] - shl edx, 3 ; edx <- const unsigned end = br->bytes * 8; - mov eax, [ebx + 4*esi] ; b = br->buffer[cwords] - xchg edx, ecx ; [edx <- cbits , ecx <- end] - mov ebx, 0xffffffff ; ebx <- FLAC__WORD_ALL_ONES - shr ebx, cl ; ebx <- FLAC__WORD_ALL_ONES >> end - not ebx ; ebx <- ~(FLAC__WORD_ALL_ONES >> end) - xchg edx, ecx ; [edx <- end , ecx <- cbits] - and eax, ebx ; b = (br->buffer[cwords] & ~(FLAC__WORD_ALL_ONES >> end)); - shl eax, cl ; b = (br->buffer[cwords] & ~(FLAC__WORD_ALL_ONES >> end)) << cbits; - test eax, eax ; (still have to test since cbits may be 0, thus ZF not updated for shl eax,0) - jz .c1_next3 ; if(b) { - bsr ebx, eax - not ebx - and ebx, 31 ; ebx = 'i' = # of leading 0 bits in 'b' (eax) - add ecx, ebx ; cbits += i; - add edi, ebx ; uval += i; - add ecx, byte 1 ; cbits++; /* skip over stop bit */ - jmp short .break1 ; goto break1; -.c1_next3: ; } else { - sub edi, ecx - add edi, edx ; uval += end - cbits; - add ecx, edx ; cbits += end - ; /* didn't find stop bit yet, have to keep going... */ - ; } - ; } -.read1: - ; flush registers and read; bitreader_read_from_client_() does - ; not touch br->consumed_bits at all but we still need to set - ; it in case it fails and we have to return false. - ;; ecx cbits - ;; esi cwords - ;; edi uval - ;; ebp br - mov [ebp + 16], esi ; br->consumed_words = cwords; - mov [ebp + 20], ecx ; br->consumed_bits = cbits; - push ecx ; /* save */ - push ebp ; /* push br argument */ -%ifdef FLAC__PUBLIC_NEEDS_UNDERSCORE - call _bitreader_read_from_client_ -%else - call bitreader_read_from_client_ -%endif - pop edx ; /* discard, unused */ - pop ecx ; /* restore */ - mov esi, [ebp + 16] ; cwords = br->consumed_words; - ; ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits; - mov ebx, [ebp + 8] ; ebx <- br->words - sub ebx, esi ; ebx <- br->words-cwords - shl ebx, 2 ; ebx <- (br->words-cwords)*FLAC__BYTES_PER_WORD - add ebx, [ebp + 12] ; ebx <- (br->words-cwords)*FLAC__BYTES_PER_WORD + br->bytes - shl ebx, 3 ; ebx <- (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - sub ebx, ecx ; ebx <- (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits - add ebx, edi ; ebx <- (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits + uval - ; + uval to offset our count by the # of unary bits already - ; consumed before the read, because we will add these back - ; in all at once at break1 - mov [esp], ebx ; ucbits <- ebx - test eax, eax ; if(!bitreader_read_from_client_(br)) - jnz near .unary_loop - jmp .end ; return false; /* eax (the return value) is already 0 */ - ; } /* end while(1) unary part */ - - ALIGN 16 -.break1: - ;; ecx cbits - ;; esi cwords - ;; edi uval - ;; ebp br - ;; [esp] ucbits - sub [esp], edi ; ucbits -= uval; - sub dword [esp], byte 1 ; ucbits--; /* account for stop bit */ - - ; - ; read binary part - ; - mov ebx, [esp + 36] ; ebx <- parameter - test ebx, ebx ; if(parameter) { - jz near .break2 -.read2: - cmp [esp], ebx ; while(ucbits < parameter) { - jae .c2_next1 - ; flush registers and read; bitreader_read_from_client_() does - ; not touch br->consumed_bits at all but we still need to set - ; it in case it fails and we have to return false. - mov [ebp + 16], esi ; br->consumed_words = cwords; - mov [ebp + 20], ecx ; br->consumed_bits = cbits; - push ecx ; /* save */ - push ebp ; /* push br argument */ -%ifdef FLAC__PUBLIC_NEEDS_UNDERSCORE - call _bitreader_read_from_client_ -%else - call bitreader_read_from_client_ -%endif - pop edx ; /* discard, unused */ - pop ecx ; /* restore */ - mov esi, [ebp + 16] ; cwords = br->consumed_words; - ; ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits; - mov edx, [ebp + 8] ; edx <- br->words - sub edx, esi ; edx <- br->words-cwords - shl edx, 2 ; edx <- (br->words-cwords)*FLAC__BYTES_PER_WORD - add edx, [ebp + 12] ; edx <- (br->words-cwords)*FLAC__BYTES_PER_WORD + br->bytes - shl edx, 3 ; edx <- (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - sub edx, ecx ; edx <- (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits - mov [esp], edx ; ucbits <- edx - test eax, eax ; if(!bitreader_read_from_client_(br)) - jnz .read2 - jmp .end ; return false; /* eax (the return value) is already 0 */ - ; } -.c2_next1: - ;; ebx parameter - ;; ecx cbits - ;; esi cwords - ;; edi uval - ;; ebp br - ;; [esp] ucbits - cmp esi, [ebp + 8] ; if(cwords < br->words) { /* if we've not consumed up to a partial tail word... */ - jae near .c2_next2 - test ecx, ecx ; if(cbits) { - jz near .c2_next3 ; /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */ - mov eax, 32 - mov edx, [ebp] - sub eax, ecx ; const unsigned n = FLAC__BITS_PER_WORD - cbits; - mov edx, [edx + 4*esi] ; const brword word = br->buffer[cwords]; - cmp ebx, eax ; if(parameter < n) { - jae .c2_next4 - ; uval <<= parameter; - ; uval |= (word & (FLAC__WORD_ALL_ONES >> cbits)) >> (n-parameter); - shl edx, cl - xchg ebx, ecx - shld edi, edx, cl - add ebx, ecx ; cbits += parameter; - xchg ebx, ecx ; ebx <- parameter, ecx <- cbits - jmp .break2 ; goto break2; - ; } -.c2_next4: - ; uval <<= n; - ; uval |= word & (FLAC__WORD_ALL_ONES >> cbits); -%if 1 - rol edx, cl ; @@@@@@OPT: may be faster to use rol to save edx so we can restore it for CRC'ing - ; @@@@@@OPT: or put parameter in ch instead and free up ebx completely again -%else - shl edx, cl -%endif - xchg eax, ecx - shld edi, edx, cl - xchg eax, ecx -%if 1 - ror edx, cl ; restored. -%else - mov edx, [ebp] - mov edx, [edx + 4*esi] -%endif - ; crc16_update_word_(br, br->buffer[cwords]); - push edi ; [need more registers] - push ebx ; [need more registers] - push eax ; [need more registers] - bswap edx ; edx = br->buffer[cwords] swapped; now we can CRC the bytes from LSByte to MSByte which makes things much easier - mov ecx, [ebp + 28] ; ecx <- br->crc16_align - mov eax, [ebp + 24] ; ax <- br->read_crc (a.k.a. crc) -%ifdef FLAC__PUBLIC_NEEDS_UNDERSCORE - mov edi, _FLAC__crc16_table -%else - mov edi, FLAC__crc16_table -%endif - ;; eax (ax) crc a.k.a. br->read_crc - ;; ebx (bl) intermediate result index into FLAC__crc16_table[] - ;; ecx br->crc16_align - ;; edx byteswapped brword to CRC - ;; esi cwords - ;; edi unsigned FLAC__crc16_table[] - ;; ebp br - test ecx, ecx ; switch(br->crc16_align) ... - jnz .c2b4 ; [br->crc16_align is 0 the vast majority of the time so we optimize the common case] -.c2b0: xor dl, ah ; dl <- (crc>>8)^(word>>24) - movzx ebx, dl - mov ecx, [ebx*4 + edi] ; cx <- FLAC__crc16_table[(crc>>8)^(word>>24)] - shl eax, 8 ; ax <- (crc<<8) - xor eax, ecx ; crc <- ax <- (crc<<8) ^ FLAC__crc16_table[(crc>>8)^(word>>24)] -.c2b1: xor dh, ah ; dh <- (crc>>8)^((word>>16)&0xff)) - movzx ebx, dh - mov ecx, [ebx*4 + edi] ; cx <- FLAC__crc16_table[(crc>>8)^((word>>16)&0xff))] - shl eax, 8 ; ax <- (crc<<8) - xor eax, ecx ; crc <- ax <- (crc<<8) ^ FLAC__crc16_table[(crc>>8)^((word>>16)&0xff))] - shr edx, 16 -.c2b2: xor dl, ah ; dl <- (crc>>8)^((word>>8)&0xff)) - movzx ebx, dl - mov ecx, [ebx*4 + edi] ; cx <- FLAC__crc16_table[(crc>>8)^((word>>8)&0xff))] - shl eax, 8 ; ax <- (crc<<8) - xor eax, ecx ; crc <- ax <- (crc<<8) ^ FLAC__crc16_table[(crc>>8)^((word>>8)&0xff))] -.c2b3: xor dh, ah ; dh <- (crc>>8)^(word&0xff) - movzx ebx, dh - mov ecx, [ebx*4 + edi] ; cx <- FLAC__crc16_table[(crc>>8)^(word&0xff)] - shl eax, 8 ; ax <- (crc<<8) - xor eax, ecx ; crc <- ax <- (crc<<8) ^ FLAC__crc16_table[(crc>>8)^(word&0xff)] - movzx eax, ax - mov [ebp + 24], eax ; br->read_crc <- crc - pop eax - pop ebx - pop edi - add esi, byte 1 ; cwords++; - mov ecx, ebx - sub ecx, eax ; cbits = parameter - n; - jz .break2 ; if(cbits) { /* parameter > n, i.e. if there are still bits left to read, there have to be less than 32 so they will all be in the next word */ - ; uval <<= cbits; - ; uval |= (br->buffer[cwords] >> (FLAC__BITS_PER_WORD-cbits)); - mov eax, [ebp] - mov eax, [eax + 4*esi] - shld edi, eax, cl - ; } - jmp .break2 ; goto break2; - - ;; this section relocated out of the way for performance -.c2b4: - mov [ebp + 28], dword 0 ; br->crc16_align <- 0 - cmp ecx, 8 - je .c2b1 - shr edx, 16 - cmp ecx, 16 - je .c2b2 - jmp .c2b3 - -.c2_next3: ; } else { - mov ecx, ebx ; cbits = parameter; - ; uval <<= cbits; - ; uval |= (br->buffer[cwords] >> (FLAC__BITS_PER_WORD-cbits)); - mov eax, [ebp] - mov eax, [eax + 4*esi] - shld edi, eax, cl - jmp .break2 ; goto break2; - ; } -.c2_next2: ; } else { - ; in this case we're starting our read at a partial tail word; - ; the reader has guaranteed that we have at least 'parameter' - ; bits available to read, which makes this case simpler. - ; uval <<= parameter; - ; if(cbits) { - ; /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */ - ; uval |= (br->buffer[cwords] & (FLAC__WORD_ALL_ONES >> cbits)) >> (FLAC__BITS_PER_WORD-cbits-parameter); - ; cbits += parameter; - ; goto break2; - ; } else { - ; cbits = parameter; - ; uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-cbits); - ; goto break2; - ; } - ; the above is much shorter in assembly: - mov eax, [ebp] - mov eax, [eax + 4*esi] ; eax <- br->buffer[cwords] - shl eax, cl ; eax <- br->buffer[cwords] << cbits - add ecx, ebx ; cbits += parameter - xchg ebx, ecx ; ebx <- cbits, ecx <- parameter - shld edi, eax, cl ; uval <<= parameter <<< 'parameter' bits of tail word - xchg ebx, ecx ; ebx <- parameter, ecx <- cbits - ; } - ; } -.break2: - sub [esp], ebx ; ucbits -= parameter; - - ; - ; compose the value - ; - mov ebx, [esp + 28] ; ebx <- vals - mov edx, edi ; edx <- uval - and edi, 1 ; edi <- uval & 1 - shr edx, 1 ; edx <- uval >> 1 - neg edi ; edi <- -(int)(uval & 1) - xor edx, edi ; edx <- (uval >> 1 ^ -(int)(uval & 1)) - mov [ebx], edx ; *vals <- edx - sub dword [esp + 32], byte 1 ; --nvals; - jz .finished ; if(nvals == 0) /* jump to finish */ - xor edi, edi ; uval = 0; - add dword [esp + 28], 4 ; ++vals - jmp .val_loop ; } - -.finished: - mov [ebp + 16], esi ; br->consumed_words = cwords; - mov [ebp + 20], ecx ; br->consumed_bits = cbits; - mov eax, 1 -.end: - add esp, 4 - pop edi - pop esi - pop ebx - pop ebp - ret - -end: - -%ifdef OBJ_FORMAT_elf - section .note.GNU-stack noalloc -%endif diff --git a/FLAC/ia32/cpu_asm.nasm b/FLAC/ia32/cpu_asm.nasm deleted file mode 100644 index 38d1972a9..000000000 --- a/FLAC/ia32/cpu_asm.nasm +++ /dev/null @@ -1,121 +0,0 @@ -; vim:filetype=nasm ts=8 - -; libFLAC - Free Lossless Audio Codec library -; Copyright (C) 2001,2002,2003,2004,2005,2006,2007 Josh Coalson -; -; Redistribution and use in source and binary forms, with or without -; modification, are permitted provided that the following conditions -; are met: -; -; - Redistributions of source code must retain the above copyright -; notice, this list of conditions and the following disclaimer. -; -; - Redistributions in binary form must reproduce the above copyright -; notice, this list of conditions and the following disclaimer in the -; documentation and/or other materials provided with the distribution. -; -; - Neither the name of the Xiph.org Foundation nor the names of its -; contributors may be used to endorse or promote products derived from -; this software without specific prior written permission. -; -; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -; ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR -; CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, -; EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR -; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF -; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING -; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -%include "ia32/nasm.h" - - data_section - -cglobal FLAC__cpu_have_cpuid_asm_ia32 -cglobal FLAC__cpu_info_asm_ia32 -cglobal FLAC__cpu_info_extended_amd_asm_ia32 - - code_section - -; ********************************************************************** -; -; FLAC__uint32 FLAC__cpu_have_cpuid_asm_ia32() -; - -cident FLAC__cpu_have_cpuid_asm_ia32 - push ebx - pushfd - pop eax - mov edx, eax - xor eax, 0x00200000 - push eax - popfd - pushfd - pop eax - cmp eax, edx - jz .no_cpuid - mov eax, 1 - jmp .end -.no_cpuid: - xor eax, eax -.end: - pop ebx - ret - -; ********************************************************************** -; -; void FLAC__cpu_info_asm_ia32(FLAC__uint32 *flags_edx, FLAC__uint32 *flags_ecx) -; - -cident FLAC__cpu_info_asm_ia32 - ;[esp + 8] == flags_edx - ;[esp + 12] == flags_ecx - - push ebx - call FLAC__cpu_have_cpuid_asm_ia32 - test eax, eax - jz .no_cpuid - mov eax, 1 - cpuid - mov ebx, [esp + 8] - mov [ebx], edx - mov ebx, [esp + 12] - mov [ebx], ecx - jmp .end -.no_cpuid: - xor eax, eax - mov ebx, [esp + 8] - mov [ebx], eax - mov ebx, [esp + 12] - mov [ebx], eax -.end: - pop ebx - ret - -cident FLAC__cpu_info_extended_amd_asm_ia32 - push ebx - call FLAC__cpu_have_cpuid_asm_ia32 - test eax, eax - jz .no_cpuid - mov eax, 0x80000000 - cpuid - cmp eax, 0x80000001 - jb .no_cpuid - mov eax, 0x80000001 - cpuid - mov eax, edx - jmp .end -.no_cpuid: - xor eax, eax -.end: - pop ebx - ret - -end: - -%ifdef OBJ_FORMAT_elf - section .note.GNU-stack noalloc -%endif diff --git a/FLAC/ia32/fixed_asm.nasm b/FLAC/ia32/fixed_asm.nasm deleted file mode 100644 index 8851a5d17..000000000 --- a/FLAC/ia32/fixed_asm.nasm +++ /dev/null @@ -1,312 +0,0 @@ -; vim:filetype=nasm ts=8 - -; libFLAC - Free Lossless Audio Codec library -; Copyright (C) 2001,2002,2003,2004,2005,2006,2007 Josh Coalson -; -; Redistribution and use in source and binary forms, with or without -; modification, are permitted provided that the following conditions -; are met: -; -; - Redistributions of source code must retain the above copyright -; notice, this list of conditions and the following disclaimer. -; -; - Redistributions in binary form must reproduce the above copyright -; notice, this list of conditions and the following disclaimer in the -; documentation and/or other materials provided with the distribution. -; -; - Neither the name of the Xiph.org Foundation nor the names of its -; contributors may be used to endorse or promote products derived from -; this software without specific prior written permission. -; -; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -; ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR -; CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, -; EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR -; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF -; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING -; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -%include "ia32/nasm.h" - - data_section - -cglobal FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov - - code_section - -; ********************************************************************** -; -; unsigned FLAC__fixed_compute_best_predictor(const FLAC__int32 *data, unsigned data_len, FLAC__float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]) -; { -; FLAC__int32 last_error_0 = data[-1]; -; FLAC__int32 last_error_1 = data[-1] - data[-2]; -; FLAC__int32 last_error_2 = last_error_1 - (data[-2] - data[-3]); -; FLAC__int32 last_error_3 = last_error_2 - (data[-2] - 2*data[-3] + data[-4]); -; FLAC__int32 error, save; -; FLAC__uint32 total_error_0 = 0, total_error_1 = 0, total_error_2 = 0, total_error_3 = 0, total_error_4 = 0; -; unsigned i, order; -; -; for(i = 0; i < data_len; i++) { -; error = data[i] ; total_error_0 += local_abs(error); save = error; -; error -= last_error_0; total_error_1 += local_abs(error); last_error_0 = save; save = error; -; error -= last_error_1; total_error_2 += local_abs(error); last_error_1 = save; save = error; -; error -= last_error_2; total_error_3 += local_abs(error); last_error_2 = save; save = error; -; error -= last_error_3; total_error_4 += local_abs(error); last_error_3 = save; -; } -; -; if(total_error_0 < min(min(min(total_error_1, total_error_2), total_error_3), total_error_4)) -; order = 0; -; else if(total_error_1 < min(min(total_error_2, total_error_3), total_error_4)) -; order = 1; -; else if(total_error_2 < min(total_error_3, total_error_4)) -; order = 2; -; else if(total_error_3 < total_error_4) -; order = 3; -; else -; order = 4; -; -; residual_bits_per_sample[0] = (FLAC__float)((data_len > 0 && total_error_0 > 0) ? log(M_LN2 * (FLAC__double)total_error_0 / (FLAC__double)data_len) / M_LN2 : 0.0); -; residual_bits_per_sample[1] = (FLAC__float)((data_len > 0 && total_error_1 > 0) ? log(M_LN2 * (FLAC__double)total_error_1 / (FLAC__double)data_len) / M_LN2 : 0.0); -; residual_bits_per_sample[2] = (FLAC__float)((data_len > 0 && total_error_2 > 0) ? log(M_LN2 * (FLAC__double)total_error_2 / (FLAC__double)data_len) / M_LN2 : 0.0); -; residual_bits_per_sample[3] = (FLAC__float)((data_len > 0 && total_error_3 > 0) ? log(M_LN2 * (FLAC__double)total_error_3 / (FLAC__double)data_len) / M_LN2 : 0.0); -; residual_bits_per_sample[4] = (FLAC__float)((data_len > 0 && total_error_4 > 0) ? log(M_LN2 * (FLAC__double)total_error_4 / (FLAC__double)data_len) / M_LN2 : 0.0); -; -; return order; -; } - ALIGN 16 -cident FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov - - ; esp + 36 == data[] - ; esp + 40 == data_len - ; esp + 44 == residual_bits_per_sample[] - - push ebp - push ebx - push esi - push edi - sub esp, byte 16 - ; qword [esp] == temp space for loading FLAC__uint64s to FPU regs - - ; ebx == &data[i] - ; ecx == loop counter (i) - ; ebp == order - ; mm0 == total_error_1:total_error_0 - ; mm1 == total_error_2:total_error_3 - ; mm2 == :total_error_4 - ; mm3 == last_error_1:last_error_0 - ; mm4 == last_error_2:last_error_3 - - mov ecx, [esp + 40] ; ecx = data_len - test ecx, ecx - jz near .data_len_is_0 - - mov ebx, [esp + 36] ; ebx = data[] - movd mm3, [ebx - 4] ; mm3 = 0:last_error_0 - movd mm2, [ebx - 8] ; mm2 = 0:data[-2] - movd mm1, [ebx - 12] ; mm1 = 0:data[-3] - movd mm0, [ebx - 16] ; mm0 = 0:data[-4] - movq mm5, mm3 ; mm5 = 0:last_error_0 - psubd mm5, mm2 ; mm5 = 0:last_error_1 - punpckldq mm3, mm5 ; mm3 = last_error_1:last_error_0 - psubd mm2, mm1 ; mm2 = 0:data[-2] - data[-3] - psubd mm5, mm2 ; mm5 = 0:last_error_2 - movq mm4, mm5 ; mm4 = 0:last_error_2 - psubd mm4, mm2 ; mm4 = 0:last_error_2 - (data[-2] - data[-3]) - paddd mm4, mm1 ; mm4 = 0:last_error_2 - (data[-2] - 2 * data[-3]) - psubd mm4, mm0 ; mm4 = 0:last_error_3 - punpckldq mm4, mm5 ; mm4 = last_error_2:last_error_3 - pxor mm0, mm0 ; mm0 = total_error_1:total_error_0 - pxor mm1, mm1 ; mm1 = total_error_2:total_error_3 - pxor mm2, mm2 ; mm2 = 0:total_error_4 - - ALIGN 16 -.loop: - movd mm7, [ebx] ; mm7 = 0:error_0 - add ebx, byte 4 - movq mm6, mm7 ; mm6 = 0:error_0 - psubd mm7, mm3 ; mm7 = :error_1 - punpckldq mm6, mm7 ; mm6 = error_1:error_0 - movq mm5, mm6 ; mm5 = error_1:error_0 - movq mm7, mm6 ; mm7 = error_1:error_0 - psubd mm5, mm3 ; mm5 = error_2: - movq mm3, mm6 ; mm3 = error_1:error_0 - psrad mm6, 31 - pxor mm7, mm6 - psubd mm7, mm6 ; mm7 = abs(error_1):abs(error_0) - paddd mm0, mm7 ; mm0 = total_error_1:total_error_0 - movq mm6, mm5 ; mm6 = error_2: - psubd mm5, mm4 ; mm5 = error_3: - punpckhdq mm5, mm6 ; mm5 = error_2:error_3 - movq mm7, mm5 ; mm7 = error_2:error_3 - movq mm6, mm5 ; mm6 = error_2:error_3 - psubd mm5, mm4 ; mm5 = :error_4 - movq mm4, mm6 ; mm4 = error_2:error_3 - psrad mm6, 31 - pxor mm7, mm6 - psubd mm7, mm6 ; mm7 = abs(error_2):abs(error_3) - paddd mm1, mm7 ; mm1 = total_error_2:total_error_3 - movq mm6, mm5 ; mm6 = :error_4 - psrad mm5, 31 - pxor mm6, mm5 - psubd mm6, mm5 ; mm6 = :abs(error_4) - paddd mm2, mm6 ; mm2 = :total_error_4 - - dec ecx - jnz short .loop - -; if(total_error_0 < min(min(min(total_error_1, total_error_2), total_error_3), total_error_4)) -; order = 0; -; else if(total_error_1 < min(min(total_error_2, total_error_3), total_error_4)) -; order = 1; -; else if(total_error_2 < min(total_error_3, total_error_4)) -; order = 2; -; else if(total_error_3 < total_error_4) -; order = 3; -; else -; order = 4; - movq mm3, mm0 ; mm3 = total_error_1:total_error_0 - movd edi, mm2 ; edi = total_error_4 - movd esi, mm1 ; esi = total_error_3 - movd eax, mm0 ; eax = total_error_0 - punpckhdq mm1, mm1 ; mm1 = total_error_2:total_error_2 - punpckhdq mm3, mm3 ; mm3 = total_error_1:total_error_1 - movd edx, mm1 ; edx = total_error_2 - movd ecx, mm3 ; ecx = total_error_1 - - xor ebx, ebx - xor ebp, ebp - inc ebx - cmp ecx, eax - cmovb eax, ecx ; eax = min(total_error_0, total_error_1) - cmovbe ebp, ebx - inc ebx - cmp edx, eax - cmovb eax, edx ; eax = min(total_error_0, total_error_1, total_error_2) - cmovbe ebp, ebx - inc ebx - cmp esi, eax - cmovb eax, esi ; eax = min(total_error_0, total_error_1, total_error_2, total_error_3) - cmovbe ebp, ebx - inc ebx - cmp edi, eax - cmovb eax, edi ; eax = min(total_error_0, total_error_1, total_error_2, total_error_3, total_error_4) - cmovbe ebp, ebx - movd ebx, mm0 ; ebx = total_error_0 - emms - - ; residual_bits_per_sample[0] = (FLAC__float)((data_len > 0 && total_error_0 > 0) ? log(M_LN2 * (FLAC__double)total_error_0 / (FLAC__double)data_len) / M_LN2 : 0.0); - ; residual_bits_per_sample[1] = (FLAC__float)((data_len > 0 && total_error_1 > 0) ? log(M_LN2 * (FLAC__double)total_error_1 / (FLAC__double)data_len) / M_LN2 : 0.0); - ; residual_bits_per_sample[2] = (FLAC__float)((data_len > 0 && total_error_2 > 0) ? log(M_LN2 * (FLAC__double)total_error_2 / (FLAC__double)data_len) / M_LN2 : 0.0); - ; residual_bits_per_sample[3] = (FLAC__float)((data_len > 0 && total_error_3 > 0) ? log(M_LN2 * (FLAC__double)total_error_3 / (FLAC__double)data_len) / M_LN2 : 0.0); - ; residual_bits_per_sample[4] = (FLAC__float)((data_len > 0 && total_error_4 > 0) ? log(M_LN2 * (FLAC__double)total_error_4 / (FLAC__double)data_len) / M_LN2 : 0.0); - xor eax, eax - fild dword [esp + 40] ; ST = data_len (NOTE: assumes data_len is <2gigs) -.rbps_0: - test ebx, ebx - jz .total_error_0_is_0 - fld1 ; ST = 1.0 data_len - mov [esp], ebx - mov [esp + 4], eax ; [esp] = (FLAC__uint64)total_error_0 - mov ebx, [esp + 44] - fild qword [esp] ; ST = total_error_0 1.0 data_len - fdiv st2 ; ST = total_error_0/data_len 1.0 data_len - fldln2 ; ST = ln2 total_error_0/data_len 1.0 data_len - fmulp st1 ; ST = ln2*total_error_0/data_len 1.0 data_len - fyl2x ; ST = log2(ln2*total_error_0/data_len) data_len - fstp dword [ebx] ; residual_bits_per_sample[0] = log2(ln2*total_error_0/data_len) ST = data_len - jmp short .rbps_1 -.total_error_0_is_0: - mov ebx, [esp + 44] - mov [ebx], eax ; residual_bits_per_sample[0] = 0.0 -.rbps_1: - test ecx, ecx - jz .total_error_1_is_0 - fld1 ; ST = 1.0 data_len - mov [esp], ecx - mov [esp + 4], eax ; [esp] = (FLAC__uint64)total_error_1 - fild qword [esp] ; ST = total_error_1 1.0 data_len - fdiv st2 ; ST = total_error_1/data_len 1.0 data_len - fldln2 ; ST = ln2 total_error_1/data_len 1.0 data_len - fmulp st1 ; ST = ln2*total_error_1/data_len 1.0 data_len - fyl2x ; ST = log2(ln2*total_error_1/data_len) data_len - fstp dword [ebx + 4] ; residual_bits_per_sample[1] = log2(ln2*total_error_1/data_len) ST = data_len - jmp short .rbps_2 -.total_error_1_is_0: - mov [ebx + 4], eax ; residual_bits_per_sample[1] = 0.0 -.rbps_2: - test edx, edx - jz .total_error_2_is_0 - fld1 ; ST = 1.0 data_len - mov [esp], edx - mov [esp + 4], eax ; [esp] = (FLAC__uint64)total_error_2 - fild qword [esp] ; ST = total_error_2 1.0 data_len - fdiv st2 ; ST = total_error_2/data_len 1.0 data_len - fldln2 ; ST = ln2 total_error_2/data_len 1.0 data_len - fmulp st1 ; ST = ln2*total_error_2/data_len 1.0 data_len - fyl2x ; ST = log2(ln2*total_error_2/data_len) data_len - fstp dword [ebx + 8] ; residual_bits_per_sample[2] = log2(ln2*total_error_2/data_len) ST = data_len - jmp short .rbps_3 -.total_error_2_is_0: - mov [ebx + 8], eax ; residual_bits_per_sample[2] = 0.0 -.rbps_3: - test esi, esi - jz .total_error_3_is_0 - fld1 ; ST = 1.0 data_len - mov [esp], esi - mov [esp + 4], eax ; [esp] = (FLAC__uint64)total_error_3 - fild qword [esp] ; ST = total_error_3 1.0 data_len - fdiv st2 ; ST = total_error_3/data_len 1.0 data_len - fldln2 ; ST = ln2 total_error_3/data_len 1.0 data_len - fmulp st1 ; ST = ln2*total_error_3/data_len 1.0 data_len - fyl2x ; ST = log2(ln2*total_error_3/data_len) data_len - fstp dword [ebx + 12] ; residual_bits_per_sample[3] = log2(ln2*total_error_3/data_len) ST = data_len - jmp short .rbps_4 -.total_error_3_is_0: - mov [ebx + 12], eax ; residual_bits_per_sample[3] = 0.0 -.rbps_4: - test edi, edi - jz .total_error_4_is_0 - fld1 ; ST = 1.0 data_len - mov [esp], edi - mov [esp + 4], eax ; [esp] = (FLAC__uint64)total_error_4 - fild qword [esp] ; ST = total_error_4 1.0 data_len - fdiv st2 ; ST = total_error_4/data_len 1.0 data_len - fldln2 ; ST = ln2 total_error_4/data_len 1.0 data_len - fmulp st1 ; ST = ln2*total_error_4/data_len 1.0 data_len - fyl2x ; ST = log2(ln2*total_error_4/data_len) data_len - fstp dword [ebx + 16] ; residual_bits_per_sample[4] = log2(ln2*total_error_4/data_len) ST = data_len - jmp short .rbps_end -.total_error_4_is_0: - mov [ebx + 16], eax ; residual_bits_per_sample[4] = 0.0 -.rbps_end: - fstp st0 ; ST = [empty] - jmp short .end -.data_len_is_0: - ; data_len == 0, so residual_bits_per_sample[*] = 0.0 - xor ebp, ebp - mov edi, [esp + 44] - mov [edi], ebp - mov [edi + 4], ebp - mov [edi + 8], ebp - mov [edi + 12], ebp - mov [edi + 16], ebp - add ebp, byte 4 ; order = 4 - -.end: - mov eax, ebp ; return order - add esp, byte 16 - pop edi - pop esi - pop ebx - pop ebp - ret - -end: - -%ifdef OBJ_FORMAT_elf - section .note.GNU-stack noalloc -%endif diff --git a/FLAC/ia32/lpc_asm.nasm b/FLAC/ia32/lpc_asm.nasm deleted file mode 100644 index 2ebc923a7..000000000 --- a/FLAC/ia32/lpc_asm.nasm +++ /dev/null @@ -1,1511 +0,0 @@ -; vim:filetype=nasm ts=8 - -; libFLAC - Free Lossless Audio Codec library -; Copyright (C) 2001,2002,2003,2004,2005,2006,2007 Josh Coalson -; -; Redistribution and use in source and binary forms, with or without -; modification, are permitted provided that the following conditions -; are met: -; -; - Redistributions of source code must retain the above copyright -; notice, this list of conditions and the following disclaimer. -; -; - Redistributions in binary form must reproduce the above copyright -; notice, this list of conditions and the following disclaimer in the -; documentation and/or other materials provided with the distribution. -; -; - Neither the name of the Xiph.org Foundation nor the names of its -; contributors may be used to endorse or promote products derived from -; this software without specific prior written permission. -; -; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -; ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR -; CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, -; EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR -; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF -; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING -; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -%include "ia32/nasm.h" - - data_section - -cglobal FLAC__lpc_compute_autocorrelation_asm_ia32 -cglobal FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4 -cglobal FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8 -cglobal FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12 -cglobal FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow -cglobal FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32 -cglobal FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx -cglobal FLAC__lpc_restore_signal_asm_ia32 -cglobal FLAC__lpc_restore_signal_asm_ia32_mmx - - code_section - -; ********************************************************************** -; -; void FLAC__lpc_compute_autocorrelation_asm(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]) -; { -; FLAC__real d; -; unsigned sample, coeff; -; const unsigned limit = data_len - lag; -; -; FLAC__ASSERT(lag > 0); -; FLAC__ASSERT(lag <= data_len); -; -; for(coeff = 0; coeff < lag; coeff++) -; autoc[coeff] = 0.0; -; for(sample = 0; sample <= limit; sample++) { -; d = data[sample]; -; for(coeff = 0; coeff < lag; coeff++) -; autoc[coeff] += d * data[sample+coeff]; -; } -; for(; sample < data_len; sample++) { -; d = data[sample]; -; for(coeff = 0; coeff < data_len - sample; coeff++) -; autoc[coeff] += d * data[sample+coeff]; -; } -; } -; - ALIGN 16 -cident FLAC__lpc_compute_autocorrelation_asm_ia32 - ;[esp + 28] == autoc[] - ;[esp + 24] == lag - ;[esp + 20] == data_len - ;[esp + 16] == data[] - - ;ASSERT(lag > 0) - ;ASSERT(lag <= 33) - ;ASSERT(lag <= data_len) - -.begin: - push esi - push edi - push ebx - - ; for(coeff = 0; coeff < lag; coeff++) - ; autoc[coeff] = 0.0; - mov edi, [esp + 28] ; edi == autoc - mov ecx, [esp + 24] ; ecx = # of dwords (=lag) of 0 to write - xor eax, eax - rep stosd - - ; const unsigned limit = data_len - lag; - mov eax, [esp + 24] ; eax == lag - mov ecx, [esp + 20] - sub ecx, eax ; ecx == limit - - mov edi, [esp + 28] ; edi == autoc - mov esi, [esp + 16] ; esi == data - inc ecx ; we are looping <= limit so we add one to the counter - - ; for(sample = 0; sample <= limit; sample++) { - ; d = data[sample]; - ; for(coeff = 0; coeff < lag; coeff++) - ; autoc[coeff] += d * data[sample+coeff]; - ; } - fld dword [esi] ; ST = d <- data[sample] - ; each iteration is 11 bytes so we need (-eax)*11, so we do (-12*eax + eax) - lea edx, [eax + eax*2] - neg edx - lea edx, [eax + edx*4 + .jumper1_0 - .get_eip1] - call .get_eip1 -.get_eip1: - pop ebx - add edx, ebx - inc edx ; compensate for the shorter opcode on the last iteration - inc edx ; compensate for the shorter opcode on the last iteration - inc edx ; compensate for the shorter opcode on the last iteration - cmp eax, 33 - jne .loop1_start - sub edx, byte 9 ; compensate for the longer opcodes on the first iteration -.loop1_start: - jmp edx - - fld st0 ; ST = d d - fmul dword [esi + (32*4)] ; ST = d*data[sample+32] d WATCHOUT: not a byte displacement here! - fadd dword [edi + (32*4)] ; ST = autoc[32]+d*data[sample+32] d WATCHOUT: not a byte displacement here! - fstp dword [edi + (32*4)] ; autoc[32]+=d*data[sample+32] ST = d WATCHOUT: not a byte displacement here! - fld st0 ; ST = d d - fmul dword [esi + (31*4)] ; ST = d*data[sample+31] d - fadd dword [edi + (31*4)] ; ST = autoc[31]+d*data[sample+31] d - fstp dword [edi + (31*4)] ; autoc[31]+=d*data[sample+31] ST = d - fld st0 ; ST = d d - fmul dword [esi + (30*4)] ; ST = d*data[sample+30] d - fadd dword [edi + (30*4)] ; ST = autoc[30]+d*data[sample+30] d - fstp dword [edi + (30*4)] ; autoc[30]+=d*data[sample+30] ST = d - fld st0 ; ST = d d - fmul dword [esi + (29*4)] ; ST = d*data[sample+29] d - fadd dword [edi + (29*4)] ; ST = autoc[29]+d*data[sample+29] d - fstp dword [edi + (29*4)] ; autoc[29]+=d*data[sample+29] ST = d - fld st0 ; ST = d d - fmul dword [esi + (28*4)] ; ST = d*data[sample+28] d - fadd dword [edi + (28*4)] ; ST = autoc[28]+d*data[sample+28] d - fstp dword [edi + (28*4)] ; autoc[28]+=d*data[sample+28] ST = d - fld st0 ; ST = d d - fmul dword [esi + (27*4)] ; ST = d*data[sample+27] d - fadd dword [edi + (27*4)] ; ST = autoc[27]+d*data[sample+27] d - fstp dword [edi + (27*4)] ; autoc[27]+=d*data[sample+27] ST = d - fld st0 ; ST = d d - fmul dword [esi + (26*4)] ; ST = d*data[sample+26] d - fadd dword [edi + (26*4)] ; ST = autoc[26]+d*data[sample+26] d - fstp dword [edi + (26*4)] ; autoc[26]+=d*data[sample+26] ST = d - fld st0 ; ST = d d - fmul dword [esi + (25*4)] ; ST = d*data[sample+25] d - fadd dword [edi + (25*4)] ; ST = autoc[25]+d*data[sample+25] d - fstp dword [edi + (25*4)] ; autoc[25]+=d*data[sample+25] ST = d - fld st0 ; ST = d d - fmul dword [esi + (24*4)] ; ST = d*data[sample+24] d - fadd dword [edi + (24*4)] ; ST = autoc[24]+d*data[sample+24] d - fstp dword [edi + (24*4)] ; autoc[24]+=d*data[sample+24] ST = d - fld st0 ; ST = d d - fmul dword [esi + (23*4)] ; ST = d*data[sample+23] d - fadd dword [edi + (23*4)] ; ST = autoc[23]+d*data[sample+23] d - fstp dword [edi + (23*4)] ; autoc[23]+=d*data[sample+23] ST = d - fld st0 ; ST = d d - fmul dword [esi + (22*4)] ; ST = d*data[sample+22] d - fadd dword [edi + (22*4)] ; ST = autoc[22]+d*data[sample+22] d - fstp dword [edi + (22*4)] ; autoc[22]+=d*data[sample+22] ST = d - fld st0 ; ST = d d - fmul dword [esi + (21*4)] ; ST = d*data[sample+21] d - fadd dword [edi + (21*4)] ; ST = autoc[21]+d*data[sample+21] d - fstp dword [edi + (21*4)] ; autoc[21]+=d*data[sample+21] ST = d - fld st0 ; ST = d d - fmul dword [esi + (20*4)] ; ST = d*data[sample+20] d - fadd dword [edi + (20*4)] ; ST = autoc[20]+d*data[sample+20] d - fstp dword [edi + (20*4)] ; autoc[20]+=d*data[sample+20] ST = d - fld st0 ; ST = d d - fmul dword [esi + (19*4)] ; ST = d*data[sample+19] d - fadd dword [edi + (19*4)] ; ST = autoc[19]+d*data[sample+19] d - fstp dword [edi + (19*4)] ; autoc[19]+=d*data[sample+19] ST = d - fld st0 ; ST = d d - fmul dword [esi + (18*4)] ; ST = d*data[sample+18] d - fadd dword [edi + (18*4)] ; ST = autoc[18]+d*data[sample+18] d - fstp dword [edi + (18*4)] ; autoc[18]+=d*data[sample+18] ST = d - fld st0 ; ST = d d - fmul dword [esi + (17*4)] ; ST = d*data[sample+17] d - fadd dword [edi + (17*4)] ; ST = autoc[17]+d*data[sample+17] d - fstp dword [edi + (17*4)] ; autoc[17]+=d*data[sample+17] ST = d - fld st0 ; ST = d d - fmul dword [esi + (16*4)] ; ST = d*data[sample+16] d - fadd dword [edi + (16*4)] ; ST = autoc[16]+d*data[sample+16] d - fstp dword [edi + (16*4)] ; autoc[16]+=d*data[sample+16] ST = d - fld st0 ; ST = d d - fmul dword [esi + (15*4)] ; ST = d*data[sample+15] d - fadd dword [edi + (15*4)] ; ST = autoc[15]+d*data[sample+15] d - fstp dword [edi + (15*4)] ; autoc[15]+=d*data[sample+15] ST = d - fld st0 ; ST = d d - fmul dword [esi + (14*4)] ; ST = d*data[sample+14] d - fadd dword [edi + (14*4)] ; ST = autoc[14]+d*data[sample+14] d - fstp dword [edi + (14*4)] ; autoc[14]+=d*data[sample+14] ST = d - fld st0 ; ST = d d - fmul dword [esi + (13*4)] ; ST = d*data[sample+13] d - fadd dword [edi + (13*4)] ; ST = autoc[13]+d*data[sample+13] d - fstp dword [edi + (13*4)] ; autoc[13]+=d*data[sample+13] ST = d - fld st0 ; ST = d d - fmul dword [esi + (12*4)] ; ST = d*data[sample+12] d - fadd dword [edi + (12*4)] ; ST = autoc[12]+d*data[sample+12] d - fstp dword [edi + (12*4)] ; autoc[12]+=d*data[sample+12] ST = d - fld st0 ; ST = d d - fmul dword [esi + (11*4)] ; ST = d*data[sample+11] d - fadd dword [edi + (11*4)] ; ST = autoc[11]+d*data[sample+11] d - fstp dword [edi + (11*4)] ; autoc[11]+=d*data[sample+11] ST = d - fld st0 ; ST = d d - fmul dword [esi + (10*4)] ; ST = d*data[sample+10] d - fadd dword [edi + (10*4)] ; ST = autoc[10]+d*data[sample+10] d - fstp dword [edi + (10*4)] ; autoc[10]+=d*data[sample+10] ST = d - fld st0 ; ST = d d - fmul dword [esi + ( 9*4)] ; ST = d*data[sample+9] d - fadd dword [edi + ( 9*4)] ; ST = autoc[9]+d*data[sample+9] d - fstp dword [edi + ( 9*4)] ; autoc[9]+=d*data[sample+9] ST = d - fld st0 ; ST = d d - fmul dword [esi + ( 8*4)] ; ST = d*data[sample+8] d - fadd dword [edi + ( 8*4)] ; ST = autoc[8]+d*data[sample+8] d - fstp dword [edi + ( 8*4)] ; autoc[8]+=d*data[sample+8] ST = d - fld st0 ; ST = d d - fmul dword [esi + ( 7*4)] ; ST = d*data[sample+7] d - fadd dword [edi + ( 7*4)] ; ST = autoc[7]+d*data[sample+7] d - fstp dword [edi + ( 7*4)] ; autoc[7]+=d*data[sample+7] ST = d - fld st0 ; ST = d d - fmul dword [esi + ( 6*4)] ; ST = d*data[sample+6] d - fadd dword [edi + ( 6*4)] ; ST = autoc[6]+d*data[sample+6] d - fstp dword [edi + ( 6*4)] ; autoc[6]+=d*data[sample+6] ST = d - fld st0 ; ST = d d - fmul dword [esi + ( 5*4)] ; ST = d*data[sample+4] d - fadd dword [edi + ( 5*4)] ; ST = autoc[4]+d*data[sample+4] d - fstp dword [edi + ( 5*4)] ; autoc[4]+=d*data[sample+4] ST = d - fld st0 ; ST = d d - fmul dword [esi + ( 4*4)] ; ST = d*data[sample+4] d - fadd dword [edi + ( 4*4)] ; ST = autoc[4]+d*data[sample+4] d - fstp dword [edi + ( 4*4)] ; autoc[4]+=d*data[sample+4] ST = d - fld st0 ; ST = d d - fmul dword [esi + ( 3*4)] ; ST = d*data[sample+3] d - fadd dword [edi + ( 3*4)] ; ST = autoc[3]+d*data[sample+3] d - fstp dword [edi + ( 3*4)] ; autoc[3]+=d*data[sample+3] ST = d - fld st0 ; ST = d d - fmul dword [esi + ( 2*4)] ; ST = d*data[sample+2] d - fadd dword [edi + ( 2*4)] ; ST = autoc[2]+d*data[sample+2] d - fstp dword [edi + ( 2*4)] ; autoc[2]+=d*data[sample+2] ST = d - fld st0 ; ST = d d - fmul dword [esi + ( 1*4)] ; ST = d*data[sample+1] d - fadd dword [edi + ( 1*4)] ; ST = autoc[1]+d*data[sample+1] d - fstp dword [edi + ( 1*4)] ; autoc[1]+=d*data[sample+1] ST = d - fld st0 ; ST = d d - fmul dword [esi] ; ST = d*data[sample] d WATCHOUT: no displacement byte here! - fadd dword [edi] ; ST = autoc[0]+d*data[sample] d WATCHOUT: no displacement byte here! - fstp dword [edi] ; autoc[0]+=d*data[sample] ST = d WATCHOUT: no displacement byte here! -.jumper1_0: - - fstp st0 ; pop d, ST = empty - add esi, byte 4 ; sample++ - dec ecx - jz .loop1_end - fld dword [esi] ; ST = d <- data[sample] - jmp edx -.loop1_end: - - ; for(; sample < data_len; sample++) { - ; d = data[sample]; - ; for(coeff = 0; coeff < data_len - sample; coeff++) - ; autoc[coeff] += d * data[sample+coeff]; - ; } - mov ecx, [esp + 24] ; ecx <- lag - dec ecx ; ecx <- lag - 1 - jz near .end ; skip loop if 0 (i.e. lag == 1) - - fld dword [esi] ; ST = d <- data[sample] - mov eax, ecx ; eax <- lag - 1 == data_len - sample the first time through - ; each iteration is 11 bytes so we need (-eax)*11, so we do (-12*eax + eax) - lea edx, [eax + eax*2] - neg edx - lea edx, [eax + edx*4 + .jumper2_0 - .get_eip2] - call .get_eip2 -.get_eip2: - pop ebx - add edx, ebx - inc edx ; compensate for the shorter opcode on the last iteration - inc edx ; compensate for the shorter opcode on the last iteration - inc edx ; compensate for the shorter opcode on the last iteration - jmp edx - - fld st0 ; ST = d d - fmul dword [esi + (31*4)] ; ST = d*data[sample+31] d - fadd dword [edi + (31*4)] ; ST = autoc[31]+d*data[sample+31] d - fstp dword [edi + (31*4)] ; autoc[31]+=d*data[sample+31] ST = d - fld st0 ; ST = d d - fmul dword [esi + (30*4)] ; ST = d*data[sample+30] d - fadd dword [edi + (30*4)] ; ST = autoc[30]+d*data[sample+30] d - fstp dword [edi + (30*4)] ; autoc[30]+=d*data[sample+30] ST = d - fld st0 ; ST = d d - fmul dword [esi + (29*4)] ; ST = d*data[sample+29] d - fadd dword [edi + (29*4)] ; ST = autoc[29]+d*data[sample+29] d - fstp dword [edi + (29*4)] ; autoc[29]+=d*data[sample+29] ST = d - fld st0 ; ST = d d - fmul dword [esi + (28*4)] ; ST = d*data[sample+28] d - fadd dword [edi + (28*4)] ; ST = autoc[28]+d*data[sample+28] d - fstp dword [edi + (28*4)] ; autoc[28]+=d*data[sample+28] ST = d - fld st0 ; ST = d d - fmul dword [esi + (27*4)] ; ST = d*data[sample+27] d - fadd dword [edi + (27*4)] ; ST = autoc[27]+d*data[sample+27] d - fstp dword [edi + (27*4)] ; autoc[27]+=d*data[sample+27] ST = d - fld st0 ; ST = d d - fmul dword [esi + (26*4)] ; ST = d*data[sample+26] d - fadd dword [edi + (26*4)] ; ST = autoc[26]+d*data[sample+26] d - fstp dword [edi + (26*4)] ; autoc[26]+=d*data[sample+26] ST = d - fld st0 ; ST = d d - fmul dword [esi + (25*4)] ; ST = d*data[sample+25] d - fadd dword [edi + (25*4)] ; ST = autoc[25]+d*data[sample+25] d - fstp dword [edi + (25*4)] ; autoc[25]+=d*data[sample+25] ST = d - fld st0 ; ST = d d - fmul dword [esi + (24*4)] ; ST = d*data[sample+24] d - fadd dword [edi + (24*4)] ; ST = autoc[24]+d*data[sample+24] d - fstp dword [edi + (24*4)] ; autoc[24]+=d*data[sample+24] ST = d - fld st0 ; ST = d d - fmul dword [esi + (23*4)] ; ST = d*data[sample+23] d - fadd dword [edi + (23*4)] ; ST = autoc[23]+d*data[sample+23] d - fstp dword [edi + (23*4)] ; autoc[23]+=d*data[sample+23] ST = d - fld st0 ; ST = d d - fmul dword [esi + (22*4)] ; ST = d*data[sample+22] d - fadd dword [edi + (22*4)] ; ST = autoc[22]+d*data[sample+22] d - fstp dword [edi + (22*4)] ; autoc[22]+=d*data[sample+22] ST = d - fld st0 ; ST = d d - fmul dword [esi + (21*4)] ; ST = d*data[sample+21] d - fadd dword [edi + (21*4)] ; ST = autoc[21]+d*data[sample+21] d - fstp dword [edi + (21*4)] ; autoc[21]+=d*data[sample+21] ST = d - fld st0 ; ST = d d - fmul dword [esi + (20*4)] ; ST = d*data[sample+20] d - fadd dword [edi + (20*4)] ; ST = autoc[20]+d*data[sample+20] d - fstp dword [edi + (20*4)] ; autoc[20]+=d*data[sample+20] ST = d - fld st0 ; ST = d d - fmul dword [esi + (19*4)] ; ST = d*data[sample+19] d - fadd dword [edi + (19*4)] ; ST = autoc[19]+d*data[sample+19] d - fstp dword [edi + (19*4)] ; autoc[19]+=d*data[sample+19] ST = d - fld st0 ; ST = d d - fmul dword [esi + (18*4)] ; ST = d*data[sample+18] d - fadd dword [edi + (18*4)] ; ST = autoc[18]+d*data[sample+18] d - fstp dword [edi + (18*4)] ; autoc[18]+=d*data[sample+18] ST = d - fld st0 ; ST = d d - fmul dword [esi + (17*4)] ; ST = d*data[sample+17] d - fadd dword [edi + (17*4)] ; ST = autoc[17]+d*data[sample+17] d - fstp dword [edi + (17*4)] ; autoc[17]+=d*data[sample+17] ST = d - fld st0 ; ST = d d - fmul dword [esi + (16*4)] ; ST = d*data[sample+16] d - fadd dword [edi + (16*4)] ; ST = autoc[16]+d*data[sample+16] d - fstp dword [edi + (16*4)] ; autoc[16]+=d*data[sample+16] ST = d - fld st0 ; ST = d d - fmul dword [esi + (15*4)] ; ST = d*data[sample+15] d - fadd dword [edi + (15*4)] ; ST = autoc[15]+d*data[sample+15] d - fstp dword [edi + (15*4)] ; autoc[15]+=d*data[sample+15] ST = d - fld st0 ; ST = d d - fmul dword [esi + (14*4)] ; ST = d*data[sample+14] d - fadd dword [edi + (14*4)] ; ST = autoc[14]+d*data[sample+14] d - fstp dword [edi + (14*4)] ; autoc[14]+=d*data[sample+14] ST = d - fld st0 ; ST = d d - fmul dword [esi + (13*4)] ; ST = d*data[sample+13] d - fadd dword [edi + (13*4)] ; ST = autoc[13]+d*data[sample+13] d - fstp dword [edi + (13*4)] ; autoc[13]+=d*data[sample+13] ST = d - fld st0 ; ST = d d - fmul dword [esi + (12*4)] ; ST = d*data[sample+12] d - fadd dword [edi + (12*4)] ; ST = autoc[12]+d*data[sample+12] d - fstp dword [edi + (12*4)] ; autoc[12]+=d*data[sample+12] ST = d - fld st0 ; ST = d d - fmul dword [esi + (11*4)] ; ST = d*data[sample+11] d - fadd dword [edi + (11*4)] ; ST = autoc[11]+d*data[sample+11] d - fstp dword [edi + (11*4)] ; autoc[11]+=d*data[sample+11] ST = d - fld st0 ; ST = d d - fmul dword [esi + (10*4)] ; ST = d*data[sample+10] d - fadd dword [edi + (10*4)] ; ST = autoc[10]+d*data[sample+10] d - fstp dword [edi + (10*4)] ; autoc[10]+=d*data[sample+10] ST = d - fld st0 ; ST = d d - fmul dword [esi + ( 9*4)] ; ST = d*data[sample+9] d - fadd dword [edi + ( 9*4)] ; ST = autoc[9]+d*data[sample+9] d - fstp dword [edi + ( 9*4)] ; autoc[9]+=d*data[sample+9] ST = d - fld st0 ; ST = d d - fmul dword [esi + ( 8*4)] ; ST = d*data[sample+8] d - fadd dword [edi + ( 8*4)] ; ST = autoc[8]+d*data[sample+8] d - fstp dword [edi + ( 8*4)] ; autoc[8]+=d*data[sample+8] ST = d - fld st0 ; ST = d d - fmul dword [esi + ( 7*4)] ; ST = d*data[sample+7] d - fadd dword [edi + ( 7*4)] ; ST = autoc[7]+d*data[sample+7] d - fstp dword [edi + ( 7*4)] ; autoc[7]+=d*data[sample+7] ST = d - fld st0 ; ST = d d - fmul dword [esi + ( 6*4)] ; ST = d*data[sample+6] d - fadd dword [edi + ( 6*4)] ; ST = autoc[6]+d*data[sample+6] d - fstp dword [edi + ( 6*4)] ; autoc[6]+=d*data[sample+6] ST = d - fld st0 ; ST = d d - fmul dword [esi + ( 5*4)] ; ST = d*data[sample+4] d - fadd dword [edi + ( 5*4)] ; ST = autoc[4]+d*data[sample+4] d - fstp dword [edi + ( 5*4)] ; autoc[4]+=d*data[sample+4] ST = d - fld st0 ; ST = d d - fmul dword [esi + ( 4*4)] ; ST = d*data[sample+4] d - fadd dword [edi + ( 4*4)] ; ST = autoc[4]+d*data[sample+4] d - fstp dword [edi + ( 4*4)] ; autoc[4]+=d*data[sample+4] ST = d - fld st0 ; ST = d d - fmul dword [esi + ( 3*4)] ; ST = d*data[sample+3] d - fadd dword [edi + ( 3*4)] ; ST = autoc[3]+d*data[sample+3] d - fstp dword [edi + ( 3*4)] ; autoc[3]+=d*data[sample+3] ST = d - fld st0 ; ST = d d - fmul dword [esi + ( 2*4)] ; ST = d*data[sample+2] d - fadd dword [edi + ( 2*4)] ; ST = autoc[2]+d*data[sample+2] d - fstp dword [edi + ( 2*4)] ; autoc[2]+=d*data[sample+2] ST = d - fld st0 ; ST = d d - fmul dword [esi + ( 1*4)] ; ST = d*data[sample+1] d - fadd dword [edi + ( 1*4)] ; ST = autoc[1]+d*data[sample+1] d - fstp dword [edi + ( 1*4)] ; autoc[1]+=d*data[sample+1] ST = d - fld st0 ; ST = d d - fmul dword [esi] ; ST = d*data[sample] d WATCHOUT: no displacement byte here! - fadd dword [edi] ; ST = autoc[0]+d*data[sample] d WATCHOUT: no displacement byte here! - fstp dword [edi] ; autoc[0]+=d*data[sample] ST = d WATCHOUT: no displacement byte here! -.jumper2_0: - - fstp st0 ; pop d, ST = empty - add esi, byte 4 ; sample++ - dec ecx - jz .loop2_end - add edx, byte 11 ; adjust our inner loop counter by adjusting the jump target - fld dword [esi] ; ST = d <- data[sample] - jmp edx -.loop2_end: - -.end: - pop ebx - pop edi - pop esi - ret - - ALIGN 16 -cident FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4 - ;[esp + 16] == autoc[] - ;[esp + 12] == lag - ;[esp + 8] == data_len - ;[esp + 4] == data[] - - ;ASSERT(lag > 0) - ;ASSERT(lag <= 4) - ;ASSERT(lag <= data_len) - - ; for(coeff = 0; coeff < lag; coeff++) - ; autoc[coeff] = 0.0; - xorps xmm5, xmm5 - - mov edx, [esp + 8] ; edx == data_len - mov eax, [esp + 4] ; eax == &data[sample] <- &data[0] - - movss xmm0, [eax] ; xmm0 = 0,0,0,data[0] - add eax, 4 - movaps xmm2, xmm0 ; xmm2 = 0,0,0,data[0] - shufps xmm0, xmm0, 0 ; xmm0 == data[sample],data[sample],data[sample],data[sample] = data[0],data[0],data[0],data[0] -.warmup: ; xmm2 == data[sample-3],data[sample-2],data[sample-1],data[sample] - mulps xmm0, xmm2 ; xmm0 = xmm0 * xmm2 - addps xmm5, xmm0 ; xmm5 += xmm0 * xmm2 - dec edx - jz .loop_end - ALIGN 16 -.loop_start: - ; start by reading the next sample - movss xmm0, [eax] ; xmm0 = 0,0,0,data[sample] - add eax, 4 - shufps xmm0, xmm0, 0 ; xmm0 = data[sample],data[sample],data[sample],data[sample] - shufps xmm2, xmm2, 93h ; 93h=2-1-0-3 => xmm2 gets rotated left by one float - movss xmm2, xmm0 - mulps xmm0, xmm2 ; xmm0 = xmm0 * xmm2 - addps xmm5, xmm0 ; xmm5 += xmm0 * xmm2 - dec edx - jnz .loop_start -.loop_end: - ; store autoc - mov edx, [esp + 16] ; edx == autoc - movups [edx], xmm5 - -.end: - ret - - ALIGN 16 -cident FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8 - ;[esp + 16] == autoc[] - ;[esp + 12] == lag - ;[esp + 8] == data_len - ;[esp + 4] == data[] - - ;ASSERT(lag > 0) - ;ASSERT(lag <= 8) - ;ASSERT(lag <= data_len) - - ; for(coeff = 0; coeff < lag; coeff++) - ; autoc[coeff] = 0.0; - xorps xmm5, xmm5 - xorps xmm6, xmm6 - - mov edx, [esp + 8] ; edx == data_len - mov eax, [esp + 4] ; eax == &data[sample] <- &data[0] - - movss xmm0, [eax] ; xmm0 = 0,0,0,data[0] - add eax, 4 - movaps xmm2, xmm0 ; xmm2 = 0,0,0,data[0] - shufps xmm0, xmm0, 0 ; xmm0 == data[sample],data[sample],data[sample],data[sample] = data[0],data[0],data[0],data[0] - movaps xmm1, xmm0 ; xmm1 == data[sample],data[sample],data[sample],data[sample] = data[0],data[0],data[0],data[0] - xorps xmm3, xmm3 ; xmm3 = 0,0,0,0 -.warmup: ; xmm3:xmm2 == data[sample-7],data[sample-6],...,data[sample] - mulps xmm0, xmm2 - mulps xmm1, xmm3 ; xmm1:xmm0 = xmm1:xmm0 * xmm3:xmm2 - addps xmm5, xmm0 - addps xmm6, xmm1 ; xmm6:xmm5 += xmm1:xmm0 * xmm3:xmm2 - dec edx - jz .loop_end - ALIGN 16 -.loop_start: - ; start by reading the next sample - movss xmm0, [eax] ; xmm0 = 0,0,0,data[sample] - ; here we reorder the instructions; see the (#) indexes for a logical order - shufps xmm2, xmm2, 93h ; (3) 93h=2-1-0-3 => xmm2 gets rotated left by one float - add eax, 4 ; (0) - shufps xmm3, xmm3, 93h ; (4) 93h=2-1-0-3 => xmm3 gets rotated left by one float - shufps xmm0, xmm0, 0 ; (1) xmm0 = data[sample],data[sample],data[sample],data[sample] - movss xmm3, xmm2 ; (5) - movaps xmm1, xmm0 ; (2) xmm1 = data[sample],data[sample],data[sample],data[sample] - movss xmm2, xmm0 ; (6) - mulps xmm1, xmm3 ; (8) - mulps xmm0, xmm2 ; (7) xmm1:xmm0 = xmm1:xmm0 * xmm3:xmm2 - addps xmm6, xmm1 ; (10) - addps xmm5, xmm0 ; (9) xmm6:xmm5 += xmm1:xmm0 * xmm3:xmm2 - dec edx - jnz .loop_start -.loop_end: - ; store autoc - mov edx, [esp + 16] ; edx == autoc - movups [edx], xmm5 - movups [edx + 16], xmm6 - -.end: - ret - - ALIGN 16 -cident FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12 - ;[esp + 16] == autoc[] - ;[esp + 12] == lag - ;[esp + 8] == data_len - ;[esp + 4] == data[] - - ;ASSERT(lag > 0) - ;ASSERT(lag <= 12) - ;ASSERT(lag <= data_len) - - ; for(coeff = 0; coeff < lag; coeff++) - ; autoc[coeff] = 0.0; - xorps xmm5, xmm5 - xorps xmm6, xmm6 - xorps xmm7, xmm7 - - mov edx, [esp + 8] ; edx == data_len - mov eax, [esp + 4] ; eax == &data[sample] <- &data[0] - - movss xmm0, [eax] ; xmm0 = 0,0,0,data[0] - add eax, 4 - movaps xmm2, xmm0 ; xmm2 = 0,0,0,data[0] - shufps xmm0, xmm0, 0 ; xmm0 == data[sample],data[sample],data[sample],data[sample] = data[0],data[0],data[0],data[0] - xorps xmm3, xmm3 ; xmm3 = 0,0,0,0 - xorps xmm4, xmm4 ; xmm4 = 0,0,0,0 -.warmup: ; xmm3:xmm2 == data[sample-7],data[sample-6],...,data[sample] - movaps xmm1, xmm0 - mulps xmm1, xmm2 - addps xmm5, xmm1 - movaps xmm1, xmm0 - mulps xmm1, xmm3 - addps xmm6, xmm1 - mulps xmm0, xmm4 - addps xmm7, xmm0 ; xmm7:xmm6:xmm5 += xmm0:xmm0:xmm0 * xmm4:xmm3:xmm2 - dec edx - jz .loop_end - ALIGN 16 -.loop_start: - ; start by reading the next sample - movss xmm0, [eax] ; xmm0 = 0,0,0,data[sample] - add eax, 4 - shufps xmm0, xmm0, 0 ; xmm0 = data[sample],data[sample],data[sample],data[sample] - - ; shift xmm4:xmm3:xmm2 left by one float - shufps xmm2, xmm2, 93h ; 93h=2-1-0-3 => xmm2 gets rotated left by one float - shufps xmm3, xmm3, 93h ; 93h=2-1-0-3 => xmm3 gets rotated left by one float - shufps xmm4, xmm4, 93h ; 93h=2-1-0-3 => xmm4 gets rotated left by one float - movss xmm4, xmm3 - movss xmm3, xmm2 - movss xmm2, xmm0 - - ; xmm7:xmm6:xmm5 += xmm0:xmm0:xmm0 * xmm3:xmm3:xmm2 - movaps xmm1, xmm0 - mulps xmm1, xmm2 - addps xmm5, xmm1 - movaps xmm1, xmm0 - mulps xmm1, xmm3 - addps xmm6, xmm1 - mulps xmm0, xmm4 - addps xmm7, xmm0 - - dec edx - jnz .loop_start -.loop_end: - ; store autoc - mov edx, [esp + 16] ; edx == autoc - movups [edx], xmm5 - movups [edx + 16], xmm6 - movups [edx + 32], xmm7 - -.end: - ret - - ALIGN 16 -cident FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow - ;[ebp + 32] autoc - ;[ebp + 28] lag - ;[ebp + 24] data_len - ;[ebp + 20] data - - push ebp - push ebx - push esi - push edi - mov ebp, esp - - mov esi, [ebp + 20] - mov edi, [ebp + 24] - mov edx, [ebp + 28] - inc edx - and edx, byte -2 - mov eax, edx - neg eax - and esp, byte -8 - lea esp, [esp + 4 * eax] - mov ecx, edx - xor eax, eax -.loop0: - dec ecx - mov [esp + 4 * ecx], eax - jnz short .loop0 - - mov eax, edi - sub eax, edx - mov ebx, edx - and ebx, byte 1 - sub eax, ebx - lea ecx, [esi + 4 * eax - 12] - cmp esi, ecx - mov eax, esi - ja short .loop2_pre - ALIGN 16 ;4 nops -.loop1_i: - movd mm0, [eax] - movd mm2, [eax + 4] - movd mm4, [eax + 8] - movd mm6, [eax + 12] - mov ebx, edx - punpckldq mm0, mm0 - punpckldq mm2, mm2 - punpckldq mm4, mm4 - punpckldq mm6, mm6 - ALIGN 16 ;3 nops -.loop1_j: - sub ebx, byte 2 - movd mm1, [eax + 4 * ebx] - movd mm3, [eax + 4 * ebx + 4] - movd mm5, [eax + 4 * ebx + 8] - movd mm7, [eax + 4 * ebx + 12] - punpckldq mm1, mm3 - punpckldq mm3, mm5 - pfmul mm1, mm0 - punpckldq mm5, mm7 - pfmul mm3, mm2 - punpckldq mm7, [eax + 4 * ebx + 16] - pfmul mm5, mm4 - pfmul mm7, mm6 - pfadd mm1, mm3 - movq mm3, [esp + 4 * ebx] - pfadd mm5, mm7 - pfadd mm1, mm5 - pfadd mm3, mm1 - movq [esp + 4 * ebx], mm3 - jg short .loop1_j - - add eax, byte 16 - cmp eax, ecx - jb short .loop1_i - -.loop2_pre: - mov ebx, eax - sub eax, esi - shr eax, 2 - lea ecx, [esi + 4 * edi] - mov esi, ebx -.loop2_i: - movd mm0, [esi] - mov ebx, edi - sub ebx, eax - cmp ebx, edx - jbe short .loop2_j - mov ebx, edx -.loop2_j: - dec ebx - movd mm1, [esi + 4 * ebx] - pfmul mm1, mm0 - movd mm2, [esp + 4 * ebx] - pfadd mm1, mm2 - movd [esp + 4 * ebx], mm1 - - jnz short .loop2_j - - add esi, byte 4 - inc eax - cmp esi, ecx - jnz short .loop2_i - - mov edi, [ebp + 32] - mov edx, [ebp + 28] -.loop3: - dec edx - mov eax, [esp + 4 * edx] - mov [edi + 4 * edx], eax - jnz short .loop3 - - femms - - mov esp, ebp - pop edi - pop esi - pop ebx - pop ebp - ret - -;void FLAC__lpc_compute_residual_from_qlp_coefficients(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]) -; -; for(i = 0; i < data_len; i++) { -; sum = 0; -; for(j = 0; j < order; j++) -; sum += qlp_coeff[j] * data[i-j-1]; -; residual[i] = data[i] - (sum >> lp_quantization); -; } -; - ALIGN 16 -cident FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32 - ;[esp + 40] residual[] - ;[esp + 36] lp_quantization - ;[esp + 32] order - ;[esp + 28] qlp_coeff[] - ;[esp + 24] data_len - ;[esp + 20] data[] - - ;ASSERT(order > 0) - - push ebp - push ebx - push esi - push edi - - mov esi, [esp + 20] ; esi = data[] - mov edi, [esp + 40] ; edi = residual[] - mov eax, [esp + 32] ; eax = order - mov ebx, [esp + 24] ; ebx = data_len - - test ebx, ebx - jz near .end ; do nothing if data_len == 0 -.begin: - cmp eax, byte 1 - jg short .i_1more - - mov ecx, [esp + 28] - mov edx, [ecx] ; edx = qlp_coeff[0] - mov eax, [esi - 4] ; eax = data[-1] - mov cl, [esp + 36] ; cl = lp_quantization - ALIGN 16 -.i_1_loop_i: - imul eax, edx - sar eax, cl - neg eax - add eax, [esi] - mov [edi], eax - mov eax, [esi] - add edi, byte 4 - add esi, byte 4 - dec ebx - jnz .i_1_loop_i - - jmp .end - -.i_1more: - cmp eax, byte 32 ; for order <= 32 there is a faster routine - jbe short .i_32 - - ; This version is here just for completeness, since FLAC__MAX_LPC_ORDER == 32 - ALIGN 16 -.i_32more_loop_i: - xor ebp, ebp - mov ecx, [esp + 32] - mov edx, ecx - shl edx, 2 - add edx, [esp + 28] - neg ecx - ALIGN 16 -.i_32more_loop_j: - sub edx, byte 4 - mov eax, [edx] - imul eax, [esi + 4 * ecx] - add ebp, eax - inc ecx - jnz short .i_32more_loop_j - - mov cl, [esp + 36] - sar ebp, cl - neg ebp - add ebp, [esi] - mov [edi], ebp - add esi, byte 4 - add edi, byte 4 - - dec ebx - jnz .i_32more_loop_i - - jmp .end - -.i_32: - sub edi, esi - neg eax - lea edx, [eax + eax * 8 + .jumper_0 - .get_eip0] - call .get_eip0 -.get_eip0: - pop eax - add edx, eax - inc edx - mov eax, [esp + 28] ; eax = qlp_coeff[] - xor ebp, ebp - jmp edx - - mov ecx, [eax + 124] - imul ecx, [esi - 128] - add ebp, ecx - mov ecx, [eax + 120] - imul ecx, [esi - 124] - add ebp, ecx - mov ecx, [eax + 116] - imul ecx, [esi - 120] - add ebp, ecx - mov ecx, [eax + 112] - imul ecx, [esi - 116] - add ebp, ecx - mov ecx, [eax + 108] - imul ecx, [esi - 112] - add ebp, ecx - mov ecx, [eax + 104] - imul ecx, [esi - 108] - add ebp, ecx - mov ecx, [eax + 100] - imul ecx, [esi - 104] - add ebp, ecx - mov ecx, [eax + 96] - imul ecx, [esi - 100] - add ebp, ecx - mov ecx, [eax + 92] - imul ecx, [esi - 96] - add ebp, ecx - mov ecx, [eax + 88] - imul ecx, [esi - 92] - add ebp, ecx - mov ecx, [eax + 84] - imul ecx, [esi - 88] - add ebp, ecx - mov ecx, [eax + 80] - imul ecx, [esi - 84] - add ebp, ecx - mov ecx, [eax + 76] - imul ecx, [esi - 80] - add ebp, ecx - mov ecx, [eax + 72] - imul ecx, [esi - 76] - add ebp, ecx - mov ecx, [eax + 68] - imul ecx, [esi - 72] - add ebp, ecx - mov ecx, [eax + 64] - imul ecx, [esi - 68] - add ebp, ecx - mov ecx, [eax + 60] - imul ecx, [esi - 64] - add ebp, ecx - mov ecx, [eax + 56] - imul ecx, [esi - 60] - add ebp, ecx - mov ecx, [eax + 52] - imul ecx, [esi - 56] - add ebp, ecx - mov ecx, [eax + 48] - imul ecx, [esi - 52] - add ebp, ecx - mov ecx, [eax + 44] - imul ecx, [esi - 48] - add ebp, ecx - mov ecx, [eax + 40] - imul ecx, [esi - 44] - add ebp, ecx - mov ecx, [eax + 36] - imul ecx, [esi - 40] - add ebp, ecx - mov ecx, [eax + 32] - imul ecx, [esi - 36] - add ebp, ecx - mov ecx, [eax + 28] - imul ecx, [esi - 32] - add ebp, ecx - mov ecx, [eax + 24] - imul ecx, [esi - 28] - add ebp, ecx - mov ecx, [eax + 20] - imul ecx, [esi - 24] - add ebp, ecx - mov ecx, [eax + 16] - imul ecx, [esi - 20] - add ebp, ecx - mov ecx, [eax + 12] - imul ecx, [esi - 16] - add ebp, ecx - mov ecx, [eax + 8] - imul ecx, [esi - 12] - add ebp, ecx - mov ecx, [eax + 4] - imul ecx, [esi - 8] - add ebp, ecx - mov ecx, [eax] ; there is one byte missing - imul ecx, [esi - 4] - add ebp, ecx -.jumper_0: - - mov cl, [esp + 36] - sar ebp, cl - neg ebp - add ebp, [esi] - mov [edi + esi], ebp - add esi, byte 4 - - dec ebx - jz short .end - xor ebp, ebp - jmp edx - -.end: - pop edi - pop esi - pop ebx - pop ebp - ret - -; WATCHOUT: this routine works on 16 bit data which means bits-per-sample for -; the channel and qlp_coeffs must be <= 16. Especially note that this routine -; cannot be used for side-channel coded 16bps channels since the effective bps -; is 17. - ALIGN 16 -cident FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx - ;[esp + 40] residual[] - ;[esp + 36] lp_quantization - ;[esp + 32] order - ;[esp + 28] qlp_coeff[] - ;[esp + 24] data_len - ;[esp + 20] data[] - - ;ASSERT(order > 0) - - push ebp - push ebx - push esi - push edi - - mov esi, [esp + 20] ; esi = data[] - mov edi, [esp + 40] ; edi = residual[] - mov eax, [esp + 32] ; eax = order - mov ebx, [esp + 24] ; ebx = data_len - - test ebx, ebx - jz near .end ; do nothing if data_len == 0 - dec ebx - test ebx, ebx - jz near .last_one - - mov edx, [esp + 28] ; edx = qlp_coeff[] - movd mm6, [esp + 36] ; mm6 = 0:lp_quantization - mov ebp, esp - - and esp, 0xfffffff8 - - xor ecx, ecx -.copy_qlp_loop: - push word [edx + 4 * ecx] - inc ecx - cmp ecx, eax - jnz short .copy_qlp_loop - - and ecx, 0x3 - test ecx, ecx - je short .za_end - sub ecx, byte 4 -.za_loop: - push word 0 - inc eax - inc ecx - jnz short .za_loop -.za_end: - - movq mm5, [esp + 2 * eax - 8] - movd mm4, [esi - 16] - punpckldq mm4, [esi - 12] - movd mm0, [esi - 8] - punpckldq mm0, [esi - 4] - packssdw mm4, mm0 - - cmp eax, byte 4 - jnbe short .mmx_4more - - ALIGN 16 -.mmx_4_loop_i: - movd mm1, [esi] - movq mm3, mm4 - punpckldq mm1, [esi + 4] - psrlq mm4, 16 - movq mm0, mm1 - psllq mm0, 48 - por mm4, mm0 - movq mm2, mm4 - psrlq mm4, 16 - pxor mm0, mm0 - punpckhdq mm0, mm1 - pmaddwd mm3, mm5 - pmaddwd mm2, mm5 - psllq mm0, 16 - por mm4, mm0 - movq mm0, mm3 - punpckldq mm3, mm2 - punpckhdq mm0, mm2 - paddd mm3, mm0 - psrad mm3, mm6 - psubd mm1, mm3 - movd [edi], mm1 - punpckhdq mm1, mm1 - movd [edi + 4], mm1 - - add edi, byte 8 - add esi, byte 8 - - sub ebx, 2 - jg .mmx_4_loop_i - jmp .mmx_end - -.mmx_4more: - shl eax, 2 - neg eax - add eax, byte 16 - - ALIGN 16 -.mmx_4more_loop_i: - movd mm1, [esi] - punpckldq mm1, [esi + 4] - movq mm3, mm4 - psrlq mm4, 16 - movq mm0, mm1 - psllq mm0, 48 - por mm4, mm0 - movq mm2, mm4 - psrlq mm4, 16 - pxor mm0, mm0 - punpckhdq mm0, mm1 - pmaddwd mm3, mm5 - pmaddwd mm2, mm5 - psllq mm0, 16 - por mm4, mm0 - - mov ecx, esi - add ecx, eax - mov edx, esp - - ALIGN 16 -.mmx_4more_loop_j: - movd mm0, [ecx - 16] - movd mm7, [ecx - 8] - punpckldq mm0, [ecx - 12] - punpckldq mm7, [ecx - 4] - packssdw mm0, mm7 - pmaddwd mm0, [edx] - punpckhdq mm7, mm7 - paddd mm3, mm0 - movd mm0, [ecx - 12] - punpckldq mm0, [ecx - 8] - punpckldq mm7, [ecx] - packssdw mm0, mm7 - pmaddwd mm0, [edx] - paddd mm2, mm0 - - add edx, byte 8 - add ecx, byte 16 - cmp ecx, esi - jnz .mmx_4more_loop_j - - movq mm0, mm3 - punpckldq mm3, mm2 - punpckhdq mm0, mm2 - paddd mm3, mm0 - psrad mm3, mm6 - psubd mm1, mm3 - movd [edi], mm1 - punpckhdq mm1, mm1 - movd [edi + 4], mm1 - - add edi, byte 8 - add esi, byte 8 - - sub ebx, 2 - jg near .mmx_4more_loop_i - -.mmx_end: - emms - mov esp, ebp -.last_one: - mov eax, [esp + 32] - inc ebx - jnz near FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32.begin - -.end: - pop edi - pop esi - pop ebx - pop ebp - ret - -; ********************************************************************** -; -; void FLAC__lpc_restore_signal(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]) -; { -; unsigned i, j; -; FLAC__int32 sum; -; -; FLAC__ASSERT(order > 0); -; -; for(i = 0; i < data_len; i++) { -; sum = 0; -; for(j = 0; j < order; j++) -; sum += qlp_coeff[j] * data[i-j-1]; -; data[i] = residual[i] + (sum >> lp_quantization); -; } -; } - ALIGN 16 -cident FLAC__lpc_restore_signal_asm_ia32 - ;[esp + 40] data[] - ;[esp + 36] lp_quantization - ;[esp + 32] order - ;[esp + 28] qlp_coeff[] - ;[esp + 24] data_len - ;[esp + 20] residual[] - - ;ASSERT(order > 0) - - push ebp - push ebx - push esi - push edi - - mov esi, [esp + 20] ; esi = residual[] - mov edi, [esp + 40] ; edi = data[] - mov eax, [esp + 32] ; eax = order - mov ebx, [esp + 24] ; ebx = data_len - - test ebx, ebx - jz near .end ; do nothing if data_len == 0 - -.begin: - cmp eax, byte 1 - jg short .x87_1more - - mov ecx, [esp + 28] - mov edx, [ecx] - mov eax, [edi - 4] - mov cl, [esp + 36] - ALIGN 16 -.x87_1_loop_i: - imul eax, edx - sar eax, cl - add eax, [esi] - mov [edi], eax - add esi, byte 4 - add edi, byte 4 - dec ebx - jnz .x87_1_loop_i - - jmp .end - -.x87_1more: - cmp eax, byte 32 ; for order <= 32 there is a faster routine - jbe short .x87_32 - - ; This version is here just for completeness, since FLAC__MAX_LPC_ORDER == 32 - ALIGN 16 -.x87_32more_loop_i: - xor ebp, ebp - mov ecx, [esp + 32] - mov edx, ecx - shl edx, 2 - add edx, [esp + 28] - neg ecx - ALIGN 16 -.x87_32more_loop_j: - sub edx, byte 4 - mov eax, [edx] - imul eax, [edi + 4 * ecx] - add ebp, eax - inc ecx - jnz short .x87_32more_loop_j - - mov cl, [esp + 36] - sar ebp, cl - add ebp, [esi] - mov [edi], ebp - add edi, byte 4 - add esi, byte 4 - - dec ebx - jnz .x87_32more_loop_i - - jmp .end - -.x87_32: - sub esi, edi - neg eax - lea edx, [eax + eax * 8 + .jumper_0 - .get_eip0] - call .get_eip0 -.get_eip0: - pop eax - add edx, eax - inc edx ; compensate for the shorter opcode on the last iteration - mov eax, [esp + 28] ; eax = qlp_coeff[] - xor ebp, ebp - jmp edx - - mov ecx, [eax + 124] ; ecx = qlp_coeff[31] - imul ecx, [edi - 128] ; ecx = qlp_coeff[31] * data[i-32] - add ebp, ecx ; sum += qlp_coeff[31] * data[i-32] - mov ecx, [eax + 120] ; ecx = qlp_coeff[30] - imul ecx, [edi - 124] ; ecx = qlp_coeff[30] * data[i-31] - add ebp, ecx ; sum += qlp_coeff[30] * data[i-31] - mov ecx, [eax + 116] ; ecx = qlp_coeff[29] - imul ecx, [edi - 120] ; ecx = qlp_coeff[29] * data[i-30] - add ebp, ecx ; sum += qlp_coeff[29] * data[i-30] - mov ecx, [eax + 112] ; ecx = qlp_coeff[28] - imul ecx, [edi - 116] ; ecx = qlp_coeff[28] * data[i-29] - add ebp, ecx ; sum += qlp_coeff[28] * data[i-29] - mov ecx, [eax + 108] ; ecx = qlp_coeff[27] - imul ecx, [edi - 112] ; ecx = qlp_coeff[27] * data[i-28] - add ebp, ecx ; sum += qlp_coeff[27] * data[i-28] - mov ecx, [eax + 104] ; ecx = qlp_coeff[26] - imul ecx, [edi - 108] ; ecx = qlp_coeff[26] * data[i-27] - add ebp, ecx ; sum += qlp_coeff[26] * data[i-27] - mov ecx, [eax + 100] ; ecx = qlp_coeff[25] - imul ecx, [edi - 104] ; ecx = qlp_coeff[25] * data[i-26] - add ebp, ecx ; sum += qlp_coeff[25] * data[i-26] - mov ecx, [eax + 96] ; ecx = qlp_coeff[24] - imul ecx, [edi - 100] ; ecx = qlp_coeff[24] * data[i-25] - add ebp, ecx ; sum += qlp_coeff[24] * data[i-25] - mov ecx, [eax + 92] ; ecx = qlp_coeff[23] - imul ecx, [edi - 96] ; ecx = qlp_coeff[23] * data[i-24] - add ebp, ecx ; sum += qlp_coeff[23] * data[i-24] - mov ecx, [eax + 88] ; ecx = qlp_coeff[22] - imul ecx, [edi - 92] ; ecx = qlp_coeff[22] * data[i-23] - add ebp, ecx ; sum += qlp_coeff[22] * data[i-23] - mov ecx, [eax + 84] ; ecx = qlp_coeff[21] - imul ecx, [edi - 88] ; ecx = qlp_coeff[21] * data[i-22] - add ebp, ecx ; sum += qlp_coeff[21] * data[i-22] - mov ecx, [eax + 80] ; ecx = qlp_coeff[20] - imul ecx, [edi - 84] ; ecx = qlp_coeff[20] * data[i-21] - add ebp, ecx ; sum += qlp_coeff[20] * data[i-21] - mov ecx, [eax + 76] ; ecx = qlp_coeff[19] - imul ecx, [edi - 80] ; ecx = qlp_coeff[19] * data[i-20] - add ebp, ecx ; sum += qlp_coeff[19] * data[i-20] - mov ecx, [eax + 72] ; ecx = qlp_coeff[18] - imul ecx, [edi - 76] ; ecx = qlp_coeff[18] * data[i-19] - add ebp, ecx ; sum += qlp_coeff[18] * data[i-19] - mov ecx, [eax + 68] ; ecx = qlp_coeff[17] - imul ecx, [edi - 72] ; ecx = qlp_coeff[17] * data[i-18] - add ebp, ecx ; sum += qlp_coeff[17] * data[i-18] - mov ecx, [eax + 64] ; ecx = qlp_coeff[16] - imul ecx, [edi - 68] ; ecx = qlp_coeff[16] * data[i-17] - add ebp, ecx ; sum += qlp_coeff[16] * data[i-17] - mov ecx, [eax + 60] ; ecx = qlp_coeff[15] - imul ecx, [edi - 64] ; ecx = qlp_coeff[15] * data[i-16] - add ebp, ecx ; sum += qlp_coeff[15] * data[i-16] - mov ecx, [eax + 56] ; ecx = qlp_coeff[14] - imul ecx, [edi - 60] ; ecx = qlp_coeff[14] * data[i-15] - add ebp, ecx ; sum += qlp_coeff[14] * data[i-15] - mov ecx, [eax + 52] ; ecx = qlp_coeff[13] - imul ecx, [edi - 56] ; ecx = qlp_coeff[13] * data[i-14] - add ebp, ecx ; sum += qlp_coeff[13] * data[i-14] - mov ecx, [eax + 48] ; ecx = qlp_coeff[12] - imul ecx, [edi - 52] ; ecx = qlp_coeff[12] * data[i-13] - add ebp, ecx ; sum += qlp_coeff[12] * data[i-13] - mov ecx, [eax + 44] ; ecx = qlp_coeff[11] - imul ecx, [edi - 48] ; ecx = qlp_coeff[11] * data[i-12] - add ebp, ecx ; sum += qlp_coeff[11] * data[i-12] - mov ecx, [eax + 40] ; ecx = qlp_coeff[10] - imul ecx, [edi - 44] ; ecx = qlp_coeff[10] * data[i-11] - add ebp, ecx ; sum += qlp_coeff[10] * data[i-11] - mov ecx, [eax + 36] ; ecx = qlp_coeff[ 9] - imul ecx, [edi - 40] ; ecx = qlp_coeff[ 9] * data[i-10] - add ebp, ecx ; sum += qlp_coeff[ 9] * data[i-10] - mov ecx, [eax + 32] ; ecx = qlp_coeff[ 8] - imul ecx, [edi - 36] ; ecx = qlp_coeff[ 8] * data[i- 9] - add ebp, ecx ; sum += qlp_coeff[ 8] * data[i- 9] - mov ecx, [eax + 28] ; ecx = qlp_coeff[ 7] - imul ecx, [edi - 32] ; ecx = qlp_coeff[ 7] * data[i- 8] - add ebp, ecx ; sum += qlp_coeff[ 7] * data[i- 8] - mov ecx, [eax + 24] ; ecx = qlp_coeff[ 6] - imul ecx, [edi - 28] ; ecx = qlp_coeff[ 6] * data[i- 7] - add ebp, ecx ; sum += qlp_coeff[ 6] * data[i- 7] - mov ecx, [eax + 20] ; ecx = qlp_coeff[ 5] - imul ecx, [edi - 24] ; ecx = qlp_coeff[ 5] * data[i- 6] - add ebp, ecx ; sum += qlp_coeff[ 5] * data[i- 6] - mov ecx, [eax + 16] ; ecx = qlp_coeff[ 4] - imul ecx, [edi - 20] ; ecx = qlp_coeff[ 4] * data[i- 5] - add ebp, ecx ; sum += qlp_coeff[ 4] * data[i- 5] - mov ecx, [eax + 12] ; ecx = qlp_coeff[ 3] - imul ecx, [edi - 16] ; ecx = qlp_coeff[ 3] * data[i- 4] - add ebp, ecx ; sum += qlp_coeff[ 3] * data[i- 4] - mov ecx, [eax + 8] ; ecx = qlp_coeff[ 2] - imul ecx, [edi - 12] ; ecx = qlp_coeff[ 2] * data[i- 3] - add ebp, ecx ; sum += qlp_coeff[ 2] * data[i- 3] - mov ecx, [eax + 4] ; ecx = qlp_coeff[ 1] - imul ecx, [edi - 8] ; ecx = qlp_coeff[ 1] * data[i- 2] - add ebp, ecx ; sum += qlp_coeff[ 1] * data[i- 2] - mov ecx, [eax] ; ecx = qlp_coeff[ 0] (NOTE: one byte missing from instruction) - imul ecx, [edi - 4] ; ecx = qlp_coeff[ 0] * data[i- 1] - add ebp, ecx ; sum += qlp_coeff[ 0] * data[i- 1] -.jumper_0: - - mov cl, [esp + 36] - sar ebp, cl ; ebp = (sum >> lp_quantization) - add ebp, [esi + edi] ; ebp = residual[i] + (sum >> lp_quantization) - mov [edi], ebp ; data[i] = residual[i] + (sum >> lp_quantization) - add edi, byte 4 - - dec ebx - jz short .end - xor ebp, ebp - jmp edx - -.end: - pop edi - pop esi - pop ebx - pop ebp - ret - -; WATCHOUT: this routine works on 16 bit data which means bits-per-sample for -; the channel and qlp_coeffs must be <= 16. Especially note that this routine -; cannot be used for side-channel coded 16bps channels since the effective bps -; is 17. -; WATCHOUT: this routine requires that each data array have a buffer of up to -; 3 zeroes in front (at negative indices) for alignment purposes, i.e. for each -; channel n, data[n][-1] through data[n][-3] should be accessible and zero. - ALIGN 16 -cident FLAC__lpc_restore_signal_asm_ia32_mmx - ;[esp + 40] data[] - ;[esp + 36] lp_quantization - ;[esp + 32] order - ;[esp + 28] qlp_coeff[] - ;[esp + 24] data_len - ;[esp + 20] residual[] - - ;ASSERT(order > 0) - - push ebp - push ebx - push esi - push edi - - mov esi, [esp + 20] - mov edi, [esp + 40] - mov eax, [esp + 32] - mov ebx, [esp + 24] - - test ebx, ebx - jz near .end ; do nothing if data_len == 0 - cmp eax, byte 4 - jb near FLAC__lpc_restore_signal_asm_ia32.begin - - mov edx, [esp + 28] - movd mm6, [esp + 36] - mov ebp, esp - - and esp, 0xfffffff8 - - xor ecx, ecx -.copy_qlp_loop: - push word [edx + 4 * ecx] - inc ecx - cmp ecx, eax - jnz short .copy_qlp_loop - - and ecx, 0x3 - test ecx, ecx - je short .za_end - sub ecx, byte 4 -.za_loop: - push word 0 - inc eax - inc ecx - jnz short .za_loop -.za_end: - - movq mm5, [esp + 2 * eax - 8] - movd mm4, [edi - 16] - punpckldq mm4, [edi - 12] - movd mm0, [edi - 8] - punpckldq mm0, [edi - 4] - packssdw mm4, mm0 - - cmp eax, byte 4 - jnbe short .mmx_4more - - ALIGN 16 -.mmx_4_loop_i: - movq mm7, mm4 - pmaddwd mm7, mm5 - movq mm0, mm7 - punpckhdq mm7, mm7 - paddd mm7, mm0 - psrad mm7, mm6 - movd mm1, [esi] - paddd mm7, mm1 - movd [edi], mm7 - psllq mm7, 48 - psrlq mm4, 16 - por mm4, mm7 - - add esi, byte 4 - add edi, byte 4 - - dec ebx - jnz .mmx_4_loop_i - jmp .mmx_end -.mmx_4more: - shl eax, 2 - neg eax - add eax, byte 16 - ALIGN 16 -.mmx_4more_loop_i: - mov ecx, edi - add ecx, eax - mov edx, esp - - movq mm7, mm4 - pmaddwd mm7, mm5 - - ALIGN 16 -.mmx_4more_loop_j: - movd mm0, [ecx - 16] - punpckldq mm0, [ecx - 12] - movd mm1, [ecx - 8] - punpckldq mm1, [ecx - 4] - packssdw mm0, mm1 - pmaddwd mm0, [edx] - paddd mm7, mm0 - - add edx, byte 8 - add ecx, byte 16 - cmp ecx, edi - jnz .mmx_4more_loop_j - - movq mm0, mm7 - punpckhdq mm7, mm7 - paddd mm7, mm0 - psrad mm7, mm6 - movd mm1, [esi] - paddd mm7, mm1 - movd [edi], mm7 - psllq mm7, 48 - psrlq mm4, 16 - por mm4, mm7 - - add esi, byte 4 - add edi, byte 4 - - dec ebx - jnz short .mmx_4more_loop_i -.mmx_end: - emms - mov esp, ebp - -.end: - pop edi - pop esi - pop ebx - pop ebp - ret - -end: - -%ifdef OBJ_FORMAT_elf - section .note.GNU-stack noalloc -%endif diff --git a/FLAC/ia32/nasm.h b/FLAC/ia32/nasm.h deleted file mode 100644 index df7f30ab8..000000000 --- a/FLAC/ia32/nasm.h +++ /dev/null @@ -1,75 +0,0 @@ -; libFLAC - Free Lossless Audio Codec library -; Copyright (C) 2001,2002,2003,2004,2005,2006,2007 Josh Coalson -; -; Redistribution and use in source and binary forms, with or without -; modification, are permitted provided that the following conditions -; are met: -; -; - Redistributions of source code must retain the above copyright -; notice, this list of conditions and the following disclaimer. -; -; - Redistributions in binary form must reproduce the above copyright -; notice, this list of conditions and the following disclaimer in the -; documentation and/or other materials provided with the distribution. -; -; - Neither the name of the Xiph.org Foundation nor the names of its -; contributors may be used to endorse or promote products derived from -; this software without specific prior written permission. -; -; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -; ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR -; CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, -; EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR -; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF -; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING -; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - bits 32 - -%ifdef OBJ_FORMAT_win32 - %define FLAC__PUBLIC_NEEDS_UNDERSCORE - %idefine code_section section .text align=16 class=CODE use32 - %idefine data_section section .data align=32 class=DATA use32 - %idefine bss_section section .bss align=32 class=DATA use32 -%elifdef OBJ_FORMAT_aout - %define FLAC__PUBLIC_NEEDS_UNDERSCORE - %idefine code_section section .text - %idefine data_section section .data - %idefine bss_section section .bss -%elifdef OBJ_FORMAT_aoutb - %define FLAC__PUBLIC_NEEDS_UNDERSCORE - %idefine code_section section .text - %idefine data_section section .data - %idefine bss_section section .bss -%elifdef OBJ_FORMAT_elf - %idefine code_section section .text align=16 - %idefine data_section section .data align=32 - %idefine bss_section section .bss align=32 -%else - %error unsupported object format! -%endif - -%imacro cglobal 1 - %ifdef FLAC__PUBLIC_NEEDS_UNDERSCORE - global _%1 - %else - global %1 - %endif -%endmacro - -%imacro cextern 1 - %ifdef FLAC__PUBLIC_NEEDS_UNDERSCORE - extern _%1 - %else - extern %1 - %endif -%endmacro - -%imacro cident 1 -_%1: -%1: -%endmacro diff --git a/FLAC/ia32/stream_encoder_asm.nasm b/FLAC/ia32/stream_encoder_asm.nasm deleted file mode 100644 index 98ebe9a38..000000000 --- a/FLAC/ia32/stream_encoder_asm.nasm +++ /dev/null @@ -1,159 +0,0 @@ -; vim:filetype=nasm ts=8 - -; libFLAC - Free Lossless Audio Codec library -; Copyright (C) 2001,2002,2003,2004,2005,2006,2007 Josh Coalson -; -; Redistribution and use in source and binary forms, with or without -; modification, are permitted provided that the following conditions -; are met: -; -; - Redistributions of source code must retain the above copyright -; notice, this list of conditions and the following disclaimer. -; -; - Redistributions in binary form must reproduce the above copyright -; notice, this list of conditions and the following disclaimer in the -; documentation and/or other materials provided with the distribution. -; -; - Neither the name of the Xiph.org Foundation nor the names of its -; contributors may be used to endorse or promote products derived from -; this software without specific prior written permission. -; -; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -; ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR -; CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, -; EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR -; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF -; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING -; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -%include "ia32/nasm.h" - - data_section - -cglobal precompute_partition_info_sums_32bit_asm_ia32_ - - code_section - - -; ********************************************************************** -; -; void FLAC__bool FLAC__bitreader_read_rice_signed_block(FLAC__BitReader *br, int vals[], unsigned nvals, unsigned parameter) -; void precompute_partition_info_sums_32bit_( -; const FLAC__int32 residual[], -; FLAC__uint64 abs_residual_partition_sums[], -; unsigned blocksize, -; unsigned predictor_order, -; unsigned min_partition_order, -; unsigned max_partition_order -; ) -; - ALIGN 16 -cident precompute_partition_info_sums_32bit_asm_ia32_ - - ;; peppered throughout the code at major checkpoints are keys like this as to where things are at that point in time - ;; [esp + 4] const FLAC__int32 residual[] - ;; [esp + 8] FLAC__uint64 abs_residual_partition_sums[] - ;; [esp + 12] unsigned blocksize - ;; [esp + 16] unsigned predictor_order - ;; [esp + 20] unsigned min_partition_order - ;; [esp + 24] unsigned max_partition_order - push ebp - push ebx - push esi - push edi - sub esp, 8 - ;; [esp + 28] const FLAC__int32 residual[] - ;; [esp + 32] FLAC__uint64 abs_residual_partition_sums[] - ;; [esp + 36] unsigned blocksize - ;; [esp + 40] unsigned predictor_order - ;; [esp + 44] unsigned min_partition_order - ;; [esp + 48] unsigned max_partition_order - ;; [esp] partitions - ;; [esp + 4] default_partition_samples - - mov ecx, [esp + 48] - mov eax, 1 - shl eax, cl - mov [esp], eax ; [esp] <- partitions = 1u << max_partition_order; - mov eax, [esp + 36] - shr eax, cl - mov [esp + 4], eax ; [esp + 4] <- default_partition_samples = blocksize >> max_partition_order; - - ; - ; first do max_partition_order - ; - mov edi, [esp + 4] - sub edi, [esp + 40] ; edi <- end = (unsigned)(-(int)predictor_order) + default_partition_samples - xor esi, esi ; esi <- residual_sample = 0 - xor ecx, ecx ; ecx <- partition = 0 - mov ebp, [esp + 28] ; ebp <- residual[] - xor ebx, ebx ; ebx <- abs_residual_partition_sum = 0; - ; note we put the updates to 'end' and 'abs_residual_partition_sum' at the end of loop0 and in the initialization above so we could align loop0 and loop1 - ALIGN 16 -.loop0: ; for(partition = residual_sample = 0; partition < partitions; partition++) { -.loop1: ; for( ; residual_sample < end; residual_sample++) - mov eax, [ebp + esi * 4] - cdq - xor eax, edx - sub eax, edx - add ebx, eax ; abs_residual_partition_sum += abs(residual[residual_sample]); - ;@@@@@@ check overflow flag and abort here? - add esi, byte 1 - cmp esi, edi ; /* since the loop will always run at least once, we can put the loop check down here */ - jb .loop1 -.next1: - add edi, [esp + 4] ; end += default_partition_samples; - mov eax, [esp + 32] - mov [eax + ecx * 8], ebx ; abs_residual_partition_sums[partition] = abs_residual_partition_sum; - mov [eax + ecx * 8 + 4], dword 0 - xor ebx, ebx ; abs_residual_partition_sum = 0; - add ecx, byte 1 - cmp ecx, [esp] ; /* since the loop will always run at least once, we can put the loop check down here */ - jb .loop0 -.next0: ; } - ; - ; now merge partitions for lower orders - ; - mov esi, [esp + 32] ; esi <- abs_residual_partition_sums[from_partition==0]; - mov eax, [esp] - lea edi, [esi + eax * 8] ; edi <- abs_residual_partition_sums[to_partition==partitions]; - mov ecx, [esp + 48] - sub ecx, byte 1 ; ecx <- partition_order = (int)max_partition_order - 1; - ALIGN 16 -.loop2: ; for(; partition_order >= (int)min_partition_order; partition_order--) { - cmp ecx, [esp + 44] - jl .next2 - mov edx, 1 - shl edx, cl ; const unsigned partitions = 1u << partition_order; - ALIGN 16 -.loop3: ; for(i = 0; i < partitions; i++) { - mov eax, [esi] - mov ebx, [esi + 4] - add eax, [esi + 8] - adc ebx, [esi + 12] - mov [edi], eax - mov [edi + 4], ebx ; a_r_p_s[to_partition] = a_r_p_s[from_partition] + a_r_p_s[from_partition+1]; - add esi, byte 16 - add edi, byte 8 - sub edx, byte 1 - jnz .loop3 ; } - sub ecx, byte 1 - jmp .loop2 ; } -.next2: - - add esp, 8 - pop edi - pop esi - pop ebx - pop ebp - ret - -end: - -%ifdef OBJ_FORMAT_elf - section .note.GNU-stack noalloc -%endif diff --git a/FLAC/lpc.c b/FLAC/lpc.c deleted file mode 100644 index 24ddd552b..000000000 --- a/FLAC/lpc.c +++ /dev/null @@ -1,1377 +0,0 @@ -/* libFLAC - Free Lossless Audio Codec library - * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 Josh Coalson - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * - Neither the name of the Xiph.org Foundation nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#if HAVE_CONFIG_H -# include -#endif - -#include -#include "FLAC/assert.h" -#include "FLAC/format.h" -#include "private/bitmath.h" -#include "private/lpc.h" -#if defined DEBUG || defined FLAC__OVERFLOW_DETECT || defined FLAC__OVERFLOW_DETECT_VERBOSE -#include -#endif - -#ifndef FLAC__INTEGER_ONLY_LIBRARY - -#ifndef M_LN2 -/* math.h in VC++ doesn't seem to have this (how Microsoft is that?) */ -#define M_LN2 0.69314718055994530942 -#endif - -/* OPT: #undef'ing this may improve the speed on some architectures */ -#define FLAC__LPC_UNROLLED_FILTER_LOOPS - - -void FLAC__lpc_window_data(const FLAC__int32 in[], const FLAC__real window[], FLAC__real out[], unsigned data_len) -{ - unsigned i; - for(i = 0; i < data_len; i++) - out[i] = in[i] * window[i]; -} - -void FLAC__lpc_compute_autocorrelation(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]) -{ - /* a readable, but slower, version */ -#if 0 - FLAC__real d; - unsigned i; - - FLAC__ASSERT(lag > 0); - FLAC__ASSERT(lag <= data_len); - - /* - * Technically we should subtract the mean first like so: - * for(i = 0; i < data_len; i++) - * data[i] -= mean; - * but it appears not to make enough of a difference to matter, and - * most signals are already closely centered around zero - */ - while(lag--) { - for(i = lag, d = 0.0; i < data_len; i++) - d += data[i] * data[i - lag]; - autoc[lag] = d; - } -#endif - - /* - * this version tends to run faster because of better data locality - * ('data_len' is usually much larger than 'lag') - */ - FLAC__real d; - unsigned sample, coeff; - const unsigned limit = data_len - lag; - - FLAC__ASSERT(lag > 0); - FLAC__ASSERT(lag <= data_len); - - for(coeff = 0; coeff < lag; coeff++) - autoc[coeff] = 0.0; - for(sample = 0; sample <= limit; sample++) { - d = data[sample]; - for(coeff = 0; coeff < lag; coeff++) - autoc[coeff] += d * data[sample+coeff]; - } - for(; sample < data_len; sample++) { - d = data[sample]; - for(coeff = 0; coeff < data_len - sample; coeff++) - autoc[coeff] += d * data[sample+coeff]; - } -} - -void FLAC__lpc_compute_lp_coefficients(const FLAC__real autoc[], unsigned *max_order, FLAC__real lp_coeff[][FLAC__MAX_LPC_ORDER], FLAC__double error[]) -{ - unsigned i, j; - FLAC__double r, err, ref[FLAC__MAX_LPC_ORDER], lpc[FLAC__MAX_LPC_ORDER]; - - FLAC__ASSERT(0 != max_order); - FLAC__ASSERT(0 < *max_order); - FLAC__ASSERT(*max_order <= FLAC__MAX_LPC_ORDER); - FLAC__ASSERT(autoc[0] != 0.0); - - err = autoc[0]; - - for(i = 0; i < *max_order; i++) { - /* Sum up this iteration's reflection coefficient. */ - r = -autoc[i+1]; - for(j = 0; j < i; j++) - r -= lpc[j] * autoc[i-j]; - ref[i] = (r/=err); - - /* Update LPC coefficients and total error. */ - lpc[i]=r; - for(j = 0; j < (i>>1); j++) { - FLAC__double tmp = lpc[j]; - lpc[j] += r * lpc[i-1-j]; - lpc[i-1-j] += r * tmp; - } - if(i & 1) - lpc[j] += lpc[j] * r; - - err *= (1.0 - r * r); - - /* save this order */ - for(j = 0; j <= i; j++) - lp_coeff[i][j] = (FLAC__real)(-lpc[j]); /* negate FIR filter coeff to get predictor coeff */ - error[i] = err; - - /* see SF bug #1601812 http://sourceforge.net/tracker/index.php?func=detail&aid=1601812&group_id=13478&atid=113478 */ - if(err == 0.0) { - *max_order = i+1; - return; - } - } -} - -int FLAC__lpc_quantize_coefficients(const FLAC__real lp_coeff[], unsigned order, unsigned precision, FLAC__int32 qlp_coeff[], int *shift) -{ - unsigned i; - FLAC__double cmax; - FLAC__int32 qmax, qmin; - - FLAC__ASSERT(precision > 0); - FLAC__ASSERT(precision >= FLAC__MIN_QLP_COEFF_PRECISION); - - /* drop one bit for the sign; from here on out we consider only |lp_coeff[i]| */ - precision--; - qmax = 1 << precision; - qmin = -qmax; - qmax--; - - /* calc cmax = max( |lp_coeff[i]| ) */ - cmax = 0.0; - for(i = 0; i < order; i++) { - const FLAC__double d = fabs(lp_coeff[i]); - if(d > cmax) - cmax = d; - } - - if(cmax <= 0.0) { - /* => coefficients are all 0, which means our constant-detect didn't work */ - return 2; - } - else { - const int max_shiftlimit = (1 << (FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN-1)) - 1; - const int min_shiftlimit = -max_shiftlimit - 1; - int log2cmax; - - (void)frexp(cmax, &log2cmax); - log2cmax--; - *shift = (int)precision - log2cmax - 1; - - if(*shift > max_shiftlimit) - *shift = max_shiftlimit; - else if(*shift < min_shiftlimit) - return 1; - } - - if(*shift >= 0) { - FLAC__double error = 0.0; - FLAC__int32 q; - for(i = 0; i < order; i++) { - error += lp_coeff[i] * (1 << *shift); -#if 1 /* unfortunately lround() is C99 */ - if(error >= 0.0) - q = (FLAC__int32)(error + 0.5); - else - q = (FLAC__int32)(error - 0.5); -#else - q = lround(error); -#endif -#ifdef FLAC__OVERFLOW_DETECT - if(q > qmax+1) /* we expect q==qmax+1 occasionally due to rounding */ - fprintf(stderr,"FLAC__lpc_quantize_coefficients: quantizer overflow: q>qmax %d>%d shift=%d cmax=%f precision=%u lpc[%u]=%f\n",q,qmax,*shift,cmax,precision+1,i,lp_coeff[i]); - else if(q < qmin) - fprintf(stderr,"FLAC__lpc_quantize_coefficients: quantizer overflow: q qmax) - q = qmax; - else if(q < qmin) - q = qmin; - error -= q; - qlp_coeff[i] = q; - } - } - /* negative shift is very rare but due to design flaw, negative shift is - * a NOP in the decoder, so it must be handled specially by scaling down - * coeffs - */ - else { - const int nshift = -(*shift); - FLAC__double error = 0.0; - FLAC__int32 q; -#ifdef DEBUG - fprintf(stderr,"FLAC__lpc_quantize_coefficients: negative shift=%d order=%u cmax=%f\n", *shift, order, cmax); -#endif - for(i = 0; i < order; i++) { - error += lp_coeff[i] / (1 << nshift); -#if 1 /* unfortunately lround() is C99 */ - if(error >= 0.0) - q = (FLAC__int32)(error + 0.5); - else - q = (FLAC__int32)(error - 0.5); -#else - q = lround(error); -#endif -#ifdef FLAC__OVERFLOW_DETECT - if(q > qmax+1) /* we expect q==qmax+1 occasionally due to rounding */ - fprintf(stderr,"FLAC__lpc_quantize_coefficients: quantizer overflow: q>qmax %d>%d shift=%d cmax=%f precision=%u lpc[%u]=%f\n",q,qmax,*shift,cmax,precision+1,i,lp_coeff[i]); - else if(q < qmin) - fprintf(stderr,"FLAC__lpc_quantize_coefficients: quantizer overflow: q qmax) - q = qmax; - else if(q < qmin) - q = qmin; - error -= q; - qlp_coeff[i] = q; - } - *shift = 0; - } - - return 0; -} - -void FLAC__lpc_compute_residual_from_qlp_coefficients(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]) -#if defined(FLAC__OVERFLOW_DETECT) || !defined(FLAC__LPC_UNROLLED_FILTER_LOOPS) -{ - FLAC__int64 sumo; - unsigned i, j; - FLAC__int32 sum; - const FLAC__int32 *history; - -#ifdef FLAC__OVERFLOW_DETECT_VERBOSE - fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization); - for(i=0;i 0); - - for(i = 0; i < data_len; i++) { - sumo = 0; - sum = 0; - history = data; - for(j = 0; j < order; j++) { - sum += qlp_coeff[j] * (*(--history)); - sumo += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*history); -#if defined _MSC_VER - if(sumo > 2147483647I64 || sumo < -2147483648I64) - fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients: OVERFLOW, i=%u, j=%u, c=%d, d=%d, sumo=%I64d\n",i,j,qlp_coeff[j],*history,sumo); -#else - if(sumo > 2147483647ll || sumo < -2147483648ll) - fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients: OVERFLOW, i=%u, j=%u, c=%d, d=%d, sumo=%lld\n",i,j,qlp_coeff[j],*history,(long long)sumo); -#endif - } - *(residual++) = *(data++) - (sum >> lp_quantization); - } - - /* Here's a slower but clearer version: - for(i = 0; i < data_len; i++) { - sum = 0; - for(j = 0; j < order; j++) - sum += qlp_coeff[j] * data[i-j-1]; - residual[i] = data[i] - (sum >> lp_quantization); - } - */ -} -#else /* fully unrolled version for normal use */ -{ - int i; - FLAC__int32 sum; - - FLAC__ASSERT(order > 0); - FLAC__ASSERT(order <= 32); - - /* - * We do unique versions up to 12th order since that's the subset limit. - * Also they are roughly ordered to match frequency of occurrence to - * minimize branching. - */ - if(order <= 12) { - if(order > 8) { - if(order > 10) { - if(order == 12) { - for(i = 0; i < (int)data_len; i++) { - sum = 0; - sum += qlp_coeff[11] * data[i-12]; - sum += qlp_coeff[10] * data[i-11]; - sum += qlp_coeff[9] * data[i-10]; - sum += qlp_coeff[8] * data[i-9]; - sum += qlp_coeff[7] * data[i-8]; - sum += qlp_coeff[6] * data[i-7]; - sum += qlp_coeff[5] * data[i-6]; - sum += qlp_coeff[4] * data[i-5]; - sum += qlp_coeff[3] * data[i-4]; - sum += qlp_coeff[2] * data[i-3]; - sum += qlp_coeff[1] * data[i-2]; - sum += qlp_coeff[0] * data[i-1]; - residual[i] = data[i] - (sum >> lp_quantization); - } - } - else { /* order == 11 */ - for(i = 0; i < (int)data_len; i++) { - sum = 0; - sum += qlp_coeff[10] * data[i-11]; - sum += qlp_coeff[9] * data[i-10]; - sum += qlp_coeff[8] * data[i-9]; - sum += qlp_coeff[7] * data[i-8]; - sum += qlp_coeff[6] * data[i-7]; - sum += qlp_coeff[5] * data[i-6]; - sum += qlp_coeff[4] * data[i-5]; - sum += qlp_coeff[3] * data[i-4]; - sum += qlp_coeff[2] * data[i-3]; - sum += qlp_coeff[1] * data[i-2]; - sum += qlp_coeff[0] * data[i-1]; - residual[i] = data[i] - (sum >> lp_quantization); - } - } - } - else { - if(order == 10) { - for(i = 0; i < (int)data_len; i++) { - sum = 0; - sum += qlp_coeff[9] * data[i-10]; - sum += qlp_coeff[8] * data[i-9]; - sum += qlp_coeff[7] * data[i-8]; - sum += qlp_coeff[6] * data[i-7]; - sum += qlp_coeff[5] * data[i-6]; - sum += qlp_coeff[4] * data[i-5]; - sum += qlp_coeff[3] * data[i-4]; - sum += qlp_coeff[2] * data[i-3]; - sum += qlp_coeff[1] * data[i-2]; - sum += qlp_coeff[0] * data[i-1]; - residual[i] = data[i] - (sum >> lp_quantization); - } - } - else { /* order == 9 */ - for(i = 0; i < (int)data_len; i++) { - sum = 0; - sum += qlp_coeff[8] * data[i-9]; - sum += qlp_coeff[7] * data[i-8]; - sum += qlp_coeff[6] * data[i-7]; - sum += qlp_coeff[5] * data[i-6]; - sum += qlp_coeff[4] * data[i-5]; - sum += qlp_coeff[3] * data[i-4]; - sum += qlp_coeff[2] * data[i-3]; - sum += qlp_coeff[1] * data[i-2]; - sum += qlp_coeff[0] * data[i-1]; - residual[i] = data[i] - (sum >> lp_quantization); - } - } - } - } - else if(order > 4) { - if(order > 6) { - if(order == 8) { - for(i = 0; i < (int)data_len; i++) { - sum = 0; - sum += qlp_coeff[7] * data[i-8]; - sum += qlp_coeff[6] * data[i-7]; - sum += qlp_coeff[5] * data[i-6]; - sum += qlp_coeff[4] * data[i-5]; - sum += qlp_coeff[3] * data[i-4]; - sum += qlp_coeff[2] * data[i-3]; - sum += qlp_coeff[1] * data[i-2]; - sum += qlp_coeff[0] * data[i-1]; - residual[i] = data[i] - (sum >> lp_quantization); - } - } - else { /* order == 7 */ - for(i = 0; i < (int)data_len; i++) { - sum = 0; - sum += qlp_coeff[6] * data[i-7]; - sum += qlp_coeff[5] * data[i-6]; - sum += qlp_coeff[4] * data[i-5]; - sum += qlp_coeff[3] * data[i-4]; - sum += qlp_coeff[2] * data[i-3]; - sum += qlp_coeff[1] * data[i-2]; - sum += qlp_coeff[0] * data[i-1]; - residual[i] = data[i] - (sum >> lp_quantization); - } - } - } - else { - if(order == 6) { - for(i = 0; i < (int)data_len; i++) { - sum = 0; - sum += qlp_coeff[5] * data[i-6]; - sum += qlp_coeff[4] * data[i-5]; - sum += qlp_coeff[3] * data[i-4]; - sum += qlp_coeff[2] * data[i-3]; - sum += qlp_coeff[1] * data[i-2]; - sum += qlp_coeff[0] * data[i-1]; - residual[i] = data[i] - (sum >> lp_quantization); - } - } - else { /* order == 5 */ - for(i = 0; i < (int)data_len; i++) { - sum = 0; - sum += qlp_coeff[4] * data[i-5]; - sum += qlp_coeff[3] * data[i-4]; - sum += qlp_coeff[2] * data[i-3]; - sum += qlp_coeff[1] * data[i-2]; - sum += qlp_coeff[0] * data[i-1]; - residual[i] = data[i] - (sum >> lp_quantization); - } - } - } - } - else { - if(order > 2) { - if(order == 4) { - for(i = 0; i < (int)data_len; i++) { - sum = 0; - sum += qlp_coeff[3] * data[i-4]; - sum += qlp_coeff[2] * data[i-3]; - sum += qlp_coeff[1] * data[i-2]; - sum += qlp_coeff[0] * data[i-1]; - residual[i] = data[i] - (sum >> lp_quantization); - } - } - else { /* order == 3 */ - for(i = 0; i < (int)data_len; i++) { - sum = 0; - sum += qlp_coeff[2] * data[i-3]; - sum += qlp_coeff[1] * data[i-2]; - sum += qlp_coeff[0] * data[i-1]; - residual[i] = data[i] - (sum >> lp_quantization); - } - } - } - else { - if(order == 2) { - for(i = 0; i < (int)data_len; i++) { - sum = 0; - sum += qlp_coeff[1] * data[i-2]; - sum += qlp_coeff[0] * data[i-1]; - residual[i] = data[i] - (sum >> lp_quantization); - } - } - else { /* order == 1 */ - for(i = 0; i < (int)data_len; i++) - residual[i] = data[i] - ((qlp_coeff[0] * data[i-1]) >> lp_quantization); - } - } - } - } - else { /* order > 12 */ - for(i = 0; i < (int)data_len; i++) { - sum = 0; - switch(order) { - case 32: sum += qlp_coeff[31] * data[i-32]; - case 31: sum += qlp_coeff[30] * data[i-31]; - case 30: sum += qlp_coeff[29] * data[i-30]; - case 29: sum += qlp_coeff[28] * data[i-29]; - case 28: sum += qlp_coeff[27] * data[i-28]; - case 27: sum += qlp_coeff[26] * data[i-27]; - case 26: sum += qlp_coeff[25] * data[i-26]; - case 25: sum += qlp_coeff[24] * data[i-25]; - case 24: sum += qlp_coeff[23] * data[i-24]; - case 23: sum += qlp_coeff[22] * data[i-23]; - case 22: sum += qlp_coeff[21] * data[i-22]; - case 21: sum += qlp_coeff[20] * data[i-21]; - case 20: sum += qlp_coeff[19] * data[i-20]; - case 19: sum += qlp_coeff[18] * data[i-19]; - case 18: sum += qlp_coeff[17] * data[i-18]; - case 17: sum += qlp_coeff[16] * data[i-17]; - case 16: sum += qlp_coeff[15] * data[i-16]; - case 15: sum += qlp_coeff[14] * data[i-15]; - case 14: sum += qlp_coeff[13] * data[i-14]; - case 13: sum += qlp_coeff[12] * data[i-13]; - sum += qlp_coeff[11] * data[i-12]; - sum += qlp_coeff[10] * data[i-11]; - sum += qlp_coeff[ 9] * data[i-10]; - sum += qlp_coeff[ 8] * data[i- 9]; - sum += qlp_coeff[ 7] * data[i- 8]; - sum += qlp_coeff[ 6] * data[i- 7]; - sum += qlp_coeff[ 5] * data[i- 6]; - sum += qlp_coeff[ 4] * data[i- 5]; - sum += qlp_coeff[ 3] * data[i- 4]; - sum += qlp_coeff[ 2] * data[i- 3]; - sum += qlp_coeff[ 1] * data[i- 2]; - sum += qlp_coeff[ 0] * data[i- 1]; - } - residual[i] = data[i] - (sum >> lp_quantization); - } - } -} -#endif - -void FLAC__lpc_compute_residual_from_qlp_coefficients_wide(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]) -#if defined(FLAC__OVERFLOW_DETECT) || !defined(FLAC__LPC_UNROLLED_FILTER_LOOPS) -{ - unsigned i, j; - FLAC__int64 sum; - const FLAC__int32 *history; - -#ifdef FLAC__OVERFLOW_DETECT_VERBOSE - fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization); - for(i=0;i 0); - - for(i = 0; i < data_len; i++) { - sum = 0; - history = data; - for(j = 0; j < order; j++) - sum += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*(--history)); - if(FLAC__bitmath_silog2_wide(sum >> lp_quantization) > 32) { -#if defined _MSC_VER - fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: OVERFLOW, i=%u, sum=%I64d\n", i, sum >> lp_quantization); -#else - fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: OVERFLOW, i=%u, sum=%lld\n", i, (long long)(sum >> lp_quantization)); -#endif - break; - } - if(FLAC__bitmath_silog2_wide((FLAC__int64)(*data) - (sum >> lp_quantization)) > 32) { -#if defined _MSC_VER - fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: OVERFLOW, i=%u, data=%d, sum=%I64d, residual=%I64d\n", i, *data, sum >> lp_quantization, (FLAC__int64)(*data) - (sum >> lp_quantization)); -#else - fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: OVERFLOW, i=%u, data=%d, sum=%lld, residual=%lld\n", i, *data, (long long)(sum >> lp_quantization), (long long)((FLAC__int64)(*data) - (sum >> lp_quantization))); -#endif - break; - } - *(residual++) = *(data++) - (FLAC__int32)(sum >> lp_quantization); - } -} -#else /* fully unrolled version for normal use */ -{ - int i; - FLAC__int64 sum; - - FLAC__ASSERT(order > 0); - FLAC__ASSERT(order <= 32); - - /* - * We do unique versions up to 12th order since that's the subset limit. - * Also they are roughly ordered to match frequency of occurrence to - * minimize branching. - */ - if(order <= 12) { - if(order > 8) { - if(order > 10) { - if(order == 12) { - for(i = 0; i < (int)data_len; i++) { - sum = 0; - sum += qlp_coeff[11] * (FLAC__int64)data[i-12]; - sum += qlp_coeff[10] * (FLAC__int64)data[i-11]; - sum += qlp_coeff[9] * (FLAC__int64)data[i-10]; - sum += qlp_coeff[8] * (FLAC__int64)data[i-9]; - sum += qlp_coeff[7] * (FLAC__int64)data[i-8]; - sum += qlp_coeff[6] * (FLAC__int64)data[i-7]; - sum += qlp_coeff[5] * (FLAC__int64)data[i-6]; - sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; - sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; - sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; - sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; - sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; - residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization); - } - } - else { /* order == 11 */ - for(i = 0; i < (int)data_len; i++) { - sum = 0; - sum += qlp_coeff[10] * (FLAC__int64)data[i-11]; - sum += qlp_coeff[9] * (FLAC__int64)data[i-10]; - sum += qlp_coeff[8] * (FLAC__int64)data[i-9]; - sum += qlp_coeff[7] * (FLAC__int64)data[i-8]; - sum += qlp_coeff[6] * (FLAC__int64)data[i-7]; - sum += qlp_coeff[5] * (FLAC__int64)data[i-6]; - sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; - sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; - sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; - sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; - sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; - residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization); - } - } - } - else { - if(order == 10) { - for(i = 0; i < (int)data_len; i++) { - sum = 0; - sum += qlp_coeff[9] * (FLAC__int64)data[i-10]; - sum += qlp_coeff[8] * (FLAC__int64)data[i-9]; - sum += qlp_coeff[7] * (FLAC__int64)data[i-8]; - sum += qlp_coeff[6] * (FLAC__int64)data[i-7]; - sum += qlp_coeff[5] * (FLAC__int64)data[i-6]; - sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; - sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; - sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; - sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; - sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; - residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization); - } - } - else { /* order == 9 */ - for(i = 0; i < (int)data_len; i++) { - sum = 0; - sum += qlp_coeff[8] * (FLAC__int64)data[i-9]; - sum += qlp_coeff[7] * (FLAC__int64)data[i-8]; - sum += qlp_coeff[6] * (FLAC__int64)data[i-7]; - sum += qlp_coeff[5] * (FLAC__int64)data[i-6]; - sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; - sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; - sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; - sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; - sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; - residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization); - } - } - } - } - else if(order > 4) { - if(order > 6) { - if(order == 8) { - for(i = 0; i < (int)data_len; i++) { - sum = 0; - sum += qlp_coeff[7] * (FLAC__int64)data[i-8]; - sum += qlp_coeff[6] * (FLAC__int64)data[i-7]; - sum += qlp_coeff[5] * (FLAC__int64)data[i-6]; - sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; - sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; - sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; - sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; - sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; - residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization); - } - } - else { /* order == 7 */ - for(i = 0; i < (int)data_len; i++) { - sum = 0; - sum += qlp_coeff[6] * (FLAC__int64)data[i-7]; - sum += qlp_coeff[5] * (FLAC__int64)data[i-6]; - sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; - sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; - sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; - sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; - sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; - residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization); - } - } - } - else { - if(order == 6) { - for(i = 0; i < (int)data_len; i++) { - sum = 0; - sum += qlp_coeff[5] * (FLAC__int64)data[i-6]; - sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; - sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; - sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; - sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; - sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; - residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization); - } - } - else { /* order == 5 */ - for(i = 0; i < (int)data_len; i++) { - sum = 0; - sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; - sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; - sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; - sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; - sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; - residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization); - } - } - } - } - else { - if(order > 2) { - if(order == 4) { - for(i = 0; i < (int)data_len; i++) { - sum = 0; - sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; - sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; - sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; - sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; - residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization); - } - } - else { /* order == 3 */ - for(i = 0; i < (int)data_len; i++) { - sum = 0; - sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; - sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; - sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; - residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization); - } - } - } - else { - if(order == 2) { - for(i = 0; i < (int)data_len; i++) { - sum = 0; - sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; - sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; - residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization); - } - } - else { /* order == 1 */ - for(i = 0; i < (int)data_len; i++) - residual[i] = data[i] - (FLAC__int32)((qlp_coeff[0] * (FLAC__int64)data[i-1]) >> lp_quantization); - } - } - } - } - else { /* order > 12 */ - for(i = 0; i < (int)data_len; i++) { - sum = 0; - switch(order) { - case 32: sum += qlp_coeff[31] * (FLAC__int64)data[i-32]; - case 31: sum += qlp_coeff[30] * (FLAC__int64)data[i-31]; - case 30: sum += qlp_coeff[29] * (FLAC__int64)data[i-30]; - case 29: sum += qlp_coeff[28] * (FLAC__int64)data[i-29]; - case 28: sum += qlp_coeff[27] * (FLAC__int64)data[i-28]; - case 27: sum += qlp_coeff[26] * (FLAC__int64)data[i-27]; - case 26: sum += qlp_coeff[25] * (FLAC__int64)data[i-26]; - case 25: sum += qlp_coeff[24] * (FLAC__int64)data[i-25]; - case 24: sum += qlp_coeff[23] * (FLAC__int64)data[i-24]; - case 23: sum += qlp_coeff[22] * (FLAC__int64)data[i-23]; - case 22: sum += qlp_coeff[21] * (FLAC__int64)data[i-22]; - case 21: sum += qlp_coeff[20] * (FLAC__int64)data[i-21]; - case 20: sum += qlp_coeff[19] * (FLAC__int64)data[i-20]; - case 19: sum += qlp_coeff[18] * (FLAC__int64)data[i-19]; - case 18: sum += qlp_coeff[17] * (FLAC__int64)data[i-18]; - case 17: sum += qlp_coeff[16] * (FLAC__int64)data[i-17]; - case 16: sum += qlp_coeff[15] * (FLAC__int64)data[i-16]; - case 15: sum += qlp_coeff[14] * (FLAC__int64)data[i-15]; - case 14: sum += qlp_coeff[13] * (FLAC__int64)data[i-14]; - case 13: sum += qlp_coeff[12] * (FLAC__int64)data[i-13]; - sum += qlp_coeff[11] * (FLAC__int64)data[i-12]; - sum += qlp_coeff[10] * (FLAC__int64)data[i-11]; - sum += qlp_coeff[ 9] * (FLAC__int64)data[i-10]; - sum += qlp_coeff[ 8] * (FLAC__int64)data[i- 9]; - sum += qlp_coeff[ 7] * (FLAC__int64)data[i- 8]; - sum += qlp_coeff[ 6] * (FLAC__int64)data[i- 7]; - sum += qlp_coeff[ 5] * (FLAC__int64)data[i- 6]; - sum += qlp_coeff[ 4] * (FLAC__int64)data[i- 5]; - sum += qlp_coeff[ 3] * (FLAC__int64)data[i- 4]; - sum += qlp_coeff[ 2] * (FLAC__int64)data[i- 3]; - sum += qlp_coeff[ 1] * (FLAC__int64)data[i- 2]; - sum += qlp_coeff[ 0] * (FLAC__int64)data[i- 1]; - } - residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization); - } - } -} -#endif - -#endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */ - -void FLAC__lpc_restore_signal(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]) -#if defined(FLAC__OVERFLOW_DETECT) || !defined(FLAC__LPC_UNROLLED_FILTER_LOOPS) -{ - FLAC__int64 sumo; - unsigned i, j; - FLAC__int32 sum; - const FLAC__int32 *r = residual, *history; - -#ifdef FLAC__OVERFLOW_DETECT_VERBOSE - fprintf(stderr,"FLAC__lpc_restore_signal: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization); - for(i=0;i 0); - - for(i = 0; i < data_len; i++) { - sumo = 0; - sum = 0; - history = data; - for(j = 0; j < order; j++) { - sum += qlp_coeff[j] * (*(--history)); - sumo += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*history); -#if defined _MSC_VER - if(sumo > 2147483647I64 || sumo < -2147483648I64) - fprintf(stderr,"FLAC__lpc_restore_signal: OVERFLOW, i=%u, j=%u, c=%d, d=%d, sumo=%I64d\n",i,j,qlp_coeff[j],*history,sumo); -#else - if(sumo > 2147483647ll || sumo < -2147483648ll) - fprintf(stderr,"FLAC__lpc_restore_signal: OVERFLOW, i=%u, j=%u, c=%d, d=%d, sumo=%lld\n",i,j,qlp_coeff[j],*history,(long long)sumo); -#endif - } - *(data++) = *(r++) + (sum >> lp_quantization); - } - - /* Here's a slower but clearer version: - for(i = 0; i < data_len; i++) { - sum = 0; - for(j = 0; j < order; j++) - sum += qlp_coeff[j] * data[i-j-1]; - data[i] = residual[i] + (sum >> lp_quantization); - } - */ -} -#else /* fully unrolled version for normal use */ -{ - int i; - FLAC__int32 sum; - - FLAC__ASSERT(order > 0); - FLAC__ASSERT(order <= 32); - - /* - * We do unique versions up to 12th order since that's the subset limit. - * Also they are roughly ordered to match frequency of occurrence to - * minimize branching. - */ - if(order <= 12) { - if(order > 8) { - if(order > 10) { - if(order == 12) { - for(i = 0; i < (int)data_len; i++) { - sum = 0; - sum += qlp_coeff[11] * data[i-12]; - sum += qlp_coeff[10] * data[i-11]; - sum += qlp_coeff[9] * data[i-10]; - sum += qlp_coeff[8] * data[i-9]; - sum += qlp_coeff[7] * data[i-8]; - sum += qlp_coeff[6] * data[i-7]; - sum += qlp_coeff[5] * data[i-6]; - sum += qlp_coeff[4] * data[i-5]; - sum += qlp_coeff[3] * data[i-4]; - sum += qlp_coeff[2] * data[i-3]; - sum += qlp_coeff[1] * data[i-2]; - sum += qlp_coeff[0] * data[i-1]; - data[i] = residual[i] + (sum >> lp_quantization); - } - } - else { /* order == 11 */ - for(i = 0; i < (int)data_len; i++) { - sum = 0; - sum += qlp_coeff[10] * data[i-11]; - sum += qlp_coeff[9] * data[i-10]; - sum += qlp_coeff[8] * data[i-9]; - sum += qlp_coeff[7] * data[i-8]; - sum += qlp_coeff[6] * data[i-7]; - sum += qlp_coeff[5] * data[i-6]; - sum += qlp_coeff[4] * data[i-5]; - sum += qlp_coeff[3] * data[i-4]; - sum += qlp_coeff[2] * data[i-3]; - sum += qlp_coeff[1] * data[i-2]; - sum += qlp_coeff[0] * data[i-1]; - data[i] = residual[i] + (sum >> lp_quantization); - } - } - } - else { - if(order == 10) { - for(i = 0; i < (int)data_len; i++) { - sum = 0; - sum += qlp_coeff[9] * data[i-10]; - sum += qlp_coeff[8] * data[i-9]; - sum += qlp_coeff[7] * data[i-8]; - sum += qlp_coeff[6] * data[i-7]; - sum += qlp_coeff[5] * data[i-6]; - sum += qlp_coeff[4] * data[i-5]; - sum += qlp_coeff[3] * data[i-4]; - sum += qlp_coeff[2] * data[i-3]; - sum += qlp_coeff[1] * data[i-2]; - sum += qlp_coeff[0] * data[i-1]; - data[i] = residual[i] + (sum >> lp_quantization); - } - } - else { /* order == 9 */ - for(i = 0; i < (int)data_len; i++) { - sum = 0; - sum += qlp_coeff[8] * data[i-9]; - sum += qlp_coeff[7] * data[i-8]; - sum += qlp_coeff[6] * data[i-7]; - sum += qlp_coeff[5] * data[i-6]; - sum += qlp_coeff[4] * data[i-5]; - sum += qlp_coeff[3] * data[i-4]; - sum += qlp_coeff[2] * data[i-3]; - sum += qlp_coeff[1] * data[i-2]; - sum += qlp_coeff[0] * data[i-1]; - data[i] = residual[i] + (sum >> lp_quantization); - } - } - } - } - else if(order > 4) { - if(order > 6) { - if(order == 8) { - for(i = 0; i < (int)data_len; i++) { - sum = 0; - sum += qlp_coeff[7] * data[i-8]; - sum += qlp_coeff[6] * data[i-7]; - sum += qlp_coeff[5] * data[i-6]; - sum += qlp_coeff[4] * data[i-5]; - sum += qlp_coeff[3] * data[i-4]; - sum += qlp_coeff[2] * data[i-3]; - sum += qlp_coeff[1] * data[i-2]; - sum += qlp_coeff[0] * data[i-1]; - data[i] = residual[i] + (sum >> lp_quantization); - } - } - else { /* order == 7 */ - for(i = 0; i < (int)data_len; i++) { - sum = 0; - sum += qlp_coeff[6] * data[i-7]; - sum += qlp_coeff[5] * data[i-6]; - sum += qlp_coeff[4] * data[i-5]; - sum += qlp_coeff[3] * data[i-4]; - sum += qlp_coeff[2] * data[i-3]; - sum += qlp_coeff[1] * data[i-2]; - sum += qlp_coeff[0] * data[i-1]; - data[i] = residual[i] + (sum >> lp_quantization); - } - } - } - else { - if(order == 6) { - for(i = 0; i < (int)data_len; i++) { - sum = 0; - sum += qlp_coeff[5] * data[i-6]; - sum += qlp_coeff[4] * data[i-5]; - sum += qlp_coeff[3] * data[i-4]; - sum += qlp_coeff[2] * data[i-3]; - sum += qlp_coeff[1] * data[i-2]; - sum += qlp_coeff[0] * data[i-1]; - data[i] = residual[i] + (sum >> lp_quantization); - } - } - else { /* order == 5 */ - for(i = 0; i < (int)data_len; i++) { - sum = 0; - sum += qlp_coeff[4] * data[i-5]; - sum += qlp_coeff[3] * data[i-4]; - sum += qlp_coeff[2] * data[i-3]; - sum += qlp_coeff[1] * data[i-2]; - sum += qlp_coeff[0] * data[i-1]; - data[i] = residual[i] + (sum >> lp_quantization); - } - } - } - } - else { - if(order > 2) { - if(order == 4) { - for(i = 0; i < (int)data_len; i++) { - sum = 0; - sum += qlp_coeff[3] * data[i-4]; - sum += qlp_coeff[2] * data[i-3]; - sum += qlp_coeff[1] * data[i-2]; - sum += qlp_coeff[0] * data[i-1]; - data[i] = residual[i] + (sum >> lp_quantization); - } - } - else { /* order == 3 */ - for(i = 0; i < (int)data_len; i++) { - sum = 0; - sum += qlp_coeff[2] * data[i-3]; - sum += qlp_coeff[1] * data[i-2]; - sum += qlp_coeff[0] * data[i-1]; - data[i] = residual[i] + (sum >> lp_quantization); - } - } - } - else { - if(order == 2) { - for(i = 0; i < (int)data_len; i++) { - sum = 0; - sum += qlp_coeff[1] * data[i-2]; - sum += qlp_coeff[0] * data[i-1]; - data[i] = residual[i] + (sum >> lp_quantization); - } - } - else { /* order == 1 */ - for(i = 0; i < (int)data_len; i++) - data[i] = residual[i] + ((qlp_coeff[0] * data[i-1]) >> lp_quantization); - } - } - } - } - else { /* order > 12 */ - for(i = 0; i < (int)data_len; i++) { - sum = 0; - switch(order) { - case 32: sum += qlp_coeff[31] * data[i-32]; - case 31: sum += qlp_coeff[30] * data[i-31]; - case 30: sum += qlp_coeff[29] * data[i-30]; - case 29: sum += qlp_coeff[28] * data[i-29]; - case 28: sum += qlp_coeff[27] * data[i-28]; - case 27: sum += qlp_coeff[26] * data[i-27]; - case 26: sum += qlp_coeff[25] * data[i-26]; - case 25: sum += qlp_coeff[24] * data[i-25]; - case 24: sum += qlp_coeff[23] * data[i-24]; - case 23: sum += qlp_coeff[22] * data[i-23]; - case 22: sum += qlp_coeff[21] * data[i-22]; - case 21: sum += qlp_coeff[20] * data[i-21]; - case 20: sum += qlp_coeff[19] * data[i-20]; - case 19: sum += qlp_coeff[18] * data[i-19]; - case 18: sum += qlp_coeff[17] * data[i-18]; - case 17: sum += qlp_coeff[16] * data[i-17]; - case 16: sum += qlp_coeff[15] * data[i-16]; - case 15: sum += qlp_coeff[14] * data[i-15]; - case 14: sum += qlp_coeff[13] * data[i-14]; - case 13: sum += qlp_coeff[12] * data[i-13]; - sum += qlp_coeff[11] * data[i-12]; - sum += qlp_coeff[10] * data[i-11]; - sum += qlp_coeff[ 9] * data[i-10]; - sum += qlp_coeff[ 8] * data[i- 9]; - sum += qlp_coeff[ 7] * data[i- 8]; - sum += qlp_coeff[ 6] * data[i- 7]; - sum += qlp_coeff[ 5] * data[i- 6]; - sum += qlp_coeff[ 4] * data[i- 5]; - sum += qlp_coeff[ 3] * data[i- 4]; - sum += qlp_coeff[ 2] * data[i- 3]; - sum += qlp_coeff[ 1] * data[i- 2]; - sum += qlp_coeff[ 0] * data[i- 1]; - } - data[i] = residual[i] + (sum >> lp_quantization); - } - } -} -#endif - -void FLAC__lpc_restore_signal_wide(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]) -#if defined(FLAC__OVERFLOW_DETECT) || !defined(FLAC__LPC_UNROLLED_FILTER_LOOPS) -{ - unsigned i, j; - FLAC__int64 sum; - const FLAC__int32 *r = residual, *history; - -#ifdef FLAC__OVERFLOW_DETECT_VERBOSE - fprintf(stderr,"FLAC__lpc_restore_signal_wide: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization); - for(i=0;i 0); - - for(i = 0; i < data_len; i++) { - sum = 0; - history = data; - for(j = 0; j < order; j++) - sum += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*(--history)); - if(FLAC__bitmath_silog2_wide(sum >> lp_quantization) > 32) { -#ifdef _MSC_VER - fprintf(stderr,"FLAC__lpc_restore_signal_wide: OVERFLOW, i=%u, sum=%I64d\n", i, sum >> lp_quantization); -#else - fprintf(stderr,"FLAC__lpc_restore_signal_wide: OVERFLOW, i=%u, sum=%lld\n", i, (long long)(sum >> lp_quantization)); -#endif - break; - } - if(FLAC__bitmath_silog2_wide((FLAC__int64)(*r) + (sum >> lp_quantization)) > 32) { -#ifdef _MSC_VER - fprintf(stderr,"FLAC__lpc_restore_signal_wide: OVERFLOW, i=%u, residual=%d, sum=%I64d, data=%I64d\n", i, *r, sum >> lp_quantization, (FLAC__int64)(*r) + (sum >> lp_quantization)); -#else - fprintf(stderr,"FLAC__lpc_restore_signal_wide: OVERFLOW, i=%u, residual=%d, sum=%lld, data=%lld\n", i, *r, (long long)(sum >> lp_quantization), (long long)((FLAC__int64)(*r) + (sum >> lp_quantization))); -#endif - break; - } - *(data++) = *(r++) + (FLAC__int32)(sum >> lp_quantization); - } -} -#else /* fully unrolled version for normal use */ -{ - int i; - FLAC__int64 sum; - - FLAC__ASSERT(order > 0); - FLAC__ASSERT(order <= 32); - - /* - * We do unique versions up to 12th order since that's the subset limit. - * Also they are roughly ordered to match frequency of occurrence to - * minimize branching. - */ - if(order <= 12) { - if(order > 8) { - if(order > 10) { - if(order == 12) { - for(i = 0; i < (int)data_len; i++) { - sum = 0; - sum += qlp_coeff[11] * (FLAC__int64)data[i-12]; - sum += qlp_coeff[10] * (FLAC__int64)data[i-11]; - sum += qlp_coeff[9] * (FLAC__int64)data[i-10]; - sum += qlp_coeff[8] * (FLAC__int64)data[i-9]; - sum += qlp_coeff[7] * (FLAC__int64)data[i-8]; - sum += qlp_coeff[6] * (FLAC__int64)data[i-7]; - sum += qlp_coeff[5] * (FLAC__int64)data[i-6]; - sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; - sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; - sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; - sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; - sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; - data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization); - } - } - else { /* order == 11 */ - for(i = 0; i < (int)data_len; i++) { - sum = 0; - sum += qlp_coeff[10] * (FLAC__int64)data[i-11]; - sum += qlp_coeff[9] * (FLAC__int64)data[i-10]; - sum += qlp_coeff[8] * (FLAC__int64)data[i-9]; - sum += qlp_coeff[7] * (FLAC__int64)data[i-8]; - sum += qlp_coeff[6] * (FLAC__int64)data[i-7]; - sum += qlp_coeff[5] * (FLAC__int64)data[i-6]; - sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; - sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; - sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; - sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; - sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; - data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization); - } - } - } - else { - if(order == 10) { - for(i = 0; i < (int)data_len; i++) { - sum = 0; - sum += qlp_coeff[9] * (FLAC__int64)data[i-10]; - sum += qlp_coeff[8] * (FLAC__int64)data[i-9]; - sum += qlp_coeff[7] * (FLAC__int64)data[i-8]; - sum += qlp_coeff[6] * (FLAC__int64)data[i-7]; - sum += qlp_coeff[5] * (FLAC__int64)data[i-6]; - sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; - sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; - sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; - sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; - sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; - data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization); - } - } - else { /* order == 9 */ - for(i = 0; i < (int)data_len; i++) { - sum = 0; - sum += qlp_coeff[8] * (FLAC__int64)data[i-9]; - sum += qlp_coeff[7] * (FLAC__int64)data[i-8]; - sum += qlp_coeff[6] * (FLAC__int64)data[i-7]; - sum += qlp_coeff[5] * (FLAC__int64)data[i-6]; - sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; - sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; - sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; - sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; - sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; - data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization); - } - } - } - } - else if(order > 4) { - if(order > 6) { - if(order == 8) { - for(i = 0; i < (int)data_len; i++) { - sum = 0; - sum += qlp_coeff[7] * (FLAC__int64)data[i-8]; - sum += qlp_coeff[6] * (FLAC__int64)data[i-7]; - sum += qlp_coeff[5] * (FLAC__int64)data[i-6]; - sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; - sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; - sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; - sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; - sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; - data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization); - } - } - else { /* order == 7 */ - for(i = 0; i < (int)data_len; i++) { - sum = 0; - sum += qlp_coeff[6] * (FLAC__int64)data[i-7]; - sum += qlp_coeff[5] * (FLAC__int64)data[i-6]; - sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; - sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; - sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; - sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; - sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; - data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization); - } - } - } - else { - if(order == 6) { - for(i = 0; i < (int)data_len; i++) { - sum = 0; - sum += qlp_coeff[5] * (FLAC__int64)data[i-6]; - sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; - sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; - sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; - sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; - sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; - data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization); - } - } - else { /* order == 5 */ - for(i = 0; i < (int)data_len; i++) { - sum = 0; - sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; - sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; - sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; - sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; - sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; - data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization); - } - } - } - } - else { - if(order > 2) { - if(order == 4) { - for(i = 0; i < (int)data_len; i++) { - sum = 0; - sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; - sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; - sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; - sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; - data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization); - } - } - else { /* order == 3 */ - for(i = 0; i < (int)data_len; i++) { - sum = 0; - sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; - sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; - sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; - data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization); - } - } - } - else { - if(order == 2) { - for(i = 0; i < (int)data_len; i++) { - sum = 0; - sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; - sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; - data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization); - } - } - else { /* order == 1 */ - for(i = 0; i < (int)data_len; i++) - data[i] = residual[i] + (FLAC__int32)((qlp_coeff[0] * (FLAC__int64)data[i-1]) >> lp_quantization); - } - } - } - } - else { /* order > 12 */ - for(i = 0; i < (int)data_len; i++) { - sum = 0; - switch(order) { - case 32: sum += qlp_coeff[31] * (FLAC__int64)data[i-32]; - case 31: sum += qlp_coeff[30] * (FLAC__int64)data[i-31]; - case 30: sum += qlp_coeff[29] * (FLAC__int64)data[i-30]; - case 29: sum += qlp_coeff[28] * (FLAC__int64)data[i-29]; - case 28: sum += qlp_coeff[27] * (FLAC__int64)data[i-28]; - case 27: sum += qlp_coeff[26] * (FLAC__int64)data[i-27]; - case 26: sum += qlp_coeff[25] * (FLAC__int64)data[i-26]; - case 25: sum += qlp_coeff[24] * (FLAC__int64)data[i-25]; - case 24: sum += qlp_coeff[23] * (FLAC__int64)data[i-24]; - case 23: sum += qlp_coeff[22] * (FLAC__int64)data[i-23]; - case 22: sum += qlp_coeff[21] * (FLAC__int64)data[i-22]; - case 21: sum += qlp_coeff[20] * (FLAC__int64)data[i-21]; - case 20: sum += qlp_coeff[19] * (FLAC__int64)data[i-20]; - case 19: sum += qlp_coeff[18] * (FLAC__int64)data[i-19]; - case 18: sum += qlp_coeff[17] * (FLAC__int64)data[i-18]; - case 17: sum += qlp_coeff[16] * (FLAC__int64)data[i-17]; - case 16: sum += qlp_coeff[15] * (FLAC__int64)data[i-16]; - case 15: sum += qlp_coeff[14] * (FLAC__int64)data[i-15]; - case 14: sum += qlp_coeff[13] * (FLAC__int64)data[i-14]; - case 13: sum += qlp_coeff[12] * (FLAC__int64)data[i-13]; - sum += qlp_coeff[11] * (FLAC__int64)data[i-12]; - sum += qlp_coeff[10] * (FLAC__int64)data[i-11]; - sum += qlp_coeff[ 9] * (FLAC__int64)data[i-10]; - sum += qlp_coeff[ 8] * (FLAC__int64)data[i- 9]; - sum += qlp_coeff[ 7] * (FLAC__int64)data[i- 8]; - sum += qlp_coeff[ 6] * (FLAC__int64)data[i- 7]; - sum += qlp_coeff[ 5] * (FLAC__int64)data[i- 6]; - sum += qlp_coeff[ 4] * (FLAC__int64)data[i- 5]; - sum += qlp_coeff[ 3] * (FLAC__int64)data[i- 4]; - sum += qlp_coeff[ 2] * (FLAC__int64)data[i- 3]; - sum += qlp_coeff[ 1] * (FLAC__int64)data[i- 2]; - sum += qlp_coeff[ 0] * (FLAC__int64)data[i- 1]; - } - data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization); - } - } -} -#endif - -#ifndef FLAC__INTEGER_ONLY_LIBRARY - -FLAC__double FLAC__lpc_compute_expected_bits_per_residual_sample(FLAC__double lpc_error, unsigned total_samples) -{ - FLAC__double error_scale; - - FLAC__ASSERT(total_samples > 0); - - error_scale = 0.5 * M_LN2 * M_LN2 / (FLAC__double)total_samples; - - return FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(lpc_error, error_scale); -} - -FLAC__double FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(FLAC__double lpc_error, FLAC__double error_scale) -{ - if(lpc_error > 0.0) { - FLAC__double bps = (FLAC__double)0.5 * log(error_scale * lpc_error) / M_LN2; - if(bps >= 0.0) - return bps; - else - return 0.0; - } - else if(lpc_error < 0.0) { /* error should not be negative but can happen due to inadequate floating-point resolution */ - return 1e32; - } - else { - return 0.0; - } -} - -unsigned FLAC__lpc_compute_best_order(const FLAC__double lpc_error[], unsigned max_order, unsigned total_samples, unsigned overhead_bits_per_order) -{ - unsigned order, index, best_index; /* 'index' the index into lpc_error; index==order-1 since lpc_error[0] is for order==1, lpc_error[1] is for order==2, etc */ - FLAC__double bits, best_bits, error_scale; - - FLAC__ASSERT(max_order > 0); - FLAC__ASSERT(total_samples > 0); - - error_scale = 0.5 * M_LN2 * M_LN2 / (FLAC__double)total_samples; - - best_index = 0; - best_bits = (unsigned)(-1); - - for(index = 0, order = 1; index < max_order; index++, order++) { - bits = FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(lpc_error[index], error_scale) * (FLAC__double)(total_samples - order) + (FLAC__double)(order * overhead_bits_per_order); - if(bits < best_bits) { - best_index = index; - best_bits = bits; - } - } - - return best_index+1; /* +1 since index of lpc_error[] is order-1 */ -} - -#endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */ diff --git a/FLAC/md5.c b/FLAC/md5.c deleted file mode 100644 index f1558e444..000000000 --- a/FLAC/md5.c +++ /dev/null @@ -1,426 +0,0 @@ -#if HAVE_CONFIG_H -# include -#endif - -#include /* for malloc() */ -#include /* for memcpy() */ - -#include "private/md5.h" -#include "share/alloc.h" - -#define _CRT_SECURE_NO_WARNINGS - -#ifndef FLaC__INLINE -#define FLaC__INLINE -#endif - -/* - * This code implements the MD5 message-digest algorithm. - * The algorithm is due to Ron Rivest. This code was - * written by Colin Plumb in 1993, no copyright is claimed. - * This code is in the public domain; do with it what you wish. - * - * Equivalent code is available from RSA Data Security, Inc. - * This code has been tested against that, and is equivalent, - * except that you don't need to include two pages of legalese - * with every copy. - * - * To compute the message digest of a chunk of bytes, declare an - * MD5Context structure, pass it to MD5Init, call MD5Update as - * needed on buffers full of bytes, and then call MD5Final, which - * will fill a supplied 16-byte array with the digest. - * - * Changed so as no longer to depend on Colin Plumb's `usual.h' header - * definitions; now uses stuff from dpkg's config.h. - * - Ian Jackson . - * Still in the public domain. - * - * Josh Coalson: made some changes to integrate with libFLAC. - * Still in the public domain. - */ - -/* The four core functions - F1 is optimized somewhat */ - -/* #define F1(x, y, z) (x & y | ~x & z) */ -#define F1(x, y, z) (z ^ (x & (y ^ z))) -#define F2(x, y, z) F1(z, x, y) -#define F3(x, y, z) (x ^ y ^ z) -#define F4(x, y, z) (y ^ (x | ~z)) - -/* This is the central step in the MD5 algorithm. */ -#define MD5STEP(f,w,x,y,z,in,s) \ - (w += f(x,y,z) + in, w = (w<>(32-s)) + x) - -/* - * The core of the MD5 algorithm, this alters an existing MD5 hash to - * reflect the addition of 16 longwords of new data. MD5Update blocks - * the data and converts bytes into longwords for this routine. - */ -static void FLAC__MD5Transform(FLAC__uint32 buf[4], FLAC__uint32 const in[16]) -{ - register FLAC__uint32 a, b, c, d; - - a = buf[0]; - b = buf[1]; - c = buf[2]; - d = buf[3]; - - MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7); - MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12); - MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17); - MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22); - MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7); - MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12); - MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17); - MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22); - MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7); - MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12); - MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17); - MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22); - MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7); - MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12); - MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17); - MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22); - - MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5); - MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9); - MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14); - MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20); - MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5); - MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9); - MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14); - MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20); - MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5); - MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9); - MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14); - MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20); - MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5); - MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9); - MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14); - MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20); - - MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4); - MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11); - MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16); - MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23); - MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4); - MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11); - MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16); - MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23); - MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4); - MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11); - MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16); - MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23); - MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4); - MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11); - MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16); - MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23); - - MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6); - MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10); - MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15); - MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21); - MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6); - MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10); - MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15); - MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21); - MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6); - MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10); - MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15); - MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21); - MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6); - MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10); - MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15); - MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21); - - buf[0] += a; - buf[1] += b; - buf[2] += c; - buf[3] += d; -} - -#if WORDS_BIGENDIAN -//@@@@@@ OPT: use bswap/intrinsics -static void byteSwap(FLAC__uint32 *buf, unsigned words) -{ - register FLAC__uint32 x; - do { - x = *buf; - x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); - *buf++ = (x >> 16) | (x << 16); - } while (--words); -} -static void byteSwapX16(FLAC__uint32 *buf) -{ - register FLAC__uint32 x; - - x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16); - x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16); - x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16); - x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16); - x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16); - x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16); - x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16); - x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16); - x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16); - x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16); - x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16); - x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16); - x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16); - x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16); - x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16); - x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf = (x >> 16) | (x << 16); -} -#else -#define byteSwap(buf, words) -#define byteSwapX16(buf) -#endif - -/* - * Update context to reflect the concatenation of another buffer full - * of bytes. - */ -static void FLAC__MD5Update(FLAC__MD5Context *ctx, FLAC__byte const *buf, unsigned len) -{ - FLAC__uint32 t; - - /* Update byte count */ - - t = ctx->bytes[0]; - if ((ctx->bytes[0] = t + len) < t) - ctx->bytes[1]++; /* Carry from low to high */ - - t = 64 - (t & 0x3f); /* Space available in ctx->in (at least 1) */ - if (t > len) { - memcpy((FLAC__byte *)ctx->in + 64 - t, buf, len); - return; - } - /* First chunk is an odd size */ - memcpy((FLAC__byte *)ctx->in + 64 - t, buf, t); - byteSwapX16(ctx->in); - FLAC__MD5Transform(ctx->buf, ctx->in); - buf += t; - len -= t; - - /* Process data in 64-byte chunks */ - while (len >= 64) { - memcpy(ctx->in, buf, 64); - byteSwapX16(ctx->in); - FLAC__MD5Transform(ctx->buf, ctx->in); - buf += 64; - len -= 64; - } - - /* Handle any remaining bytes of data. */ - memcpy(ctx->in, buf, len); -} - -/* - * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious - * initialization constants. - */ -void FLAC__MD5Init(FLAC__MD5Context *ctx) -{ - ctx->buf[0] = 0x67452301; - ctx->buf[1] = 0xefcdab89; - ctx->buf[2] = 0x98badcfe; - ctx->buf[3] = 0x10325476; - - ctx->bytes[0] = 0; - ctx->bytes[1] = 0; - - ctx->internal_buf = 0; - ctx->capacity = 0; -} - -/* - * Final wrapup - pad to 64-byte boundary with the bit pattern - * 1 0* (64-bit count of bits processed, MSB-first) - */ -void FLAC__MD5Final(FLAC__byte digest[16], FLAC__MD5Context *ctx) -{ - int count = ctx->bytes[0] & 0x3f; /* Number of bytes in ctx->in */ - FLAC__byte *p = (FLAC__byte *)ctx->in + count; - - /* Set the first char of padding to 0x80. There is always room. */ - *p++ = 0x80; - - /* Bytes of padding needed to make 56 bytes (-8..55) */ - count = 56 - 1 - count; - - if (count < 0) { /* Padding forces an extra block */ - memset(p, 0, count + 8); - byteSwapX16(ctx->in); - FLAC__MD5Transform(ctx->buf, ctx->in); - p = (FLAC__byte *)ctx->in; - count = 56; - } - memset(p, 0, count); - byteSwap(ctx->in, 14); - - /* Append length in bits and transform */ - ctx->in[14] = ctx->bytes[0] << 3; - ctx->in[15] = ctx->bytes[1] << 3 | ctx->bytes[0] >> 29; - FLAC__MD5Transform(ctx->buf, ctx->in); - - byteSwap(ctx->buf, 4); - memcpy(digest, ctx->buf, 16); - memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */ - if(0 != ctx->internal_buf) { - free(ctx->internal_buf); - ctx->internal_buf = 0; - ctx->capacity = 0; - } -} - -/* - * Convert the incoming audio signal to a byte stream - */ -static void format_input_(FLAC__byte *buf, const FLAC__int32 * const signal[], unsigned channels, unsigned samples, unsigned bytes_per_sample) -{ - unsigned channel, sample; - register FLAC__int32 a_word; - register FLAC__byte *buf_ = buf; - -#if WORDS_BIGENDIAN -#else - if(channels == 2 && bytes_per_sample == 2) { - FLAC__int16 *buf1_ = ((FLAC__int16*)buf_) + 1; - memcpy(buf_, signal[0], sizeof(FLAC__int32) * samples); - for(sample = 0; sample < samples; sample++, buf1_+=2) - *buf1_ = (FLAC__int16)signal[1][sample]; - } - else if(channels == 1 && bytes_per_sample == 2) { - FLAC__int16 *buf1_ = (FLAC__int16*)buf_; - for(sample = 0; sample < samples; sample++) - *buf1_++ = (FLAC__int16)signal[0][sample]; - } - else -#endif - if(bytes_per_sample == 2) { - if(channels == 2) { - for(sample = 0; sample < samples; sample++) { - a_word = signal[0][sample]; - *buf_++ = (FLAC__byte)a_word; a_word >>= 8; - *buf_++ = (FLAC__byte)a_word; - a_word = signal[1][sample]; - *buf_++ = (FLAC__byte)a_word; a_word >>= 8; - *buf_++ = (FLAC__byte)a_word; - } - } - else if(channels == 1) { - for(sample = 0; sample < samples; sample++) { - a_word = signal[0][sample]; - *buf_++ = (FLAC__byte)a_word; a_word >>= 8; - *buf_++ = (FLAC__byte)a_word; - } - } - else { - for(sample = 0; sample < samples; sample++) { - for(channel = 0; channel < channels; channel++) { - a_word = signal[channel][sample]; - *buf_++ = (FLAC__byte)a_word; a_word >>= 8; - *buf_++ = (FLAC__byte)a_word; - } - } - } - } - else if(bytes_per_sample == 3) { - if(channels == 2) { - for(sample = 0; sample < samples; sample++) { - a_word = signal[0][sample]; - *buf_++ = (FLAC__byte)a_word; a_word >>= 8; - *buf_++ = (FLAC__byte)a_word; a_word >>= 8; - *buf_++ = (FLAC__byte)a_word; - a_word = signal[1][sample]; - *buf_++ = (FLAC__byte)a_word; a_word >>= 8; - *buf_++ = (FLAC__byte)a_word; a_word >>= 8; - *buf_++ = (FLAC__byte)a_word; - } - } - else if(channels == 1) { - for(sample = 0; sample < samples; sample++) { - a_word = signal[0][sample]; - *buf_++ = (FLAC__byte)a_word; a_word >>= 8; - *buf_++ = (FLAC__byte)a_word; a_word >>= 8; - *buf_++ = (FLAC__byte)a_word; - } - } - else { - for(sample = 0; sample < samples; sample++) { - for(channel = 0; channel < channels; channel++) { - a_word = signal[channel][sample]; - *buf_++ = (FLAC__byte)a_word; a_word >>= 8; - *buf_++ = (FLAC__byte)a_word; a_word >>= 8; - *buf_++ = (FLAC__byte)a_word; - } - } - } - } - else if(bytes_per_sample == 1) { - if(channels == 2) { - for(sample = 0; sample < samples; sample++) { - a_word = signal[0][sample]; - *buf_++ = (FLAC__byte)a_word; - a_word = signal[1][sample]; - *buf_++ = (FLAC__byte)a_word; - } - } - else if(channels == 1) { - for(sample = 0; sample < samples; sample++) { - a_word = signal[0][sample]; - *buf_++ = (FLAC__byte)a_word; - } - } - else { - for(sample = 0; sample < samples; sample++) { - for(channel = 0; channel < channels; channel++) { - a_word = signal[channel][sample]; - *buf_++ = (FLAC__byte)a_word; - } - } - } - } - else { /* bytes_per_sample == 4, maybe optimize more later */ - for(sample = 0; sample < samples; sample++) { - for(channel = 0; channel < channels; channel++) { - a_word = signal[channel][sample]; - *buf_++ = (FLAC__byte)a_word; a_word >>= 8; - *buf_++ = (FLAC__byte)a_word; a_word >>= 8; - *buf_++ = (FLAC__byte)a_word; a_word >>= 8; - *buf_++ = (FLAC__byte)a_word; - } - } - } -} - -/* - * Convert the incoming audio signal to a byte stream and FLAC__MD5Update it. - */ -FLAC__bool FLAC__MD5Accumulate(FLAC__MD5Context *ctx, const FLAC__int32 * const signal[], unsigned channels, unsigned samples, unsigned bytes_per_sample) -{ - const size_t bytes_needed = (size_t)channels * (size_t)samples * (size_t)bytes_per_sample; - - /* overflow check */ - if((size_t)channels > SIZE_MAX / (size_t)bytes_per_sample) - return false; - if((size_t)channels * (size_t)bytes_per_sample > SIZE_MAX / (size_t)samples) - return false; - - if(ctx->capacity < bytes_needed) { - FLAC__byte *tmp = (FLAC__byte*)realloc(ctx->internal_buf, bytes_needed); - if(0 == tmp) { - free(ctx->internal_buf); - if(0 == (ctx->internal_buf = (FLAC__byte*)safe_malloc_(bytes_needed))) - return false; - } - ctx->internal_buf = tmp; - ctx->capacity = bytes_needed; - } - - format_input_(ctx->internal_buf, signal, channels, samples, bytes_per_sample); - - FLAC__MD5Update(ctx, ctx->internal_buf, (unsigned int)bytes_needed); - - return true; -} diff --git a/FLAC/memory.c b/FLAC/memory.c deleted file mode 100644 index e514e9044..000000000 --- a/FLAC/memory.c +++ /dev/null @@ -1,221 +0,0 @@ -/* libFLAC - Free Lossless Audio Codec library - * Copyright (C) 2001,2002,2003,2004,2005,2006,2007 Josh Coalson - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * - Neither the name of the Xiph.org Foundation nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#if HAVE_CONFIG_H -# include -#endif - -#include "private/memory.h" -#include "FLAC/assert.h" -#include "share/alloc.h" - -void *FLAC__memory_alloc_aligned(size_t bytes, void **aligned_address) -{ - void *x; - - FLAC__ASSERT(0 != aligned_address); - -#ifdef FLAC__ALIGN_MALLOC_DATA - /* align on 32-byte (256-bit) boundary */ - x = safe_malloc_add_2op_(bytes, /*+*/31); -#ifdef SIZEOF_VOIDP -#if SIZEOF_VOIDP == 4 - /* could do *aligned_address = x + ((unsigned) (32 - (((unsigned)x) & 31))) & 31; */ - *aligned_address = (void*)(((unsigned)x + 31) & -32); -#elif SIZEOF_VOIDP == 8 - *aligned_address = (void*)(((FLAC__uint64)x + 31) & (FLAC__uint64)(-((FLAC__int64)32))); -#else -# error Unsupported sizeof(void*) -#endif -#else - /* there's got to be a better way to do this right for all archs */ - if(sizeof(void*) == sizeof(unsigned)) - *aligned_address = (void*)(((unsigned)x + 31) & -32); - else if(sizeof(void*) == sizeof(FLAC__uint64)) - *aligned_address = (void*)(((FLAC__uint64)x + 31) & (FLAC__uint64)(-((FLAC__int64)32))); - else - return 0; -#endif -#else - x = safe_malloc_(bytes); - *aligned_address = x; -#endif - return x; -} - -FLAC__bool FLAC__memory_alloc_aligned_int32_array(unsigned elements, FLAC__int32 **unaligned_pointer, FLAC__int32 **aligned_pointer) -{ - FLAC__int32 *pu; /* unaligned pointer */ - union { /* union needed to comply with C99 pointer aliasing rules */ - FLAC__int32 *pa; /* aligned pointer */ - void *pv; /* aligned pointer alias */ - } u; - - FLAC__ASSERT(elements > 0); - FLAC__ASSERT(0 != unaligned_pointer); - FLAC__ASSERT(0 != aligned_pointer); - FLAC__ASSERT(unaligned_pointer != aligned_pointer); - - if((size_t)elements > SIZE_MAX / sizeof(*pu)) /* overflow check */ - return false; - - pu = (FLAC__int32*)FLAC__memory_alloc_aligned(sizeof(*pu) * (size_t)elements, &u.pv); - if(0 == pu) { - return false; - } - else { - if(*unaligned_pointer != 0) - free(*unaligned_pointer); - *unaligned_pointer = pu; - *aligned_pointer = u.pa; - return true; - } -} - -FLAC__bool FLAC__memory_alloc_aligned_uint32_array(unsigned elements, FLAC__uint32 **unaligned_pointer, FLAC__uint32 **aligned_pointer) -{ - FLAC__uint32 *pu; /* unaligned pointer */ - union { /* union needed to comply with C99 pointer aliasing rules */ - FLAC__uint32 *pa; /* aligned pointer */ - void *pv; /* aligned pointer alias */ - } u; - - FLAC__ASSERT(elements > 0); - FLAC__ASSERT(0 != unaligned_pointer); - FLAC__ASSERT(0 != aligned_pointer); - FLAC__ASSERT(unaligned_pointer != aligned_pointer); - - if((size_t)elements > SIZE_MAX / sizeof(*pu)) /* overflow check */ - return false; - - pu = (FLAC__uint32*)FLAC__memory_alloc_aligned(sizeof(*pu) * elements, &u.pv); - if(0 == pu) { - return false; - } - else { - if(*unaligned_pointer != 0) - free(*unaligned_pointer); - *unaligned_pointer = pu; - *aligned_pointer = u.pa; - return true; - } -} - -FLAC__bool FLAC__memory_alloc_aligned_uint64_array(unsigned elements, FLAC__uint64 **unaligned_pointer, FLAC__uint64 **aligned_pointer) -{ - FLAC__uint64 *pu; /* unaligned pointer */ - union { /* union needed to comply with C99 pointer aliasing rules */ - FLAC__uint64 *pa; /* aligned pointer */ - void *pv; /* aligned pointer alias */ - } u; - - FLAC__ASSERT(elements > 0); - FLAC__ASSERT(0 != unaligned_pointer); - FLAC__ASSERT(0 != aligned_pointer); - FLAC__ASSERT(unaligned_pointer != aligned_pointer); - - if((size_t)elements > SIZE_MAX / sizeof(*pu)) /* overflow check */ - return false; - - pu = (FLAC__uint64*)FLAC__memory_alloc_aligned(sizeof(*pu) * elements, &u.pv); - if(0 == pu) { - return false; - } - else { - if(*unaligned_pointer != 0) - free(*unaligned_pointer); - *unaligned_pointer = pu; - *aligned_pointer = u.pa; - return true; - } -} - -FLAC__bool FLAC__memory_alloc_aligned_unsigned_array(unsigned elements, unsigned **unaligned_pointer, unsigned **aligned_pointer) -{ - unsigned *pu; /* unaligned pointer */ - union { /* union needed to comply with C99 pointer aliasing rules */ - unsigned *pa; /* aligned pointer */ - void *pv; /* aligned pointer alias */ - } u; - - FLAC__ASSERT(elements > 0); - FLAC__ASSERT(0 != unaligned_pointer); - FLAC__ASSERT(0 != aligned_pointer); - FLAC__ASSERT(unaligned_pointer != aligned_pointer); - - if((size_t)elements > SIZE_MAX / sizeof(*pu)) /* overflow check */ - return false; - - pu = (unsigned*)FLAC__memory_alloc_aligned(sizeof(*pu) * elements, &u.pv); - if(0 == pu) { - return false; - } - else { - if(*unaligned_pointer != 0) - free(*unaligned_pointer); - *unaligned_pointer = pu; - *aligned_pointer = u.pa; - return true; - } -} - -#ifndef FLAC__INTEGER_ONLY_LIBRARY - -FLAC__bool FLAC__memory_alloc_aligned_real_array(unsigned elements, FLAC__real **unaligned_pointer, FLAC__real **aligned_pointer) -{ - FLAC__real *pu; /* unaligned pointer */ - union { /* union needed to comply with C99 pointer aliasing rules */ - FLAC__real *pa; /* aligned pointer */ - void *pv; /* aligned pointer alias */ - } u; - - FLAC__ASSERT(elements > 0); - FLAC__ASSERT(0 != unaligned_pointer); - FLAC__ASSERT(0 != aligned_pointer); - FLAC__ASSERT(unaligned_pointer != aligned_pointer); - - if((size_t)elements > SIZE_MAX / sizeof(*pu)) /* overflow check */ - return false; - - pu = (FLAC__real*)FLAC__memory_alloc_aligned(sizeof(*pu) * elements, &u.pv); - if(0 == pu) { - return false; - } - else { - if(*unaligned_pointer != 0) - free(*unaligned_pointer); - *unaligned_pointer = pu; - *aligned_pointer = u.pa; - return true; - } -} - -#endif diff --git a/FLAC/private/bitmath.h b/FLAC/private/bitmath.h deleted file mode 100644 index 529f7a588..000000000 --- a/FLAC/private/bitmath.h +++ /dev/null @@ -1,42 +0,0 @@ -/* libFLAC - Free Lossless Audio Codec library - * Copyright (C) 2001,2002,2003,2004,2005,2006,2007 Josh Coalson - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * - Neither the name of the Xiph.org Foundation nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef FLAC__PRIVATE__BITMATH_H -#define FLAC__PRIVATE__BITMATH_H - -#include "FLAC/ordinals.h" - -unsigned FLAC__bitmath_ilog2(FLAC__uint32 v); -unsigned FLAC__bitmath_ilog2_wide(FLAC__uint64 v); -unsigned FLAC__bitmath_silog2(int v); -unsigned FLAC__bitmath_silog2_wide(FLAC__int64 v); - -#endif diff --git a/FLAC/private/bitreader.h b/FLAC/private/bitreader.h deleted file mode 100644 index fd0f6aac6..000000000 --- a/FLAC/private/bitreader.h +++ /dev/null @@ -1,99 +0,0 @@ -/* libFLAC - Free Lossless Audio Codec library - * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 Josh Coalson - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * - Neither the name of the Xiph.org Foundation nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef FLAC__PRIVATE__BITREADER_H -#define FLAC__PRIVATE__BITREADER_H - -#include /* for FILE */ -#include "FLAC/ordinals.h" -#include "cpu.h" - -/* - * opaque structure definition - */ -struct FLAC__BitReader; -typedef struct FLAC__BitReader FLAC__BitReader; - -typedef FLAC__bool (*FLAC__BitReaderReadCallback)(FLAC__byte buffer[], size_t *bytes, void *client_data); - -/* - * construction, deletion, initialization, etc functions - */ -FLAC__BitReader *FLAC__bitreader_new(void); -void FLAC__bitreader_delete(FLAC__BitReader *br); -FLAC__bool FLAC__bitreader_init(FLAC__BitReader *br, FLAC__CPUInfo cpu, FLAC__BitReaderReadCallback rcb, void *cd); -void FLAC__bitreader_free(FLAC__BitReader *br); /* does not 'free(br)' */ -FLAC__bool FLAC__bitreader_clear(FLAC__BitReader *br); -void FLAC__bitreader_dump(const FLAC__BitReader *br, FILE *out); - -/* - * CRC functions - */ -void FLAC__bitreader_reset_read_crc16(FLAC__BitReader *br, FLAC__uint16 seed); -FLAC__uint16 FLAC__bitreader_get_read_crc16(FLAC__BitReader *br); - -/* - * info functions - */ -FLAC__bool FLAC__bitreader_is_consumed_byte_aligned(const FLAC__BitReader *br); -unsigned FLAC__bitreader_bits_left_for_byte_alignment(const FLAC__BitReader *br); -unsigned FLAC__bitreader_get_input_bits_unconsumed(const FLAC__BitReader *br); - -/* - * read functions - */ - -FLAC__bool FLAC__bitreader_read_raw_uint32(FLAC__BitReader *br, FLAC__uint32 *val, unsigned bits); -FLAC__bool FLAC__bitreader_read_raw_int32(FLAC__BitReader *br, FLAC__int32 *val, unsigned bits); -FLAC__bool FLAC__bitreader_read_raw_uint64(FLAC__BitReader *br, FLAC__uint64 *val, unsigned bits); -FLAC__bool FLAC__bitreader_read_uint32_little_endian(FLAC__BitReader *br, FLAC__uint32 *val); /*only for bits=32*/ -FLAC__bool FLAC__bitreader_skip_bits_no_crc(FLAC__BitReader *br, unsigned bits); /* WATCHOUT: does not CRC the skipped data! */ /*@@@@ add to unit tests */ -FLAC__bool FLAC__bitreader_skip_byte_block_aligned_no_crc(FLAC__BitReader *br, unsigned nvals); /* WATCHOUT: does not CRC the read data! */ -FLAC__bool FLAC__bitreader_read_byte_block_aligned_no_crc(FLAC__BitReader *br, FLAC__byte *val, unsigned nvals); /* WATCHOUT: does not CRC the read data! */ -FLAC__bool FLAC__bitreader_read_unary_unsigned(FLAC__BitReader *br, unsigned *val); -FLAC__bool FLAC__bitreader_read_rice_signed(FLAC__BitReader *br, int *val, unsigned parameter); -FLAC__bool FLAC__bitreader_read_rice_signed_block(FLAC__BitReader *br, int vals[], unsigned nvals, unsigned parameter); -#ifndef FLAC__NO_ASM -# ifdef FLAC__CPU_IA32 -# ifdef FLAC__HAS_NASM -FLAC__bool FLAC__bitreader_read_rice_signed_block_asm_ia32_bswap(FLAC__BitReader *br, int vals[], unsigned nvals, unsigned parameter); -# endif -# endif -#endif -#if 0 /* UNUSED */ -FLAC__bool FLAC__bitreader_read_golomb_signed(FLAC__BitReader *br, int *val, unsigned parameter); -FLAC__bool FLAC__bitreader_read_golomb_unsigned(FLAC__BitReader *br, unsigned *val, unsigned parameter); -#endif -FLAC__bool FLAC__bitreader_read_utf8_uint32(FLAC__BitReader *br, FLAC__uint32 *val, FLAC__byte *raw, unsigned *rawlen); -FLAC__bool FLAC__bitreader_read_utf8_uint64(FLAC__BitReader *br, FLAC__uint64 *val, FLAC__byte *raw, unsigned *rawlen); - -FLAC__bool bitreader_read_from_client_(FLAC__BitReader *br); -#endif diff --git a/FLAC/private/cpu.h b/FLAC/private/cpu.h deleted file mode 100644 index aa0f41443..000000000 --- a/FLAC/private/cpu.h +++ /dev/null @@ -1,88 +0,0 @@ -/* libFLAC - Free Lossless Audio Codec library - * Copyright (C) 2001,2002,2003,2004,2005,2006,2007 Josh Coalson - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * - Neither the name of the Xiph.org Foundation nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef FLAC__PRIVATE__CPU_H -#define FLAC__PRIVATE__CPU_H - -#include "FLAC/ordinals.h" - -#ifdef HAVE_CONFIG_H -#include -#endif - -typedef enum { - FLAC__CPUINFO_TYPE_IA32, - FLAC__CPUINFO_TYPE_PPC, - FLAC__CPUINFO_TYPE_UNKNOWN -} FLAC__CPUInfo_Type; - -typedef struct { - FLAC__bool cpuid; - FLAC__bool bswap; - FLAC__bool cmov; - FLAC__bool mmx; - FLAC__bool fxsr; - FLAC__bool sse; - FLAC__bool sse2; - FLAC__bool sse3; - FLAC__bool ssse3; - FLAC__bool _3dnow; - FLAC__bool ext3dnow; - FLAC__bool extmmx; -} FLAC__CPUInfo_IA32; - -typedef struct { - FLAC__bool altivec; - FLAC__bool ppc64; -} FLAC__CPUInfo_PPC; - -typedef struct { - FLAC__bool use_asm; - FLAC__CPUInfo_Type type; - union { - FLAC__CPUInfo_IA32 ia32; - FLAC__CPUInfo_PPC ppc; - } data; -} FLAC__CPUInfo; - -void FLAC__cpu_info(FLAC__CPUInfo *info); - -#ifndef FLAC__NO_ASM -#ifdef FLAC__CPU_IA32 -#ifdef FLAC__HAS_NASM -FLAC__uint32 FLAC__cpu_have_cpuid_asm_ia32(void); -void FLAC__cpu_info_asm_ia32(FLAC__uint32 *flags_edx, FLAC__uint32 *flags_ecx); -FLAC__uint32 FLAC__cpu_info_extended_amd_asm_ia32(void); -#endif -#endif -#endif - -#endif diff --git a/FLAC/private/crc.h b/FLAC/private/crc.h deleted file mode 100644 index 414c42c60..000000000 --- a/FLAC/private/crc.h +++ /dev/null @@ -1,61 +0,0 @@ -/* libFLAC - Free Lossless Audio Codec library - * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 Josh Coalson - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * - Neither the name of the Xiph.org Foundation nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef FLAC__PRIVATE__CRC_H -#define FLAC__PRIVATE__CRC_H - -#include "FLAC/ordinals.h" - -/* 8 bit CRC generator, MSB shifted first -** polynomial = x^8 + x^2 + x^1 + x^0 -** init = 0 -*/ -extern FLAC__byte const FLAC__crc8_table[256]; -#define FLAC__CRC8_UPDATE(data, crc) (crc) = FLAC__crc8_table[(crc) ^ (data)]; -void FLAC__crc8_update(const FLAC__byte data, FLAC__uint8 *crc); -void FLAC__crc8_update_block(const FLAC__byte *data, unsigned len, FLAC__uint8 *crc); -FLAC__uint8 FLAC__crc8(const FLAC__byte *data, unsigned len); - -/* 16 bit CRC generator, MSB shifted first -** polynomial = x^16 + x^15 + x^2 + x^0 -** init = 0 -*/ -extern unsigned FLAC__crc16_table[256]; - -#define FLAC__CRC16_UPDATE(data, crc) (((((crc)<<8) & 0xffff) ^ FLAC__crc16_table[((crc)>>8) ^ (data)])) -/* this alternate may be faster on some systems/compilers */ -#if 0 -#define FLAC__CRC16_UPDATE(data, crc) ((((crc)<<8) ^ FLAC__crc16_table[((crc)>>8) ^ (data)]) & 0xffff) -#endif - -unsigned FLAC__crc16(const FLAC__byte *data, unsigned len); - -#endif diff --git a/FLAC/private/fixed.h b/FLAC/private/fixed.h deleted file mode 100644 index 98383cf22..000000000 --- a/FLAC/private/fixed.h +++ /dev/null @@ -1,97 +0,0 @@ -/* libFLAC - Free Lossless Audio Codec library - * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 Josh Coalson - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * - Neither the name of the Xiph.org Foundation nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef FLAC__PRIVATE__FIXED_H -#define FLAC__PRIVATE__FIXED_H - -#ifdef HAVE_CONFIG_H -#include -#endif - -#include "private/float.h" -#include "FLAC/format.h" - -/* - * FLAC__fixed_compute_best_predictor() - * -------------------------------------------------------------------- - * Compute the best fixed predictor and the expected bits-per-sample - * of the residual signal for each order. The _wide() version uses - * 64-bit integers which is statistically necessary when bits-per- - * sample + log2(blocksize) > 30 - * - * IN data[0,data_len-1] - * IN data_len - * OUT residual_bits_per_sample[0,FLAC__MAX_FIXED_ORDER] - */ -#ifndef FLAC__INTEGER_ONLY_LIBRARY -unsigned FLAC__fixed_compute_best_predictor(const FLAC__int32 data[], unsigned data_len, FLAC__float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]); -# ifndef FLAC__NO_ASM -# ifdef FLAC__CPU_IA32 -# ifdef FLAC__HAS_NASM -unsigned FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov(const FLAC__int32 data[], unsigned data_len, FLAC__float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]); -# endif -# endif -# endif -unsigned FLAC__fixed_compute_best_predictor_wide(const FLAC__int32 data[], unsigned data_len, FLAC__float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]); -#else -unsigned FLAC__fixed_compute_best_predictor(const FLAC__int32 data[], unsigned data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]); -unsigned FLAC__fixed_compute_best_predictor_wide(const FLAC__int32 data[], unsigned data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]); -#endif - -/* - * FLAC__fixed_compute_residual() - * -------------------------------------------------------------------- - * Compute the residual signal obtained from sutracting the predicted - * signal from the original. - * - * IN data[-order,data_len-1] original signal (NOTE THE INDICES!) - * IN data_len length of original signal - * IN order <= FLAC__MAX_FIXED_ORDER fixed-predictor order - * OUT residual[0,data_len-1] residual signal - */ -void FLAC__fixed_compute_residual(const FLAC__int32 data[], unsigned data_len, unsigned order, FLAC__int32 residual[]); - -/* - * FLAC__fixed_restore_signal() - * -------------------------------------------------------------------- - * Restore the original signal by summing the residual and the - * predictor. - * - * IN residual[0,data_len-1] residual signal - * IN data_len length of original signal - * IN order <= FLAC__MAX_FIXED_ORDER fixed-predictor order - * *** IMPORTANT: the caller must pass in the historical samples: - * IN data[-order,-1] previously-reconstructed historical samples - * OUT data[0,data_len-1] original signal - */ -void FLAC__fixed_restore_signal(const FLAC__int32 residual[], unsigned data_len, unsigned order, FLAC__int32 data[]); - -#endif diff --git a/FLAC/private/float.h b/FLAC/private/float.h deleted file mode 100644 index 73313f6db..000000000 --- a/FLAC/private/float.h +++ /dev/null @@ -1,97 +0,0 @@ -/* libFLAC - Free Lossless Audio Codec library - * Copyright (C) 2004,2005,2006,2007 Josh Coalson - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * - Neither the name of the Xiph.org Foundation nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef FLAC__PRIVATE__FLOAT_H -#define FLAC__PRIVATE__FLOAT_H - -#ifdef HAVE_CONFIG_H -#include -#endif - -#include "FLAC/ordinals.h" - -/* - * These typedefs make it easier to ensure that integer versions of - * the library really only contain integer operations. All the code - * in libFLAC should use FLAC__float and FLAC__double in place of - * float and double, and be protected by checks of the macro - * FLAC__INTEGER_ONLY_LIBRARY. - * - * FLAC__real is the basic floating point type used in LPC analysis. - */ -#ifndef FLAC__INTEGER_ONLY_LIBRARY -typedef double FLAC__double; -typedef float FLAC__float; -/* - * WATCHOUT: changing FLAC__real will change the signatures of many - * functions that have assembly language equivalents and break them. - */ -typedef float FLAC__real; -#else -/* - * The convention for FLAC__fixedpoint is to use the upper 16 bits - * for the integer part and lower 16 bits for the fractional part. - */ -typedef FLAC__int32 FLAC__fixedpoint; -extern const FLAC__fixedpoint FLAC__FP_ZERO; -extern const FLAC__fixedpoint FLAC__FP_ONE_HALF; -extern const FLAC__fixedpoint FLAC__FP_ONE; -extern const FLAC__fixedpoint FLAC__FP_LN2; -extern const FLAC__fixedpoint FLAC__FP_E; - -#define FLAC__fixedpoint_trunc(x) ((x)>>16) - -#define FLAC__fixedpoint_mul(x, y) ( (FLAC__fixedpoint) ( ((FLAC__int64)(x)*(FLAC__int64)(y)) >> 16 ) ) - -#define FLAC__fixedpoint_div(x, y) ( (FLAC__fixedpoint) ( ( ((FLAC__int64)(x)<<32) / (FLAC__int64)(y) ) >> 16 ) ) - -/* - * FLAC__fixedpoint_log2() - * -------------------------------------------------------------------- - * Returns the base-2 logarithm of the fixed-point number 'x' using an - * algorithm by Knuth for x >= 1.0 - * - * 'fracbits' is the number of fractional bits of 'x'. 'fracbits' must - * be < 32 and evenly divisible by 4 (0 is OK but not very precise). - * - * 'precision' roughly limits the number of iterations that are done; - * use (unsigned)(-1) for maximum precision. - * - * If 'x' is less than one -- that is, x < (1< -#endif - -#include "private/float.h" -#include "FLAC/format.h" - -#ifndef FLAC__INTEGER_ONLY_LIBRARY - -/* - * FLAC__lpc_window_data() - * -------------------------------------------------------------------- - * Applies the given window to the data. - * OPT: asm implementation - * - * IN in[0,data_len-1] - * IN window[0,data_len-1] - * OUT out[0,lag-1] - * IN data_len - */ -void FLAC__lpc_window_data(const FLAC__int32 in[], const FLAC__real window[], FLAC__real out[], unsigned data_len); - -/* - * FLAC__lpc_compute_autocorrelation() - * -------------------------------------------------------------------- - * Compute the autocorrelation for lags between 0 and lag-1. - * Assumes data[] outside of [0,data_len-1] == 0. - * Asserts that lag > 0. - * - * IN data[0,data_len-1] - * IN data_len - * IN 0 < lag <= data_len - * OUT autoc[0,lag-1] - */ -void FLAC__lpc_compute_autocorrelation(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]); -#ifndef FLAC__NO_ASM -# ifdef FLAC__CPU_IA32 -# ifdef FLAC__HAS_NASM -void FLAC__lpc_compute_autocorrelation_asm_ia32(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]); -void FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]); -void FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]); -void FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]); -void FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]); -# endif -# endif -#endif - -/* - * FLAC__lpc_compute_lp_coefficients() - * -------------------------------------------------------------------- - * Computes LP coefficients for orders 1..max_order. - * Do not call if autoc[0] == 0.0. This means the signal is zero - * and there is no point in calculating a predictor. - * - * IN autoc[0,max_order] autocorrelation values - * IN 0 < max_order <= FLAC__MAX_LPC_ORDER max LP order to compute - * OUT lp_coeff[0,max_order-1][0,max_order-1] LP coefficients for each order - * *** IMPORTANT: - * *** lp_coeff[0,max_order-1][max_order,FLAC__MAX_LPC_ORDER-1] are untouched - * OUT error[0,max_order-1] error for each order (more - * specifically, the variance of - * the error signal times # of - * samples in the signal) - * - * Example: if max_order is 9, the LP coefficients for order 9 will be - * in lp_coeff[8][0,8], the LP coefficients for order 8 will be - * in lp_coeff[7][0,7], etc. - */ -void FLAC__lpc_compute_lp_coefficients(const FLAC__real autoc[], unsigned *max_order, FLAC__real lp_coeff[][FLAC__MAX_LPC_ORDER], FLAC__double error[]); - -/* - * FLAC__lpc_quantize_coefficients() - * -------------------------------------------------------------------- - * Quantizes the LP coefficients. NOTE: precision + bits_per_sample - * must be less than 32 (sizeof(FLAC__int32)*8). - * - * IN lp_coeff[0,order-1] LP coefficients - * IN order LP order - * IN FLAC__MIN_QLP_COEFF_PRECISION < precision - * desired precision (in bits, including sign - * bit) of largest coefficient - * OUT qlp_coeff[0,order-1] quantized coefficients - * OUT shift # of bits to shift right to get approximated - * LP coefficients. NOTE: could be negative. - * RETURN 0 => quantization OK - * 1 => coefficients require too much shifting for *shift to - * fit in the LPC subframe header. 'shift' is unset. - * 2 => coefficients are all zero, which is bad. 'shift' is - * unset. - */ -int FLAC__lpc_quantize_coefficients(const FLAC__real lp_coeff[], unsigned order, unsigned precision, FLAC__int32 qlp_coeff[], int *shift); - -/* - * FLAC__lpc_compute_residual_from_qlp_coefficients() - * -------------------------------------------------------------------- - * Compute the residual signal obtained from sutracting the predicted - * signal from the original. - * - * IN data[-order,data_len-1] original signal (NOTE THE INDICES!) - * IN data_len length of original signal - * IN qlp_coeff[0,order-1] quantized LP coefficients - * IN order > 0 LP order - * IN lp_quantization quantization of LP coefficients in bits - * OUT residual[0,data_len-1] residual signal - */ -void FLAC__lpc_compute_residual_from_qlp_coefficients(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]); -void FLAC__lpc_compute_residual_from_qlp_coefficients_wide(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]); -#ifndef FLAC__NO_ASM -# ifdef FLAC__CPU_IA32 -# ifdef FLAC__HAS_NASM -void FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]); -void FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]); -# endif -# endif -#endif - -#endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */ - -/* - * FLAC__lpc_restore_signal() - * -------------------------------------------------------------------- - * Restore the original signal by summing the residual and the - * predictor. - * - * IN residual[0,data_len-1] residual signal - * IN data_len length of original signal - * IN qlp_coeff[0,order-1] quantized LP coefficients - * IN order > 0 LP order - * IN lp_quantization quantization of LP coefficients in bits - * *** IMPORTANT: the caller must pass in the historical samples: - * IN data[-order,-1] previously-reconstructed historical samples - * OUT data[0,data_len-1] original signal - */ -void FLAC__lpc_restore_signal(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]); -void FLAC__lpc_restore_signal_wide(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]); -#ifndef FLAC__NO_ASM -# ifdef FLAC__CPU_IA32 -# ifdef FLAC__HAS_NASM -void FLAC__lpc_restore_signal_asm_ia32(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]); -void FLAC__lpc_restore_signal_asm_ia32_mmx(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]); -# endif /* FLAC__HAS_NASM */ -# elif defined FLAC__CPU_PPC -void FLAC__lpc_restore_signal_asm_ppc_altivec_16(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]); -void FLAC__lpc_restore_signal_asm_ppc_altivec_16_order8(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]); -# endif/* FLAC__CPU_IA32 || FLAC__CPU_PPC */ -#endif /* FLAC__NO_ASM */ - -#ifndef FLAC__INTEGER_ONLY_LIBRARY - -/* - * FLAC__lpc_compute_expected_bits_per_residual_sample() - * -------------------------------------------------------------------- - * Compute the expected number of bits per residual signal sample - * based on the LP error (which is related to the residual variance). - * - * IN lpc_error >= 0.0 error returned from calculating LP coefficients - * IN total_samples > 0 # of samples in residual signal - * RETURN expected bits per sample - */ -FLAC__double FLAC__lpc_compute_expected_bits_per_residual_sample(FLAC__double lpc_error, unsigned total_samples); -FLAC__double FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(FLAC__double lpc_error, FLAC__double error_scale); - -/* - * FLAC__lpc_compute_best_order() - * -------------------------------------------------------------------- - * Compute the best order from the array of signal errors returned - * during coefficient computation. - * - * IN lpc_error[0,max_order-1] >= 0.0 error returned from calculating LP coefficients - * IN max_order > 0 max LP order - * IN total_samples > 0 # of samples in residual signal - * IN overhead_bits_per_order # of bits overhead for each increased LP order - * (includes warmup sample size and quantized LP coefficient) - * RETURN [1,max_order] best order - */ -unsigned FLAC__lpc_compute_best_order(const FLAC__double lpc_error[], unsigned max_order, unsigned total_samples, unsigned overhead_bits_per_order); - -#endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */ - -#endif diff --git a/FLAC/private/md5.h b/FLAC/private/md5.h deleted file mode 100644 index e5f675a83..000000000 --- a/FLAC/private/md5.h +++ /dev/null @@ -1,44 +0,0 @@ -#ifndef FLAC__PRIVATE__MD5_H -#define FLAC__PRIVATE__MD5_H - -/* - * This is the header file for the MD5 message-digest algorithm. - * The algorithm is due to Ron Rivest. This code was - * written by Colin Plumb in 1993, no copyright is claimed. - * This code is in the public domain; do with it what you wish. - * - * Equivalent code is available from RSA Data Security, Inc. - * This code has been tested against that, and is equivalent, - * except that you don't need to include two pages of legalese - * with every copy. - * - * To compute the message digest of a chunk of bytes, declare an - * MD5Context structure, pass it to MD5Init, call MD5Update as - * needed on buffers full of bytes, and then call MD5Final, which - * will fill a supplied 16-byte array with the digest. - * - * Changed so as no longer to depend on Colin Plumb's `usual.h' - * header definitions; now uses stuff from dpkg's config.h - * - Ian Jackson . - * Still in the public domain. - * - * Josh Coalson: made some changes to integrate with libFLAC. - * Still in the public domain, with no warranty. - */ - -#include "FLAC/ordinals.h" - -typedef struct { - FLAC__uint32 in[16]; - FLAC__uint32 buf[4]; - FLAC__uint32 bytes[2]; - FLAC__byte *internal_buf; - size_t capacity; -} FLAC__MD5Context; - -void FLAC__MD5Init(FLAC__MD5Context *context); -void FLAC__MD5Final(FLAC__byte digest[16], FLAC__MD5Context *context); - -FLAC__bool FLAC__MD5Accumulate(FLAC__MD5Context *ctx, const FLAC__int32 * const signal[], unsigned channels, unsigned samples, unsigned bytes_per_sample); - -#endif diff --git a/FLAC/private/memory.h b/FLAC/private/memory.h deleted file mode 100644 index d39e48d95..000000000 --- a/FLAC/private/memory.h +++ /dev/null @@ -1,56 +0,0 @@ -/* libFLAC - Free Lossless Audio Codec library - * Copyright (C) 2001,2002,2003,2004,2005,2006,2007 Josh Coalson - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * - Neither the name of the Xiph.org Foundation nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef FLAC__PRIVATE__MEMORY_H -#define FLAC__PRIVATE__MEMORY_H - -#ifdef HAVE_CONFIG_H -#include -#endif - -#include /* for size_t */ - -#include "private/float.h" -#include "FLAC/ordinals.h" /* for FLAC__bool */ - -/* Returns the unaligned address returned by malloc. - * Use free() on this address to deallocate. - */ -void *FLAC__memory_alloc_aligned(size_t bytes, void **aligned_address); -FLAC__bool FLAC__memory_alloc_aligned_int32_array(unsigned elements, FLAC__int32 **unaligned_pointer, FLAC__int32 **aligned_pointer); -FLAC__bool FLAC__memory_alloc_aligned_uint32_array(unsigned elements, FLAC__uint32 **unaligned_pointer, FLAC__uint32 **aligned_pointer); -FLAC__bool FLAC__memory_alloc_aligned_uint64_array(unsigned elements, FLAC__uint64 **unaligned_pointer, FLAC__uint64 **aligned_pointer); -FLAC__bool FLAC__memory_alloc_aligned_unsigned_array(unsigned elements, unsigned **unaligned_pointer, unsigned **aligned_pointer); -#ifndef FLAC__INTEGER_ONLY_LIBRARY -FLAC__bool FLAC__memory_alloc_aligned_real_array(unsigned elements, FLAC__real **unaligned_pointer, FLAC__real **aligned_pointer); -#endif - -#endif diff --git a/FLAC/protected/stream_decoder.h b/FLAC/protected/stream_decoder.h deleted file mode 100644 index 42f542b37..000000000 --- a/FLAC/protected/stream_decoder.h +++ /dev/null @@ -1,58 +0,0 @@ -/* libFLAC - Free Lossless Audio Codec library - * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 Josh Coalson - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * - Neither the name of the Xiph.org Foundation nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef FLAC__PROTECTED__STREAM_DECODER_H -#define FLAC__PROTECTED__STREAM_DECODER_H - -#include "FLAC/stream_decoder.h" -#if FLAC__HAS_OGG -#include "private/ogg_decoder_aspect.h" -#endif - -typedef struct FLAC__StreamDecoderProtected { - FLAC__StreamDecoderState state; - unsigned channels; - FLAC__ChannelAssignment channel_assignment; - unsigned bits_per_sample; - unsigned sample_rate; /* in Hz */ - unsigned blocksize; /* in samples (per channel) */ - FLAC__bool md5_checking; /* if true, generate MD5 signature of decoded data and compare against signature in the STREAMINFO metadata block */ -#if FLAC__HAS_OGG - FLAC__OggDecoderAspect ogg_decoder_aspect; -#endif -} FLAC__StreamDecoderProtected; - -/* - * return the number of input bytes consumed - */ -unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder); - -#endif diff --git a/FLAC/share/alloc.h b/FLAC/share/alloc.h deleted file mode 100644 index 73ac0d246..000000000 --- a/FLAC/share/alloc.h +++ /dev/null @@ -1,214 +0,0 @@ -/* alloc - Convenience routines for safely allocating memory - * Copyright (C) 2007 Josh Coalson - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#ifndef FLAC__SHARE__ALLOC_H -#define FLAC__SHARE__ALLOC_H - -#if HAVE_CONFIG_H -# include -#endif - -/* WATCHOUT: for c++ you may have to #define __STDC_LIMIT_MACROS 1 real early - * before #including this file, otherwise SIZE_MAX might not be defined - */ - -#include /* for SIZE_MAX */ -#if !defined _MSC_VER && !defined __MINGW32__ && !defined __EMX__ -#include /* for SIZE_MAX in case limits.h didn't get it */ -#endif -#include /* for size_t, malloc(), etc */ - -#ifndef SIZE_T_MAX -# ifdef _WIN64 -# define SIZE_T_MAX ULLONG_MAX -# elif defined(_WIN32) -# define SIZE_T_MAX UINT_MAX /* Why doesn't the standard MinGW distribution define this? */ -# else -# error -# endif -#endif -#ifndef SIZE_MAX -# define SIZE_MAX SIZE_T_MAX -#endif - -#ifndef FLaC__INLINE -#define FLaC__INLINE -#endif - -/* avoid malloc()ing 0 bytes, see: - * https://www.securecoding.cert.org/confluence/display/seccode/MEM04-A.+Do+not+make+assumptions+about+the+result+of+allocating+0+bytes?focusedCommentId=5407003 -*/ -static FLaC__INLINE void *safe_malloc_(size_t size) -{ - /* malloc(0) is undefined; FLAC src convention is to always allocate */ - if(!size) - size++; - return malloc(size); -} - -static FLaC__INLINE void *safe_calloc_(size_t nmemb, size_t size) -{ - if(!nmemb || !size) - return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */ - return calloc(nmemb, size); -} - -/*@@@@ there's probably a better way to prevent overflows when allocating untrusted sums but this works for now */ - -static FLaC__INLINE void *safe_malloc_add_2op_(size_t size1, size_t size2) -{ - size2 += size1; - if(size2 < size1) - return 0; - return safe_malloc_(size2); -} - -static FLaC__INLINE void *safe_malloc_add_3op_(size_t size1, size_t size2, size_t size3) -{ - size2 += size1; - if(size2 < size1) - return 0; - size3 += size2; - if(size3 < size2) - return 0; - return safe_malloc_(size3); -} - -static FLaC__INLINE void *safe_malloc_add_4op_(size_t size1, size_t size2, size_t size3, size_t size4) -{ - size2 += size1; - if(size2 < size1) - return 0; - size3 += size2; - if(size3 < size2) - return 0; - size4 += size3; - if(size4 < size3) - return 0; - return safe_malloc_(size4); -} - -static FLaC__INLINE void *safe_malloc_mul_2op_(size_t size1, size_t size2) -#if 0 -needs support for cases where sizeof(size_t) != 4 -{ - /* could be faster #ifdef'ing off SIZEOF_SIZE_T */ - if(sizeof(size_t) == 4) { - if ((double)size1 * (double)size2 < 4294967296.0) - return malloc(size1*size2); - } - return 0; -} -#else -/* better? */ -{ - if(!size1 || !size2) - return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */ - if(size1 > SIZE_MAX / size2) - return 0; - return malloc(size1*size2); -} -#endif - -static FLaC__INLINE void *safe_malloc_mul_3op_(size_t size1, size_t size2, size_t size3) -{ - if(!size1 || !size2 || !size3) - return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */ - if(size1 > SIZE_MAX / size2) - return 0; - size1 *= size2; - if(size1 > SIZE_MAX / size3) - return 0; - return malloc(size1*size3); -} - -/* size1*size2 + size3 */ -static FLaC__INLINE void *safe_malloc_mul2add_(size_t size1, size_t size2, size_t size3) -{ - if(!size1 || !size2) - return safe_malloc_(size3); - if(size1 > SIZE_MAX / size2) - return 0; - return safe_malloc_add_2op_(size1*size2, size3); -} - -/* size1 * (size2 + size3) */ -static FLaC__INLINE void *safe_malloc_muladd2_(size_t size1, size_t size2, size_t size3) -{ - if(!size1 || (!size2 && !size3)) - return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */ - size2 += size3; - if(size2 < size3) - return 0; - return safe_malloc_mul_2op_(size1, size2); -} - -static FLaC__INLINE void *safe_realloc_add_2op_(void *ptr, size_t size1, size_t size2) -{ - size2 += size1; - if(size2 < size1) - return 0; - return realloc(ptr, size2); -} - -static FLaC__INLINE void *safe_realloc_add_3op_(void *ptr, size_t size1, size_t size2, size_t size3) -{ - size2 += size1; - if(size2 < size1) - return 0; - size3 += size2; - if(size3 < size2) - return 0; - return realloc(ptr, size3); -} - -static FLaC__INLINE void *safe_realloc_add_4op_(void *ptr, size_t size1, size_t size2, size_t size3, size_t size4) -{ - size2 += size1; - if(size2 < size1) - return 0; - size3 += size2; - if(size3 < size2) - return 0; - size4 += size3; - if(size4 < size3) - return 0; - return realloc(ptr, size4); -} - -static FLaC__INLINE void *safe_realloc_mul_2op_(void *ptr, size_t size1, size_t size2) -{ - if(!size1 || !size2) - return realloc(ptr, 0); /* preserve POSIX realloc(ptr, 0) semantics */ - if(size1 > SIZE_MAX / size2) - return 0; - return realloc(ptr, size1*size2); -} - -/* size1 * (size2 + size3) */ -static FLaC__INLINE void *safe_realloc_muladd2_(void *ptr, size_t size1, size_t size2, size_t size3) -{ - if(!size1 || (!size2 && !size3)) - return realloc(ptr, 0); /* preserve POSIX realloc(ptr, 0) semantics */ - size2 += size3; - if(size2 < size3) - return 0; - return safe_realloc_mul_2op_(ptr, size1, size2); -} - -#endif diff --git a/FLAC/stream_decoder.c b/FLAC/stream_decoder.c deleted file mode 100644 index 1751e50f8..000000000 --- a/FLAC/stream_decoder.c +++ /dev/null @@ -1,3388 +0,0 @@ -/* libFLAC - Free Lossless Audio Codec library - * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 Josh Coalson - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * - Neither the name of the Xiph.org Foundation nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#if HAVE_CONFIG_H -# include -#endif - -#if defined _MSC_VER || defined __MINGW32__ -#define _CRT_SECURE_NO_WARNINGS -#include /* for _setmode() */ -#include /* for _O_BINARY */ -#endif -#if defined __CYGWIN__ || defined __EMX__ -#include /* for setmode(), O_BINARY */ -#include /* for _O_BINARY */ -#endif -#include -#include /* for malloc() */ -#include /* for memset/memcpy() */ -#include /* for stat() */ -#include /* for off_t */ -#if defined _MSC_VER || defined __BORLANDC__ || defined __MINGW32__ -#if _MSC_VER <= 1600 || defined __BORLANDC__ /* @@@ [2G limit] */ -#define fseeko fseek -#define ftello ftell -#endif -#endif -#include "FLAC/assert.h" -#include "share/alloc.h" -#include "protected/stream_decoder.h" -#include "private/bitreader.h" -#include "private/bitmath.h" -#include "private/cpu.h" -#include "private/crc.h" -#include "private/fixed.h" -#include "private/format.h" -#include "private/lpc.h" -#include "private/md5.h" -#include "private/memory.h" - -#ifdef max -#undef max -#endif -#define max(a,b) ((a)>(b)?(a):(b)) - -/* adjust for compilers that can't understand using LLU suffix for uint64_t literals */ -#ifdef _MSC_VER -#define FLAC__U64L(x) x -#else -#define FLAC__U64L(x) x##LLU -#endif - - -/* technically this should be in an "export.c" but this is convenient enough */ -FLAC_API int FLAC_API_SUPPORTS_OGG_FLAC = -#if FLAC__HAS_OGG - 1 -#else - 0 -#endif -; - - -/*********************************************************************** - * - * Private static data - * - ***********************************************************************/ - -static FLAC__byte ID3V2_TAG_[3] = { 'I', 'D', '3' }; - -/*********************************************************************** - * - * Private class method prototypes - * - ***********************************************************************/ - -static void set_defaults_(FLAC__StreamDecoder *decoder); -static FILE *get_binary_stdin_(void); -static FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels); -static FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id); -static FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder); -static FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder); -static FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length); -static FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length); -static FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj); -static FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj); -static FLAC__bool read_metadata_picture_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_Picture *obj); -static FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder); -static FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder); -static FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FLAC__bool do_full_decode); -static FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder); -static FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode); -static FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode); -static FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode); -static FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode); -static FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode); -static FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, FLAC__int32 *residual, FLAC__bool is_extended); -static FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder); -static FLAC__bool read_callback_(FLAC__byte buffer[], size_t *bytes, void *client_data); -#if FLAC__HAS_OGG -static FLAC__StreamDecoderReadStatus read_callback_ogg_aspect_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes); -static FLAC__OggDecoderAspectReadStatus read_callback_proxy_(const void *void_decoder, FLAC__byte buffer[], size_t *bytes, void *client_data); -#endif -static FLAC__StreamDecoderWriteStatus write_audio_frame_to_client_(FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[]); -static void send_error_to_client_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status); -static FLAC__bool seek_to_absolute_sample_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample); -#if FLAC__HAS_OGG -static FLAC__bool seek_to_absolute_sample_ogg_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample); -#endif -static FLAC__StreamDecoderReadStatus file_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data); -static FLAC__StreamDecoderSeekStatus file_seek_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data); -static FLAC__StreamDecoderTellStatus file_tell_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data); -static FLAC__StreamDecoderLengthStatus file_length_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data); -static FLAC__bool file_eof_callback_(const FLAC__StreamDecoder *decoder, void *client_data); - -/*********************************************************************** - * - * Private class data - * - ***********************************************************************/ - -typedef struct FLAC__StreamDecoderPrivate { -#if FLAC__HAS_OGG - FLAC__bool is_ogg; -#endif - FLAC__StreamDecoderReadCallback read_callback; - FLAC__StreamDecoderSeekCallback seek_callback; - FLAC__StreamDecoderTellCallback tell_callback; - FLAC__StreamDecoderLengthCallback length_callback; - FLAC__StreamDecoderEofCallback eof_callback; - FLAC__StreamDecoderWriteCallback write_callback; - FLAC__StreamDecoderMetadataCallback metadata_callback; - FLAC__StreamDecoderErrorCallback error_callback; - /* generic 32-bit datapath: */ - void (*local_lpc_restore_signal)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]); - /* generic 64-bit datapath: */ - void (*local_lpc_restore_signal_64bit)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]); - /* for use when the signal is <= 16 bits-per-sample, or <= 15 bits-per-sample on a side channel (which requires 1 extra bit): */ - void (*local_lpc_restore_signal_16bit)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]); - /* for use when the signal is <= 16 bits-per-sample, or <= 15 bits-per-sample on a side channel (which requires 1 extra bit), AND order <= 8: */ - void (*local_lpc_restore_signal_16bit_order8)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]); - FLAC__bool (*local_bitreader_read_rice_signed_block)(FLAC__BitReader *br, int vals[], unsigned nvals, unsigned parameter); - void *client_data; - FILE *file; /* only used if FLAC__stream_decoder_init_file()/FLAC__stream_decoder_init_file() called, else NULL */ - FLAC__BitReader *input; - FLAC__int32 *output[FLAC__MAX_CHANNELS]; - FLAC__int32 *residual[FLAC__MAX_CHANNELS]; /* WATCHOUT: these are the aligned pointers; the real pointers that should be free()'d are residual_unaligned[] below */ - FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents[FLAC__MAX_CHANNELS]; - unsigned output_capacity, output_channels; - FLAC__uint32 fixed_block_size, next_fixed_block_size; - FLAC__uint64 samples_decoded; - FLAC__bool has_stream_info, has_seek_table; - FLAC__StreamMetadata stream_info; - FLAC__StreamMetadata seek_table; - FLAC__bool metadata_filter[128]; /* MAGIC number 128 == total number of metadata block types == 1 << 7 */ - FLAC__byte *metadata_filter_ids; - size_t metadata_filter_ids_count, metadata_filter_ids_capacity; /* units for both are IDs, not bytes */ - FLAC__Frame frame; - FLAC__bool cached; /* true if there is a byte in lookahead */ - FLAC__CPUInfo cpuinfo; - FLAC__byte header_warmup[2]; /* contains the sync code and reserved bits */ - FLAC__byte lookahead; /* temp storage when we need to look ahead one byte in the stream */ - /* unaligned (original) pointers to allocated data */ - FLAC__int32 *residual_unaligned[FLAC__MAX_CHANNELS]; - FLAC__bool do_md5_checking; /* initially gets protected_->md5_checking but is turned off after a seek or if the metadata has a zero MD5 */ - FLAC__bool internal_reset_hack; /* used only during init() so we can call reset to set up the decoder without rewinding the input */ - FLAC__bool is_seeking; - FLAC__MD5Context md5context; - FLAC__byte computed_md5sum[16]; /* this is the sum we computed from the decoded data */ - /* (the rest of these are only used for seeking) */ - FLAC__Frame last_frame; /* holds the info of the last frame we seeked to */ - FLAC__uint64 first_frame_offset; /* hint to the seek routine of where in the stream the first audio frame starts */ - FLAC__uint64 target_sample; - unsigned unparseable_frame_count; /* used to tell whether we're decoding a future version of FLAC or just got a bad sync */ -#if FLAC__HAS_OGG - FLAC__bool got_a_frame; /* hack needed in Ogg FLAC seek routine to check when process_single() actually writes a frame */ -#endif -} FLAC__StreamDecoderPrivate; - -/*********************************************************************** - * - * Public static class data - * - ***********************************************************************/ - -FLAC_API const char * const FLAC__StreamDecoderStateString[] = { - "FLAC__STREAM_DECODER_SEARCH_FOR_METADATA", - "FLAC__STREAM_DECODER_READ_METADATA", - "FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC", - "FLAC__STREAM_DECODER_READ_FRAME", - "FLAC__STREAM_DECODER_END_OF_STREAM", - "FLAC__STREAM_DECODER_OGG_ERROR", - "FLAC__STREAM_DECODER_SEEK_ERROR", - "FLAC__STREAM_DECODER_ABORTED", - "FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR", - "FLAC__STREAM_DECODER_UNINITIALIZED" -}; - -FLAC_API const char * const FLAC__StreamDecoderInitStatusString[] = { - "FLAC__STREAM_DECODER_INIT_STATUS_OK", - "FLAC__STREAM_DECODER_INIT_STATUS_UNSUPPORTED_CONTAINER", - "FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS", - "FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR", - "FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE", - "FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED" -}; - -FLAC_API const char * const FLAC__StreamDecoderReadStatusString[] = { - "FLAC__STREAM_DECODER_READ_STATUS_CONTINUE", - "FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM", - "FLAC__STREAM_DECODER_READ_STATUS_ABORT" -}; - -FLAC_API const char * const FLAC__StreamDecoderSeekStatusString[] = { - "FLAC__STREAM_DECODER_SEEK_STATUS_OK", - "FLAC__STREAM_DECODER_SEEK_STATUS_ERROR", - "FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED" -}; - -FLAC_API const char * const FLAC__StreamDecoderTellStatusString[] = { - "FLAC__STREAM_DECODER_TELL_STATUS_OK", - "FLAC__STREAM_DECODER_TELL_STATUS_ERROR", - "FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED" -}; - -FLAC_API const char * const FLAC__StreamDecoderLengthStatusString[] = { - "FLAC__STREAM_DECODER_LENGTH_STATUS_OK", - "FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR", - "FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED" -}; - -FLAC_API const char * const FLAC__StreamDecoderWriteStatusString[] = { - "FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE", - "FLAC__STREAM_DECODER_WRITE_STATUS_ABORT" -}; - -FLAC_API const char * const FLAC__StreamDecoderErrorStatusString[] = { - "FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC", - "FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER", - "FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH", - "FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM" -}; - -/*********************************************************************** - * - * Class constructor/destructor - * - ***********************************************************************/ -FLAC_API FLAC__StreamDecoder *FLAC__stream_decoder_new(void) -{ - FLAC__StreamDecoder *decoder; - unsigned i; - - FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */ - - decoder = (FLAC__StreamDecoder*)calloc(1, sizeof(FLAC__StreamDecoder)); - if(decoder == 0) { - return 0; - } - - decoder->protected_ = (FLAC__StreamDecoderProtected*)calloc(1, sizeof(FLAC__StreamDecoderProtected)); - if(decoder->protected_ == 0) { - free(decoder); - return 0; - } - - decoder->private_ = (FLAC__StreamDecoderPrivate*)calloc(1, sizeof(FLAC__StreamDecoderPrivate)); - if(decoder->private_ == 0) { - free(decoder->protected_); - free(decoder); - return 0; - } - - decoder->private_->input = FLAC__bitreader_new(); - if(decoder->private_->input == 0) { - free(decoder->private_); - free(decoder->protected_); - free(decoder); - return 0; - } - - decoder->private_->metadata_filter_ids_capacity = 16; - if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)malloc((FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) * decoder->private_->metadata_filter_ids_capacity))) { - FLAC__bitreader_delete(decoder->private_->input); - free(decoder->private_); - free(decoder->protected_); - free(decoder); - return 0; - } - - for(i = 0; i < FLAC__MAX_CHANNELS; i++) { - decoder->private_->output[i] = 0; - decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0; - } - - decoder->private_->output_capacity = 0; - decoder->private_->output_channels = 0; - decoder->private_->has_seek_table = false; - - for(i = 0; i < FLAC__MAX_CHANNELS; i++) - FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&decoder->private_->partitioned_rice_contents[i]); - - decoder->private_->file = 0; - - set_defaults_(decoder); - - decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED; - - return decoder; -} - -FLAC_API void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder) -{ - unsigned i; - - FLAC__ASSERT(0 != decoder); - FLAC__ASSERT(0 != decoder->protected_); - FLAC__ASSERT(0 != decoder->private_); - FLAC__ASSERT(0 != decoder->private_->input); - - (void)FLAC__stream_decoder_finish(decoder); - - if(0 != decoder->private_->metadata_filter_ids) - free(decoder->private_->metadata_filter_ids); - - FLAC__bitreader_delete(decoder->private_->input); - - for(i = 0; i < FLAC__MAX_CHANNELS; i++) - FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&decoder->private_->partitioned_rice_contents[i]); - - free(decoder->private_); - free(decoder->protected_); - free(decoder); -} - -/*********************************************************************** - * - * Public class methods - * - ***********************************************************************/ - -static FLAC__StreamDecoderInitStatus init_stream_internal_( - FLAC__StreamDecoder *decoder, - FLAC__StreamDecoderReadCallback read_callback, - FLAC__StreamDecoderSeekCallback seek_callback, - FLAC__StreamDecoderTellCallback tell_callback, - FLAC__StreamDecoderLengthCallback length_callback, - FLAC__StreamDecoderEofCallback eof_callback, - FLAC__StreamDecoderWriteCallback write_callback, - FLAC__StreamDecoderMetadataCallback metadata_callback, - FLAC__StreamDecoderErrorCallback error_callback, - void *client_data, - FLAC__bool is_ogg -) -{ - FLAC__ASSERT(0 != decoder); - - if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) - return FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED; - -#if !FLAC__HAS_OGG - if(is_ogg) - return FLAC__STREAM_DECODER_INIT_STATUS_UNSUPPORTED_CONTAINER; -#endif - - if( - 0 == read_callback || - 0 == write_callback || - 0 == error_callback || - (seek_callback && (0 == tell_callback || 0 == length_callback || 0 == eof_callback)) - ) - return FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS; - -#if FLAC__HAS_OGG - decoder->private_->is_ogg = is_ogg; - if(is_ogg && !FLAC__ogg_decoder_aspect_init(&decoder->protected_->ogg_decoder_aspect)) - return decoder->protected_->state = FLAC__STREAM_DECODER_OGG_ERROR; -#endif - - /* - * get the CPU info and set the function pointers - */ - FLAC__cpu_info(&decoder->private_->cpuinfo); - /* first default to the non-asm routines */ - decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal; - decoder->private_->local_lpc_restore_signal_64bit = FLAC__lpc_restore_signal_wide; - decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal; - decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal; - decoder->private_->local_bitreader_read_rice_signed_block = FLAC__bitreader_read_rice_signed_block; - /* now override with asm where appropriate */ -#ifndef FLAC__NO_ASM - if(decoder->private_->cpuinfo.use_asm) { -#ifdef FLAC__CPU_IA32 - FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32); -#ifdef FLAC__HAS_NASM -#if 1 /*@@@@@@ OPT: not clearly faster, needs more testing */ - if(decoder->private_->cpuinfo.data.ia32.bswap) - decoder->private_->local_bitreader_read_rice_signed_block = FLAC__bitreader_read_rice_signed_block_asm_ia32_bswap; -#endif - if(decoder->private_->cpuinfo.data.ia32.mmx) { - decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32; - decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32_mmx; - decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ia32_mmx; - } - else { - decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32; - decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32; - decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ia32; - } -#endif -#elif defined FLAC__CPU_PPC - FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_PPC); - if(decoder->private_->cpuinfo.data.ppc.altivec) { - decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ppc_altivec_16; - decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ppc_altivec_16_order8; - } -#endif - } -#endif - - /* from here on, errors are fatal */ - - if(!FLAC__bitreader_init(decoder->private_->input, decoder->private_->cpuinfo, read_callback_, decoder)) { - decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; - return FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR; - } - - decoder->private_->read_callback = read_callback; - decoder->private_->seek_callback = seek_callback; - decoder->private_->tell_callback = tell_callback; - decoder->private_->length_callback = length_callback; - decoder->private_->eof_callback = eof_callback; - decoder->private_->write_callback = write_callback; - decoder->private_->metadata_callback = metadata_callback; - decoder->private_->error_callback = error_callback; - decoder->private_->client_data = client_data; - decoder->private_->fixed_block_size = decoder->private_->next_fixed_block_size = 0; - decoder->private_->samples_decoded = 0; - decoder->private_->has_stream_info = false; - decoder->private_->cached = false; - - decoder->private_->do_md5_checking = decoder->protected_->md5_checking; - decoder->private_->is_seeking = false; - - decoder->private_->internal_reset_hack = true; /* so the following reset does not try to rewind the input */ - if(!FLAC__stream_decoder_reset(decoder)) { - /* above call sets the state for us */ - return FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR; - } - - return FLAC__STREAM_DECODER_INIT_STATUS_OK; -} - -FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_stream( - FLAC__StreamDecoder *decoder, - FLAC__StreamDecoderReadCallback read_callback, - FLAC__StreamDecoderSeekCallback seek_callback, - FLAC__StreamDecoderTellCallback tell_callback, - FLAC__StreamDecoderLengthCallback length_callback, - FLAC__StreamDecoderEofCallback eof_callback, - FLAC__StreamDecoderWriteCallback write_callback, - FLAC__StreamDecoderMetadataCallback metadata_callback, - FLAC__StreamDecoderErrorCallback error_callback, - void *client_data -) -{ - return init_stream_internal_( - decoder, - read_callback, - seek_callback, - tell_callback, - length_callback, - eof_callback, - write_callback, - metadata_callback, - error_callback, - client_data, - /*is_ogg=*/false - ); -} - -FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_stream( - FLAC__StreamDecoder *decoder, - FLAC__StreamDecoderReadCallback read_callback, - FLAC__StreamDecoderSeekCallback seek_callback, - FLAC__StreamDecoderTellCallback tell_callback, - FLAC__StreamDecoderLengthCallback length_callback, - FLAC__StreamDecoderEofCallback eof_callback, - FLAC__StreamDecoderWriteCallback write_callback, - FLAC__StreamDecoderMetadataCallback metadata_callback, - FLAC__StreamDecoderErrorCallback error_callback, - void *client_data -) -{ - return init_stream_internal_( - decoder, - read_callback, - seek_callback, - tell_callback, - length_callback, - eof_callback, - write_callback, - metadata_callback, - error_callback, - client_data, - /*is_ogg=*/true - ); -} - -static FLAC__StreamDecoderInitStatus init_FILE_internal_( - FLAC__StreamDecoder *decoder, - FILE *file, - FLAC__StreamDecoderWriteCallback write_callback, - FLAC__StreamDecoderMetadataCallback metadata_callback, - FLAC__StreamDecoderErrorCallback error_callback, - void *client_data, - FLAC__bool is_ogg -) -{ - FLAC__ASSERT(0 != decoder); - FLAC__ASSERT(0 != file); - - if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) - return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED; - - if(0 == write_callback || 0 == error_callback) - return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS; - - /* - * To make sure that our file does not go unclosed after an error, we - * must assign the FILE pointer before any further error can occur in - * this routine. - */ - if(file == stdin) - file = get_binary_stdin_(); /* just to be safe */ - - decoder->private_->file = file; - - return init_stream_internal_( - decoder, - file_read_callback_, - decoder->private_->file == stdin? 0: file_seek_callback_, - decoder->private_->file == stdin? 0: file_tell_callback_, - decoder->private_->file == stdin? 0: file_length_callback_, - file_eof_callback_, - write_callback, - metadata_callback, - error_callback, - client_data, - is_ogg - ); -} - -FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_FILE( - FLAC__StreamDecoder *decoder, - FILE *file, - FLAC__StreamDecoderWriteCallback write_callback, - FLAC__StreamDecoderMetadataCallback metadata_callback, - FLAC__StreamDecoderErrorCallback error_callback, - void *client_data -) -{ - return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/false); -} - -FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_FILE( - FLAC__StreamDecoder *decoder, - FILE *file, - FLAC__StreamDecoderWriteCallback write_callback, - FLAC__StreamDecoderMetadataCallback metadata_callback, - FLAC__StreamDecoderErrorCallback error_callback, - void *client_data -) -{ - return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/true); -} - -static FLAC__StreamDecoderInitStatus init_file_internal_( - FLAC__StreamDecoder *decoder, - const char *filename, - FLAC__StreamDecoderWriteCallback write_callback, - FLAC__StreamDecoderMetadataCallback metadata_callback, - FLAC__StreamDecoderErrorCallback error_callback, - void *client_data, - FLAC__bool is_ogg -) -{ - FILE *file; - - FLAC__ASSERT(0 != decoder); - - /* - * To make sure that our file does not go unclosed after an error, we - * have to do the same entrance checks here that are later performed - * in FLAC__stream_decoder_init_FILE() before the FILE* is assigned. - */ - if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) - return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED; - - if(0 == write_callback || 0 == error_callback) - return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS; - - file = filename? fopen(filename, "rb") : stdin; - - if(0 == file) - return FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE; - - return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, is_ogg); -} - -FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_file( - FLAC__StreamDecoder *decoder, - const char *filename, - FLAC__StreamDecoderWriteCallback write_callback, - FLAC__StreamDecoderMetadataCallback metadata_callback, - FLAC__StreamDecoderErrorCallback error_callback, - void *client_data -) -{ - return init_file_internal_(decoder, filename, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/false); -} - -FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_file( - FLAC__StreamDecoder *decoder, - const char *filename, - FLAC__StreamDecoderWriteCallback write_callback, - FLAC__StreamDecoderMetadataCallback metadata_callback, - FLAC__StreamDecoderErrorCallback error_callback, - void *client_data -) -{ - return init_file_internal_(decoder, filename, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/true); -} - -FLAC_API FLAC__bool FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder) -{ - FLAC__bool md5_failed = false; - unsigned i; - - FLAC__ASSERT(0 != decoder); - FLAC__ASSERT(0 != decoder->private_); - FLAC__ASSERT(0 != decoder->protected_); - - if(decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED) - return true; - - /* see the comment in FLAC__seekable_stream_decoder_reset() as to why we - * always call FLAC__MD5Final() - */ - FLAC__MD5Final(decoder->private_->computed_md5sum, &decoder->private_->md5context); - - if(decoder->private_->has_seek_table && 0 != decoder->private_->seek_table.data.seek_table.points) { - free(decoder->private_->seek_table.data.seek_table.points); - decoder->private_->seek_table.data.seek_table.points = 0; - decoder->private_->has_seek_table = false; - } - FLAC__bitreader_free(decoder->private_->input); - for(i = 0; i < FLAC__MAX_CHANNELS; i++) { - /* WATCHOUT: - * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the - * output arrays have a buffer of up to 3 zeroes in front - * (at negative indices) for alignment purposes; we use 4 - * to keep the data well-aligned. - */ - if(0 != decoder->private_->output[i]) { - free(decoder->private_->output[i]-4); - decoder->private_->output[i] = 0; - } - if(0 != decoder->private_->residual_unaligned[i]) { - free(decoder->private_->residual_unaligned[i]); - decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0; - } - } - decoder->private_->output_capacity = 0; - decoder->private_->output_channels = 0; - -#if FLAC__HAS_OGG - if(decoder->private_->is_ogg) - FLAC__ogg_decoder_aspect_finish(&decoder->protected_->ogg_decoder_aspect); -#endif - - if(0 != decoder->private_->file) { - if(decoder->private_->file != stdin) - fclose(decoder->private_->file); - decoder->private_->file = 0; - } - - if(decoder->private_->do_md5_checking) { - if(memcmp(decoder->private_->stream_info.data.stream_info.md5sum, decoder->private_->computed_md5sum, 16)) - md5_failed = true; - } - decoder->private_->is_seeking = false; - - set_defaults_(decoder); - - decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED; - - return !md5_failed; -} - -FLAC_API FLAC__bool FLAC__stream_decoder_set_ogg_serial_number(FLAC__StreamDecoder *decoder, long value) -{ - FLAC__ASSERT(0 != decoder); - FLAC__ASSERT(0 != decoder->private_); - FLAC__ASSERT(0 != decoder->protected_); - if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) - return false; -#if FLAC__HAS_OGG - /* can't check decoder->private_->is_ogg since that's not set until init time */ - FLAC__ogg_decoder_aspect_set_serial_number(&decoder->protected_->ogg_decoder_aspect, value); - return true; -#else - (void)value; - return false; -#endif -} - -FLAC_API FLAC__bool FLAC__stream_decoder_set_md5_checking(FLAC__StreamDecoder *decoder, FLAC__bool value) -{ - FLAC__ASSERT(0 != decoder); - FLAC__ASSERT(0 != decoder->protected_); - if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) - return false; - decoder->protected_->md5_checking = value; - return true; -} - -FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond(FLAC__StreamDecoder *decoder, FLAC__MetadataType type) -{ - FLAC__ASSERT(0 != decoder); - FLAC__ASSERT(0 != decoder->private_); - FLAC__ASSERT(0 != decoder->protected_); - FLAC__ASSERT((unsigned)type <= FLAC__MAX_METADATA_TYPE_CODE); - /* double protection */ - if((unsigned)type > FLAC__MAX_METADATA_TYPE_CODE) - return false; - if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) - return false; - decoder->private_->metadata_filter[type] = true; - if(type == FLAC__METADATA_TYPE_APPLICATION) - decoder->private_->metadata_filter_ids_count = 0; - return true; -} - -FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4]) -{ - FLAC__ASSERT(0 != decoder); - FLAC__ASSERT(0 != decoder->private_); - FLAC__ASSERT(0 != decoder->protected_); - FLAC__ASSERT(0 != id); - if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) - return false; - - if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION]) - return true; - - FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids); - - if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) { - if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2))) { - decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; - return false; - } - decoder->private_->metadata_filter_ids_capacity *= 2; - } - - memcpy(decoder->private_->metadata_filter_ids + decoder->private_->metadata_filter_ids_count * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8)); - decoder->private_->metadata_filter_ids_count++; - - return true; -} - -FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_all(FLAC__StreamDecoder *decoder) -{ - unsigned i; - FLAC__ASSERT(0 != decoder); - FLAC__ASSERT(0 != decoder->private_); - FLAC__ASSERT(0 != decoder->protected_); - if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) - return false; - for(i = 0; i < sizeof(decoder->private_->metadata_filter) / sizeof(decoder->private_->metadata_filter[0]); i++) - decoder->private_->metadata_filter[i] = true; - decoder->private_->metadata_filter_ids_count = 0; - return true; -} - -FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore(FLAC__StreamDecoder *decoder, FLAC__MetadataType type) -{ - FLAC__ASSERT(0 != decoder); - FLAC__ASSERT(0 != decoder->private_); - FLAC__ASSERT(0 != decoder->protected_); - FLAC__ASSERT((unsigned)type <= FLAC__MAX_METADATA_TYPE_CODE); - /* double protection */ - if((unsigned)type > FLAC__MAX_METADATA_TYPE_CODE) - return false; - if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) - return false; - decoder->private_->metadata_filter[type] = false; - if(type == FLAC__METADATA_TYPE_APPLICATION) - decoder->private_->metadata_filter_ids_count = 0; - return true; -} - -FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4]) -{ - FLAC__ASSERT(0 != decoder); - FLAC__ASSERT(0 != decoder->private_); - FLAC__ASSERT(0 != decoder->protected_); - FLAC__ASSERT(0 != id); - if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) - return false; - - if(!decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION]) - return true; - - FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids); - - if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) { - if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2))) { - decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; - return false; - } - decoder->private_->metadata_filter_ids_capacity *= 2; - } - - memcpy(decoder->private_->metadata_filter_ids + decoder->private_->metadata_filter_ids_count * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8)); - decoder->private_->metadata_filter_ids_count++; - - return true; -} - -FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_all(FLAC__StreamDecoder *decoder) -{ - FLAC__ASSERT(0 != decoder); - FLAC__ASSERT(0 != decoder->private_); - FLAC__ASSERT(0 != decoder->protected_); - if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) - return false; - memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter)); - decoder->private_->metadata_filter_ids_count = 0; - return true; -} - -FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder) -{ - FLAC__ASSERT(0 != decoder); - FLAC__ASSERT(0 != decoder->protected_); - return decoder->protected_->state; -} - -FLAC_API const char *FLAC__stream_decoder_get_resolved_state_string(const FLAC__StreamDecoder *decoder) -{ - return FLAC__StreamDecoderStateString[decoder->protected_->state]; -} - -FLAC_API FLAC__bool FLAC__stream_decoder_get_md5_checking(const FLAC__StreamDecoder *decoder) -{ - FLAC__ASSERT(0 != decoder); - FLAC__ASSERT(0 != decoder->protected_); - return decoder->protected_->md5_checking; -} - -FLAC_API FLAC__uint64 FLAC__stream_decoder_get_total_samples(const FLAC__StreamDecoder *decoder) -{ - FLAC__ASSERT(0 != decoder); - FLAC__ASSERT(0 != decoder->protected_); - return decoder->private_->has_stream_info? decoder->private_->stream_info.data.stream_info.total_samples : 0; -} - -FLAC_API unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder) -{ - FLAC__ASSERT(0 != decoder); - FLAC__ASSERT(0 != decoder->protected_); - return decoder->protected_->channels; -} - -FLAC_API FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder) -{ - FLAC__ASSERT(0 != decoder); - FLAC__ASSERT(0 != decoder->protected_); - return decoder->protected_->channel_assignment; -} - -FLAC_API unsigned FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder) -{ - FLAC__ASSERT(0 != decoder); - FLAC__ASSERT(0 != decoder->protected_); - return decoder->protected_->bits_per_sample; -} - -FLAC_API unsigned FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder) -{ - FLAC__ASSERT(0 != decoder); - FLAC__ASSERT(0 != decoder->protected_); - return decoder->protected_->sample_rate; -} - -FLAC_API unsigned FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder) -{ - FLAC__ASSERT(0 != decoder); - FLAC__ASSERT(0 != decoder->protected_); - return decoder->protected_->blocksize; -} - -FLAC_API FLAC__bool FLAC__stream_decoder_get_decode_position(const FLAC__StreamDecoder *decoder, FLAC__uint64 *position) -{ - FLAC__ASSERT(0 != decoder); - FLAC__ASSERT(0 != decoder->private_); - FLAC__ASSERT(0 != position); - -#if FLAC__HAS_OGG - if(decoder->private_->is_ogg) - return false; -#endif - if(0 == decoder->private_->tell_callback) - return false; - if(decoder->private_->tell_callback(decoder, position, decoder->private_->client_data) != FLAC__STREAM_DECODER_TELL_STATUS_OK) - return false; - /* should never happen since all FLAC frames and metadata blocks are byte aligned, but check just in case */ - if(!FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)) - return false; - FLAC__ASSERT(*position >= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder)); - *position -= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder); - return true; -} - -FLAC_API FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder) -{ - FLAC__ASSERT(0 != decoder); - FLAC__ASSERT(0 != decoder->private_); - FLAC__ASSERT(0 != decoder->protected_); - - decoder->private_->samples_decoded = 0; - decoder->private_->do_md5_checking = false; - -#if FLAC__HAS_OGG - if(decoder->private_->is_ogg) - FLAC__ogg_decoder_aspect_flush(&decoder->protected_->ogg_decoder_aspect); -#endif - - if(!FLAC__bitreader_clear(decoder->private_->input)) { - decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; - return false; - } - decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; - - return true; -} - -FLAC_API FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder) -{ - FLAC__ASSERT(0 != decoder); - FLAC__ASSERT(0 != decoder->private_); - FLAC__ASSERT(0 != decoder->protected_); - - if(!FLAC__stream_decoder_flush(decoder)) { - /* above call sets the state for us */ - return false; - } - -#if FLAC__HAS_OGG - /*@@@ could go in !internal_reset_hack block below */ - if(decoder->private_->is_ogg) - FLAC__ogg_decoder_aspect_reset(&decoder->protected_->ogg_decoder_aspect); -#endif - - /* Rewind if necessary. If FLAC__stream_decoder_init() is calling us, - * (internal_reset_hack) don't try to rewind since we are already at - * the beginning of the stream and don't want to fail if the input is - * not seekable. - */ - if(!decoder->private_->internal_reset_hack) { - if(decoder->private_->file == stdin) - return false; /* can't rewind stdin, reset fails */ - if(decoder->private_->seek_callback && decoder->private_->seek_callback(decoder, 0, decoder->private_->client_data) == FLAC__STREAM_DECODER_SEEK_STATUS_ERROR) - return false; /* seekable and seek fails, reset fails */ - } - else - decoder->private_->internal_reset_hack = false; - - decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA; - - decoder->private_->has_stream_info = false; - if(decoder->private_->has_seek_table && 0 != decoder->private_->seek_table.data.seek_table.points) { - free(decoder->private_->seek_table.data.seek_table.points); - decoder->private_->seek_table.data.seek_table.points = 0; - decoder->private_->has_seek_table = false; - } - decoder->private_->do_md5_checking = decoder->protected_->md5_checking; - /* - * This goes in reset() and not flush() because according to the spec, a - * fixed-blocksize stream must stay that way through the whole stream. - */ - decoder->private_->fixed_block_size = decoder->private_->next_fixed_block_size = 0; - - /* We initialize the FLAC__MD5Context even though we may never use it. This - * is because md5 checking may be turned on to start and then turned off if - * a seek occurs. So we init the context here and finalize it in - * FLAC__stream_decoder_finish() to make sure things are always cleaned up - * properly. - */ - FLAC__MD5Init(&decoder->private_->md5context); - - decoder->private_->first_frame_offset = 0; - decoder->private_->unparseable_frame_count = 0; - - return true; -} - -FLAC_API FLAC__bool FLAC__stream_decoder_process_single(FLAC__StreamDecoder *decoder) -{ - FLAC__bool got_a_frame; - FLAC__ASSERT(0 != decoder); - FLAC__ASSERT(0 != decoder->protected_); - - while(1) { - switch(decoder->protected_->state) { - case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA: - if(!find_metadata_(decoder)) - return false; /* above function sets the status for us */ - break; - case FLAC__STREAM_DECODER_READ_METADATA: - if(!read_metadata_(decoder)) - return false; /* above function sets the status for us */ - else - return true; - case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC: - if(!frame_sync_(decoder)) - return true; /* above function sets the status for us */ - break; - case FLAC__STREAM_DECODER_READ_FRAME: - if(!read_frame_(decoder, &got_a_frame, /*do_full_decode=*/true)) - return false; /* above function sets the status for us */ - if(got_a_frame) - return true; /* above function sets the status for us */ - break; - case FLAC__STREAM_DECODER_END_OF_STREAM: - case FLAC__STREAM_DECODER_ABORTED: - return true; - default: - FLAC__ASSERT(0); - return false; - } - } -} - -FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_metadata(FLAC__StreamDecoder *decoder) -{ - FLAC__ASSERT(0 != decoder); - FLAC__ASSERT(0 != decoder->protected_); - - while(1) { - switch(decoder->protected_->state) { - case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA: - if(!find_metadata_(decoder)) - return false; /* above function sets the status for us */ - break; - case FLAC__STREAM_DECODER_READ_METADATA: - if(!read_metadata_(decoder)) - return false; /* above function sets the status for us */ - break; - case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC: - case FLAC__STREAM_DECODER_READ_FRAME: - case FLAC__STREAM_DECODER_END_OF_STREAM: - case FLAC__STREAM_DECODER_ABORTED: - return true; - default: - FLAC__ASSERT(0); - return false; - } - } -} - -FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_stream(FLAC__StreamDecoder *decoder) -{ - FLAC__bool dummy; - FLAC__ASSERT(0 != decoder); - FLAC__ASSERT(0 != decoder->protected_); - - while(1) { - switch(decoder->protected_->state) { - case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA: - if(!find_metadata_(decoder)) - return false; /* above function sets the status for us */ - break; - case FLAC__STREAM_DECODER_READ_METADATA: - if(!read_metadata_(decoder)) - return false; /* above function sets the status for us */ - break; - case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC: - if(!frame_sync_(decoder)) - return true; /* above function sets the status for us */ - break; - case FLAC__STREAM_DECODER_READ_FRAME: - if(!read_frame_(decoder, &dummy, /*do_full_decode=*/true)) - return false; /* above function sets the status for us */ - break; - case FLAC__STREAM_DECODER_END_OF_STREAM: - case FLAC__STREAM_DECODER_ABORTED: - return true; - default: - FLAC__ASSERT(0); - return false; - } - } -} - -FLAC_API FLAC__bool FLAC__stream_decoder_skip_single_frame(FLAC__StreamDecoder *decoder) -{ - FLAC__bool got_a_frame; - FLAC__ASSERT(0 != decoder); - FLAC__ASSERT(0 != decoder->protected_); - - while(1) { - switch(decoder->protected_->state) { - case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA: - case FLAC__STREAM_DECODER_READ_METADATA: - return false; /* above function sets the status for us */ - case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC: - if(!frame_sync_(decoder)) - return true; /* above function sets the status for us */ - break; - case FLAC__STREAM_DECODER_READ_FRAME: - if(!read_frame_(decoder, &got_a_frame, /*do_full_decode=*/false)) - return false; /* above function sets the status for us */ - if(got_a_frame) - return true; /* above function sets the status for us */ - break; - case FLAC__STREAM_DECODER_END_OF_STREAM: - case FLAC__STREAM_DECODER_ABORTED: - return true; - default: - FLAC__ASSERT(0); - return false; - } - } -} - -FLAC_API FLAC__bool FLAC__stream_decoder_seek_absolute(FLAC__StreamDecoder *decoder, FLAC__uint64 sample) -{ - FLAC__uint64 length; - - FLAC__ASSERT(0 != decoder); - - if( - decoder->protected_->state != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA && - decoder->protected_->state != FLAC__STREAM_DECODER_READ_METADATA && - decoder->protected_->state != FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC && - decoder->protected_->state != FLAC__STREAM_DECODER_READ_FRAME && - decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_STREAM - ) - return false; - - if(0 == decoder->private_->seek_callback) - return false; - - FLAC__ASSERT(decoder->private_->seek_callback); - FLAC__ASSERT(decoder->private_->tell_callback); - FLAC__ASSERT(decoder->private_->length_callback); - FLAC__ASSERT(decoder->private_->eof_callback); - - if(FLAC__stream_decoder_get_total_samples(decoder) > 0 && sample >= FLAC__stream_decoder_get_total_samples(decoder)) - return false; - - decoder->private_->is_seeking = true; - - /* turn off md5 checking if a seek is attempted */ - decoder->private_->do_md5_checking = false; - - /* get the file length (currently our algorithm needs to know the length so it's also an error to get FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED) */ - if(decoder->private_->length_callback(decoder, &length, decoder->private_->client_data) != FLAC__STREAM_DECODER_LENGTH_STATUS_OK) { - decoder->private_->is_seeking = false; - return false; - } - - /* if we haven't finished processing the metadata yet, do that so we have the STREAMINFO, SEEK_TABLE, and first_frame_offset */ - if( - decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA || - decoder->protected_->state == FLAC__STREAM_DECODER_READ_METADATA - ) { - if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder)) { - /* above call sets the state for us */ - decoder->private_->is_seeking = false; - return false; - } - /* check this again in case we didn't know total_samples the first time */ - if(FLAC__stream_decoder_get_total_samples(decoder) > 0 && sample >= FLAC__stream_decoder_get_total_samples(decoder)) { - decoder->private_->is_seeking = false; - return false; - } - } - - { - const FLAC__bool ok = -#if FLAC__HAS_OGG - decoder->private_->is_ogg? - seek_to_absolute_sample_ogg_(decoder, length, sample) : -#endif - seek_to_absolute_sample_(decoder, length, sample) - ; - decoder->private_->is_seeking = false; - return ok; - } -} - -/*********************************************************************** - * - * Protected class methods - * - ***********************************************************************/ - -unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder) -{ - FLAC__ASSERT(0 != decoder); - FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)); - FLAC__ASSERT(!(FLAC__bitreader_get_input_bits_unconsumed(decoder->private_->input) & 7)); - return FLAC__bitreader_get_input_bits_unconsumed(decoder->private_->input) / 8; -} - -/*********************************************************************** - * - * Private class methods - * - ***********************************************************************/ - -void set_defaults_(FLAC__StreamDecoder *decoder) -{ -#if FLAC__HAS_OGG - decoder->private_->is_ogg = false; -#endif - decoder->private_->read_callback = 0; - decoder->private_->seek_callback = 0; - decoder->private_->tell_callback = 0; - decoder->private_->length_callback = 0; - decoder->private_->eof_callback = 0; - decoder->private_->write_callback = 0; - decoder->private_->metadata_callback = 0; - decoder->private_->error_callback = 0; - decoder->private_->client_data = 0; - - memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter)); - decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] = true; - decoder->private_->metadata_filter_ids_count = 0; - - decoder->protected_->md5_checking = false; - -#if FLAC__HAS_OGG - FLAC__ogg_decoder_aspect_set_defaults(&decoder->protected_->ogg_decoder_aspect); -#endif -} - -/* - * This will forcibly set stdin to binary mode (for OSes that require it) - */ -FILE *get_binary_stdin_(void) -{ - /* if something breaks here it is probably due to the presence or - * absence of an underscore before the identifiers 'setmode', - * 'fileno', and/or 'O_BINARY'; check your system header files. - */ -#if defined _MSC_VER || defined __MINGW32__ - _setmode(_fileno(stdin), _O_BINARY); -#elif defined __CYGWIN__ - /* almost certainly not needed for any modern Cygwin, but let's be safe... */ - setmode(_fileno(stdin), _O_BINARY); -#elif defined __EMX__ - setmode(fileno(stdin), O_BINARY); -#endif - - return stdin; -} - -FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels) -{ - unsigned i; - FLAC__int32 *tmp; - - if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels) - return true; - - /* simply using realloc() is not practical because the number of channels may change mid-stream */ - - for(i = 0; i < FLAC__MAX_CHANNELS; i++) { - if(0 != decoder->private_->output[i]) { - free(decoder->private_->output[i]-4); - decoder->private_->output[i] = 0; - } - if(0 != decoder->private_->residual_unaligned[i]) { - free(decoder->private_->residual_unaligned[i]); - decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0; - } - } - - for(i = 0; i < channels; i++) { - /* WATCHOUT: - * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the - * output arrays have a buffer of up to 3 zeroes in front - * (at negative indices) for alignment purposes; we use 4 - * to keep the data well-aligned. - */ - tmp = (FLAC__int32*)safe_malloc_muladd2_(sizeof(FLAC__int32), /*times (*/size, /*+*/4/*)*/); - if(tmp == 0) { - decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; - return false; - } - memset(tmp, 0, sizeof(FLAC__int32)*4); - decoder->private_->output[i] = tmp + 4; - - /* WATCHOUT: - * minimum of quadword alignment for PPC vector optimizations is REQUIRED: - */ - if(!FLAC__memory_alloc_aligned_int32_array(size, &decoder->private_->residual_unaligned[i], &decoder->private_->residual[i])) { - decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; - return false; - } - } - - decoder->private_->output_capacity = size; - decoder->private_->output_channels = channels; - - return true; -} - -FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id) -{ - size_t i; - - FLAC__ASSERT(0 != decoder); - FLAC__ASSERT(0 != decoder->private_); - - for(i = 0; i < decoder->private_->metadata_filter_ids_count; i++) - if(0 == memcmp(decoder->private_->metadata_filter_ids + i * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8))) - return true; - - return false; -} - -FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder) -{ - FLAC__uint32 x; - unsigned i, id; - FLAC__bool first = true; - - FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)); - - for(i = id = 0; i < 4; ) { - if(decoder->private_->cached) { - x = (FLAC__uint32)decoder->private_->lookahead; - decoder->private_->cached = false; - } - else { - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) - return false; /* read_callback_ sets the state for us */ - } - if(x == FLAC__STREAM_SYNC_STRING[i]) { - first = true; - i++; - id = 0; - continue; - } - if(x == ID3V2_TAG_[id]) { - id++; - i = 0; - if(id == 3) { - if(!skip_id3v2_tag_(decoder)) - return false; /* skip_id3v2_tag_ sets the state for us */ - } - continue; - } - id = 0; - if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */ - decoder->private_->header_warmup[0] = (FLAC__byte)x; - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) - return false; /* read_callback_ sets the state for us */ - - /* we have to check if we just read two 0xff's in a row; the second may actually be the beginning of the sync code */ - /* else we have to check if the second byte is the end of a sync code */ - if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */ - decoder->private_->lookahead = (FLAC__byte)x; - decoder->private_->cached = true; - } - else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */ - decoder->private_->header_warmup[1] = (FLAC__byte)x; - decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME; - return true; - } - } - i = 0; - if(first) { - send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC); - first = false; - } - } - - decoder->protected_->state = FLAC__STREAM_DECODER_READ_METADATA; - return true; -} - -FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder) -{ - FLAC__bool is_last; - FLAC__uint32 i, x, type, length; - - FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)); - - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_IS_LAST_LEN)) - return false; /* read_callback_ sets the state for us */ - is_last = x? true : false; - - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &type, FLAC__STREAM_METADATA_TYPE_LEN)) - return false; /* read_callback_ sets the state for us */ - - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN)) - return false; /* read_callback_ sets the state for us */ - - if(type == FLAC__METADATA_TYPE_STREAMINFO) { - if(!read_metadata_streaminfo_(decoder, is_last, length)) - return false; - - decoder->private_->has_stream_info = true; - if(0 == memcmp(decoder->private_->stream_info.data.stream_info.md5sum, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16)) - decoder->private_->do_md5_checking = false; - if(!decoder->private_->is_seeking && decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] && decoder->private_->metadata_callback) - decoder->private_->metadata_callback(decoder, &decoder->private_->stream_info, decoder->private_->client_data); - } - else if(type == FLAC__METADATA_TYPE_SEEKTABLE) { - if(!read_metadata_seektable_(decoder, is_last, length)) - return false; - - decoder->private_->has_seek_table = true; - if(!decoder->private_->is_seeking && decoder->private_->metadata_filter[FLAC__METADATA_TYPE_SEEKTABLE] && decoder->private_->metadata_callback) - decoder->private_->metadata_callback(decoder, &decoder->private_->seek_table, decoder->private_->client_data); - } - else { - FLAC__bool skip_it = !decoder->private_->metadata_filter[type]; - unsigned real_length = length; - FLAC__StreamMetadata block; - - block.is_last = is_last; - block.type = (FLAC__MetadataType)type; - block.length = length; - - if(type == FLAC__METADATA_TYPE_APPLICATION) { - if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8)) - return false; /* read_callback_ sets the state for us */ - - if(real_length < FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) { /* underflow check */ - decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;/*@@@@@@ maybe wrong error? need to resync?*/ - return false; - } - - real_length -= FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8; - - if(decoder->private_->metadata_filter_ids_count > 0 && has_id_filtered_(decoder, block.data.application.id)) - skip_it = !skip_it; - } - - if(skip_it) { - if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, real_length)) - return false; /* read_callback_ sets the state for us */ - } - else { - switch(type) { - case FLAC__METADATA_TYPE_PADDING: - /* skip the padding bytes */ - if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, real_length)) - return false; /* read_callback_ sets the state for us */ - break; - case FLAC__METADATA_TYPE_APPLICATION: - /* remember, we read the ID already */ - if(real_length > 0) { - if(0 == (block.data.application.data = (FLAC__byte*)malloc(real_length))) { - decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; - return false; - } - if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.data, real_length)) - return false; /* read_callback_ sets the state for us */ - } - else - block.data.application.data = 0; - break; - case FLAC__METADATA_TYPE_VORBIS_COMMENT: - if(!read_metadata_vorbiscomment_(decoder, &block.data.vorbis_comment)) - return false; - break; - case FLAC__METADATA_TYPE_CUESHEET: - if(!read_metadata_cuesheet_(decoder, &block.data.cue_sheet)) - return false; - break; - case FLAC__METADATA_TYPE_PICTURE: - if(!read_metadata_picture_(decoder, &block.data.picture)) - return false; - break; - case FLAC__METADATA_TYPE_STREAMINFO: - case FLAC__METADATA_TYPE_SEEKTABLE: - FLAC__ASSERT(0); - break; - default: - if(real_length > 0) { - if(0 == (block.data.unknown.data = (FLAC__byte*)malloc(real_length))) { - decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; - return false; - } - if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.unknown.data, real_length)) - return false; /* read_callback_ sets the state for us */ - } - else - block.data.unknown.data = 0; - break; - } - if(!decoder->private_->is_seeking && decoder->private_->metadata_callback) - decoder->private_->metadata_callback(decoder, &block, decoder->private_->client_data); - - /* now we have to free any malloc()ed data in the block */ - switch(type) { - case FLAC__METADATA_TYPE_PADDING: - break; - case FLAC__METADATA_TYPE_APPLICATION: - if(0 != block.data.application.data) - free(block.data.application.data); - break; - case FLAC__METADATA_TYPE_VORBIS_COMMENT: - if(0 != block.data.vorbis_comment.vendor_string.entry) - free(block.data.vorbis_comment.vendor_string.entry); - if(block.data.vorbis_comment.num_comments > 0) - for(i = 0; i < block.data.vorbis_comment.num_comments; i++) - if(0 != block.data.vorbis_comment.comments[i].entry) - free(block.data.vorbis_comment.comments[i].entry); - if(0 != block.data.vorbis_comment.comments) - free(block.data.vorbis_comment.comments); - break; - case FLAC__METADATA_TYPE_CUESHEET: - if(block.data.cue_sheet.num_tracks > 0) - for(i = 0; i < block.data.cue_sheet.num_tracks; i++) - if(0 != block.data.cue_sheet.tracks[i].indices) - free(block.data.cue_sheet.tracks[i].indices); - if(0 != block.data.cue_sheet.tracks) - free(block.data.cue_sheet.tracks); - break; - case FLAC__METADATA_TYPE_PICTURE: - if(0 != block.data.picture.mime_type) - free(block.data.picture.mime_type); - if(0 != block.data.picture.description) - free(block.data.picture.description); - if(0 != block.data.picture.data) - free(block.data.picture.data); - break; - case FLAC__METADATA_TYPE_STREAMINFO: - case FLAC__METADATA_TYPE_SEEKTABLE: - FLAC__ASSERT(0); - default: - if(0 != block.data.unknown.data) - free(block.data.unknown.data); - break; - } - } - } - - if(is_last) { - /* if this fails, it's OK, it's just a hint for the seek routine */ - if(!FLAC__stream_decoder_get_decode_position(decoder, &decoder->private_->first_frame_offset)) - decoder->private_->first_frame_offset = 0; - decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; - } - - return true; -} - -FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length) -{ - FLAC__uint32 x; - unsigned bits, used_bits = 0; - - FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)); - - decoder->private_->stream_info.type = FLAC__METADATA_TYPE_STREAMINFO; - decoder->private_->stream_info.is_last = is_last; - decoder->private_->stream_info.length = length; - - bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN; - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, bits)) - return false; /* read_callback_ sets the state for us */ - decoder->private_->stream_info.data.stream_info.min_blocksize = x; - used_bits += bits; - - bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN; - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN)) - return false; /* read_callback_ sets the state for us */ - decoder->private_->stream_info.data.stream_info.max_blocksize = x; - used_bits += bits; - - bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN; - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN)) - return false; /* read_callback_ sets the state for us */ - decoder->private_->stream_info.data.stream_info.min_framesize = x; - used_bits += bits; - - bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN; - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN)) - return false; /* read_callback_ sets the state for us */ - decoder->private_->stream_info.data.stream_info.max_framesize = x; - used_bits += bits; - - bits = FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN; - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN)) - return false; /* read_callback_ sets the state for us */ - decoder->private_->stream_info.data.stream_info.sample_rate = x; - used_bits += bits; - - bits = FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN; - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN)) - return false; /* read_callback_ sets the state for us */ - decoder->private_->stream_info.data.stream_info.channels = x+1; - used_bits += bits; - - bits = FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN; - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN)) - return false; /* read_callback_ sets the state for us */ - decoder->private_->stream_info.data.stream_info.bits_per_sample = x+1; - used_bits += bits; - - bits = FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN; - if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &decoder->private_->stream_info.data.stream_info.total_samples, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN)) - return false; /* read_callback_ sets the state for us */ - used_bits += bits; - - if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, decoder->private_->stream_info.data.stream_info.md5sum, 16)) - return false; /* read_callback_ sets the state for us */ - used_bits += 16*8; - - /* skip the rest of the block */ - FLAC__ASSERT(used_bits % 8 == 0); - length -= (used_bits / 8); - if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, length)) - return false; /* read_callback_ sets the state for us */ - - return true; -} - -FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length) -{ - FLAC__uint32 i, x; - FLAC__uint64 xx; - - FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)); - - decoder->private_->seek_table.type = FLAC__METADATA_TYPE_SEEKTABLE; - decoder->private_->seek_table.is_last = is_last; - decoder->private_->seek_table.length = length; - - decoder->private_->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH; - - /* use realloc since we may pass through here several times (e.g. after seeking) */ - if(0 == (decoder->private_->seek_table.data.seek_table.points = (FLAC__StreamMetadata_SeekPoint*)safe_realloc_mul_2op_(decoder->private_->seek_table.data.seek_table.points, decoder->private_->seek_table.data.seek_table.num_points, /*times*/sizeof(FLAC__StreamMetadata_SeekPoint)))) { - decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; - return false; - } - for(i = 0; i < decoder->private_->seek_table.data.seek_table.num_points; i++) { - if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN)) - return false; /* read_callback_ sets the state for us */ - decoder->private_->seek_table.data.seek_table.points[i].sample_number = xx; - - if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN)) - return false; /* read_callback_ sets the state for us */ - decoder->private_->seek_table.data.seek_table.points[i].stream_offset = xx; - - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN)) - return false; /* read_callback_ sets the state for us */ - decoder->private_->seek_table.data.seek_table.points[i].frame_samples = x; - } - length -= (decoder->private_->seek_table.data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH); - /* if there is a partial point left, skip over it */ - if(length > 0) { - /*@@@ do a send_error_to_client_() here? there's an argument for either way */ - if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, length)) - return false; /* read_callback_ sets the state for us */ - } - - return true; -} - -FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj) -{ - FLAC__uint32 i; - - FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)); - - /* read vendor string */ - FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32); - if(!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->vendor_string.length)) - return false; /* read_callback_ sets the state for us */ - if(obj->vendor_string.length > 0) { - if(0 == (obj->vendor_string.entry = (FLAC__byte*)safe_malloc_add_2op_(obj->vendor_string.length, /*+*/1))) { - decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; - return false; - } - if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->vendor_string.entry, obj->vendor_string.length)) - return false; /* read_callback_ sets the state for us */ - obj->vendor_string.entry[obj->vendor_string.length] = '\0'; - } - else - obj->vendor_string.entry = 0; - - /* read num comments */ - FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN == 32); - if(!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->num_comments)) - return false; /* read_callback_ sets the state for us */ - - /* read comments */ - if(obj->num_comments > 0) { - if(0 == (obj->comments = (FLAC__StreamMetadata_VorbisComment_Entry*)safe_malloc_mul_2op_(obj->num_comments, /*times*/sizeof(FLAC__StreamMetadata_VorbisComment_Entry)))) { - decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; - return false; - } - for(i = 0; i < obj->num_comments; i++) { - FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32); - if(!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->comments[i].length)) - return false; /* read_callback_ sets the state for us */ - if(obj->comments[i].length > 0) { - if(0 == (obj->comments[i].entry = (FLAC__byte*)safe_malloc_add_2op_(obj->comments[i].length, /*+*/1))) { - decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; - return false; - } - if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->comments[i].entry, obj->comments[i].length)) - return false; /* read_callback_ sets the state for us */ - obj->comments[i].entry[obj->comments[i].length] = '\0'; - } - else - obj->comments[i].entry = 0; - } - } - else { - obj->comments = 0; - } - - return true; -} - -FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj) -{ - FLAC__uint32 i, j, x; - - FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)); - - memset(obj, 0, sizeof(FLAC__StreamMetadata_CueSheet)); - - FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0); - if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)obj->media_catalog_number, FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN/8)) - return false; /* read_callback_ sets the state for us */ - - if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &obj->lead_in, FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN)) - return false; /* read_callback_ sets the state for us */ - - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN)) - return false; /* read_callback_ sets the state for us */ - obj->is_cd = x? true : false; - - if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN)) - return false; /* read_callback_ sets the state for us */ - - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN)) - return false; /* read_callback_ sets the state for us */ - obj->num_tracks = x; - - if(obj->num_tracks > 0) { - if(0 == (obj->tracks = (FLAC__StreamMetadata_CueSheet_Track*)safe_calloc_(obj->num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track)))) { - decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; - return false; - } - for(i = 0; i < obj->num_tracks; i++) { - FLAC__StreamMetadata_CueSheet_Track *track = &obj->tracks[i]; - if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &track->offset, FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN)) - return false; /* read_callback_ sets the state for us */ - - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN)) - return false; /* read_callback_ sets the state for us */ - track->number = (FLAC__byte)x; - - FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0); - if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)track->isrc, FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN/8)) - return false; /* read_callback_ sets the state for us */ - - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN)) - return false; /* read_callback_ sets the state for us */ - track->type = x; - - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN)) - return false; /* read_callback_ sets the state for us */ - track->pre_emphasis = x; - - if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN)) - return false; /* read_callback_ sets the state for us */ - - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN)) - return false; /* read_callback_ sets the state for us */ - track->num_indices = (FLAC__byte)x; - - if(track->num_indices > 0) { - if(0 == (track->indices = (FLAC__StreamMetadata_CueSheet_Index*)safe_calloc_(track->num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index)))) { - decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; - return false; - } - for(j = 0; j < track->num_indices; j++) { - FLAC__StreamMetadata_CueSheet_Index *index = &track->indices[j]; - if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &index->offset, FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN)) - return false; /* read_callback_ sets the state for us */ - - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN)) - return false; /* read_callback_ sets the state for us */ - index->number = (FLAC__byte)x; - - if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN)) - return false; /* read_callback_ sets the state for us */ - } - } - } - } - - return true; -} - -FLAC__bool read_metadata_picture_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_Picture *obj) -{ - FLAC__uint32 x; - - FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)); - - /* read type */ - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_PICTURE_TYPE_LEN)) - return false; /* read_callback_ sets the state for us */ - obj->type = x; - - /* read MIME type */ - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN)) - return false; /* read_callback_ sets the state for us */ - if(0 == (obj->mime_type = (char*)safe_malloc_add_2op_(x, /*+*/1))) { - decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; - return false; - } - if(x > 0) { - if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)obj->mime_type, x)) - return false; /* read_callback_ sets the state for us */ - } - obj->mime_type[x] = '\0'; - - /* read description */ - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN)) - return false; /* read_callback_ sets the state for us */ - if(0 == (obj->description = (FLAC__byte*)safe_malloc_add_2op_(x, /*+*/1))) { - decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; - return false; - } - if(x > 0) { - if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->description, x)) - return false; /* read_callback_ sets the state for us */ - } - obj->description[x] = '\0'; - - /* read width */ - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->width, FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN)) - return false; /* read_callback_ sets the state for us */ - - /* read height */ - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->height, FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN)) - return false; /* read_callback_ sets the state for us */ - - /* read depth */ - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->depth, FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN)) - return false; /* read_callback_ sets the state for us */ - - /* read colors */ - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->colors, FLAC__STREAM_METADATA_PICTURE_COLORS_LEN)) - return false; /* read_callback_ sets the state for us */ - - /* read data */ - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &(obj->data_length), FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN)) - return false; /* read_callback_ sets the state for us */ - if(0 == (obj->data = (FLAC__byte*)safe_malloc_(obj->data_length))) { - decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; - return false; - } - if(obj->data_length > 0) { - if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->data, obj->data_length)) - return false; /* read_callback_ sets the state for us */ - } - - return true; -} - -FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder) -{ - FLAC__uint32 x; - unsigned i, skip; - - /* skip the version and flags bytes */ - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 24)) - return false; /* read_callback_ sets the state for us */ - /* get the size (in bytes) to skip */ - skip = 0; - for(i = 0; i < 4; i++) { - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) - return false; /* read_callback_ sets the state for us */ - skip <<= 7; - skip |= (x & 0x7f); - } - /* skip the rest of the tag */ - if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, skip)) - return false; /* read_callback_ sets the state for us */ - return true; -} - -FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder) -{ - FLAC__uint32 x; - FLAC__bool first = true; - - /* If we know the total number of samples in the stream, stop if we've read that many. */ - /* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */ - if(FLAC__stream_decoder_get_total_samples(decoder) > 0) { - if(decoder->private_->samples_decoded >= FLAC__stream_decoder_get_total_samples(decoder)) { - decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM; - return true; - } - } - - /* make sure we're byte aligned */ - if(!FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)) { - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__bitreader_bits_left_for_byte_alignment(decoder->private_->input))) - return false; /* read_callback_ sets the state for us */ - } - - while(1) { - if(decoder->private_->cached) { - x = (FLAC__uint32)decoder->private_->lookahead; - decoder->private_->cached = false; - } - else { - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) - return false; /* read_callback_ sets the state for us */ - } - if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */ - decoder->private_->header_warmup[0] = (FLAC__byte)x; - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) - return false; /* read_callback_ sets the state for us */ - - /* we have to check if we just read two 0xff's in a row; the second may actually be the beginning of the sync code */ - /* else we have to check if the second byte is the end of a sync code */ - if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */ - decoder->private_->lookahead = (FLAC__byte)x; - decoder->private_->cached = true; - } - else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */ - decoder->private_->header_warmup[1] = (FLAC__byte)x; - decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME; - return true; - } - } - if(first) { - send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC); - first = false; - } - } - - return true; -} - -FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FLAC__bool do_full_decode) -{ - unsigned channel; - unsigned i; - FLAC__int32 mid, side; - unsigned frame_crc; /* the one we calculate from the input stream */ - FLAC__uint32 x; - - *got_a_frame = false; - - /* init the CRC */ - frame_crc = 0; - frame_crc = FLAC__CRC16_UPDATE(decoder->private_->header_warmup[0], frame_crc); - frame_crc = FLAC__CRC16_UPDATE(decoder->private_->header_warmup[1], frame_crc); - FLAC__bitreader_reset_read_crc16(decoder->private_->input, (FLAC__uint16)frame_crc); - - if(!read_frame_header_(decoder)) - return false; - if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means we didn't sync on a valid header */ - return true; - if(!allocate_output_(decoder, decoder->private_->frame.header.blocksize, decoder->private_->frame.header.channels)) - return false; - for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) { - /* - * first figure the correct bits-per-sample of the subframe - */ - unsigned bps = decoder->private_->frame.header.bits_per_sample; - switch(decoder->private_->frame.header.channel_assignment) { - case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT: - /* no adjustment needed */ - break; - case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE: - FLAC__ASSERT(decoder->private_->frame.header.channels == 2); - if(channel == 1) - bps++; - break; - case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE: - FLAC__ASSERT(decoder->private_->frame.header.channels == 2); - if(channel == 0) - bps++; - break; - case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE: - FLAC__ASSERT(decoder->private_->frame.header.channels == 2); - if(channel == 1) - bps++; - break; - default: - FLAC__ASSERT(0); - } - /* - * now read it - */ - if(!read_subframe_(decoder, channel, bps, do_full_decode)) - return false; - if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */ - return true; - } - if(!read_zero_padding_(decoder)) - return false; - if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption (i.e. "zero bits" were not all zeroes) */ - return true; - - /* - * Read the frame CRC-16 from the footer and check - */ - frame_crc = FLAC__bitreader_get_read_crc16(decoder->private_->input); - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__FRAME_FOOTER_CRC_LEN)) - return false; /* read_callback_ sets the state for us */ - if(frame_crc == x) { - if(do_full_decode) { - /* Undo any special channel coding */ - switch(decoder->private_->frame.header.channel_assignment) { - case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT: - /* do nothing */ - break; - case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE: - FLAC__ASSERT(decoder->private_->frame.header.channels == 2); - for(i = 0; i < decoder->private_->frame.header.blocksize; i++) - decoder->private_->output[1][i] = decoder->private_->output[0][i] - decoder->private_->output[1][i]; - break; - case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE: - FLAC__ASSERT(decoder->private_->frame.header.channels == 2); - for(i = 0; i < decoder->private_->frame.header.blocksize; i++) - decoder->private_->output[0][i] += decoder->private_->output[1][i]; - break; - case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE: - FLAC__ASSERT(decoder->private_->frame.header.channels == 2); - for(i = 0; i < decoder->private_->frame.header.blocksize; i++) { -#if 1 - mid = decoder->private_->output[0][i]; - side = decoder->private_->output[1][i]; - mid <<= 1; - mid |= (side & 1); /* i.e. if 'side' is odd... */ - decoder->private_->output[0][i] = (mid + side) >> 1; - decoder->private_->output[1][i] = (mid - side) >> 1; -#else - /* OPT: without 'side' temp variable */ - mid = (decoder->private_->output[0][i] << 1) | (decoder->private_->output[1][i] & 1); /* i.e. if 'side' is odd... */ - decoder->private_->output[0][i] = (mid + decoder->private_->output[1][i]) >> 1; - decoder->private_->output[1][i] = (mid - decoder->private_->output[1][i]) >> 1; -#endif - } - break; - default: - FLAC__ASSERT(0); - break; - } - } - } - else { - /* Bad frame, emit error and zero the output signal */ - send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH); - if(do_full_decode) { - for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) { - memset(decoder->private_->output[channel], 0, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize); - } - } - } - - *got_a_frame = true; - - /* we wait to update fixed_block_size until here, when we're sure we've got a proper frame and hence a correct blocksize */ - if(decoder->private_->next_fixed_block_size) - decoder->private_->fixed_block_size = decoder->private_->next_fixed_block_size; - - /* put the latest values into the public section of the decoder instance */ - decoder->protected_->channels = decoder->private_->frame.header.channels; - decoder->protected_->channel_assignment = decoder->private_->frame.header.channel_assignment; - decoder->protected_->bits_per_sample = decoder->private_->frame.header.bits_per_sample; - decoder->protected_->sample_rate = decoder->private_->frame.header.sample_rate; - decoder->protected_->blocksize = decoder->private_->frame.header.blocksize; - - FLAC__ASSERT(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER); - decoder->private_->samples_decoded = decoder->private_->frame.header.number.sample_number + decoder->private_->frame.header.blocksize; - - /* write it */ - if(do_full_decode) { - if(write_audio_frame_to_client_(decoder, &decoder->private_->frame, (const FLAC__int32 * const *)decoder->private_->output) != FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE) - return false; - } - - decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; - return true; -} - -FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder) -{ - FLAC__uint32 x; - FLAC__uint64 xx; - unsigned i, blocksize_hint = 0, sample_rate_hint = 0; - FLAC__byte crc8, raw_header[16]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */ - unsigned raw_header_len; - FLAC__bool is_unparseable = false; - - FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)); - - /* init the raw header with the saved bits from synchronization */ - raw_header[0] = decoder->private_->header_warmup[0]; - raw_header[1] = decoder->private_->header_warmup[1]; - raw_header_len = 2; - - /* check to make sure that reserved bit is 0 */ - if(raw_header[1] & 0x02) /* MAGIC NUMBER */ - is_unparseable = true; - - /* - * Note that along the way as we read the header, we look for a sync - * code inside. If we find one it would indicate that our original - * sync was bad since there cannot be a sync code in a valid header. - * - * Three kinds of things can go wrong when reading the frame header: - * 1) We may have sync'ed incorrectly and not landed on a frame header. - * If we don't find a sync code, it can end up looking like we read - * a valid but unparseable header, until getting to the frame header - * CRC. Even then we could get a false positive on the CRC. - * 2) We may have sync'ed correctly but on an unparseable frame (from a - * future encoder). - * 3) We may be on a damaged frame which appears valid but unparseable. - * - * For all these reasons, we try and read a complete frame header as - * long as it seems valid, even if unparseable, up until the frame - * header CRC. - */ - - /* - * read in the raw header as bytes so we can CRC it, and parse it on the way - */ - for(i = 0; i < 2; i++) { - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) - return false; /* read_callback_ sets the state for us */ - if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */ - /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */ - decoder->private_->lookahead = (FLAC__byte)x; - decoder->private_->cached = true; - send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER); - decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; - return true; - } - raw_header[raw_header_len++] = (FLAC__byte)x; - } - - switch(x = raw_header[2] >> 4) { - case 0: - is_unparseable = true; - break; - case 1: - decoder->private_->frame.header.blocksize = 192; - break; - case 2: - case 3: - case 4: - case 5: - decoder->private_->frame.header.blocksize = 576 << (x-2); - break; - case 6: - case 7: - blocksize_hint = x; - break; - case 8: - case 9: - case 10: - case 11: - case 12: - case 13: - case 14: - case 15: - decoder->private_->frame.header.blocksize = 256 << (x-8); - break; - default: - FLAC__ASSERT(0); - break; - } - - switch(x = raw_header[2] & 0x0f) { - case 0: - if(decoder->private_->has_stream_info) - decoder->private_->frame.header.sample_rate = decoder->private_->stream_info.data.stream_info.sample_rate; - else - is_unparseable = true; - break; - case 1: - decoder->private_->frame.header.sample_rate = 88200; - break; - case 2: - decoder->private_->frame.header.sample_rate = 176400; - break; - case 3: - decoder->private_->frame.header.sample_rate = 192000; - break; - case 4: - decoder->private_->frame.header.sample_rate = 8000; - break; - case 5: - decoder->private_->frame.header.sample_rate = 16000; - break; - case 6: - decoder->private_->frame.header.sample_rate = 22050; - break; - case 7: - decoder->private_->frame.header.sample_rate = 24000; - break; - case 8: - decoder->private_->frame.header.sample_rate = 32000; - break; - case 9: - decoder->private_->frame.header.sample_rate = 44100; - break; - case 10: - decoder->private_->frame.header.sample_rate = 48000; - break; - case 11: - decoder->private_->frame.header.sample_rate = 96000; - break; - case 12: - case 13: - case 14: - sample_rate_hint = x; - break; - case 15: - send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER); - decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; - return true; - default: - FLAC__ASSERT(0); - } - - x = (unsigned)(raw_header[3] >> 4); - if(x & 8) { - decoder->private_->frame.header.channels = 2; - switch(x & 7) { - case 0: - decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE; - break; - case 1: - decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE; - break; - case 2: - decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE; - break; - default: - is_unparseable = true; - break; - } - } - else { - decoder->private_->frame.header.channels = (unsigned)x + 1; - decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; - } - - switch(x = (unsigned)(raw_header[3] & 0x0e) >> 1) { - case 0: - if(decoder->private_->has_stream_info) - decoder->private_->frame.header.bits_per_sample = decoder->private_->stream_info.data.stream_info.bits_per_sample; - else - is_unparseable = true; - break; - case 1: - decoder->private_->frame.header.bits_per_sample = 8; - break; - case 2: - decoder->private_->frame.header.bits_per_sample = 12; - break; - case 4: - decoder->private_->frame.header.bits_per_sample = 16; - break; - case 5: - decoder->private_->frame.header.bits_per_sample = 20; - break; - case 6: - decoder->private_->frame.header.bits_per_sample = 24; - break; - case 3: - case 7: - is_unparseable = true; - break; - default: - FLAC__ASSERT(0); - break; - } - - /* check to make sure that reserved bit is 0 */ - if(raw_header[3] & 0x01) /* MAGIC NUMBER */ - is_unparseable = true; - - /* read the frame's starting sample number (or frame number as the case may be) */ - if( - raw_header[1] & 0x01 || - /*@@@ this clause is a concession to the old way of doing variable blocksize; the only known implementation is flake and can probably be removed without inconveniencing anyone */ - (decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.min_blocksize != decoder->private_->stream_info.data.stream_info.max_blocksize) - ) { /* variable blocksize */ - if(!FLAC__bitreader_read_utf8_uint64(decoder->private_->input, &xx, raw_header, &raw_header_len)) - return false; /* read_callback_ sets the state for us */ - if(xx == FLAC__U64L(0xffffffffffffffff)) { /* i.e. non-UTF8 code... */ - decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */ - decoder->private_->cached = true; - send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER); - decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; - return true; - } - decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER; - decoder->private_->frame.header.number.sample_number = xx; - } - else { /* fixed blocksize */ - if(!FLAC__bitreader_read_utf8_uint32(decoder->private_->input, &x, raw_header, &raw_header_len)) - return false; /* read_callback_ sets the state for us */ - if(x == 0xffffffff) { /* i.e. non-UTF8 code... */ - decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */ - decoder->private_->cached = true; - send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER); - decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; - return true; - } - decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER; - decoder->private_->frame.header.number.frame_number = x; - } - - if(blocksize_hint) { - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) - return false; /* read_callback_ sets the state for us */ - raw_header[raw_header_len++] = (FLAC__byte)x; - if(blocksize_hint == 7) { - FLAC__uint32 _x; - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &_x, 8)) - return false; /* read_callback_ sets the state for us */ - raw_header[raw_header_len++] = (FLAC__byte)_x; - x = (x << 8) | _x; - } - decoder->private_->frame.header.blocksize = x+1; - } - - if(sample_rate_hint) { - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) - return false; /* read_callback_ sets the state for us */ - raw_header[raw_header_len++] = (FLAC__byte)x; - if(sample_rate_hint != 12) { - FLAC__uint32 _x; - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &_x, 8)) - return false; /* read_callback_ sets the state for us */ - raw_header[raw_header_len++] = (FLAC__byte)_x; - x = (x << 8) | _x; - } - if(sample_rate_hint == 12) - decoder->private_->frame.header.sample_rate = x*1000; - else if(sample_rate_hint == 13) - decoder->private_->frame.header.sample_rate = x; - else - decoder->private_->frame.header.sample_rate = x*10; - } - - /* read the CRC-8 byte */ - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) - return false; /* read_callback_ sets the state for us */ - crc8 = (FLAC__byte)x; - - if(FLAC__crc8(raw_header, raw_header_len) != crc8) { - send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER); - decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; - return true; - } - - /* calculate the sample number from the frame number if needed */ - decoder->private_->next_fixed_block_size = 0; - if(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER) { - x = decoder->private_->frame.header.number.frame_number; - decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER; - if(decoder->private_->fixed_block_size) - decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->fixed_block_size * (FLAC__uint64)x; - else if(decoder->private_->has_stream_info) { - if(decoder->private_->stream_info.data.stream_info.min_blocksize == decoder->private_->stream_info.data.stream_info.max_blocksize) { - decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->stream_info.data.stream_info.min_blocksize * (FLAC__uint64)x; - decoder->private_->next_fixed_block_size = decoder->private_->stream_info.data.stream_info.max_blocksize; - } - else - is_unparseable = true; - } - else if(x == 0) { - decoder->private_->frame.header.number.sample_number = 0; - decoder->private_->next_fixed_block_size = decoder->private_->frame.header.blocksize; - } - else { - /* can only get here if the stream has invalid frame numbering and no STREAMINFO, so assume it's not the last (possibly short) frame */ - decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->frame.header.blocksize * (FLAC__uint64)x; - } - } - - if(is_unparseable) { - send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM); - decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; - return true; - } - - return true; -} - -FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode) -{ - FLAC__uint32 x; - FLAC__bool wasted_bits; - unsigned i; - - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) /* MAGIC NUMBER */ - return false; /* read_callback_ sets the state for us */ - - wasted_bits = (x & 1); - x &= 0xfe; - - if(wasted_bits) { - unsigned u; - if(!FLAC__bitreader_read_unary_unsigned(decoder->private_->input, &u)) - return false; /* read_callback_ sets the state for us */ - decoder->private_->frame.subframes[channel].wasted_bits = u+1; - bps -= decoder->private_->frame.subframes[channel].wasted_bits; - } - else - decoder->private_->frame.subframes[channel].wasted_bits = 0; - - /* - * Lots of magic numbers here - */ - if(x & 0x80) { - send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC); - decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; - return true; - } - else if(x == 0) { - if(!read_subframe_constant_(decoder, channel, bps, do_full_decode)) - return false; - } - else if(x == 2) { - if(!read_subframe_verbatim_(decoder, channel, bps, do_full_decode)) - return false; - } - else if(x < 16) { - send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM); - decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; - return true; - } - else if(x <= 24) { - if(!read_subframe_fixed_(decoder, channel, bps, (x>>1)&7, do_full_decode)) - return false; - if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */ - return true; - } - else if(x < 64) { - send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM); - decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; - return true; - } - else { - if(!read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1, do_full_decode)) - return false; - if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */ - return true; - } - - if(wasted_bits && do_full_decode) { - x = decoder->private_->frame.subframes[channel].wasted_bits; - for(i = 0; i < decoder->private_->frame.header.blocksize; i++) - decoder->private_->output[channel][i] <<= x; - } - - return true; -} - -FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode) -{ - FLAC__Subframe_Constant *subframe = &decoder->private_->frame.subframes[channel].data.constant; - FLAC__int32 x; - unsigned i; - FLAC__int32 *output = decoder->private_->output[channel]; - - decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT; - - if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &x, bps)) - return false; /* read_callback_ sets the state for us */ - - subframe->value = x; - - /* decode the subframe */ - if(do_full_decode) { - for(i = 0; i < decoder->private_->frame.header.blocksize; i++) - output[i] = x; - } - - return true; -} - -FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode) -{ - FLAC__Subframe_Fixed *subframe = &decoder->private_->frame.subframes[channel].data.fixed; - FLAC__int32 i32; - FLAC__uint32 u32; - unsigned u; - - decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED; - - subframe->residual = decoder->private_->residual[channel]; - subframe->order = order; - - /* read warm-up samples */ - for(u = 0; u < order; u++) { - if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, bps)) - return false; /* read_callback_ sets the state for us */ - subframe->warmup[u] = i32; - } - - /* read entropy coding method info */ - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN)) - return false; /* read_callback_ sets the state for us */ - subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32; - switch(subframe->entropy_coding_method.type) { - case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE: - case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2: - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN)) - return false; /* read_callback_ sets the state for us */ - subframe->entropy_coding_method.data.partitioned_rice.order = u32; - subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel]; - break; - default: - send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM); - decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; - return true; - } - - /* read residual */ - switch(subframe->entropy_coding_method.type) { - case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE: - case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2: - if(!read_residual_partitioned_rice_(decoder, order, subframe->entropy_coding_method.data.partitioned_rice.order, &decoder->private_->partitioned_rice_contents[channel], decoder->private_->residual[channel], /*is_extended=*/subframe->entropy_coding_method.type == FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2)) - return false; - break; - default: - FLAC__ASSERT(0); - } - - /* decode the subframe */ - if(do_full_decode) { - memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order); - FLAC__fixed_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, order, decoder->private_->output[channel]+order); - } - - return true; -} - -FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode) -{ - FLAC__Subframe_LPC *subframe = &decoder->private_->frame.subframes[channel].data.lpc; - FLAC__int32 i32; - FLAC__uint32 u32; - unsigned u; - - decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC; - - subframe->residual = decoder->private_->residual[channel]; - subframe->order = order; - - /* read warm-up samples */ - for(u = 0; u < order; u++) { - if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, bps)) - return false; /* read_callback_ sets the state for us */ - subframe->warmup[u] = i32; - } - - /* read qlp coeff precision */ - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN)) - return false; /* read_callback_ sets the state for us */ - if(u32 == (1u << FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN) - 1) { - send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC); - decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; - return true; - } - subframe->qlp_coeff_precision = u32+1; - - /* read qlp shift */ - if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN)) - return false; /* read_callback_ sets the state for us */ - subframe->quantization_level = i32; - - /* read quantized lp coefficiencts */ - for(u = 0; u < order; u++) { - if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, subframe->qlp_coeff_precision)) - return false; /* read_callback_ sets the state for us */ - subframe->qlp_coeff[u] = i32; - } - - /* read entropy coding method info */ - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN)) - return false; /* read_callback_ sets the state for us */ - subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32; - switch(subframe->entropy_coding_method.type) { - case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE: - case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2: - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN)) - return false; /* read_callback_ sets the state for us */ - subframe->entropy_coding_method.data.partitioned_rice.order = u32; - subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel]; - break; - default: - send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM); - decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; - return true; - } - - /* read residual */ - switch(subframe->entropy_coding_method.type) { - case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE: - case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2: - if(!read_residual_partitioned_rice_(decoder, order, subframe->entropy_coding_method.data.partitioned_rice.order, &decoder->private_->partitioned_rice_contents[channel], decoder->private_->residual[channel], /*is_extended=*/subframe->entropy_coding_method.type == FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2)) - return false; - break; - default: - FLAC__ASSERT(0); - } - - /* decode the subframe */ - if(do_full_decode) { - memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order); - /*@@@@@@ technically not pessimistic enough, should be more like - if( (FLAC__uint64)order * ((((FLAC__uint64)1)<qlp_coeff_precision)-1) < (((FLAC__uint64)-1) << 32) ) - */ - if(bps + subframe->qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32) - if(bps <= 16 && subframe->qlp_coeff_precision <= 16) { - if(order <= 8) - decoder->private_->local_lpc_restore_signal_16bit_order8(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order); - else - decoder->private_->local_lpc_restore_signal_16bit(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order); - } - else - decoder->private_->local_lpc_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order); - else - decoder->private_->local_lpc_restore_signal_64bit(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order); - } - - return true; -} - -FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode) -{ - FLAC__Subframe_Verbatim *subframe = &decoder->private_->frame.subframes[channel].data.verbatim; - FLAC__int32 x, *residual = decoder->private_->residual[channel]; - unsigned i; - - decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM; - - subframe->data = residual; - - for(i = 0; i < decoder->private_->frame.header.blocksize; i++) { - if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &x, bps)) - return false; /* read_callback_ sets the state for us */ - residual[i] = x; - } - - /* decode the subframe */ - if(do_full_decode) - memcpy(decoder->private_->output[channel], subframe->data, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize); - - return true; -} - -FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, FLAC__int32 *residual, FLAC__bool is_extended) -{ - FLAC__uint32 rice_parameter; - int i; - unsigned partition, sample, u; - const unsigned partitions = 1u << partition_order; - const unsigned partition_samples = partition_order > 0? decoder->private_->frame.header.blocksize >> partition_order : decoder->private_->frame.header.blocksize - predictor_order; - const unsigned plen = is_extended? FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN : FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN; - const unsigned pesc = is_extended? FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER : FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER; - - /* sanity checks */ - if(partition_order == 0) { - if(decoder->private_->frame.header.blocksize < predictor_order) { - send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC); - decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; - return true; - } - } - else { - if(partition_samples < predictor_order) { - send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC); - decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; - return true; - } - } - - if(!FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order))) { - decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; - return false; - } - - sample = 0; - for(partition = 0; partition < partitions; partition++) { - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &rice_parameter, plen)) - return false; /* read_callback_ sets the state for us */ - partitioned_rice_contents->parameters[partition] = rice_parameter; - if(rice_parameter < pesc) { - partitioned_rice_contents->raw_bits[partition] = 0; - u = (partition_order == 0 || partition > 0)? partition_samples : partition_samples - predictor_order; - if(!decoder->private_->local_bitreader_read_rice_signed_block(decoder->private_->input, residual + sample, u, rice_parameter)) - return false; /* read_callback_ sets the state for us */ - sample += u; - } - else { - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN)) - return false; /* read_callback_ sets the state for us */ - partitioned_rice_contents->raw_bits[partition] = rice_parameter; - for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) { - if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i, rice_parameter)) - return false; /* read_callback_ sets the state for us */ - residual[sample] = i; - } - } - } - - return true; -} - -FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder) -{ - if(!FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)) { - FLAC__uint32 zero = 0; - if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &zero, FLAC__bitreader_bits_left_for_byte_alignment(decoder->private_->input))) - return false; /* read_callback_ sets the state for us */ - if(zero != 0) { - send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC); - decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; - } - } - return true; -} - -FLAC__bool read_callback_(FLAC__byte buffer[], size_t *bytes, void *client_data) -{ - FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data; - - if( -#if FLAC__HAS_OGG - /* see [1] HACK NOTE below for why we don't call the eof_callback when decoding Ogg FLAC */ - !decoder->private_->is_ogg && -#endif - decoder->private_->eof_callback && decoder->private_->eof_callback(decoder, decoder->private_->client_data) - ) { - *bytes = 0; - decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM; - return false; - } - else if(*bytes > 0) { - /* While seeking, it is possible for our seek to land in the - * middle of audio data that looks exactly like a frame header - * from a future version of an encoder. When that happens, our - * error callback will get an - * FLAC__STREAM_DECODER_UNPARSEABLE_STREAM and increment its - * unparseable_frame_count. But there is a remote possibility - * that it is properly synced at such a "future-codec frame", - * so to make sure, we wait to see many "unparseable" errors in - * a row before bailing out. - */ - if(decoder->private_->is_seeking && decoder->private_->unparseable_frame_count > 20) { - decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED; - return false; - } - else { - const FLAC__StreamDecoderReadStatus status = -#if FLAC__HAS_OGG - decoder->private_->is_ogg? - read_callback_ogg_aspect_(decoder, buffer, bytes) : -#endif - decoder->private_->read_callback(decoder, buffer, bytes, decoder->private_->client_data) - ; - if(status == FLAC__STREAM_DECODER_READ_STATUS_ABORT) { - decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED; - return false; - } - else if(*bytes == 0) { - if( - status == FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM || - ( -#if FLAC__HAS_OGG - /* see [1] HACK NOTE below for why we don't call the eof_callback when decoding Ogg FLAC */ - !decoder->private_->is_ogg && -#endif - decoder->private_->eof_callback && decoder->private_->eof_callback(decoder, decoder->private_->client_data) - ) - ) { - decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM; - return false; - } - else - return true; - } - else - return true; - } - } - else { - /* abort to avoid a deadlock */ - decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED; - return false; - } - /* [1] @@@ HACK NOTE: The end-of-stream checking has to be hacked around - * for Ogg FLAC. This is because the ogg decoder aspect can lose sync - * and at the same time hit the end of the stream (for example, seeking - * to a point that is after the beginning of the last Ogg page). There - * is no way to report an Ogg sync loss through the callbacks (see note - * in read_callback_ogg_aspect_()) so it returns CONTINUE with *bytes==0. - * So to keep the decoder from stopping at this point we gate the call - * to the eof_callback and let the Ogg decoder aspect set the - * end-of-stream state when it is needed. - */ -} - -#if FLAC__HAS_OGG -FLAC__StreamDecoderReadStatus read_callback_ogg_aspect_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes) -{ - switch(FLAC__ogg_decoder_aspect_read_callback_wrapper(&decoder->protected_->ogg_decoder_aspect, buffer, bytes, read_callback_proxy_, decoder, decoder->private_->client_data)) { - case FLAC__OGG_DECODER_ASPECT_READ_STATUS_OK: - return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; - /* we don't really have a way to handle lost sync via read - * callback so we'll let it pass and let the underlying - * FLAC decoder catch the error - */ - case FLAC__OGG_DECODER_ASPECT_READ_STATUS_LOST_SYNC: - return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; - case FLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_STREAM: - return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM; - case FLAC__OGG_DECODER_ASPECT_READ_STATUS_NOT_FLAC: - case FLAC__OGG_DECODER_ASPECT_READ_STATUS_UNSUPPORTED_MAPPING_VERSION: - case FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT: - case FLAC__OGG_DECODER_ASPECT_READ_STATUS_ERROR: - case FLAC__OGG_DECODER_ASPECT_READ_STATUS_MEMORY_ALLOCATION_ERROR: - return FLAC__STREAM_DECODER_READ_STATUS_ABORT; - default: - FLAC__ASSERT(0); - /* double protection */ - return FLAC__STREAM_DECODER_READ_STATUS_ABORT; - } -} - -FLAC__OggDecoderAspectReadStatus read_callback_proxy_(const void *void_decoder, FLAC__byte buffer[], size_t *bytes, void *client_data) -{ - FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder*)void_decoder; - - switch(decoder->private_->read_callback(decoder, buffer, bytes, client_data)) { - case FLAC__STREAM_DECODER_READ_STATUS_CONTINUE: - return FLAC__OGG_DECODER_ASPECT_READ_STATUS_OK; - case FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM: - return FLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_STREAM; - case FLAC__STREAM_DECODER_READ_STATUS_ABORT: - return FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT; - default: - /* double protection: */ - FLAC__ASSERT(0); - return FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT; - } -} -#endif - -FLAC__StreamDecoderWriteStatus write_audio_frame_to_client_(FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[]) -{ - if(decoder->private_->is_seeking) { - FLAC__uint64 this_frame_sample = frame->header.number.sample_number; - FLAC__uint64 next_frame_sample = this_frame_sample + (FLAC__uint64)frame->header.blocksize; - FLAC__uint64 target_sample = decoder->private_->target_sample; - - FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER); - -#if FLAC__HAS_OGG - decoder->private_->got_a_frame = true; -#endif - decoder->private_->last_frame = *frame; /* save the frame */ - if(this_frame_sample <= target_sample && target_sample < next_frame_sample) { /* we hit our target frame */ - unsigned delta = (unsigned)(target_sample - this_frame_sample); - /* kick out of seek mode */ - decoder->private_->is_seeking = false; - /* shift out the samples before target_sample */ - if(delta > 0) { - unsigned channel; - const FLAC__int32 *newbuffer[FLAC__MAX_CHANNELS]; - for(channel = 0; channel < frame->header.channels; channel++) - newbuffer[channel] = buffer[channel] + delta; - decoder->private_->last_frame.header.blocksize -= delta; - decoder->private_->last_frame.header.number.sample_number += (FLAC__uint64)delta; - /* write the relevant samples */ - return decoder->private_->write_callback(decoder, &decoder->private_->last_frame, newbuffer, decoder->private_->client_data); - } - else { - /* write the relevant samples */ - return decoder->private_->write_callback(decoder, frame, buffer, decoder->private_->client_data); - } - } - else { - return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE; - } - } - else { - /* - * If we never got STREAMINFO, turn off MD5 checking to save - * cycles since we don't have a sum to compare to anyway - */ - if(!decoder->private_->has_stream_info) - decoder->private_->do_md5_checking = false; - if(decoder->private_->do_md5_checking) { - if(!FLAC__MD5Accumulate(&decoder->private_->md5context, buffer, frame->header.channels, frame->header.blocksize, (frame->header.bits_per_sample+7) / 8)) - return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; - } - return decoder->private_->write_callback(decoder, frame, buffer, decoder->private_->client_data); - } -} - -void send_error_to_client_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status) -{ - if(!decoder->private_->is_seeking) - decoder->private_->error_callback(decoder, status, decoder->private_->client_data); - else if(status == FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM) - decoder->private_->unparseable_frame_count++; -} - -FLAC__bool seek_to_absolute_sample_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample) -{ - FLAC__uint64 first_frame_offset = decoder->private_->first_frame_offset, lower_bound, upper_bound, lower_bound_sample, upper_bound_sample, this_frame_sample; - FLAC__int64 pos = -1; - int i; - unsigned approx_bytes_per_frame; - FLAC__bool first_seek = true; - const FLAC__uint64 total_samples = FLAC__stream_decoder_get_total_samples(decoder); - const unsigned min_blocksize = decoder->private_->stream_info.data.stream_info.min_blocksize; - const unsigned max_blocksize = decoder->private_->stream_info.data.stream_info.max_blocksize; - const unsigned max_framesize = decoder->private_->stream_info.data.stream_info.max_framesize; - const unsigned min_framesize = decoder->private_->stream_info.data.stream_info.min_framesize; - /* take these from the current frame in case they've changed mid-stream */ - unsigned channels = FLAC__stream_decoder_get_channels(decoder); - unsigned bps = FLAC__stream_decoder_get_bits_per_sample(decoder); - const FLAC__StreamMetadata_SeekTable *seek_table = decoder->private_->has_seek_table? &decoder->private_->seek_table.data.seek_table : 0; - - /* use values from stream info if we didn't decode a frame */ - if(channels == 0) - channels = decoder->private_->stream_info.data.stream_info.channels; - if(bps == 0) - bps = decoder->private_->stream_info.data.stream_info.bits_per_sample; - - /* we are just guessing here */ - if(max_framesize > 0) - approx_bytes_per_frame = (max_framesize + min_framesize) / 2 + 1; - /* - * Check if it's a known fixed-blocksize stream. Note that though - * the spec doesn't allow zeroes in the STREAMINFO block, we may - * never get a STREAMINFO block when decoding so the value of - * min_blocksize might be zero. - */ - else if(min_blocksize == max_blocksize && min_blocksize > 0) { - /* note there are no () around 'bps/8' to keep precision up since it's an integer calulation */ - approx_bytes_per_frame = min_blocksize * channels * bps/8 + 64; - } - else - approx_bytes_per_frame = 4096 * channels * bps/8 + 64; - - /* - * First, we set an upper and lower bound on where in the - * stream we will search. For now we assume the worst case - * scenario, which is our best guess at the beginning of - * the first frame and end of the stream. - */ - lower_bound = first_frame_offset; - lower_bound_sample = 0; - upper_bound = stream_length; - upper_bound_sample = total_samples > 0 ? total_samples : target_sample /*estimate it*/; - - /* - * Now we refine the bounds if we have a seektable with - * suitable points. Note that according to the spec they - * must be ordered by ascending sample number. - * - * Note: to protect against invalid seek tables we will ignore points - * that have frame_samples==0 or sample_number>=total_samples - */ - if(seek_table) { - FLAC__uint64 new_lower_bound = lower_bound; - FLAC__uint64 new_upper_bound = upper_bound; - FLAC__uint64 new_lower_bound_sample = lower_bound_sample; - FLAC__uint64 new_upper_bound_sample = upper_bound_sample; - - /* find the closest seek point <= target_sample, if it exists */ - for(i = (int)seek_table->num_points - 1; i >= 0; i--) { - if( - seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER && - seek_table->points[i].frame_samples > 0 && /* defense against bad seekpoints */ - (total_samples <= 0 || seek_table->points[i].sample_number < total_samples) && /* defense against bad seekpoints */ - seek_table->points[i].sample_number <= target_sample - ) - break; - } - if(i >= 0) { /* i.e. we found a suitable seek point... */ - new_lower_bound = first_frame_offset + seek_table->points[i].stream_offset; - new_lower_bound_sample = seek_table->points[i].sample_number; - } - - /* find the closest seek point > target_sample, if it exists */ - for(i = 0; i < (int)seek_table->num_points; i++) { - if( - seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER && - seek_table->points[i].frame_samples > 0 && /* defense against bad seekpoints */ - (total_samples <= 0 || seek_table->points[i].sample_number < total_samples) && /* defense against bad seekpoints */ - seek_table->points[i].sample_number > target_sample - ) - break; - } - if(i < (int)seek_table->num_points) { /* i.e. we found a suitable seek point... */ - new_upper_bound = first_frame_offset + seek_table->points[i].stream_offset; - new_upper_bound_sample = seek_table->points[i].sample_number; - } - /* final protection against unsorted seek tables; keep original values if bogus */ - if(new_upper_bound >= new_lower_bound) { - lower_bound = new_lower_bound; - upper_bound = new_upper_bound; - lower_bound_sample = new_lower_bound_sample; - upper_bound_sample = new_upper_bound_sample; - } - } - - FLAC__ASSERT(upper_bound_sample >= lower_bound_sample); - /* there are 2 insidious ways that the following equality occurs, which - * we need to fix: - * 1) total_samples is 0 (unknown) and target_sample is 0 - * 2) total_samples is 0 (unknown) and target_sample happens to be - * exactly equal to the last seek point in the seek table; this - * means there is no seek point above it, and upper_bound_samples - * remains equal to the estimate (of target_samples) we made above - * in either case it does not hurt to move upper_bound_sample up by 1 - */ - if(upper_bound_sample == lower_bound_sample) - upper_bound_sample++; - - decoder->private_->target_sample = target_sample; - while(1) { - /* check if the bounds are still ok */ - if (lower_bound_sample >= upper_bound_sample || lower_bound > upper_bound) { - decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; - return false; - } -#ifndef FLAC__INTEGER_ONLY_LIBRARY -#if defined _MSC_VER || defined __MINGW32__ - /* with VC++ you have to spoon feed it the casting */ - pos = (FLAC__int64)lower_bound + (FLAC__int64)((FLAC__double)(FLAC__int64)(target_sample - lower_bound_sample) / (FLAC__double)(FLAC__int64)(upper_bound_sample - lower_bound_sample) * (FLAC__double)(FLAC__int64)(upper_bound - lower_bound)) - approx_bytes_per_frame; -#else - pos = (FLAC__int64)lower_bound + (FLAC__int64)((FLAC__double)(target_sample - lower_bound_sample) / (FLAC__double)(upper_bound_sample - lower_bound_sample) * (FLAC__double)(upper_bound - lower_bound)) - approx_bytes_per_frame; -#endif -#else - /* a little less accurate: */ - if(upper_bound - lower_bound < 0xffffffff) - pos = (FLAC__int64)lower_bound + (FLAC__int64)(((target_sample - lower_bound_sample) * (upper_bound - lower_bound)) / (upper_bound_sample - lower_bound_sample)) - approx_bytes_per_frame; - else /* @@@ WATCHOUT, ~2TB limit */ - pos = (FLAC__int64)lower_bound + (FLAC__int64)((((target_sample - lower_bound_sample)>>8) * ((upper_bound - lower_bound)>>8)) / ((upper_bound_sample - lower_bound_sample)>>16)) - approx_bytes_per_frame; -#endif - if(pos >= (FLAC__int64)upper_bound) - pos = (FLAC__int64)upper_bound - 1; - if(pos < (FLAC__int64)lower_bound) - pos = (FLAC__int64)lower_bound; - if(decoder->private_->seek_callback(decoder, (FLAC__uint64)pos, decoder->private_->client_data) != FLAC__STREAM_DECODER_SEEK_STATUS_OK) { - decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; - return false; - } - if(!FLAC__stream_decoder_flush(decoder)) { - /* above call sets the state for us */ - return false; - } - /* Now we need to get a frame. First we need to reset our - * unparseable_frame_count; if we get too many unparseable - * frames in a row, the read callback will return - * FLAC__STREAM_DECODER_READ_STATUS_ABORT, causing - * FLAC__stream_decoder_process_single() to return false. - */ - decoder->private_->unparseable_frame_count = 0; - if(!FLAC__stream_decoder_process_single(decoder)) { - decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; - return false; - } - /* our write callback will change the state when it gets to the target frame */ - /* actually, we could have got_a_frame if our decoder is at FLAC__STREAM_DECODER_END_OF_STREAM so we need to check for that also */ -#if 0 - /*@@@@@@ used to be the following; not clear if the check for end of stream is needed anymore */ - if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_SEEKING && decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_STREAM) - break; -#endif - if(!decoder->private_->is_seeking) - break; - - FLAC__ASSERT(decoder->private_->last_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER); - this_frame_sample = decoder->private_->last_frame.header.number.sample_number; - - if (0 == decoder->private_->samples_decoded || (this_frame_sample + decoder->private_->last_frame.header.blocksize >= upper_bound_sample && !first_seek)) { - if (pos == (FLAC__int64)lower_bound) { - /* can't move back any more than the first frame, something is fatally wrong */ - decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; - return false; - } - /* our last move backwards wasn't big enough, try again */ - approx_bytes_per_frame = approx_bytes_per_frame? approx_bytes_per_frame * 2 : 16; - continue; - } - /* allow one seek over upper bound, so we can get a correct upper_bound_sample for streams with unknown total_samples */ - first_seek = false; - - /* make sure we are not seeking in corrupted stream */ - if (this_frame_sample < lower_bound_sample) { - decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; - return false; - } - - /* we need to narrow the search */ - if(target_sample < this_frame_sample) { - upper_bound_sample = this_frame_sample + decoder->private_->last_frame.header.blocksize; -/*@@@@@@ what will decode position be if at end of stream? */ - if(!FLAC__stream_decoder_get_decode_position(decoder, &upper_bound)) { - decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; - return false; - } - approx_bytes_per_frame = (unsigned)(2 * (upper_bound - pos) / 3 + 16); - } - else { /* target_sample >= this_frame_sample + this frame's blocksize */ - lower_bound_sample = this_frame_sample + decoder->private_->last_frame.header.blocksize; - if(!FLAC__stream_decoder_get_decode_position(decoder, &lower_bound)) { - decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; - return false; - } - approx_bytes_per_frame = (unsigned)(2 * (lower_bound - pos) / 3 + 16); - } - } - - return true; -} - -#if FLAC__HAS_OGG -FLAC__bool seek_to_absolute_sample_ogg_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample) -{ - FLAC__uint64 left_pos = 0, right_pos = stream_length; - FLAC__uint64 left_sample = 0, right_sample = FLAC__stream_decoder_get_total_samples(decoder); - FLAC__uint64 this_frame_sample = (FLAC__uint64)0 - 1; - FLAC__uint64 pos = 0; /* only initialized to avoid compiler warning */ - FLAC__bool did_a_seek; - unsigned iteration = 0; - - /* In the first iterations, we will calculate the target byte position - * by the distance from the target sample to left_sample and - * right_sample (let's call it "proportional search"). After that, we - * will switch to binary search. - */ - unsigned BINARY_SEARCH_AFTER_ITERATION = 2; - - /* We will switch to a linear search once our current sample is less - * than this number of samples ahead of the target sample - */ - static const FLAC__uint64 LINEAR_SEARCH_WITHIN_SAMPLES = FLAC__MAX_BLOCK_SIZE * 2; - - /* If the total number of samples is unknown, use a large value, and - * force binary search immediately. - */ - if(right_sample == 0) { - right_sample = (FLAC__uint64)(-1); - BINARY_SEARCH_AFTER_ITERATION = 0; - } - - decoder->private_->target_sample = target_sample; - for( ; ; iteration++) { - if (iteration == 0 || this_frame_sample > target_sample || target_sample - this_frame_sample > LINEAR_SEARCH_WITHIN_SAMPLES) { - if (iteration >= BINARY_SEARCH_AFTER_ITERATION) { - pos = (right_pos + left_pos) / 2; - } - else { -#ifndef FLAC__INTEGER_ONLY_LIBRARY -#if defined _MSC_VER || defined __MINGW32__ - /* with MSVC you have to spoon feed it the casting */ - pos = (FLAC__uint64)((FLAC__double)(FLAC__int64)(target_sample - left_sample) / (FLAC__double)(FLAC__int64)(right_sample - left_sample) * (FLAC__double)(FLAC__int64)(right_pos - left_pos)); -#else - pos = (FLAC__uint64)((FLAC__double)(target_sample - left_sample) / (FLAC__double)(right_sample - left_sample) * (FLAC__double)(right_pos - left_pos)); -#endif -#else - /* a little less accurate: */ - if ((target_sample-left_sample <= 0xffffffff) && (right_pos-left_pos <= 0xffffffff)) - pos = (FLAC__int64)(((target_sample-left_sample) * (right_pos-left_pos)) / (right_sample-left_sample)); - else /* @@@ WATCHOUT, ~2TB limit */ - pos = (FLAC__int64)((((target_sample-left_sample)>>8) * ((right_pos-left_pos)>>8)) / ((right_sample-left_sample)>>16)); -#endif - /* @@@ TODO: might want to limit pos to some distance - * before EOF, to make sure we land before the last frame, - * thereby getting a this_frame_sample and so having a better - * estimate. - */ - } - - /* physical seek */ - if(decoder->private_->seek_callback((FLAC__StreamDecoder*)decoder, (FLAC__uint64)pos, decoder->private_->client_data) != FLAC__STREAM_DECODER_SEEK_STATUS_OK) { - decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; - return false; - } - if(!FLAC__stream_decoder_flush(decoder)) { - /* above call sets the state for us */ - return false; - } - did_a_seek = true; - } - else - did_a_seek = false; - - decoder->private_->got_a_frame = false; - if(!FLAC__stream_decoder_process_single(decoder)) { - decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; - return false; - } - if(!decoder->private_->got_a_frame) { - if(did_a_seek) { - /* this can happen if we seek to a point after the last frame; we drop - * to binary search right away in this case to avoid any wasted - * iterations of proportional search. - */ - right_pos = pos; - BINARY_SEARCH_AFTER_ITERATION = 0; - } - else { - /* this can probably only happen if total_samples is unknown and the - * target_sample is past the end of the stream - */ - decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; - return false; - } - } - /* our write callback will change the state when it gets to the target frame */ - else if(!decoder->private_->is_seeking) { - break; - } - else { - this_frame_sample = decoder->private_->last_frame.header.number.sample_number; - FLAC__ASSERT(decoder->private_->last_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER); - - if (did_a_seek) { - if (this_frame_sample <= target_sample) { - /* The 'equal' case should not happen, since - * FLAC__stream_decoder_process_single() - * should recognize that it has hit the - * target sample and we would exit through - * the 'break' above. - */ - FLAC__ASSERT(this_frame_sample != target_sample); - - left_sample = this_frame_sample; - /* sanity check to avoid infinite loop */ - if (left_pos == pos) { - decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; - return false; - } - left_pos = pos; - } - else if(this_frame_sample > target_sample) { - right_sample = this_frame_sample; - /* sanity check to avoid infinite loop */ - if (right_pos == pos) { - decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; - return false; - } - right_pos = pos; - } - } - } - } - - return true; -} -#endif - -FLAC__StreamDecoderReadStatus file_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data) -{ - (void)client_data; - - if(*bytes > 0) { - *bytes = fread(buffer, sizeof(FLAC__byte), *bytes, decoder->private_->file); - if(ferror(decoder->private_->file)) - return FLAC__STREAM_DECODER_READ_STATUS_ABORT; - else if(*bytes == 0) - return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM; - else - return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; - } - else - return FLAC__STREAM_DECODER_READ_STATUS_ABORT; /* abort to avoid a deadlock */ -} - -FLAC__StreamDecoderSeekStatus file_seek_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data) -{ - (void)client_data; - - if(decoder->private_->file == stdin) - return FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED; - else if(fseeko(decoder->private_->file, (off_t)absolute_byte_offset, SEEK_SET) < 0) - return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR; - else - return FLAC__STREAM_DECODER_SEEK_STATUS_OK; -} - -FLAC__StreamDecoderTellStatus file_tell_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data) -{ - off_t pos; - (void)client_data; - - if(decoder->private_->file == stdin) - return FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED; - else if((pos = ftello(decoder->private_->file)) < 0) - return FLAC__STREAM_DECODER_TELL_STATUS_ERROR; - else { - *absolute_byte_offset = (FLAC__uint64)pos; - return FLAC__STREAM_DECODER_TELL_STATUS_OK; - } -} - -FLAC__StreamDecoderLengthStatus file_length_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data) -{ - struct stat filestats; - (void)client_data; - - if(decoder->private_->file == stdin) - return FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED; - else if(fstat(fileno(decoder->private_->file), &filestats) != 0) - return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR; - else { - *stream_length = (FLAC__uint64)filestats.st_size; - return FLAC__STREAM_DECODER_LENGTH_STATUS_OK; - } -} - -FLAC__bool file_eof_callback_(const FLAC__StreamDecoder *decoder, void *client_data) -{ - (void)client_data; - - return feof(decoder->private_->file)? true : false; -} diff --git a/FLAC/stream_decoder_pp.cpp b/FLAC/stream_decoder_pp.cpp deleted file mode 100644 index 8b76c95f5..000000000 --- a/FLAC/stream_decoder_pp.cpp +++ /dev/null @@ -1,379 +0,0 @@ -/* libFLAC++ - Free Lossless Audio Codec library - * Copyright (C) 2002,2003,2004,2005,2006,2007 Josh Coalson - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * - Neither the name of the Xiph.org Foundation nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include "FLAC++/decoder.h" -#include "FLAC/assert.h" - -#ifdef _MSC_VER -// warning C4800: 'int' : forcing to bool 'true' or 'false' (performance warning) -#pragma warning ( disable : 4800 ) -#endif - -namespace FLAC { - namespace Decoder { - - // ------------------------------------------------------------ - // - // Stream - // - // ------------------------------------------------------------ - - Stream::Stream(): - decoder_(::FLAC__stream_decoder_new()) - { } - - Stream::~Stream() - { - if(0 != decoder_) { - (void)::FLAC__stream_decoder_finish(decoder_); - ::FLAC__stream_decoder_delete(decoder_); - } - } - - bool Stream::is_valid() const - { - return 0 != decoder_; - } - - bool Stream::set_ogg_serial_number(long value) - { - FLAC__ASSERT(is_valid()); - return (bool)::FLAC__stream_decoder_set_ogg_serial_number(decoder_, value); - } - - bool Stream::set_md5_checking(bool value) - { - FLAC__ASSERT(is_valid()); - return (bool)::FLAC__stream_decoder_set_md5_checking(decoder_, value); - } - - bool Stream::set_metadata_respond(::FLAC__MetadataType type) - { - FLAC__ASSERT(is_valid()); - return (bool)::FLAC__stream_decoder_set_metadata_respond(decoder_, type); - } - - bool Stream::set_metadata_respond_application(const FLAC__byte id[4]) - { - FLAC__ASSERT(is_valid()); - return (bool)::FLAC__stream_decoder_set_metadata_respond_application(decoder_, id); - } - - bool Stream::set_metadata_respond_all() - { - FLAC__ASSERT(is_valid()); - return (bool)::FLAC__stream_decoder_set_metadata_respond_all(decoder_); - } - - bool Stream::set_metadata_ignore(::FLAC__MetadataType type) - { - FLAC__ASSERT(is_valid()); - return (bool)::FLAC__stream_decoder_set_metadata_ignore(decoder_, type); - } - - bool Stream::set_metadata_ignore_application(const FLAC__byte id[4]) - { - FLAC__ASSERT(is_valid()); - return (bool)::FLAC__stream_decoder_set_metadata_ignore_application(decoder_, id); - } - - bool Stream::set_metadata_ignore_all() - { - FLAC__ASSERT(is_valid()); - return (bool)::FLAC__stream_decoder_set_metadata_ignore_all(decoder_); - } - - Stream::State Stream::get_state() const - { - FLAC__ASSERT(is_valid()); - return State(::FLAC__stream_decoder_get_state(decoder_)); - } - - bool Stream::get_md5_checking() const - { - FLAC__ASSERT(is_valid()); - return (bool)::FLAC__stream_decoder_get_md5_checking(decoder_); - } - - FLAC__uint64 Stream::get_total_samples() const - { - FLAC__ASSERT(is_valid()); - return ::FLAC__stream_decoder_get_total_samples(decoder_); - } - - unsigned Stream::get_channels() const - { - FLAC__ASSERT(is_valid()); - return ::FLAC__stream_decoder_get_channels(decoder_); - } - - ::FLAC__ChannelAssignment Stream::get_channel_assignment() const - { - FLAC__ASSERT(is_valid()); - return ::FLAC__stream_decoder_get_channel_assignment(decoder_); - } - - unsigned Stream::get_bits_per_sample() const - { - FLAC__ASSERT(is_valid()); - return ::FLAC__stream_decoder_get_bits_per_sample(decoder_); - } - - unsigned Stream::get_sample_rate() const - { - FLAC__ASSERT(is_valid()); - return ::FLAC__stream_decoder_get_sample_rate(decoder_); - } - - unsigned Stream::get_blocksize() const - { - FLAC__ASSERT(is_valid()); - return ::FLAC__stream_decoder_get_blocksize(decoder_); - } - - bool Stream::get_decode_position(FLAC__uint64 *position) const - { - FLAC__ASSERT(is_valid()); - return ::FLAC__stream_decoder_get_decode_position(decoder_, position); - } - - ::FLAC__StreamDecoderInitStatus Stream::init() - { - FLAC__ASSERT(is_valid()); - return ::FLAC__stream_decoder_init_stream(decoder_, read_callback_, seek_callback_, tell_callback_, length_callback_, eof_callback_, write_callback_, metadata_callback_, error_callback_, /*client_data=*/(void*)this); - } - - ::FLAC__StreamDecoderInitStatus Stream::init_ogg() - { - FLAC__ASSERT(is_valid()); - return ::FLAC__stream_decoder_init_ogg_stream(decoder_, read_callback_, seek_callback_, tell_callback_, length_callback_, eof_callback_, write_callback_, metadata_callback_, error_callback_, /*client_data=*/(void*)this); - } - - bool Stream::finish() - { - FLAC__ASSERT(is_valid()); - return (bool)::FLAC__stream_decoder_finish(decoder_); - } - - bool Stream::flush() - { - FLAC__ASSERT(is_valid()); - return (bool)::FLAC__stream_decoder_flush(decoder_); - } - - bool Stream::reset() - { - FLAC__ASSERT(is_valid()); - return (bool)::FLAC__stream_decoder_reset(decoder_); - } - - bool Stream::process_single() - { - FLAC__ASSERT(is_valid()); - return (bool)::FLAC__stream_decoder_process_single(decoder_); - } - - bool Stream::process_until_end_of_metadata() - { - FLAC__ASSERT(is_valid()); - return (bool)::FLAC__stream_decoder_process_until_end_of_metadata(decoder_); - } - - bool Stream::process_until_end_of_stream() - { - FLAC__ASSERT(is_valid()); - return (bool)::FLAC__stream_decoder_process_until_end_of_stream(decoder_); - } - - bool Stream::skip_single_frame() - { - FLAC__ASSERT(is_valid()); - return (bool)::FLAC__stream_decoder_skip_single_frame(decoder_); - } - - bool Stream::seek_absolute(FLAC__uint64 sample) - { - FLAC__ASSERT(is_valid()); - return (bool)::FLAC__stream_decoder_seek_absolute(decoder_, sample); - } - - ::FLAC__StreamDecoderSeekStatus Stream::seek_callback(FLAC__uint64 absolute_byte_offset) - { - (void)absolute_byte_offset; - return ::FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED; - } - - ::FLAC__StreamDecoderTellStatus Stream::tell_callback(FLAC__uint64 *absolute_byte_offset) - { - (void)absolute_byte_offset; - return ::FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED; - } - - ::FLAC__StreamDecoderLengthStatus Stream::length_callback(FLAC__uint64 *stream_length) - { - (void)stream_length; - return ::FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED; - } - - bool Stream::eof_callback() - { - return false; - } - - void Stream::metadata_callback(const ::FLAC__StreamMetadata *metadata) - { - (void)metadata; - } - - ::FLAC__StreamDecoderReadStatus Stream::read_callback_(const ::FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data) - { - (void)decoder; - FLAC__ASSERT(0 != client_data); - Stream *instance = reinterpret_cast(client_data); - FLAC__ASSERT(0 != instance); - return instance->read_callback(buffer, bytes); - } - - ::FLAC__StreamDecoderSeekStatus Stream::seek_callback_(const ::FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data) - { - (void) decoder; - FLAC__ASSERT(0 != client_data); - Stream *instance = reinterpret_cast(client_data); - FLAC__ASSERT(0 != instance); - return instance->seek_callback(absolute_byte_offset); - } - - ::FLAC__StreamDecoderTellStatus Stream::tell_callback_(const ::FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data) - { - (void) decoder; - FLAC__ASSERT(0 != client_data); - Stream *instance = reinterpret_cast(client_data); - FLAC__ASSERT(0 != instance); - return instance->tell_callback(absolute_byte_offset); - } - - ::FLAC__StreamDecoderLengthStatus Stream::length_callback_(const ::FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data) - { - (void) decoder; - FLAC__ASSERT(0 != client_data); - Stream *instance = reinterpret_cast(client_data); - FLAC__ASSERT(0 != instance); - return instance->length_callback(stream_length); - } - - FLAC__bool Stream::eof_callback_(const ::FLAC__StreamDecoder *decoder, void *client_data) - { - (void) decoder; - FLAC__ASSERT(0 != client_data); - Stream *instance = reinterpret_cast(client_data); - FLAC__ASSERT(0 != instance); - return instance->eof_callback(); - } - - ::FLAC__StreamDecoderWriteStatus Stream::write_callback_(const ::FLAC__StreamDecoder *decoder, const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data) - { - (void)decoder; - FLAC__ASSERT(0 != client_data); - Stream *instance = reinterpret_cast(client_data); - FLAC__ASSERT(0 != instance); - return instance->write_callback(frame, buffer); - } - - void Stream::metadata_callback_(const ::FLAC__StreamDecoder *decoder, const ::FLAC__StreamMetadata *metadata, void *client_data) - { - (void)decoder; - FLAC__ASSERT(0 != client_data); - Stream *instance = reinterpret_cast(client_data); - FLAC__ASSERT(0 != instance); - instance->metadata_callback(metadata); - } - - void Stream::error_callback_(const ::FLAC__StreamDecoder *decoder, ::FLAC__StreamDecoderErrorStatus status, void *client_data) - { - (void)decoder; - FLAC__ASSERT(0 != client_data); - Stream *instance = reinterpret_cast(client_data); - FLAC__ASSERT(0 != instance); - instance->error_callback(status); - } - - // ------------------------------------------------------------ - // - // File - // - // ------------------------------------------------------------ - - File::File(): - Stream() - { } - - File::~File() - { - } - - ::FLAC__StreamDecoderInitStatus File::init(FILE *file) - { - FLAC__ASSERT(0 != decoder_); - return ::FLAC__stream_decoder_init_FILE(decoder_, file, write_callback_, metadata_callback_, error_callback_, /*client_data=*/(void*)this); - } - - ::FLAC__StreamDecoderInitStatus File::init(const char *filename) - { - FLAC__ASSERT(0 != decoder_); - return ::FLAC__stream_decoder_init_file(decoder_, filename, write_callback_, metadata_callback_, error_callback_, /*client_data=*/(void*)this); - } - - ::FLAC__StreamDecoderInitStatus File::init_ogg(FILE *file) - { - FLAC__ASSERT(0 != decoder_); - return ::FLAC__stream_decoder_init_ogg_FILE(decoder_, file, write_callback_, metadata_callback_, error_callback_, /*client_data=*/(void*)this); - } - - ::FLAC__StreamDecoderInitStatus File::init_ogg(const char *filename) - { - FLAC__ASSERT(0 != decoder_); - return ::FLAC__stream_decoder_init_ogg_file(decoder_, filename, write_callback_, metadata_callback_, error_callback_, /*client_data=*/(void*)this); - } - - // This is a dummy to satisfy the pure virtual from Stream; the - // read callback will never be called since we are initializing - // with FLAC__stream_decoder_init_FILE() or - // FLAC__stream_decoder_init_file() and those supply the read - // callback internally. - ::FLAC__StreamDecoderReadStatus File::read_callback(FLAC__byte buffer[], size_t *bytes) - { - (void)buffer, (void)bytes; - FLAC__ASSERT(false); - return ::FLAC__STREAM_DECODER_READ_STATUS_ABORT; // double protection - } - - } -} diff --git a/Makefile b/Makefile index 7eb7e8855..d783ee1c6 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,12 @@ -ifeq (Windows_NT,$(OS)) - WIN=1 -endif +ifeq (Windows_NT,$(OS)) + WIN=1 +endif ifeq ($(findstring msys,$(shell sh --version 2>nul)),msys) - WIN=1 -endif - -ifeq (1,$(WIN)) -include Makefile.mgw -else -include Makefile.linux -endif + WIN=1 +endif + +ifeq (1,$(WIN)) +include Makefile.mgw +else +include Makefile.linux +endif diff --git a/Makefile.linux b/Makefile.linux index 13ef0fb05..058ce7bbb 100644 --- a/Makefile.linux +++ b/Makefile.linux @@ -16,7 +16,7 @@ ifdef GC endif CFLAGS += -MMD -DHAVE_FILELENGTH -D__forceinline=inline `sdl-config --cflags` `pkg-config gtk+-2.0 --cflags` CFLAGS += -Dstricmp=strcasecmp -Dstrnicmp=strncasecmp -DNEED_STRUPR -LDFLAGS += -lFLAC++ -lFLAC -lz -ljpeg -lfmod `sdl-config --libs` `pkg-config gtk+-2.0 --libs` +LDFLAGS += -lz -ljpeg -lfmodex `sdl-config --libs` `pkg-config gtk+-2.0 --libs` NASMFLAGS += -f elf -DM_TARGET_LINUX SRCDIRS = src/ $(addprefix src/,g_doom/ g_heretic/ g_hexen/ g_raven/ g_shared/ g_strife/ oplsynth/ sound/ sdl/ textures/ thingdef/) diff --git a/Makefile.mgw b/Makefile.mgw index 9e890aa8e..8cc2db50f 100644 --- a/Makefile.mgw +++ b/Makefile.mgw @@ -1,55 +1,56 @@ -RELEASETARGET = zdoomgcc.exe -DEBUGTARGET = zdoomgccd.exe - -all: basetools game -debug: basetools debuggame -release: basetools game - -ifndef CONFIG - CONFIG=Release -endif - -game: basetools ccdv.exe - @$(MAKE) -f Makefile.mingw - -debuggame: basetools ccdv.exe - @$(MAKE) CONFIG=Debug -f Makefile.mingw - -$(RELEASETARGET): game -$(DEBUGTARGET): debuggame - -basetools: ccdv.exe - $(MAKE) -C tools/lemon - $(MAKE) -C tools/re2c - $(MAKE) -C zlib -f Makefile.mgw - $(MAKE) -C tools/makewad - $(MAKE) -C tools/dehsupp - $(MAKE) -C tools/xlatcc - $(MAKE) -C tools/fixrtext - $(MAKE) -C wadsrc -f Makefile.mgw - $(MAKE) -C flac -f Makefile.mgw - $(MAKE) -C jpeg-6b -f Makefile.mgw - -cleanexe: - @$(MAKE) -C . -f Makefile.mingw clean - -clean: - @$(MAKE) -C tools/lemon clean - @$(MAKE) -C tools/re2c clean - @$(MAKE) -C tools/dehsupp clean - @$(MAKE) -C tools/makewad clean - @$(MAKE) -C tools/xlatcc clean - @$(MAKE) -C tools/fixrtext clean - @$(MAKE) -C wadsrc -f Makefile.mgw clean - @$(MAKE) -C . -f Makefile.mingw clean - @$(MAKE) -C zlib -f Makefile.mgw clean - @$(MAKE) -C flac -f Makefile.mgw clean - @$(MAKE) -C jpeg-6b -f Makefile.mgw clean +RELEASETARGET = zdoomgcc.exe +DEBUGTARGET = zdoomgccd.exe + +all: basetools game +debug: basetools debuggame +release: basetools game + +ifndef CONFIG + CONFIG=Release +endif + +game: basetools ccdv.exe + @$(MAKE) -f Makefile.mingw + +debuggame: basetools ccdv.exe + @$(MAKE) CONFIG=Debug -f Makefile.mingw + +$(RELEASETARGET): game +$(DEBUGTARGET): debuggame + +basetools: ccdv.exe + $(MAKE) -C tools/lemon + $(MAKE) -C tools/re2c + $(MAKE) -C zlib -f Makefile.mgw + $(MAKE) -C tools/makewad + $(MAKE) -C tools/dehsupp + $(MAKE) -C tools/xlatcc + $(MAKE) -C tools/fixrtext + $(MAKE) -C wadsrc -f Makefile.mgw + $(MAKE) -C jpeg-6b -f Makefile.mgw + +cleanexe: + @$(MAKE) -C . -f Makefile.mingw clean + +clean: + @$(MAKE) -C tools/lemon clean + @$(MAKE) -C tools/re2c clean + @$(MAKE) -C tools/dehsupp clean + @$(MAKE) -C tools/makewad clean + @$(MAKE) -C tools/xlatcc clean + @$(MAKE) -C tools/fixrtext clean + @$(MAKE) -C wadsrc -f Makefile.mgw clean + @$(MAKE) -C . -f Makefile.mingw clean + @$(MAKE) -C zlib -f Makefile.mgw clean + @$(MAKE) -C jpeg-6b -f Makefile.mgw clean ifeq ($(findstring msys,$(shell sh --version 2>nul)),msys) - rm -f ccdv.exe -else - del /q /f ccdv.exe 2>nul -endif - -ccdv.exe: ccdv-win32.c - @gcc -Os -s -nostdlib -fomit-frame-pointer -o ccdv.exe ccdv-win32.c -lkernel32 -luser32 + rm -f ccdv.exe +else + del /q /f ccdv.exe 2>nul +endif + +cleandep: + @$(MAKE) -C . -f Makefile.mingw cleandep + +ccdv.exe: ccdv-win32.c + @gcc -Os -s -nostdlib -fomit-frame-pointer -o ccdv.exe ccdv-win32.c -lkernel32 -luser32 diff --git a/Makefile.mingw b/Makefile.mingw index 9200f0949..26bf98d76 100644 --- a/Makefile.mingw +++ b/Makefile.mingw @@ -5,7 +5,7 @@ # And then tweaked by hand # Where did you install the FMOD API to? Change this line so that the build process can find it. -FMODDIR = "c:/program files/fmodapi375win" +FMODDIR = "e:/program files (x86)/FMOD SoundSystem/FMOD Programmers API Win32" ifeq ($(findstring msys,$(shell sh --version 2>nul)),msys) WINCMD=0 @@ -29,7 +29,7 @@ CCDV ?= @ccdv RE2C = tools/re2c/re2c CPPFLAGS = -DWIN32 -D_WIN32 -D_WINDOWS -DHAVE_STRUPR -DHAVE_FILELENGTH -DI_DO_NOT_LIKE_BIG_DOWNLOADS -D__forceinline=inline -MMD -Izlib -IFLAC -Ijpeg-6b -Isrc -Isrc/win32 -Isrc/g_doom -Isrc/g_heretic -Isrc/g_hexen -Isrc/g_raven -Isrc/g_strife -Isrc/g_shared -Isrc/oplsynth -Isrc/sound -Isrc/textures -Isrc/thingdef -LDFLAGS += flac/libflac.a zlib/libz.a jpeg-6b/libjpeg.a -lfmod -lwsock32 -lwinmm -lddraw -ldsound -ldxguid -ldinput8 -lole32 -luser32 -lgdi32 -lcomctl32 -lcomdlg32 -lsetupapi -lws2_32 -Wl,--subsystem,windows +LDFLAGS += zlib/libz.a jpeg-6b/libjpeg.a -lfmodex -lwsock32 -lwinmm -lddraw -ldsound -ldxguid -ldinput8 -lole32 -luser32 -lgdi32 -lcomctl32 -lcomdlg32 -lsetupapi -lws2_32 -Wl,--subsystem,windows ifdef FMODDIR CPPFLAGS += -I$(FMODDIR)/api/inc @@ -125,8 +125,8 @@ clean: rm -fr $(RELEASEOBJDIR) cleandep: - rm -f $(DEBUGOBJDIR)\*.d - rm -f $(RELEASEOBJDIR)\*.d + rm -f $(DEBUGOBJDIR)/*.d + rm -f $(RELEASEOBJDIR)/*.d endif testobjdir: diff --git a/docs/rh-log.txt b/docs/rh-log.txt index 62326c52d..6a035b55b 100644 --- a/docs/rh-log.txt +++ b/docs/rh-log.txt @@ -1,3 +1,34 @@ +March 8, 2008 +- Fixed: If you wanted to make cleandep with MinGW, you had to specifically + specify Makefile.mingw as the makefile to use. +- Added a normalizer to the OPL synth. It helped bring up the volume a little, + but not nearly as much as I would have liked. +- Removed MIDI Mapper references. It doesn't work with the stream API, and + it doesn't really exist on NT kernels, either. +- Reworked music volume: Except for MIDI, all music volume is controlled + through GSnd and not at the individual song level. +- Removed the mididevice global variable. +- Added back support for custom streams. + +March 7, 2008 +- I think the new FMOD Ex code should support everything the old FMOD 3 code + did, sans custom streaming sounds. +- Removed snd_midivolume. Now that all music uses a linear volume scale, + there's no need for two separate music volume controls. +- Increased snd_samplerate default up to 48000. +- Added snd_format, defaulting to "PCM-16". +- Added snd_speakermode, defaulting to "Auto". +- Replaced snd_fpu with snd_resampler, defaulting to "Linear". +- Bumped the snd_channels default up from a pitiful 12 to 32. +- Changed snd_3d default to true. The new cvar snd_hw3d determines if + hardware 3D support is used and default to false. +- Removed the libFLAC source, since FMOD Ex has native FLAC support. +- Removed the altsound code, since it was terribly gimped in comparison to + the FMOD code. It's original purpose was to have been as a springboard for + writing a non-FMOD sound system for Unix-y systems, but that never + happened. +- Finished preliminary FMOD Ex support. + March 6, 2008 - Fixed: If P_BounceWall() can't find a wall when it does its trace, but it was entered because a line blocked the projectile, then it should still use diff --git a/src/m_options.cpp b/src/m_options.cpp index 26b2c34ce..c3a7ddf46 100644 --- a/src/m_options.cpp +++ b/src/m_options.cpp @@ -1115,7 +1115,6 @@ static menu_t CompatibilityMenu = *=======================================*/ #ifdef _WIN32 -EXTERN_CVAR (Float, snd_midivolume) EXTERN_CVAR (Float, snd_movievolume) #endif EXTERN_CVAR (Bool, snd_flipstereo) @@ -1156,14 +1155,13 @@ static menuitem_t SoundItems[] = { { slider, "Sound effects volume", {&snd_sfxvolume}, {0.0}, {1.0}, {0.05}, {NULL} }, #ifdef _WIN32 - { slider, "MIDI music volume", {&snd_midivolume}, {0.0}, {1.0}, {0.05}, {NULL} }, - { slider, "Other music volume", {&snd_musicvolume}, {0.0}, {1.0}, {0.05}, {NULL} }, + { slider, "Music volume", {&snd_musicvolume}, {0.0}, {1.0}, {0.05}, {NULL} }, { slider, "Movie volume", {&snd_movievolume}, {0.0}, {1.0}, {0.05}, {NULL} }, #else { slider, "Music volume", {&snd_musicvolume}, {0.0}, {1.0}, {0.05}, {NULL} }, #endif { redtext, " ", {NULL}, {0.0}, {0.0}, {0.0}, {NULL} }, - { discrete, "Underwater EAX Reverb",{&snd_waterreverb}, {2.0}, {0.0}, {0.0}, {OnOff} }, + { discrete, "Underwater Reverb", {&snd_waterreverb}, {2.0}, {0.0}, {0.0}, {OnOff} }, { discrete, "Flip Stereo Channels", {&snd_flipstereo}, {2.0}, {0.0}, {0.0}, {OnOff} }, { discrete, "Random Pitch Variations", {&snd_pitched}, {2.0}, {0.0}, {0.0}, {OnOff} }, { redtext, " ", {NULL}, {0.0}, {0.0}, {0.0}, {NULL} }, diff --git a/src/oplsynth/opl_mus_player.cpp b/src/oplsynth/opl_mus_player.cpp index a1b28a94a..cf4d1bb3a 100644 --- a/src/oplsynth/opl_mus_player.cpp +++ b/src/oplsynth/opl_mus_player.cpp @@ -26,6 +26,7 @@ OPLmusicBlock::OPLmusicBlock (FILE *file, char * musiccache, int len, int rate, { scoredata = NULL; SampleBuff = NULL; + SampleBuffSize = 0; TwoChips = !opl_onechip; io = new OPLio; @@ -116,7 +117,6 @@ OPLmusicBlock::OPLmusicBlock (FILE *file, char * musiccache, int len, int rate, } } - SampleBuff = new int[maxSamples]; Restart (); } @@ -209,12 +209,22 @@ void OPLmusicBlock::Restart () bool OPLmusicBlock::ServiceStream (void *buff, int numbytes) { short *samples = (short *)buff; - int *samples1 = SampleBuff; + int *samples1; int numsamples = numbytes / 2; bool prevEnded = false; bool res = true; - memset (SampleBuff, 0, numsamples * 4); + // SampleBuff is allocated here, since FMOD might not give us exactly + // the buffer size we requested. (Actually, it's giving us twice what + // was asked for. Am I doing something wrong?) + numbytes = numsamples * sizeof(int); + if (SampleBuff == NULL || numbytes > SampleBuffSize) + { + SampleBuff = (int *)M_Realloc(SampleBuff, numbytes); + SampleBuffSize = numbytes; + } + memset(SampleBuff, 0, numbytes); + samples1 = SampleBuff; #ifdef _WIN32 EnterCriticalSection (&ChipAccess); @@ -278,7 +288,7 @@ bool OPLmusicBlock::ServiceStream (void *buff, int numbytes) #else SDL_mutexV (ChipAccess); #endif - numsamples = numbytes / 2; + numsamples = numbytes / sizeof(int); samples1 = SampleBuff; #if defined(_MSC_VER) && defined(USEASM) diff --git a/src/oplsynth/opl_mus_player.h b/src/oplsynth/opl_mus_player.h index fbe87ab9e..f7daee46e 100644 --- a/src/oplsynth/opl_mus_player.h +++ b/src/oplsynth/opl_mus_player.h @@ -32,6 +32,7 @@ protected: int ScoreLen; int *SampleBuff; + int SampleBuffSize; #ifdef _WIN32 CRITICAL_SECTION ChipAccess; diff --git a/src/s_environment.cpp b/src/s_environment.cpp index 4ff1cd21b..ac48f414c 100644 --- a/src/s_environment.cpp +++ b/src/s_environment.cpp @@ -110,7 +110,7 @@ static ReverbContainer Psychotic = 0x1900, true, false, - {25, 1.0f, 0.50f, -1000, -151, 0, 7.56f, 0.91f, 1.0f, -626, 0.020f, 0.0f,0.0f,0.0f, 774, 0.030f, 0.0f,0.0f,0.0f, 0.250f, 0.00f, 4.00f, 1.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 100.0f, 100.0f, 0x1f } + {0,25, 1.0f, 0.50f, -1000, -151, 0, 7.56f, 0.91f, 1.0f, -626, 0.020f, 0.0f,0.0f,0.0f, 774, 0.030f, 0.0f,0.0f,0.0f, 0.250f, 0.00f, 4.00f, 1.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 100.0f, 100.0f, 0x1f } }; static ReverbContainer Dizzy = @@ -120,7 +120,7 @@ static ReverbContainer Dizzy = 0x1800, true, false, - {24, 1.8f, 0.60f, -1000, -400, 0, 17.23f, 0.56f, 1.0f, -1713, 0.020f, 0.0f,0.0f,0.0f, -613, 0.030f, 0.0f,0.0f,0.0f, 0.250f, 1.00f, 0.81f, 0.310f, -5.0f, 5000.0f, 250.0f, 0.0f, 100.0f, 100.0f, 0x1f } + {0,24, 1.8f, 0.60f, -1000, -400, 0, 17.23f, 0.56f, 1.0f, -1713, 0.020f, 0.0f,0.0f,0.0f, -613, 0.030f, 0.0f,0.0f,0.0f, 0.250f, 1.00f, 0.81f, 0.310f, -5.0f, 5000.0f, 250.0f, 0.0f, 100.0f, 100.0f, 0x1f } }; static ReverbContainer Drugged = @@ -130,7 +130,7 @@ static ReverbContainer Drugged = 0x1700, true, false, - {23, 1.9f, 0.50f, -1000, 0, 0, 8.39f, 1.39f, 1.0f, -115, 0.002f, 0.0f,0.0f,0.0f, 985, 0.030f, 0.0f,0.0f,0.0f, 0.250f, 0.00f, 0.25f, 1.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 100.0f, 100.0f, 0x1f } + {0,23, 1.9f, 0.50f, -1000, 0, 0, 8.39f, 1.39f, 1.0f, -115, 0.002f, 0.0f,0.0f,0.0f, 985, 0.030f, 0.0f,0.0f,0.0f, 0.250f, 0.00f, 0.25f, 1.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 100.0f, 100.0f, 0x1f } }; static ReverbContainer Underwater = @@ -140,7 +140,7 @@ static ReverbContainer Underwater = 0x1600, true, false, - {22, 1.8f, 1.00f, -1000, -4000, 0, 1.49f, 0.10f, 1.0f, -449, 0.007f, 0.0f,0.0f,0.0f, 1700, 0.011f, 0.0f,0.0f,0.0f, 0.250f, 0.00f, 1.18f, 0.348f, -5.0f, 5000.0f, 250.0f, 0.0f, 100.0f, 100.0f, 0x3f } + {0,22, 1.8f, 1.00f, -1000, -4000, 0, 1.49f, 0.10f, 1.0f, -449, 0.007f, 0.0f,0.0f,0.0f, 1700, 0.011f, 0.0f,0.0f,0.0f, 0.250f, 0.00f, 1.18f, 0.348f, -5.0f, 5000.0f, 250.0f, 0.0f, 100.0f, 100.0f, 0x3f } }; static ReverbContainer SewerPipe = @@ -150,7 +150,7 @@ static ReverbContainer SewerPipe = 0x1500, true, false, - {21, 1.7f, 0.80f, -1000, -1000, 0, 2.81f, 0.14f, 1.0f, 429, 0.014f, 0.0f,0.0f,0.0f, 1023, 0.021f, 0.0f,0.0f,0.0f, 0.250f, 0.00f, 0.25f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 80.0f, 60.0f, 0x3f } + {0,21, 1.7f, 0.80f, -1000, -1000, 0, 2.81f, 0.14f, 1.0f, 429, 0.014f, 0.0f,0.0f,0.0f, 1023, 0.021f, 0.0f,0.0f,0.0f, 0.250f, 0.00f, 0.25f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 80.0f, 60.0f, 0x3f } }; static ReverbContainer ParkingLot = @@ -160,7 +160,7 @@ static ReverbContainer ParkingLot = 0x1400, true, false, - {20, 8.3f, 1.00f, -1000, 0, 0, 1.65f, 1.50f, 1.0f, -1363, 0.008f, 0.0f,0.0f,0.0f, -1153, 0.012f, 0.0f,0.0f,0.0f, 0.250f, 0.00f, 0.25f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 100.0f, 100.0f, 0x1f } + {0,20, 8.3f, 1.00f, -1000, 0, 0, 1.65f, 1.50f, 1.0f, -1363, 0.008f, 0.0f,0.0f,0.0f, -1153, 0.012f, 0.0f,0.0f,0.0f, 0.250f, 0.00f, 0.25f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 100.0f, 100.0f, 0x1f } }; static ReverbContainer Plain = @@ -170,7 +170,7 @@ static ReverbContainer Plain = 0x1300, true, false, - {19, 42.5f, 0.21f, -1000, -2000, 0, 1.49f, 0.50f, 1.0f, -2466, 0.179f, 0.0f,0.0f,0.0f, -1926, 0.100f, 0.0f,0.0f,0.0f, 0.250f, 1.00f, 0.25f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 21.0f, 100.0f, 0x3f } + {0,19, 42.5f, 0.21f, -1000, -2000, 0, 1.49f, 0.50f, 1.0f, -2466, 0.179f, 0.0f,0.0f,0.0f, -1926, 0.100f, 0.0f,0.0f,0.0f, 0.250f, 1.00f, 0.25f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 21.0f, 100.0f, 0x3f } }; static ReverbContainer Quarry = @@ -180,7 +180,7 @@ static ReverbContainer Quarry = 0x1200, true, false, - {18, 17.5f, 1.00f, -1000, -1000, 0, 1.49f, 0.83f, 1.0f, -10000, 0.061f, 0.0f,0.0f,0.0f, 500, 0.025f, 0.0f,0.0f,0.0f, 0.125f, 0.70f, 0.25f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 100.0f, 100.0f, 0x3f } + {0,18, 17.5f, 1.00f, -1000, -1000, 0, 1.49f, 0.83f, 1.0f, -10000, 0.061f, 0.0f,0.0f,0.0f, 500, 0.025f, 0.0f,0.0f,0.0f, 0.125f, 0.70f, 0.25f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 100.0f, 100.0f, 0x3f } }; static ReverbContainer Mountains = @@ -190,7 +190,7 @@ static ReverbContainer Mountains = 0x1100, true, false, - {17, 100.0f, 0.27f, -1000, -2500, 0, 1.49f, 0.21f, 1.0f, -2780, 0.300f, 0.0f,0.0f,0.0f, -1434, 0.100f, 0.0f,0.0f,0.0f, 0.250f, 1.00f, 0.25f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 27.0f, 100.0f, 0x1f } + {0,17, 100.0f, 0.27f, -1000, -2500, 0, 1.49f, 0.21f, 1.0f, -2780, 0.300f, 0.0f,0.0f,0.0f, -1434, 0.100f, 0.0f,0.0f,0.0f, 0.250f, 1.00f, 0.25f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 27.0f, 100.0f, 0x1f } }; static ReverbContainer City = @@ -200,7 +200,7 @@ static ReverbContainer City = 0x1000, true, false, - {16, 7.5f, 0.50f, -1000, -800, 0, 1.49f, 0.67f, 1.0f, -2273, 0.007f, 0.0f,0.0f,0.0f, -1691, 0.011f, 0.0f,0.0f,0.0f, 0.250f, 0.00f, 0.25f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 50.0f, 100.0f, 0x3f } + {0,16, 7.5f, 0.50f, -1000, -800, 0, 1.49f, 0.67f, 1.0f, -2273, 0.007f, 0.0f,0.0f,0.0f, -1691, 0.011f, 0.0f,0.0f,0.0f, 0.250f, 0.00f, 0.25f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 50.0f, 100.0f, 0x3f } }; static ReverbContainer Forest = @@ -210,7 +210,7 @@ static ReverbContainer Forest = 0x0F00, true, false, - {15, 38.0f, 0.30f, -1000, -3300, 0, 1.49f, 0.54f, 1.0f, -2560, 0.162f, 0.0f,0.0f,0.0f, -229, 0.088f, 0.0f,0.0f,0.0f, 0.125f, 1.00f, 0.25f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 79.0f, 100.0f, 0x3f } + {0,15, 38.0f, 0.30f, -1000, -3300, 0, 1.49f, 0.54f, 1.0f, -2560, 0.162f, 0.0f,0.0f,0.0f, -229, 0.088f, 0.0f,0.0f,0.0f, 0.125f, 1.00f, 0.25f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 79.0f, 100.0f, 0x3f } }; static ReverbContainer Alley = @@ -220,7 +220,7 @@ static ReverbContainer Alley = 0x0E00, true, false, - {14, 7.5f, 0.30f, -1000, -270, 0, 1.49f, 0.86f, 1.0f, -1204, 0.007f, 0.0f,0.0f,0.0f, -4, 0.011f, 0.0f,0.0f,0.0f, 0.125f, 0.95f, 0.25f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 100.0f, 100.0f, 0x3f } + {0,14, 7.5f, 0.30f, -1000, -270, 0, 1.49f, 0.86f, 1.0f, -1204, 0.007f, 0.0f,0.0f,0.0f, -4, 0.011f, 0.0f,0.0f,0.0f, 0.125f, 0.95f, 0.25f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 100.0f, 100.0f, 0x3f } }; static ReverbContainer StoneCorridor = @@ -230,7 +230,7 @@ static ReverbContainer StoneCorridor = 0x0D00, true, false, - {13, 13.5f, 1.00f, -1000, -237, 0, 2.70f, 0.79f, 1.0f, -1214, 0.013f, 0.0f,0.0f,0.0f, 395, 0.020f, 0.0f,0.0f,0.0f, 0.250f, 0.00f, 0.25f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 100.0f, 100.0f, 0x3f } + {0,13, 13.5f, 1.00f, -1000, -237, 0, 2.70f, 0.79f, 1.0f, -1214, 0.013f, 0.0f,0.0f,0.0f, 395, 0.020f, 0.0f,0.0f,0.0f, 0.250f, 0.00f, 0.25f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 100.0f, 100.0f, 0x3f } }; static ReverbContainer Hallway = @@ -240,7 +240,7 @@ static ReverbContainer Hallway = 0x0C00, true, false, - {12, 1.8f, 1.00f, -1000, -300, 0, 1.49f, 0.59f, 1.0f, -1219, 0.007f, 0.0f,0.0f,0.0f, 441, 0.011f, 0.0f,0.0f,0.0f, 0.250f, 0.00f, 0.25f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 100.0f, 100.0f, 0x3f } + {0,12, 1.8f, 1.00f, -1000, -300, 0, 1.49f, 0.59f, 1.0f, -1219, 0.007f, 0.0f,0.0f,0.0f, 441, 0.011f, 0.0f,0.0f,0.0f, 0.250f, 0.00f, 0.25f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 100.0f, 100.0f, 0x3f } }; static ReverbContainer CarpettedHallway = @@ -250,7 +250,7 @@ static ReverbContainer CarpettedHallway = 0x0B00, true, false, - {11, 1.9f, 1.00f, -1000, -4000, 0, 0.30f, 0.10f, 1.0f, -1831, 0.002f, 0.0f,0.0f,0.0f, -1630, 0.030f, 0.0f,0.0f,0.0f, 0.250f, 0.00f, 0.25f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 100.0f, 100.0f, 0x3f } + {0,11, 1.9f, 1.00f, -1000, -4000, 0, 0.30f, 0.10f, 1.0f, -1831, 0.002f, 0.0f,0.0f,0.0f, -1630, 0.030f, 0.0f,0.0f,0.0f, 0.250f, 0.00f, 0.25f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 100.0f, 100.0f, 0x3f } }; static ReverbContainer Hangar = @@ -260,7 +260,7 @@ static ReverbContainer Hangar = 0x0A00, true, false, - {10, 50.3f, 1.00f, -1000, -1000, 0, 10.05f, 0.23f, 1.0f, -602, 0.020f, 0.0f,0.0f,0.0f, 198, 0.030f, 0.0f,0.0f,0.0f, 0.250f, 0.00f, 0.25f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 100.0f, 100.0f, 0x3f } + {0,10, 50.3f, 1.00f, -1000, -1000, 0, 10.05f, 0.23f, 1.0f, -602, 0.020f, 0.0f,0.0f,0.0f, 198, 0.030f, 0.0f,0.0f,0.0f, 0.250f, 0.00f, 0.25f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 100.0f, 100.0f, 0x3f } }; static ReverbContainer Arena = @@ -270,7 +270,7 @@ static ReverbContainer Arena = 0x0900, true, false, - {9, 36.2f, 1.00f, -1000, -698, 0, 7.24f, 0.33f, 1.0f, -1166, 0.020f, 0.0f,0.0f,0.0f, 16, 0.030f, 0.0f,0.0f,0.0f, 0.250f, 0.00f, 0.25f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 100.0f, 100.0f, 0x3f } + {0, 9, 36.2f, 1.00f, -1000, -698, 0, 7.24f, 0.33f, 1.0f, -1166, 0.020f, 0.0f,0.0f,0.0f, 16, 0.030f, 0.0f,0.0f,0.0f, 0.250f, 0.00f, 0.25f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 100.0f, 100.0f, 0x3f } }; static ReverbContainer Cave = @@ -280,7 +280,7 @@ static ReverbContainer Cave = 0x0800, true, false, - {8, 14.6f, 1.00f, -1000, 0, 0, 2.91f, 1.30f, 1.0f, -602, 0.015f, 0.0f,0.0f,0.0f, -302, 0.022f, 0.0f,0.0f,0.0f, 0.250f, 0.00f, 0.25f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 100.0f, 100.0f, 0x1f } + {0, 8, 14.6f, 1.00f, -1000, 0, 0, 2.91f, 1.30f, 1.0f, -602, 0.015f, 0.0f,0.0f,0.0f, -302, 0.022f, 0.0f,0.0f,0.0f, 0.250f, 0.00f, 0.25f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 100.0f, 100.0f, 0x1f } }; static ReverbContainer ConcertHall = @@ -290,7 +290,7 @@ static ReverbContainer ConcertHall = 0x0700, true, false, - {7, 19.6f, 1.00f, -1000, -500, 0, 3.92f, 0.70f, 1.0f, -1230, 0.020f, 0.0f,0.0f,0.0f, -2, 0.029f, 0.0f,0.0f,0.0f, 0.250f, 0.00f, 0.25f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 100.0f, 100.0f, 0x3f } + {0, 7, 19.6f, 1.00f, -1000, -500, 0, 3.92f, 0.70f, 1.0f, -1230, 0.020f, 0.0f,0.0f,0.0f, -2, 0.029f, 0.0f,0.0f,0.0f, 0.250f, 0.00f, 0.25f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 100.0f, 100.0f, 0x3f } }; static ReverbContainer Auditorium = @@ -300,7 +300,7 @@ static ReverbContainer Auditorium = 0x0600, true, false, - {6, 21.6f, 1.00f, -1000, -476, 0, 4.32f, 0.59f, 1.0f, -789, 0.020f, 0.0f,0.0f,0.0f, -289, 0.030f, 0.0f,0.0f,0.0f, 0.250f, 0.00f, 0.25f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 100.0f, 100.0f, 0x3f } + {0, 6, 21.6f, 1.00f, -1000, -476, 0, 4.32f, 0.59f, 1.0f, -789, 0.020f, 0.0f,0.0f,0.0f, -289, 0.030f, 0.0f,0.0f,0.0f, 0.250f, 0.00f, 0.25f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 100.0f, 100.0f, 0x3f } }; static ReverbContainer StoneRoom = @@ -310,7 +310,7 @@ static ReverbContainer StoneRoom = 0x0500, true, false, - {5, 11.6f, 1.00f, -1000, -300, 0, 2.31f, 0.64f, 1.0f, -711, 0.012f, 0.0f,0.0f,0.0f, 83, 0.017f, 0.0f,0.0f,0.0f, 0.250f, 0.00f, 0.25f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 100.0f, 100.0f, 0x3f } + {0, 5, 11.6f, 1.00f, -1000, -300, 0, 2.31f, 0.64f, 1.0f, -711, 0.012f, 0.0f,0.0f,0.0f, 83, 0.017f, 0.0f,0.0f,0.0f, 0.250f, 0.00f, 0.25f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 100.0f, 100.0f, 0x3f } }; static ReverbContainer LivingRoom = @@ -320,7 +320,7 @@ static ReverbContainer LivingRoom = 0x0400, true, false, - {4, 2.5f, 1.00f, -1000, -6000, 0, 0.50f, 0.10f, 1.0f, -1376, 0.003f, 0.0f,0.0f,0.0f, -1104, 0.004f, 0.0f,0.0f,0.0f, 0.250f, 0.00f, 0.25f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 100.0f, 100.0f, 0x3f } + {0, 4, 2.5f, 1.00f, -1000, -6000, 0, 0.50f, 0.10f, 1.0f, -1376, 0.003f, 0.0f,0.0f,0.0f, -1104, 0.004f, 0.0f,0.0f,0.0f, 0.250f, 0.00f, 0.25f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 100.0f, 100.0f, 0x3f } }; static ReverbContainer Bathroom = @@ -330,7 +330,7 @@ static ReverbContainer Bathroom = 0x0300, true, false, - {3, 1.4f, 1.00f, -1000, -1200, 0, 1.49f, 0.54f, 1.0f, -370, 0.007f, 0.0f,0.0f,0.0f, 1030, 0.011f, 0.0f,0.0f,0.0f, 0.250f, 0.00f, 0.25f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 100.0f, 60.0f, 0x3f } + {0, 3, 1.4f, 1.00f, -1000, -1200, 0, 1.49f, 0.54f, 1.0f, -370, 0.007f, 0.0f,0.0f,0.0f, 1030, 0.011f, 0.0f,0.0f,0.0f, 0.250f, 0.00f, 0.25f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 100.0f, 60.0f, 0x3f } }; static ReverbContainer Room = @@ -340,7 +340,7 @@ static ReverbContainer Room = 0x0200, true, false, - {2, 1.9f, 1.00f, -1000, -454, 0, 0.40f, 0.83f, 1.0f, -1646, 0.002f, 0.0f,0.0f,0.0f, 53, 0.003f, 0.0f,0.0f,0.0f, 0.250f, 0.00f, 0.25f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 100.0f, 100.0f, 0x3f } + {0, 2, 1.9f, 1.00f, -1000, -454, 0, 0.40f, 0.83f, 1.0f, -1646, 0.002f, 0.0f,0.0f,0.0f, 53, 0.003f, 0.0f,0.0f,0.0f, 0.250f, 0.00f, 0.25f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 100.0f, 100.0f, 0x3f } }; static ReverbContainer PaddedCell = @@ -350,7 +350,7 @@ static ReverbContainer PaddedCell = 0x0100, true, false, - {1, 1.4f, 1.00f, -1000, -6000, 0, 0.17f, 0.10f, 1.0f, -1204, 0.001f, 0.0f,0.0f,0.0f, 207, 0.002f, 0.0f,0.0f,0.0f, 0.250f, 0.00f, 0.25f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 100.0f, 100.0f, 0x3f } + {0, 1, 1.4f, 1.00f, -1000, -6000, 0, 0.17f, 0.10f, 1.0f, -1204, 0.001f, 0.0f,0.0f,0.0f, 207, 0.002f, 0.0f,0.0f,0.0f, 0.250f, 0.00f, 0.25f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 100.0f, 100.0f, 0x3f } }; static ReverbContainer Generic = @@ -360,7 +360,7 @@ static ReverbContainer Generic = 0x0001, true, false, - {0, 7.5f, 1.00f, -1000, -100, 0, 1.49f, 0.83f, 1.0f, -2602, 0.007f, 0.0f,0.0f,0.0f, 200, 0.011f, 0.0f,0.0f,0.0f, 0.250f, 0.00f, 0.25f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 100.0f, 100.0f, 0x3f } + {0, 0, 7.5f, 1.00f, -1000, -100, 0, 1.49f, 0.83f, 1.0f, -2602, 0.007f, 0.0f,0.0f,0.0f, 200, 0.011f, 0.0f,0.0f,0.0f, 0.250f, 0.00f, 0.25f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 100.0f, 100.0f, 0x3f } }; static ReverbContainer Off = @@ -370,7 +370,7 @@ static ReverbContainer Off = 0x0000, true, false, - {0, 7.5f, 1.00f, -10000, -10000, 0, 1.00f, 1.00f, 1.0f, -2602, 0.007f, 0.0f,0.0f,0.0f, 200, 0.011f, 0.0f,0.0f,0.0f, 0.250f, 0.00f, 0.25f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 0.0f, 0.0f, 0x33f } + {0, 0, 7.5f, 1.00f, -10000, -10000, 0, 1.00f, 1.00f, 1.0f, -2602, 0.007f, 0.0f,0.0f,0.0f, 200, 0.011f, 0.0f,0.0f,0.0f, 0.250f, 0.00f, 0.25f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.0f, 0.0f, 0.0f, 0x33f } }; ReverbContainer *DefaultEnvironments[26] = @@ -486,6 +486,7 @@ static void ReadEAX (int lump, const char *lumpname) id2 = sc.Number; sc.MustGetStringName ("{"); memset (inited, 0, sizeof(inited)); + props.Instance = 0; props.Flags = 0; while (sc.MustGetString (), NUM_EAX_FIELDS > (i = sc.MustMatchString (EAXFieldNames))) { diff --git a/src/s_sound.cpp b/src/s_sound.cpp index fc121843f..a47b81157 100644 --- a/src/s_sound.cpp +++ b/src/s_sound.cpp @@ -16,7 +16,6 @@ // // // DESCRIPTION: none -// Dolby Pro Logic code by Gavino. // //----------------------------------------------------------------------------- @@ -74,11 +73,11 @@ #define NORM_PITCH 128 #define NORM_PRIORITY 64 -#define NORM_SEP 128 +#define NORM_SEP 0 #define NONE_SEP -2 #define S_PITCH_PERTURB 1 -#define S_STEREO_SWING (96<= 0) { MAX_SND_DIST = Wads.LumpLength (curvelump); - SoundCurve = new BYTE[MAX_SND_DIST]; - Wads.ReadLump (curvelump, SoundCurve); + SoundCurve = new float[MAX_SND_DIST]; + FMemLump lump = Wads.ReadLump(curvelump); + BYTE *lumpmem = (BYTE *)lump.GetMem(); // The maximum value in a SNDCURVE lump is 127, so scale it to - // fit our sound system's volume levels. + // fit our sound system's volume levels. Also, FMOD's volumes + // are linear, whereas we want the "dumb" logarithmic volumes + // for our "2D" sounds. for (i = 0; i < S_CLIPPING_DIST; ++i) { - SoundCurve[i] = SoundCurve[i] * 255 / 127; + SoundCurve[i] = (powf(10.f, (lumpmem[i] / 127.f)) - 1.f) / 9.f; } } else { MAX_SND_DIST = S_CLIPPING_DIST; - SoundCurve = new BYTE[S_CLIPPING_DIST]; + SoundCurve = new float[S_CLIPPING_DIST]; for (i = 0; i < S_CLIPPING_DIST; ++i) { - SoundCurve[i] = MIN (255, (S_CLIPPING_DIST - i) * 255 / S_ATTENUATOR); + SoundCurve[i] = (powf(10.f, MIN(1.f, (S_CLIPPING_DIST - i) / S_ATTENUATOR)) - 1.f) / 9.f; } } // Allocating the virtual channels - numChannels = GSnd ? GSnd->SetChannels (snd_channels) : 0; + numChannels = GSnd ? GSnd->GetNumChannels() : 0; if (Channel != NULL) { delete[] Channel; @@ -594,11 +598,12 @@ static void S_StartSound (fixed_t *pt, AActor *mover, int channel, int sound_id, float volume, float attenuation, bool looping) { sfxinfo_t *sfx; - int dist, vol; + int dist; + float vol; int i; int chanflags; int basepriority, priority; - int sep; + float sep; int org_id; fixed_t x, y, z; angle_t angle; @@ -655,7 +660,7 @@ static void S_StartSound (fixed_t *pt, AActor *mover, int channel, } else if (attenuation < 0) { - sep = snd_surround ? -1 : NONE_SEP; + sep = NONE_SEP; dist = 0; } else @@ -827,7 +832,7 @@ static void S_StartSound (fixed_t *pt, AActor *mover, int channel, } } - vol = (int)(SoundCurve[dist]*volume); + vol = SoundCurve[dist] * volume; if (sep == -3) { AActor *listener = players[consoleplayer].camera; @@ -846,21 +851,10 @@ static void S_StartSound (fixed_t *pt, AActor *mover, int channel, angle = angle - listener->angle; else angle = angle + (ANGLE_MAX - listener->angle); - angle >>= ANGLETOFINESHIFT; - sep = NORM_SEP - (FixedMul (S_STEREO_SWING, finesine[angle])>>FRACBITS); + sep = NORM_SEP - S_STEREO_SWING * sin((angle >> 1) * M_PI / 2147483648.0); if (snd_flipstereo) { - sep = 255-sep; - } - if (snd_matrix) - { - int rearsep = NORM_SEP - - (FixedMul (S_STEREO_SWING, finecosine[angle])>>FRACBITS); - if (rearsep > 128) - { - sep = -1; - attenuation = -1; - } + sep = -sep; } } } @@ -1308,9 +1302,9 @@ void S_UpdateSounds (void *listener_p) { fixed_t *listener; fixed_t x, y; - int i, dist, vol; + int i, dist; angle_t angle; - int sep; + float vol, sep; I_UpdateMusic(); @@ -1366,7 +1360,7 @@ void S_UpdateSounds (void *listener_p) { dist = 0; } - vol = (int)(SoundCurve[dist]*Channel[i].volume); + vol = SoundCurve[dist] * Channel[i].volume; if (dist > 0) { angle = R_PointToAngle2(listener[0], listener[1], x, y); @@ -1374,20 +1368,10 @@ void S_UpdateSounds (void *listener_p) angle = angle - players[consoleplayer].camera->angle; else angle = angle + (ANGLE_MAX - players[consoleplayer].camera->angle); - angle >>= ANGLETOFINESHIFT; - sep = NORM_SEP - (FixedMul (S_STEREO_SWING, finesine[angle])>>FRACBITS); + sep = NORM_SEP - S_STEREO_SWING * sin((angle >> 1) * M_PI / 2147483648.0); if (snd_flipstereo) { - sep = 255-sep; - } - if (snd_matrix) - { - int rearsep = NORM_SEP - - (FixedMul (S_STEREO_SWING, finecosine[angle])>>FRACBITS); - if (rearsep > 128) - { - sep = -1; - } + sep = -sep; } } else diff --git a/src/s_sound.h b/src/s_sound.h index c139bd20d..5f75eee01 100644 --- a/src/s_sound.h +++ b/src/s_sound.h @@ -208,12 +208,13 @@ void S_ShrinkPlayerSoundLists (); // Modelled after Hexen's noise cheat. void S_NoiseDebug (); -// For convenience, this structure matches FSOUND_REVERB_PROPERTIES. +// For convenience, this structure matches FMOD_REVERB_PROPERTIES. // Since I can't very well #include system-specific stuff in the // main game files, I duplicate it here. struct REVERB_PROPERTIES -{ - /*unsigned*/int Environment; +{ + int Instance; + int Environment; float EnvSize; float EnvDiffusion; int Room; diff --git a/src/sound/altsound.cpp b/src/sound/altsound.cpp deleted file mode 100644 index 8651516aa..000000000 --- a/src/sound/altsound.cpp +++ /dev/null @@ -1,1422 +0,0 @@ -/* -** i_altsound.cpp -** -** Simple, no frills DirectSound player. -** -**--------------------------------------------------------------------------- -** Copyright 2002-2006 Randy Heit -** All rights reserved. -** -** Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions -** are met: -** -** 1. Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** 2. Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in the -** documentation and/or other materials provided with the distribution. -** 3. The name of the author may not be used to endorse or promote products -** derived from this software without specific prior written permission. -** -** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR -** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES -** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. -** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, -** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF -** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -**--------------------------------------------------------------------------- -** -*/ - -#ifdef _WIN32 - -// HEADER FILES ------------------------------------------------------------ - -#define WIN32_LEAN_AND_MEAN -#include -#include -#include -#include -#include - -#define USE_WINDOWS_DWORD -#include "altsound.h" -#include "doomtype.h" -#include "c_cvars.h" -#include "templates.h" -#include "i_system.h" -#include "i_music.h" -#include "s_sound.h" -#include "w_wad.h" -#include "m_swap.h" -#include "sample_flac.h" -#include "stats.h" - -// MACROS ------------------------------------------------------------------ - -#define PITCH(f,x) (snd_pitched ? ((f)*(x))/128 : (f)) -#define ID_RIFF MAKE_ID('R','I','F','F') -#define ID_WAVE MAKE_ID('W','A','V','E') -#define ID_fmt MAKE_ID('f','m','t',' ') -#define ID_data MAKE_ID('d','a','t','a') -#define ID_fLaC MAKE_ID('f','L','a','C') - -// TYPES ------------------------------------------------------------------- - -struct AltSoundRenderer::Channel -{ - sfxinfo_t *Sample; - SQWORD SamplePos; - SQWORD SampleStep; - SDWORD LeftVolume; - SDWORD RightVolume; - bool Looping; - bool bPaused; - bool bIsPauseable; - CRITICAL_SECTION CriticalSection; -}; - -struct AltSoundRenderer::Stream : public SoundStream -{ - Stream (AltSoundRenderer *renderer, SoundStreamCallback callback, int buffbytes, - int flags, int samplerate, int mixfreq, void *userdata) - : Renderer(renderer), Next(NULL), Prev(NULL), - Paused(true), DidFirstRun(false), Callback(callback), UserData(userdata) - { - InitializeCriticalSection (&CriticalSection); - SamplePos = 0; - SampleStep = ((QWORD)samplerate << 32) / mixfreq; - Volume = 256; - Bits8 = !!(flags & SoundStream::Bits8); - Mono = !!(flags & SoundStream::Mono); - - BufferLen = buffbytes; - Buffer = new BYTE[buffbytes]; - } - - ~Stream () - { - if (Prev != NULL) - { - if (Renderer != NULL) EnterCriticalSection (&Renderer->StreamCriticalSection); - *Prev = Next; - if (Renderer != NULL) LeaveCriticalSection (&Renderer->StreamCriticalSection); - } - if (Buffer != NULL) delete[] Buffer; - DeleteCriticalSection (&CriticalSection); - } - - bool Play (bool looping, float volume) - { - EnterCriticalSection (&CriticalSection); - Paused = false; - SamplePos = 0; - LeaveCriticalSection (&CriticalSection); - return true; - } - - void Stop () - { - EnterCriticalSection (&CriticalSection); - Paused = true; - DidFirstRun = false; - SamplePos = 0; - LeaveCriticalSection (&CriticalSection); - } - - void SetVolume (float volume) - { - EnterCriticalSection (&CriticalSection); - Volume = int(volume * 256.f); - LeaveCriticalSection (&CriticalSection); - } - - bool SetPaused (bool paused) - { - EnterCriticalSection (&CriticalSection); - Paused = paused; - LeaveCriticalSection (&CriticalSection); - return true; - } - - unsigned int GetPosition () - { - // Only needed for streams created with OpenStream, which aren't supported - return 0; - } - - AltSoundRenderer *Renderer; - Stream *Next, **Prev; - bool Paused, DidFirstRun; - SoundStreamCallback Callback; - void *UserData; - bool Bits8; - bool Mono; - int BufferLen; - BYTE *Buffer; - - SQWORD SamplePos; - SQWORD SampleStep; - SDWORD Volume; - CRITICAL_SECTION CriticalSection; -}; - -// EXTERNAL FUNCTION PROTOTYPES -------------------------------------------- - -// PUBLIC FUNCTION PROTOTYPES ---------------------------------------------- - -// PRIVATE FUNCTION PROTOTYPES --------------------------------------------- - -// EXTERNAL DATA DECLARATIONS ---------------------------------------------- - -extern HWND Window; - -EXTERN_CVAR (Int, snd_samplerate) -EXTERN_CVAR (Int, snd_buffersize) -EXTERN_CVAR (Bool, snd_pitched) - -// PUBLIC DATA DEFINITIONS ------------------------------------------------- - -CVAR (Int, snd_interpolate, 1, CVAR_ARCHIVE) - -// PRIVATE DATA DEFINITIONS ------------------------------------------------ - -// CODE -------------------------------------------------------------------- - -//========================================================================== -// -// AltSoundRenderer Constructor -// -//========================================================================== - -AltSoundRenderer::AltSoundRenderer () -{ - DidInit = false; - NumChannels = 0; - Channels = NULL; - RenderBuffer = NULL; - DidInit = Init (); -} - -//========================================================================== -// -// AltSoundRenderer Destructor -// -//========================================================================== - -AltSoundRenderer::~AltSoundRenderer () -{ - Shutdown (); -} - -//========================================================================== -// -// AltSoundRenderer :: IsValid -// -//========================================================================== - -bool AltSoundRenderer::IsValid () -{ - return DidInit; -} - -//========================================================================== -// -// AltSoundRenderer :: Init -// -//========================================================================== - -bool AltSoundRenderer::Init () -{ - HRESULT hr; - WAVEFORMATEX wfx = - { - WAVE_FORMAT_PCM, // wFormatTag - 2, // nChannels - snd_samplerate, // nSamplesPerSec - snd_samplerate * 4, // nAvgBytesPerSec - 4, // nBlockAlign - 16, // wBitsPerSample - 0 // cbSize - }; - DSBUFFERDESC dsbdesc = { sizeof(dsbdesc), DSBCAPS_PRIMARYBUFFER }; - - lpds = NULL; - MixerEvent = NULL; - MixerQuit = false; - Streams = NULL; - for (int i = 0; i < NUM_PERFMETERS; ++i) - { - PerfMeter[i] = 0.0; - } - CurPerfMeter = 0; - - InitializeCriticalSection (&StreamCriticalSection); - - //hr = DirectSoundCreate (NULL, &lpds, NULL); - hr = CoCreateInstance (CLSID_DirectSound, 0, CLSCTX_INPROC_SERVER, IID_IDirectSound, (void **)&lpds); - if (FAILED (hr)) - { - Printf ("Could not create DirectSound interface: %08lx\n", hr); - goto fail; - } - - hr = lpds->Initialize (0); - if (FAILED (hr)) - { - Printf ("Could not initialize DirectSound interface: %08lx\n", hr); - goto fail; - } - - hr = lpds->SetCooperativeLevel (Window, DSSCL_PRIORITY); - if (FAILED (hr)) - { - Printf ("Could not set DirectSound co-op level: %08lx\n", hr); - lpds->Release (); - goto fail; - } - - hr = lpds->CreateSoundBuffer (&dsbdesc, &lpdsbPrimary, NULL); - if (FAILED (hr)) - { - Printf ("Could not get DirectSound primary buffer: %08lx\n", hr); - lpds->Release (); - goto fail; - } - - hr = lpdsbPrimary->SetFormat (&wfx); - - // Now see what format we really got - hr = lpdsbPrimary->GetFormat (&wfx, sizeof(wfx), NULL); - - Frequency = wfx.nSamplesPerSec; - - BufferSamples = MAX (*snd_buffersize, 30); - BufferSamples = (wfx.nSamplesPerSec * BufferSamples / 1000); - BufferBytes = BufferSamples << 2; - dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2; - dsbdesc.dwBufferBytes = BufferBytes; - dsbdesc.lpwfxFormat = &wfx; - wfx.wBitsPerSample = 16; // Let DirectSound worry about 8-bit primary buffers - wfx.nChannels = 2; // Also let DirectSound worry about mono primary buffers - - hr = lpds->CreateSoundBuffer (&dsbdesc, &lpdsb, NULL); - if (FAILED (hr)) - { - Printf ("Could not create secondary DirectSound buffer: %08lx\n", hr); - goto fail; - } - - hr = lpdsb->Play (0, 0, DSBPLAY_LOOPING); - if (FAILED (hr)) - { - Printf ("Could not play secondary buffer: %08lx\n", hr); - goto fail; - } - - RenderBuffer = (SDWORD *)malloc (BufferBytes << 1); - if (RenderBuffer == NULL) - { - Printf ("Could not alloc sound render buffer\n"); - goto fail; - } - - MixerEvent = CreateEvent (NULL, FALSE, FALSE, NULL); - if (MixerEvent == NULL) - { - Printf ("Could not create mixer event: %08lx\n", GetLastError()); - goto fail; - } - - MaxWaitTime = BufferSamples * 333 / wfx.nSamplesPerSec; - - DWORD dummy; - MixerThread = CreateThread (NULL, 0, MixerThreadFunc, this, 0, &dummy); - if (MixerThread == NULL) - { - Printf ("Could not create mixer thread: %08lx\n", GetLastError()); - goto fail; - } - SetThreadPriority (MixerThread, THREAD_PRIORITY_ABOVE_NORMAL); - - WritePos = 0; - return true; - -fail: - if (MixerEvent != NULL) - { - CloseHandle (MixerEvent); - MixerEvent = NULL; - } - if (RenderBuffer != NULL) - { - free (RenderBuffer); - RenderBuffer = NULL; - } - if (lpds != NULL) - { - lpds->Release (); - lpds = NULL; - } - return false; -} - -//========================================================================== -// -// AltSoundRenderer :: Shutdown -// -//========================================================================== - -void AltSoundRenderer::Shutdown () -{ - if (lpds != NULL && DidInit) - { - MixerQuit = true; - SetEvent (MixerEvent); - WaitForSingleObject (MixerThread, INFINITE); - CloseHandle (MixerEvent); - MixerEvent = NULL; - - lpds->Release (); - lpds = NULL; - - free (RenderBuffer); - RenderBuffer = NULL; - - if (Channels != NULL) - { - for (int i = 0; i < NumChannels; ++i) - { - DeleteCriticalSection (&Channels[i].CriticalSection); - } - delete[] Channels; - NumChannels = 0; - } - - for (unsigned int i = 0; i < S_sfx.Size(); ++i) - { - if (S_sfx[i].data != NULL) - { - delete[] (BYTE *)S_sfx[i].data; - S_sfx[i].data = NULL; - } - } - while (Streams != NULL) - { - delete Streams; - } - } - DeleteCriticalSection (&StreamCriticalSection); - DidInit = false; -} - -//========================================================================== -// -// AltSoundRenderer :: MovieDisableSound -// -//========================================================================== - -void AltSoundRenderer::MovieDisableSound () -{ - I_ShutdownMusic (); - Shutdown (); -} - -//========================================================================== -// -// AltSoundRenderer :: MovieResumeSound -// -//========================================================================== - -void AltSoundRenderer::MovieResumeSound () -{ - Init (); - S_Init (); - S_RestartMusic (); -} - -//========================================================================== -// -// AltSoundRenderer :: SetChannels -// -//========================================================================== - -int AltSoundRenderer::SetChannels (int numchannels) -{ - int i; - - Channels = new Channel[numchannels]; - NumChannels = numchannels; - - for (i = 0; i < numchannels; ++i) - { - Channels[i].Sample = NULL; - Channels[i].SamplePos = 0; - Channels[i].SampleStep = 0; - Channels[i].LeftVolume = 0; - Channels[i].RightVolume = 0; - Channels[i].Looping = false; - InitializeCriticalSection (&Channels[i].CriticalSection); - } - - return numchannels; -} - -//========================================================================== -// -// AltSoundRenderer :: SetSfxVolume -// -//========================================================================== - -void AltSoundRenderer :: SetSfxVolume (float volume) -{ - Amp = int(volume * 256.0); - SetEvent (MixerEvent); -} - -//========================================================================== -// -// AltSoundRenderer :: StartSound -// -//========================================================================== - -long AltSoundRenderer::StartSound (sfxinfo_t *sfx, int vol, int sep, int pitch, int channel, bool looping, bool pauseable) -{ - if (sfx->data == NULL || Channels == NULL) - { - return 0; - } - - Channel *chan = Channels + channel; - QWORD step = ((QWORD)PITCH (sfx->frequency, pitch) << 32) / Frequency; - SDWORD left, right; - - if (sep < -1) - { - left = right = 191 * vol / 255; - } - else if (sep == -1) - { - left = 191 * vol / 255; - right = -left; - } - else - { - sep += 1; - left = vol - ((vol*sep*sep) >> 16); - sep -= 257; - right = vol - ((vol*sep*sep) >> 16); - /* - left = 256 * (255 - sep) / 255; - right = 256 * sep / 255; - */ - } - - EnterCriticalSection (&chan->CriticalSection); - chan->Sample = sfx; - chan->SamplePos = 0; - chan->SampleStep = step; - chan->LeftVolume = left; - chan->RightVolume = right; - chan->Looping = !!looping; - chan->bPaused = false; - chan->bIsPauseable = pauseable; - LeaveCriticalSection (&chan->CriticalSection); - - return channel + 1; -} - -//========================================================================== -// -// AltSoundRenderer :: StopSound -// -//========================================================================== - -void AltSoundRenderer::StopSound (long handle) -{ - if (Channels == NULL) return; - - Channel *chan = Channels + handle - 1; - - chan->Sample = NULL; -} - -//========================================================================== -// -// AltSoundRenderer :: StopAllChannels -// -//========================================================================== - -void AltSoundRenderer::StopAllChannels () -{ - if (Channels != NULL) - { - for (int i = 0; i < NumChannels; ++i) - { - Channels[i].Sample = NULL; - } - } -} - -//========================================================================== -// -// AltSoundRenderer :: SetSfxPaused -// -//========================================================================== - -void AltSoundRenderer::SetSfxPaused (bool paused) -{ - if (Channels == NULL) return; - - for (int i = 0; i < NumChannels; ++i) - { - if (Channels[i].bIsPauseable) - { - Channels[i].bPaused = paused; - } - } -} - -//========================================================================== -// -// AltSoundRenderer :: IsPlayingSound -// -//========================================================================== - -bool AltSoundRenderer::IsPlayingSound (long handle) -{ - if (Channels == NULL) return 0; - - Channel *chan = Channels + handle - 1; - - return chan->Sample != NULL; -} - -//========================================================================== -// -// AltSoundRenderer :: UpdateSoundParams -// -//========================================================================== - -void AltSoundRenderer :: UpdateSoundParams (long handle, int vol, int sep, int pitch) -{ - if (Channels == NULL) return; - - Channel *chan = Channels + handle - 1; - - SDWORD left, right; - - if (sep < -1) - { - left = right = 191 * vol / 255; - } - else if (sep == -1) - { - left = 191 * vol / 255; - right = -left; - } - else - { - sep += 1; - left = vol - ((vol*sep*sep) >> 16); - sep -= 257; - right = vol - ((vol*sep*sep) >> 16); - } - - EnterCriticalSection (&chan->CriticalSection); - if (chan->Sample != NULL) - { - chan->SampleStep = ((QWORD)PITCH (chan->Sample->frequency, pitch) << 32) / Frequency; - chan->LeftVolume = left; - chan->RightVolume = right; - } - LeaveCriticalSection (&chan->CriticalSection); -} - -//========================================================================== -// -// AltSoundRenderer :: LoadSound -// -//========================================================================== - -void AltSoundRenderer::LoadSound (sfxinfo_t *sfx) -{ - bool stereo = false, signed8 = false; - SDWORD len; - BYTE *sfxdata, *sfxstart; - SDWORD size; - - if (sfx->data != NULL) - { - return; - } - - size = sfx->lumpnum >= 0 ? Wads.LumpLength (sfx->lumpnum) : 0; - if (size == 0) - { - sfx->lumpnum = Wads.GetNumForName ("dsempty", ns_sounds); - size = Wads.LumpLength (sfx->lumpnum); - if (size == 0) - { - return; - } - } - - sfxdata = new BYTE[size]; - Wads.ReadLump (sfx->lumpnum, sfxdata); - sfxstart = NULL; - len = ((SDWORD *)sfxdata)[1]; - - if (sfx->bLoadRAW || *(DWORD *)sfxdata == ID_RIFF || *(DWORD *)sfxdata == ID_fLaC || - (sfxdata[0] == 3 && sfxdata[1] == 0 && len <= size - 8)) - { - if (sfx->bLoadRAW) - { // raw - len = size; - sfx->frequency = (sfx->bForce22050 ? 22050 : 11025); - sfxstart = sfxdata; - sfx->b16bit = false; - } - else if (*(DWORD *)sfxdata == ID_RIFF) - { // WAVE - BYTE *sfxend, *sfx_p; - WAVEFORMAT fmtchunk; - - if (LittleLong(((SDWORD *)sfxdata)[1]) > size - 8) - { // lump is too short - goto badwave; - } - if (((DWORD *)sfxdata)[2] != ID_WAVE) - { // not really a WAVE - goto badwave; - } - - sfxend = sfxdata + LittleLong(((DWORD *)sfxdata)[1]) + 8; - sfx_p = sfxdata + 4*3; - fmtchunk.wFormatTag = ~0; - - while (sfx_p < sfxend) - { - DWORD chunkid = ((DWORD *)sfx_p)[0]; - DWORD chunklen = LittleLong(((DWORD *)sfx_p)[1]); - sfx_p += 4*2; - if (chunkid == ID_fmt) - { - if (chunklen < sizeof(fmtchunk)) - { // fmt chunk is too short - continue; - } - memcpy (&fmtchunk, sfx_p, sizeof(fmtchunk)); - fmtchunk.wFormatTag = LittleShort(fmtchunk.wFormatTag); - fmtchunk.nChannels = LittleShort(fmtchunk.nChannels); - fmtchunk.nSamplesPerSec = LittleShort(fmtchunk.nSamplesPerSec); - fmtchunk.nAvgBytesPerSec = LittleShort(fmtchunk.nAvgBytesPerSec); - fmtchunk.nBlockAlign = LittleShort(fmtchunk.nBlockAlign); - } - else if (chunkid == ID_data) - { - if (fmtchunk.wFormatTag == WAVE_FORMAT_PCM) - { - sfxstart = sfx_p; - len = chunklen; - sfx->frequency = fmtchunk.nSamplesPerSec; - stereo = fmtchunk.nChannels > 1; - sfx->b16bit = (fmtchunk.nBlockAlign >> (stereo?1:0)) == 2; - } - break; - } - sfx_p += chunklen; - } - } - else if (*(DWORD *)sfxdata == ID_fLaC) - { - delete[] sfxdata; - FLACSampleLoader loader (sfx); - signed8 = true; - sfxstart = sfxdata = loader.ReadSample (&len); - if (sfxdata == NULL) - { - goto badwave; - } - } - else - { // DMX - sfx->frequency = LittleShort(((WORD *)sfxdata)[1]); - if (sfx->frequency == 0) - { - sfx->frequency = 11025; - } - sfxstart = sfxdata + 8; - sfx->b16bit = false; - } - if (sfxstart != NULL) - { - if (!sfx->b16bit && signed8) - { // Copy signed 8-bit mono as-is - sfx->length = len; - sfx->data = new BYTE[len + 2]; - memcpy (sfx->data, sfxstart, len); - } - else if (!sfx->b16bit && !stereo) - { // Convert 8-bit mono to signed - sfx->length = len; - sfx->data = new BYTE[len + 2]; - for (SDWORD i = 0; i < len; ++i) - { - ((SBYTE *)sfx->data)[i] = sfxstart[i] - 128; - } - } - else if (!sfx->b16bit && stereo) - { // Convert 8-bit stereo to signed and merge into one channel - sfx->length = len / 2; - sfx->data = new BYTE[len / 2 + 2]; - for (SDWORD i = 0; i < len/2; ++i) - { - ((SBYTE *)sfx->data)[i] = (sfxstart[i*2] + sfxstart[i*2+1] - 256) / 2; - } - } - else if (sfx->b16bit && !stereo) - { // Copy 16-bit mono as-is - sfx->length = len / 2; - sfx->data = new BYTE[len + 4]; - for (SDWORD i = 0; i < len/2; ++i) - { - ((SWORD *)sfx->data)[i] = LittleShort(((SWORD *)sfxstart)[i]); - } - } - else - { // Merge 16-bit stereo into one channel - sfx->length = len / 4; - sfx->data = new BYTE[len / 2 + 4]; - for (SDWORD i = 0; i < len/4; ++i) - { - ((SWORD *)sfx->data)[i] = (LittleShort(((SWORD *)sfxstart)[i*2]) + LittleShort(((SWORD *)sfxstart)[i*2+1])) / 2; - } - } - sfx->ms = sfx->length * 1000 / sfx->frequency; - } - DPrintf ("sound loaded: %d Hz %d samples\n", sfx->frequency, sfx->length); - } - if (sfx->data == NULL) - { -badwave: - if (sfxdata != NULL) - { - delete[] sfxdata; - sfxdata = NULL; - } - sfx->lumpnum = Wads.GetNumForName ("dsempty", ns_sounds); - LoadSound (sfx); - } - if (sfxdata != NULL) delete[] sfxdata; -} - -//========================================================================== -// -// AltSoundRenderer :: UnloadSound -// -//========================================================================== - -void AltSoundRenderer::UnloadSound (sfxinfo_t *sfx) -{ - if (sfx->data != NULL) - { - delete[] (BYTE *)sfx->data; - sfx->data = NULL; - } -} - -//========================================================================== -// -// AltSoundRenderer :: UpdateSounds -// -//========================================================================== - -void AltSoundRenderer::UpdateSounds () -{ - SetEvent (MixerEvent); -} - -//========================================================================== -// -// AltSoundRenderer :: MixerThreadFunc -// -// Sits in a loop, periodically updating the sound buffer. The idea to -// use an event to get sound changes to happen at specific times comes -// from one of KB's articles about FR-08's sound system. So is the idea to -// use just a single buffer rather than doublebuffering with position -// notifications. See -// -//========================================================================== - -DWORD WINAPI AltSoundRenderer::MixerThreadFunc (LPVOID param) -{ - AltSoundRenderer *me = (AltSoundRenderer *)param; - for (;;) - { - WaitForSingleObject (me->MixerEvent, me->MaxWaitTime); - if (me->MixerQuit) - { - return 0; - } - me->UpdateSound (); - } -} - -//========================================================================== -// -// AltSoundRenderer :: UpdateSound -// -//========================================================================== - -void AltSoundRenderer::UpdateSound () -{ - HRESULT hr; - DWORD play, write, total; - SWORD *ptr1; DWORD bytes1; - SWORD *ptr2; DWORD bytes2; - cycle_t meter = 0; - - clock(meter); - - hr = lpdsb->GetCurrentPosition (&play, &write); - if (FAILED (hr)) - { - return; - } - - if (play < WritePos) - { - total = (BufferBytes - WritePos) + play; - } - else - { - total = play - WritePos; - } - - memset (RenderBuffer, 0, total << 1); - for (int i = 0; i < NumChannels; ++i) - { - EnterCriticalSection (&Channels[i].CriticalSection); - if (Channels[i].Sample != NULL && !Channels[i].bPaused) - { - if (Channels[i].Sample->b16bit) - { - AddChannel16 (Channels + i, total >> 2); - } - else - { - AddChannel8 (Channels + i, total >> 2); - } - } - LeaveCriticalSection (&Channels[i].CriticalSection); - } - EnterCriticalSection (&StreamCriticalSection); - for (Stream *stream = Streams; stream != NULL; stream = stream->Next) - { - if (!stream->Paused) - { - if (stream->Bits8) - { - AddStream8 (stream, total >> 2); - } - else - { - AddStream16 (stream, total >> 2); - } - } - } - LeaveCriticalSection (&StreamCriticalSection); - - hr = lpdsb->Lock (WritePos, total, (LPVOID *)&ptr1, &bytes1, (LPVOID *)&ptr2, &bytes2, 0); - if (FAILED (hr)) - { - goto addperf; - } - - CopyAndClip (ptr1, bytes1 >> 1, 0); - if (ptr2 != NULL) CopyAndClip (ptr2, bytes2 >> 1, bytes1 >> 1); - - lpdsb->Unlock (ptr1, bytes1, ptr2, bytes2); - - WritePos = play; - BufferTime += total; - -addperf: - unclock(meter); - - // % of CPU is determined as the time spent to calculate this chunk - // divided by the time it takes to play the chunk. I'm not sure I'm - // doing this right, since I get numbers that fluctuate a lot. - - PerfMeter[CurPerfMeter] = meter * SecondsPerCycle * double(Frequency) / double(total/4); - CurPerfMeter = (CurPerfMeter + 1) & (NUM_PERFMETERS-1); -} - -//========================================================================== -// -// AltSoundRenderer :: CopyAndClip -// -//========================================================================== - -void AltSoundRenderer::CopyAndClip (SWORD *buffer, DWORD count, DWORD start) -{ - SDWORD *from = RenderBuffer + start; -#if defined(_MSC_VER) && defined(USEASM) - if (CPU.bMMX) - { - __asm - { - mov ecx, count - mov esi, from - and ecx, ~15 - mov edi, buffer - lea esi, [esi+ecx*4] - lea edi, [edi+ecx*2] - neg ecx - -loopermmx: movq mm0, [esi+ecx*4] - movq mm1, [esi+ecx*4+8] - psrad mm0, 8 - movq mm2, [esi+ecx*4+16] - psrad mm1, 8 - movq mm3, [esi+ecx*4+24] - psrad mm2, 8 - movq mm4, [esi+ecx*4+32] - psrad mm3, 8 - movq mm5, [esi+ecx*4+40] - psrad mm4, 8 - movq mm6, [esi+ecx*4+48] - psrad mm5, 8 - movq mm7, [esi+ecx*4+56] - - packssdw mm1,mm0 - psrad mm6, 8 - movq [edi+ecx*2],mm1 - packssdw mm3,mm2 - psrad mm7, 8 - packssdw mm5,mm4 - movq [edi+ecx*2+8],mm3 - packssdw mm7,mm6 - movq [edi+ecx*2+16],mm5 - movq [edi+ecx*2+24],mm7 - - add ecx, 16 - jl loopermmx - - emms - mov from, esi - mov buffer, edi - } - count &= 15; - if (count == 0) - return; - } - if (CPU.bCMOV) - { - __asm - { - mov ecx, count - mov esi, from - mov edi, buffer - lea esi, [esi+ecx*4] - lea edi, [edi+ecx*2] - shr ecx, 1 - neg ecx - mov edx, 0x00007fff -looper: mov eax, [esi+ecx*8] - mov ebx, [esi+ecx*8+4] - sar eax, 8 - sar ebx, 8 - - // Clamp high - cmp eax, edx - cmovg eax, edx - cmp ebx, edx - cmovg ebx, edx - - // Clamp low - not edx - cmp eax, edx - cmovl eax, edx - cmp ebx, edx - cmovl ebx, edx - - not edx - mov [edi+ecx*4], ax - mov [edi+ecx*4+2], bx - - inc ecx - jnz looper - } - return; - } -#endif - for (; count; --count) - { - SDWORD val = *from++ >> 8; - if (val > 32767) - { - val = 32767; - } - else if (val < -32768) - { - val = -32768; - } - - *buffer++ = (SWORD)val; - } -} - -//========================================================================== -// -// AltSoundRenderer :: AddChannel8 -// -//========================================================================== - -void AltSoundRenderer::AddChannel8 (Channel *chan, DWORD count) -{ - SBYTE *from; - QWORD step; - SQWORD stop; - SDWORD *buffer, left, right; - SQWORD pos; - - if ((chan->LeftVolume | chan->RightVolume) == 0) - { - return; - } - - buffer = RenderBuffer; - from = (SBYTE *)chan->Sample->data; - stop = SQWORD(chan->Sample->length) << 32; - pos = chan->SamplePos; - - left = chan->LeftVolume * Amp * 257 / 256; - right = chan->RightVolume * Amp * 257 / 256; - step = chan->SampleStep; - - // LoadSample allocates an extra byte at the end of the sample just - // so that we can alter it for interpolated sounds. - from[chan->Sample->length] = chan->Looping ? from[0] : 0; - from[chan->Sample->length+1] = chan->Looping ? from[1] : 0; - while (count > 0) - { - DWORD avail = DWORD((stop - pos + step - 1) / step); - if (avail > count) avail = count; - pos = MixMono8 (buffer, from, avail, pos, step, left, right); - buffer += avail * 2; - count -= avail; - if (pos >= stop) - { - if (chan->Looping) - { - pos -= stop; - } - else - { - chan->Sample = NULL; - break; - } - } - } - chan->SamplePos = pos; -} - -//========================================================================== -// -// AltSoundRenderer :: AddChannel16 -// -//========================================================================== - -void AltSoundRenderer::AddChannel16 (Channel *chan, DWORD count) -{ - SWORD *from; - QWORD step; - SQWORD stop; - SDWORD *buffer, left, right; - SQWORD pos; - - if ((chan->LeftVolume | chan->RightVolume) == 0) - { - return; - } - - buffer = RenderBuffer; - from = (SWORD *)chan->Sample->data; - stop = SQWORD(chan->Sample->length) << 32; - - left = (chan->LeftVolume * Amp) >> 8; - right = (chan->RightVolume * Amp) >> 8; - step = chan->SampleStep; - pos = chan->SamplePos; - - // LoadSample allocates an extra byte at the end of the sample just - // so that we can alter it for interpolated sounds. - from[chan->Sample->length] = chan->Looping ? from[0] : 0; - from[chan->Sample->length+1] = chan->Looping ? from[1] : 0; - while (count > 0) - { - DWORD avail = DWORD((stop - pos + step - 1) / step); - if (avail > count) avail = count; - pos = MixMono16 (buffer, from, avail, pos, step, left, right); - buffer += avail * 2; - count -= avail; - if (pos >= stop) - { - if (chan->Looping) - { - pos -= stop; - } - else - { - chan->Sample = NULL; - break; - } - } - } - chan->SamplePos = pos; -} - -//========================================================================== -// -// AltSoundRenderer :: CreateStream -// -//========================================================================== - -SoundStream *AltSoundRenderer::CreateStream (SoundStreamCallback callback, - int buffbytes, int flags, int samplerate, void *userdata) -{ - Stream *stream = new Stream (this, callback, buffbytes, flags, samplerate, Frequency, userdata); - - EnterCriticalSection (&StreamCriticalSection); - stream->Prev = &Streams; - stream->Next = Streams; - Streams = stream; - LeaveCriticalSection (&StreamCriticalSection); - return stream; -} - -//========================================================================== -// -// AltSoundRenderer :: OpenStream -// -//========================================================================== - -SoundStream *AltSoundRenderer::OpenStream (const char *filename, int flags, int offset, int length) -{ - return NULL; -} - -//========================================================================== -// -// AltSoundRenderer :: AddStream8 -// -//========================================================================== - -void AltSoundRenderer::AddStream8 (Stream *stream, DWORD count) -{ - SBYTE *from; - QWORD step; - SQWORD stop; - SDWORD *buffer, vol; - SQWORD pos; - - if (stream->Volume == 0) - { - return; - } - - buffer = RenderBuffer; - from = (SBYTE *)stream->Buffer; - if (stream->Mono) - { - stop = SQWORD(stream->BufferLen - 4) << 32; - } - else - { - stop = SQWORD(stream->BufferLen / 2 - 8) << 32; - } - - vol = stream->Volume * Amp * 257 / 256; - step = stream->SampleStep; - pos = stream->SamplePos; - - if (!stream->DidFirstRun) - { - if (!stream->Callback (stream, stream->Buffer, stream->BufferLen, stream->UserData)) - { - stream->Stop (); - return; - } - stream->DidFirstRun = true; - } - - while (count > 0) - { - DWORD avail = DWORD((stop - pos + step - 1) / step); - if (avail > count) avail = count; - if (stream->Mono) - { - pos = MixMono8 (buffer, from, avail, pos, step, vol, vol); - } - else - { - pos = MixStereo8 (buffer, from, avail, pos, step, vol); - } - buffer += avail * 2; - count -= avail; - if (pos >= stop) - { - pos -= stop; - const int extra = stream->Mono ? 4 : 8; - memcpy (stream->Buffer, stream->Buffer + stream->BufferLen - extra, extra); - if (!stream->Callback (stream, stream->Buffer + extra, stream->BufferLen - extra, stream->UserData)) - { - stream->Stop (); - return; - } - } - } - stream->SamplePos = pos; -} - -//========================================================================== -// -// AltSoundRenderer :: AddStream16 -// -// -//========================================================================== - -void AltSoundRenderer::AddStream16 (Stream *stream, DWORD count) -{ - SWORD *from; - QWORD step; - SQWORD stop; - SDWORD *buffer, vol; - SQWORD pos; - - if (stream->Volume == 0) - { - return; - } - - buffer = RenderBuffer; - from = (SWORD *)stream->Buffer; - if (stream->Mono) - { - stop = SQWORD(stream->BufferLen / 2 - 4) << 32; - } - else - { - stop = SQWORD(stream->BufferLen / 4 - 8) << 32; - } - - vol = (stream->Volume * Amp) >> 8; - step = stream->SampleStep; - pos = stream->SamplePos; - - if (!stream->DidFirstRun) - { - if (!stream->Callback (stream, stream->Buffer, stream->BufferLen, stream->UserData)) - { - stream->Stop (); - return; - } - stream->DidFirstRun = true; - } - - while (count > 0) - { - DWORD avail = int((stop - pos + step - 1) / step); - if (avail > count) avail = count; - if (stream->Mono) - { - pos = MixMono16 (buffer, from, avail, pos, step, vol, vol); - } - else - { - pos = MixStereo16 (buffer, from, avail, pos, step, vol); - } - buffer += avail * 2; - count -= avail; - if (pos >= stop) - { - pos -= stop; - const int extra = stream->Mono ? 8 : 16; - memcpy (stream->Buffer, stream->Buffer + stream->BufferLen - extra, extra); - if (!stream->Callback (stream, stream->Buffer + extra, stream->BufferLen - extra, stream->UserData)) - { - stream->Stop (); - return; - } - } - } - stream->SamplePos = pos; -} - -//========================================================================== -// -// AltSoundRenderer :: PrintStatus -// -//========================================================================== - -void AltSoundRenderer::PrintStatus () -{ - Printf ("Using non-FMOD sound renderer.\n"); -} - -//========================================================================== -// -// AltSoundRenderer :: PrintDriversList -// -//========================================================================== - -void AltSoundRenderer::PrintDriversList () -{ - Printf ("No user-selectable drivers.\n"); -} - -//========================================================================== -// -// AltSoundRenderer :: GatherStats -// -//========================================================================== - -FString AltSoundRenderer::GatherStats () -{ - int i, countc, counts, totals; - for (i = countc = 0; i < NumChannels; ++i) - { - if (Channels[i].Sample != NULL) - { - countc++; - } - } - EnterCriticalSection (&StreamCriticalSection); - counts = totals = 0; - for (Stream *stream = Streams; stream != NULL; stream = stream->Next) - { - totals++; - if (!stream->Paused) - { - counts++; - } - } - LeaveCriticalSection (&StreamCriticalSection); - double perf = 0.0; - for (i = 0; i < NUM_PERFMETERS; ++i) - { - perf += PerfMeter[i]; - } - FString out; - out.Format ("%2d/%2d channels, %d/%d streams, %.2f%%", countc, NumChannels, counts, totals, perf*100.0/NUM_PERFMETERS); - return out; -} - -#endif diff --git a/src/sound/altsound.h b/src/sound/altsound.h deleted file mode 100644 index c79851d37..000000000 --- a/src/sound/altsound.h +++ /dev/null @@ -1,106 +0,0 @@ -#ifndef ALTSOUND_H -#define ALTSOUND_H - -#define WIN32_LEAN_AND_MEAN -#include -#include -#include - -#include "i_sound.h" - -class AltSoundRenderer : public SoundRenderer -{ -public: - AltSoundRenderer (); - ~AltSoundRenderer (); - bool IsValid (); - - void SetSfxVolume (float volume); - int SetChannels (int numchans); - void LoadSound (sfxinfo_t *sfx); - void UnloadSound (sfxinfo_t *sfx); - - // Streaming sounds. PlayStream returns a channel handle that can be used with StopSound. - SoundStream *CreateStream (SoundStreamCallback callback, int buffsamples, int flags, int samplerate, void *userdata); - SoundStream *OpenStream (const char *filename, int flags, int offset, int length); - - // Starts a sound in a particular sound channel. - long StartSound (sfxinfo_t *sfx, int vol, int sep, int pitch, int channel, bool looping, bool pauseable); - - // Stops a sound channel. - void StopSound (long handle); - - // Stops all sounds. - void StopAllChannels (); - - // Pauses or resumes all sound effect channels. - void SetSfxPaused (bool paused); - - // Returns true if the channel is still playing a sound. - bool IsPlayingSound (long handle); - - // Updates the volume, separation, and pitch of a sound channel. - void UpdateSoundParams (long handle, int vol, int sep, int pitch); - - // For use by I_PlayMovie - void MovieDisableSound (); - void MovieResumeSound (); - - void UpdateSounds (); - - void PrintStatus (); - void PrintDriversList (); - FString GatherStats (); - -private: - struct Channel; - struct Stream; - - LPDIRECTSOUND lpds; - LPDIRECTSOUNDBUFFER lpdsb, lpdsbPrimary; - - int Frequency; - bool SimpleDown; - int Amp; - DWORD BufferSamples, BufferBytes; - DWORD WritePos; - DWORD BufferTime; - DWORD MaxWaitTime; - SDWORD *RenderBuffer; - HANDLE MixerThread; - HANDLE MixerEvent; - bool MixerQuit; - - enum { NUM_PERFMETERS = 32 }; - double PerfMeter[NUM_PERFMETERS]; - int CurPerfMeter; - - Channel *Channels; - int NumChannels; - Stream *Streams; - CRITICAL_SECTION StreamCriticalSection; - - bool DidInit; - - bool Init (); - void Shutdown (); - - void CopyAndClip (SWORD *buffer, DWORD count, DWORD start); - void UpdateSound (); - void AddChannel8 (Channel *chan, DWORD count); - void AddChannel16 (Channel *chan, DWORD count); - void AddStream8 (Stream *chan, DWORD count); - void AddStream16 (Stream *chan, DWORD count); - static DWORD WINAPI MixerThreadFunc (LPVOID param); - - static SQWORD MixMono8 (SDWORD *dest, const SBYTE *src, DWORD count, SQWORD pos, SQWORD step, int leftvol, int rightvol); - static SQWORD MixMono16 (SDWORD *dest, const SWORD *src, DWORD count, SQWORD pos, SQWORD step, int leftvol, int rightvol); - static SQWORD MixStereo8 (SDWORD *dest, const SBYTE *src, DWORD count, SQWORD pos, SQWORD step, int vol); - static SQWORD MixStereo16 (SDWORD *dest, const SWORD *src, DWORD count, SQWORD pos, SQWORD step, int vol); -}; - -class AltSoundStream : public SoundStream -{ -}; - -#endif diff --git a/src/sound/altsoundmixer.cpp b/src/sound/altsoundmixer.cpp deleted file mode 100644 index 232f968d2..000000000 --- a/src/sound/altsoundmixer.cpp +++ /dev/null @@ -1,156 +0,0 @@ -#ifdef _WIN32 - -#define USE_WINDOWS_DWORD -#include "altsound.h" -#include "c_cvars.h" - -EXTERN_CVAR (Int, snd_interpolate) - -// These macros implement linear interpolation - -__forceinline SDWORD lininterp (SDWORD sample1, SDWORD sample2, DWORD frac) -{ - return sample1 + MulScale30 (frac >> 2, sample2 - sample1); -} - -#define INTERPOLATEL \ - SDWORD sample = lininterp ((SDWORD)src[pos.HighPart], (SDWORD)src[pos.HighPart+1], pos.LowPart); - -#define INTERPOLATEL2 \ - SDWORD samplel = lininterp ((SDWORD)src[pos.HighPart*2+0], (SDWORD)src[pos.HighPart*2+2], pos.LowPart); \ - SDWORD sampler = lininterp ((SDWORD)src[pos.HighPart*2+1], (SDWORD)src[pos.HighPart*2+3], pos.LowPart); - -// These macros implement quadratic interpolation - -__forceinline SDWORD quadraticinterp (SDWORD sample1, SDWORD sample2, SDWORD sample3, DWORD frac) -{ - - SDWORD a = sample1 - 2*sample2 + sample3; - SDWORD b = -3*sample1 + 4*sample2 - sample3; - frac >>= 17; - return MulScale31 (a, frac*frac) + MulScale16 (b, frac) + sample1; -} - -#define INTERPOLATEQ \ - SDWORD sample = quadraticinterp ((SDWORD)src[pos.HighPart], \ - (SDWORD)src[pos.HighPart + 1], \ - (SDWORD)src[pos.HighPart + 2], \ - pos.LowPart); - -#define INTERPOLATEQ2 \ - SDWORD samplel = quadraticinterp ((SDWORD)src[pos.HighPart*2], \ - (SDWORD)src[pos.HighPart*2 + 2], \ - (SDWORD)src[pos.HighPart*2 + 4], \ - pos.LowPart); \ - SDWORD sampler = quadraticinterp ((SDWORD)src[pos.HighPart*2 + 1], \ - (SDWORD)src[pos.HighPart*2 + 3], \ - (SDWORD)src[pos.HighPart*2 + 5], \ - pos.LowPart); - -/* Possible snd_interpolate values: - 0: No interpolation - 1: Linear interpolation - 2: Quadratic interpolation -*/ -#define MONO_MIXER \ - LARGE_INTEGER pos; \ - \ - switch (snd_interpolate) \ - { \ - default: \ - for (pos.QuadPart = inpos; count; --count) \ - { \ - SDWORD sample = (SDWORD)src[pos.HighPart]; \ - pos.QuadPart += step; \ - dest[0] += sample * leftvol; \ - dest[1] += sample * rightvol; \ - dest += 2; \ - } \ - break; \ - \ - case 1: \ - for (pos.QuadPart = inpos; count; --count) \ - { \ - INTERPOLATEL \ - pos.QuadPart += step; \ - dest[0] += sample * leftvol; \ - dest[1] += sample * rightvol; \ - dest += 2; \ - } \ - break; \ - \ - case 2: \ - for (pos.QuadPart = inpos; count; --count) \ - { \ - INTERPOLATEQ \ - pos.QuadPart += step; \ - dest[0] += sample * leftvol; \ - dest[1] += sample * rightvol; \ - dest += 2; \ - } \ - break; \ - } \ - return pos.QuadPart; - -#define STEREO_MIXER \ - LARGE_INTEGER pos; \ - \ - switch (snd_interpolate) \ - { \ - default: \ - for (pos.QuadPart = inpos; count; --count) \ - { \ - SDWORD samplel = (SDWORD)src[pos.HighPart*2]; \ - SDWORD sampler = (SDWORD)src[pos.HighPart*2+1]; \ - pos.QuadPart += step; \ - dest[0] += samplel * vol; \ - dest[1] += sampler * vol; \ - dest += 2; \ - } \ - break; \ - \ - case 1: \ - for (pos.QuadPart = inpos; count; --count) \ - { \ - INTERPOLATEL2 \ - pos.QuadPart += step; \ - dest[0] += samplel * vol; \ - dest[1] += sampler * vol; \ - dest += 2; \ - } \ - break; \ - \ - case 2: \ - for (pos.QuadPart = inpos; count; --count) \ - { \ - INTERPOLATEQ2 \ - pos.QuadPart += step; \ - dest[0] += samplel * vol; \ - dest[1] += sampler * vol; \ - dest += 2; \ - } \ - break; \ - } \ - return pos.QuadPart; - -SQWORD AltSoundRenderer::MixMono8 (SDWORD *dest, const SBYTE *src, DWORD count, SQWORD inpos, SQWORD step, int leftvol, int rightvol) -{ - MONO_MIXER -} - -SQWORD AltSoundRenderer::MixMono16 (SDWORD *dest, const SWORD *src, DWORD count, SQWORD inpos, SQWORD step, int leftvol, int rightvol) -{ - MONO_MIXER -} - -SQWORD AltSoundRenderer::MixStereo8 (SDWORD *dest, const SBYTE *src, DWORD count, SQWORD inpos, SQWORD step, int vol) -{ - STEREO_MIXER -} - -SQWORD AltSoundRenderer::MixStereo16 (SDWORD *dest, const SWORD *src, DWORD count, SQWORD inpos, SQWORD step, int vol) -{ - STEREO_MIXER -} - -#endif diff --git a/src/sound/fmod_wrap.h b/src/sound/fmod_wrap.h new file mode 100644 index 000000000..7bda4bbbd --- /dev/null +++ b/src/sound/fmod_wrap.h @@ -0,0 +1,568 @@ + +#ifndef FMOD_WRAP_H +#define FMOD_WRAP_H + +#if !defined(_WIN32) || defined(_MSC_VER) +// Use the real C++ interface if it's supported on this platform. +#include "fmod.hpp" +#else +// Use a wrapper C++ interface for non-Microsoft compilers on Windows. + +#include "fmod.h" + +// Create fake definitions for these structs so they can be subclassed. +struct FMOD_SYSTEM {}; +struct FMOD_SOUND {}; +struct FMOD_CHANNEL {}; +struct FMOD_CHANNELGROUP {}; +struct FMOD_SOUNDGROUP {}; +struct FMOD_REVERB {}; +struct FMOD_DSP {}; +struct FMOD_POLYGON {}; +struct FMOD_GEOMETRY {}; +struct FMOD_SYNCPOINT {}; +/* + Constant and defines +*/ + +/* + FMOD Namespace +*/ + +namespace FMOD +{ + + class System; + class Sound; + class Channel; + class ChannelGroup; + class SoundGroup; + class Reverb; + class DSP; + class Geometry; + + /* + FMOD global system functions (optional). + */ + inline FMOD_RESULT Memory_Initialize(void *poolmem, int poollen, FMOD_MEMORY_ALLOCCALLBACK useralloc, FMOD_MEMORY_REALLOCCALLBACK userrealloc, FMOD_MEMORY_FREECALLBACK userfree) { return FMOD_Memory_Initialize(poolmem, poollen, useralloc, userrealloc, userfree); } + inline FMOD_RESULT Memory_GetStats (int *currentalloced, int *maxalloced) { return FMOD_Memory_GetStats(currentalloced, maxalloced); } + inline FMOD_RESULT Debug_SetLevel(FMOD_DEBUGLEVEL level) { return FMOD_Debug_SetLevel(level); } + inline FMOD_RESULT Debug_GetLevel(FMOD_DEBUGLEVEL *level) { return FMOD_Debug_GetLevel(level); } + inline FMOD_RESULT File_SetDiskBusy(int busy) { return FMOD_File_SetDiskBusy(busy); } + inline FMOD_RESULT File_GetDiskBusy(int *busy) { return FMOD_File_GetDiskBusy(busy); } + + /* + FMOD System factory functions. + */ + inline FMOD_RESULT System_Create(System **system) { return FMOD_System_Create((FMOD_SYSTEM **)system); } + + /* + 'System' API + */ + + class System : FMOD_SYSTEM + { + private: + + System(); /* Constructor made private so user cannot statically instance a System class. + System_Create must be used. */ + public: + + FMOD_RESULT release () { return FMOD_System_Release(this); } + + // Pre-init functions. + FMOD_RESULT setOutput (FMOD_OUTPUTTYPE output) { return FMOD_System_SetOutput(this, output); } + FMOD_RESULT getOutput (FMOD_OUTPUTTYPE *output) { return FMOD_System_GetOutput(this, output); } + FMOD_RESULT getNumDrivers (int *numdrivers) { return FMOD_System_GetNumDrivers(this, numdrivers); } + FMOD_RESULT getDriverInfo (int id, char *name, int namelen, FMOD_GUID *guid) { return FMOD_System_GetDriverInfo(this, id, name, namelen, guid); } + FMOD_RESULT getDriverCaps (int id, FMOD_CAPS *caps, int *minfrequency, int *maxfrequency, FMOD_SPEAKERMODE *controlpanelspeakermode) { return FMOD_System_GetDriverCaps(this, id, caps, minfrequency, maxfrequency, controlpanelspeakermode); } + FMOD_RESULT setDriver (int driver) { return FMOD_System_SetDriver(this, driver); } + FMOD_RESULT getDriver (int *driver) { return FMOD_System_GetDriver(this, driver); } + FMOD_RESULT setHardwareChannels (int min2d, int max2d, int min3d, int max3d) { return FMOD_System_SetHardwareChannels(this, min2d, max2d, min3d, max3d); } + FMOD_RESULT setSoftwareChannels (int numsoftwarechannels) { return FMOD_System_SetSoftwareChannels(this, numsoftwarechannels); } + FMOD_RESULT getSoftwareChannels (int *numsoftwarechannels) { return FMOD_System_GetSoftwareChannels(this, numsoftwarechannels); } + FMOD_RESULT setSoftwareFormat (int samplerate, FMOD_SOUND_FORMAT format, int numoutputchannels, int maxinputchannels, FMOD_DSP_RESAMPLER resamplemethod) { return FMOD_System_SetSoftwareFormat(this, samplerate, format, numoutputchannels, maxinputchannels, resamplemethod); } + FMOD_RESULT getSoftwareFormat (int *samplerate, FMOD_SOUND_FORMAT *format, int *numoutputchannels, int *maxinputchannels, FMOD_DSP_RESAMPLER *resamplemethod, int *bits) { return FMOD_System_GetSoftwareFormat(this, samplerate, format, numoutputchannels, maxinputchannels, resamplemethod, bits); } + FMOD_RESULT setDSPBufferSize (unsigned int bufferlength, int numbuffers) { return FMOD_System_SetDSPBufferSize(this, bufferlength, numbuffers); } + FMOD_RESULT getDSPBufferSize (unsigned int *bufferlength, int *numbuffers) { return FMOD_System_GetDSPBufferSize(this, bufferlength, numbuffers); } + FMOD_RESULT setFileSystem (FMOD_FILE_OPENCALLBACK useropen, FMOD_FILE_CLOSECALLBACK userclose, FMOD_FILE_READCALLBACK userread, FMOD_FILE_SEEKCALLBACK userseek, int blockalign) { return FMOD_System_SetFileSystem(this, useropen, userclose, userread, userseek, blockalign); } + FMOD_RESULT attachFileSystem (FMOD_FILE_OPENCALLBACK useropen, FMOD_FILE_CLOSECALLBACK userclose, FMOD_FILE_READCALLBACK userread, FMOD_FILE_SEEKCALLBACK userseek) { return FMOD_System_AttachFileSystem(this, useropen, userclose, userread, userseek); } + FMOD_RESULT setAdvancedSettings (FMOD_ADVANCEDSETTINGS *settings) { return FMOD_System_SetAdvancedSettings(this, settings); } + FMOD_RESULT getAdvancedSettings (FMOD_ADVANCEDSETTINGS *settings) { return FMOD_System_GetAdvancedSettings(this, settings); } + FMOD_RESULT setSpeakerMode (FMOD_SPEAKERMODE speakermode) { return FMOD_System_SetSpeakerMode(this, speakermode); } + FMOD_RESULT getSpeakerMode (FMOD_SPEAKERMODE *speakermode) { return FMOD_System_GetSpeakerMode(this, speakermode); } + FMOD_RESULT setCallback (FMOD_SYSTEM_CALLBACKTYPE type, FMOD_SYSTEM_CALLBACK callback) { return FMOD_System_SetCallback(this, type, callback); } + + // Plug-in support + FMOD_RESULT setPluginPath (const char *path) { return FMOD_System_SetPluginPath(this, path); } + FMOD_RESULT loadPlugin (const char *filename, FMOD_PLUGINTYPE *plugintype, int *index) { return FMOD_System_LoadPlugin(this, filename, plugintype, index); } + FMOD_RESULT getNumPlugins (FMOD_PLUGINTYPE plugintype, int *numplugins) { return FMOD_System_GetNumPlugins(this, plugintype, numplugins); } + FMOD_RESULT getPluginInfo (FMOD_PLUGINTYPE plugintype, int index, char *name, int namelen, unsigned int *version) { return FMOD_System_GetPluginInfo(this, plugintype, index, name, namelen, version); } + FMOD_RESULT unloadPlugin (FMOD_PLUGINTYPE plugintype, int index) { return FMOD_System_UnloadPlugin(this, plugintype, index); } + FMOD_RESULT setOutputByPlugin (int index) { return FMOD_System_SetOutputByPlugin(this, index); } + FMOD_RESULT getOutputByPlugin (int *index) { return FMOD_System_GetOutputByPlugin(this, index); } + FMOD_RESULT createCodec (FMOD_CODEC_DESCRIPTION *description) { return FMOD_System_CreateCodec(this, description); } + + // Init/Close + FMOD_RESULT init (int maxchannels, FMOD_INITFLAGS flags, void *extradriverdata) { return FMOD_System_Init(this, maxchannels, flags, extradriverdata); } + FMOD_RESULT close () { return FMOD_System_Close(this); } + + // General post-init system functions + FMOD_RESULT update () /* IMPORTANT! CALL THIS ONCE PER FRAME! */ { return FMOD_System_Update(this); } + + FMOD_RESULT set3DSettings (float dopplerscale, float distancefactor, float rolloffscale) { return FMOD_System_Set3DSettings(this, dopplerscale, distancefactor, rolloffscale); } + FMOD_RESULT get3DSettings (float *dopplerscale, float *distancefactor, float *rolloffscale) { return FMOD_System_Get3DSettings(this, dopplerscale, distancefactor, rolloffscale); } + FMOD_RESULT set3DNumListeners (int numlisteners) { return FMOD_System_Set3DNumListeners(this, numlisteners); } + FMOD_RESULT get3DNumListeners (int *numlisteners) { return FMOD_System_Get3DNumListeners(this, numlisteners); } + FMOD_RESULT set3DListenerAttributes(int listener, const FMOD_VECTOR *pos, const FMOD_VECTOR *vel, const FMOD_VECTOR *forward, const FMOD_VECTOR *up) { return FMOD_System_Set3DListenerAttributes(this, listener, pos, vel, forward, up); } + FMOD_RESULT get3DListenerAttributes(int listener, FMOD_VECTOR *pos, FMOD_VECTOR *vel, FMOD_VECTOR *forward, FMOD_VECTOR *up) { return FMOD_System_Get3DListenerAttributes(this, listener, pos, vel, forward, up); } + FMOD_RESULT set3DRolloffCallback (FMOD_3D_ROLLOFFCALLBACK callback) { return FMOD_System_Set3DRolloffCallback(this, callback); } + FMOD_RESULT set3DSpeakerPosition (FMOD_SPEAKER speaker, float x, float y, bool active) { return FMOD_System_Set3DSpeakerPosition(this, speaker, x, y, active); } + FMOD_RESULT get3DSpeakerPosition (FMOD_SPEAKER speaker, float *x, float *y, bool *active) { FMOD_BOOL b; FMOD_RESULT res = FMOD_System_Get3DSpeakerPosition(this, speaker, x, y, &b); *active = b; return res; } + + FMOD_RESULT setStreamBufferSize (unsigned int filebuffersize, FMOD_TIMEUNIT filebuffersizetype) { return FMOD_System_SetStreamBufferSize(this, filebuffersize, filebuffersizetype); } + FMOD_RESULT getStreamBufferSize (unsigned int *filebuffersize, FMOD_TIMEUNIT *filebuffersizetype) { return FMOD_System_GetStreamBufferSize(this, filebuffersize, filebuffersizetype); } + + // System information functions. + FMOD_RESULT getVersion (unsigned int *version) { return FMOD_System_GetVersion(this, version); } + FMOD_RESULT getOutputHandle (void **handle) { return FMOD_System_GetOutputHandle(this, handle); } + FMOD_RESULT getChannelsPlaying (int *channels) { return FMOD_System_GetChannelsPlaying(this, channels); } + FMOD_RESULT getHardwareChannels (int *num2d, int *num3d, int *total) { return FMOD_System_GetHardwareChannels(this, num2d, num3d, total); } + FMOD_RESULT getCPUUsage (float *dsp, float *stream, float *update, float *total) { return FMOD_System_GetCPUUsage(this, dsp, stream, update, total); } + FMOD_RESULT getSoundRAM (int *currentalloced, int *maxalloced, int *total) { return FMOD_System_GetSoundRAM(this, currentalloced, maxalloced, total); } + FMOD_RESULT getNumCDROMDrives (int *numdrives) { return FMOD_System_GetNumCDROMDrives(this, numdrives); } + FMOD_RESULT getCDROMDriveName (int drive, char *drivename, int drivenamelen, char *scsiname, int scsinamelen, char *devicename, int devicenamelen) { return FMOD_System_GetCDROMDriveName(this, drive, drivename, drivenamelen, scsiname, scsinamelen, devicename, devicenamelen); } + FMOD_RESULT getSpectrum (float *spectrumarray, int numvalues, int channeloffset, FMOD_DSP_FFT_WINDOW windowtype) { return FMOD_System_GetSpectrum(this, spectrumarray, numvalues, channeloffset, windowtype); } + FMOD_RESULT getWaveData (float *wavearray, int numvalues, int channeloffset) { return FMOD_System_GetWaveData(this, wavearray, numvalues, channeloffset); } + + // Sound/DSP/Channel/FX creation and retrieval. + FMOD_RESULT createSound (const char *name_or_data, FMOD_MODE mode, FMOD_CREATESOUNDEXINFO *exinfo, Sound **sound) { return FMOD_System_CreateSound(this, name_or_data, mode, exinfo, (FMOD_SOUND **)sound); } + FMOD_RESULT createStream (const char *name_or_data, FMOD_MODE mode, FMOD_CREATESOUNDEXINFO *exinfo, Sound **sound) { return FMOD_System_CreateStream(this, name_or_data, mode, exinfo, (FMOD_SOUND **)sound); } + FMOD_RESULT createDSP (FMOD_DSP_DESCRIPTION *description, DSP **dsp) { return FMOD_System_CreateDSP(this, description, (FMOD_DSP **)dsp); } + FMOD_RESULT createDSPByType (FMOD_DSP_TYPE type, DSP **dsp) { return FMOD_System_CreateDSPByType(this, type, (FMOD_DSP **)dsp); } + FMOD_RESULT createDSPByIndex (int index, DSP **dsp) { return FMOD_System_CreateDSPByIndex(this, index, (FMOD_DSP **)dsp); } + FMOD_RESULT createChannelGroup (const char *name, ChannelGroup **channelgroup) { return FMOD_System_CreateChannelGroup(this, name, (FMOD_CHANNELGROUP **)channelgroup); } + FMOD_RESULT createSoundGroup (const char *name, SoundGroup **soundgroup) { return FMOD_System_CreateSoundGroup(this, name, (FMOD_SOUNDGROUP **)soundgroup); } + FMOD_RESULT createReverb (Reverb **reverb) { return FMOD_System_CreateReverb(this, (FMOD_REVERB **)reverb); } + + FMOD_RESULT playSound (FMOD_CHANNELINDEX channelid, Sound *sound, bool paused, Channel **channel) { return FMOD_System_PlaySound(this, channelid, (FMOD_SOUND *)sound, paused, (FMOD_CHANNEL **)channel); } + FMOD_RESULT playDSP (FMOD_CHANNELINDEX channelid, DSP *dsp, bool paused, Channel **channel) { return FMOD_System_PlayDSP(this, channelid, (FMOD_DSP *)dsp, paused, (FMOD_CHANNEL **)channel); } + FMOD_RESULT getChannel (int channelid, Channel **channel) { return FMOD_System_GetChannel(this, channelid, (FMOD_CHANNEL **)channel); } + FMOD_RESULT getMasterChannelGroup (ChannelGroup **channelgroup) { return FMOD_System_GetMasterChannelGroup(this, (FMOD_CHANNELGROUP **)channelgroup); } + FMOD_RESULT getMasterSoundGroup (SoundGroup **soundgroup) { return FMOD_System_GetMasterSoundGroup(this, (FMOD_SOUNDGROUP **)soundgroup); } + + // Reverb API + FMOD_RESULT setReverbProperties (const FMOD_REVERB_PROPERTIES *prop) { return FMOD_System_SetReverbProperties(this, prop); } + FMOD_RESULT getReverbProperties (FMOD_REVERB_PROPERTIES *prop) { return FMOD_System_GetReverbProperties(this, prop); } + FMOD_RESULT setReverbAmbientProperties(FMOD_REVERB_PROPERTIES *prop) { return FMOD_System_SetReverbAmbientProperties(this, prop); } + FMOD_RESULT getReverbAmbientProperties(FMOD_REVERB_PROPERTIES *prop) { return FMOD_System_GetReverbAmbientProperties(this, prop); } + + // System level DSP access. + FMOD_RESULT getDSPHead (DSP **dsp) { return FMOD_System_GetDSPHead(this, (FMOD_DSP **)dsp); } + FMOD_RESULT addDSP (DSP *dsp) { return FMOD_System_AddDSP(this, (FMOD_DSP *)dsp); } + FMOD_RESULT lockDSP () { return FMOD_System_LockDSP(this); } + FMOD_RESULT unlockDSP () { return FMOD_System_UnlockDSP(this); } + + // Recording API. + FMOD_RESULT setRecordDriver (int driver) { return FMOD_System_SetRecordDriver(this, driver); } + FMOD_RESULT getRecordDriver (int *driver) { return FMOD_System_GetRecordDriver(this, driver); } + FMOD_RESULT getRecordNumDrivers (int *numdrivers) { return FMOD_System_GetRecordNumDrivers(this, numdrivers); } + FMOD_RESULT getRecordDriverInfo (int id, char *name, int namelen, FMOD_GUID *guid) { return FMOD_System_GetRecordDriverInfo(this, id, name, namelen, guid); } + FMOD_RESULT getRecordDriverCaps (int id, FMOD_CAPS *caps, int *minfrequency, int *maxfrequency) { return FMOD_System_GetRecordDriverCaps(this, id, caps, minfrequency, maxfrequency); } + FMOD_RESULT getRecordPosition (unsigned int *position) { return FMOD_System_GetRecordPosition(this, position); } + + FMOD_RESULT recordStart (Sound *sound, bool loop) { return FMOD_System_RecordStart(this, (FMOD_SOUND *)sound, loop); } + FMOD_RESULT recordStop () { return FMOD_System_RecordStop(this); } + FMOD_RESULT isRecording (bool *recording) { FMOD_BOOL b; FMOD_RESULT res = FMOD_System_IsRecording(this, &b); *recording = b; return res; } + + // Geometry API. + FMOD_RESULT createGeometry (int maxpolygons, int maxvertices, Geometry **geometry) { return FMOD_System_CreateGeometry(this, maxpolygons, maxvertices, (FMOD_GEOMETRY **)geometry); } + FMOD_RESULT setGeometrySettings (float maxworldsize) { return FMOD_System_SetGeometrySettings(this, maxworldsize); } + FMOD_RESULT getGeometrySettings (float *maxworldsize) { return FMOD_System_GetGeometrySettings(this, maxworldsize); } + FMOD_RESULT loadGeometry (const void *data, int datasize, Geometry **geometry) { return FMOD_System_LoadGeometry(this, data, datasize, (FMOD_GEOMETRY **)geometry); } + + // Network functions. + FMOD_RESULT setNetworkProxy (const char *proxy) { return FMOD_System_SetNetworkProxy(this, proxy); } + FMOD_RESULT getNetworkProxy (char *proxy, int proxylen) { return FMOD_System_GetNetworkProxy(this, proxy, proxylen); } + FMOD_RESULT setNetworkTimeout (int timeout) { return FMOD_System_SetNetworkTimeout(this, timeout); } + FMOD_RESULT getNetworkTimeout (int *timeout) { return FMOD_System_GetNetworkTimeout(this, timeout); } + + // Userdata set/get. + FMOD_RESULT setUserData (void *userdata) { return FMOD_System_SetUserData(this, userdata); } + FMOD_RESULT getUserData (void **userdata) { return FMOD_System_GetUserData(this, userdata); } + }; + + /* + 'Sound' API + */ + class Sound : FMOD_SOUND + { + private: + + Sound(); /* Constructor made private so user cannot statically instance a Sound class. + Appropriate Sound creation or retrieval function must be used. */ + public: + + FMOD_RESULT release () { return FMOD_Sound_Release(this); } + FMOD_RESULT getSystemObject (System **system) { return FMOD_Sound_GetSystemObject(this, (FMOD_SYSTEM **)system); } + + // Standard sound manipulation functions. + FMOD_RESULT lock (unsigned int offset, unsigned int length, void **ptr1, void **ptr2, unsigned int *len1, unsigned int *len2) { return FMOD_Sound_Lock(this, offset, length, ptr1, ptr2, len1, len2); } + FMOD_RESULT unlock (void *ptr1, void *ptr2, unsigned int len1, unsigned int len2) { return FMOD_Sound_Unlock(this, ptr1, ptr2, len1, len2); } + FMOD_RESULT setDefaults (float frequency, float volume, float pan, int priority) { return FMOD_Sound_SetDefaults(this, frequency, volume, pan, priority); } + FMOD_RESULT getDefaults (float *frequency, float *volume, float *pan, int *priority) { return FMOD_Sound_GetDefaults(this, frequency, volume, pan, priority); } + FMOD_RESULT setVariations (float frequencyvar, float volumevar, float panvar) { return FMOD_Sound_SetVariations(this, frequencyvar, volumevar, panvar); } + FMOD_RESULT getVariations (float *frequencyvar, float *volumevar, float *panvar) { return FMOD_Sound_GetVariations(this, frequencyvar, volumevar, panvar); } + FMOD_RESULT set3DMinMaxDistance (float min, float max) { return FMOD_Sound_Set3DMinMaxDistance(this, min, max); } + FMOD_RESULT get3DMinMaxDistance (float *min, float *max) { return FMOD_Sound_Get3DMinMaxDistance(this, min, max); } + FMOD_RESULT set3DConeSettings (float insideconeangle, float outsideconeangle, float outsidevolume) { return FMOD_Sound_Set3DConeSettings(this, insideconeangle, outsideconeangle, outsidevolume); } + FMOD_RESULT get3DConeSettings (float *insideconeangle, float *outsideconeangle, float *outsidevolume) { return FMOD_Sound_Get3DConeSettings(this, insideconeangle, outsideconeangle, outsidevolume); } + FMOD_RESULT set3DCustomRolloff (FMOD_VECTOR *points, int numpoints) { return FMOD_Sound_Set3DCustomRolloff(this, points, numpoints); } + FMOD_RESULT get3DCustomRolloff (FMOD_VECTOR **points, int *numpoints) { return FMOD_Sound_Get3DCustomRolloff(this, points, numpoints); } + FMOD_RESULT setSubSound (int index, Sound *subsound) { return FMOD_Sound_SetSubSound(this, index, subsound); } + FMOD_RESULT getSubSound (int index, Sound **subsound) { return FMOD_Sound_GetSubSound(this, index, (FMOD_SOUND **)subsound); } + FMOD_RESULT setSubSoundSentence (int *subsoundlist, int numsubsounds) { return FMOD_Sound_SetSubSoundSentence(this, subsoundlist, numsubsounds); } + FMOD_RESULT getName (char *name, int namelen) { return FMOD_Sound_GetName(this, name, namelen); } + FMOD_RESULT getLength (unsigned int *length, FMOD_TIMEUNIT lengthtype) { return FMOD_Sound_GetLength(this, length, lengthtype); } + FMOD_RESULT getFormat (FMOD_SOUND_TYPE *type, FMOD_SOUND_FORMAT *format, int *channels, int *bits) { return FMOD_Sound_GetFormat(this, type, format, channels, bits); } + FMOD_RESULT getNumSubSounds (int *numsubsounds) { return FMOD_Sound_GetNumSubSounds(this, numsubsounds); } + FMOD_RESULT getNumTags (int *numtags, int *numtagsupdated) { return FMOD_Sound_GetNumTags(this, numtags, numtagsupdated); } + FMOD_RESULT getTag (const char *name, int index, FMOD_TAG *tag) { return FMOD_Sound_GetTag(this, name, index, tag); } + FMOD_RESULT getOpenState (FMOD_OPENSTATE *openstate, unsigned int *percentbuffered, bool *starving) { FMOD_BOOL b; FMOD_RESULT res = FMOD_Sound_GetOpenState(this, openstate, percentbuffered, &b); *starving = b; return res; } + FMOD_RESULT readData (void *buffer, unsigned int lenbytes, unsigned int *read) { return FMOD_Sound_ReadData(this, buffer, lenbytes, read); } + FMOD_RESULT seekData (unsigned int pcm) { return FMOD_Sound_SeekData(this, pcm); } + + FMOD_RESULT setSoundGroup (SoundGroup *soundgroup) { return FMOD_Sound_SetSoundGroup(this, (FMOD_SOUNDGROUP *)soundgroup); } + FMOD_RESULT getSoundGroup (SoundGroup **soundgroup) { return FMOD_Sound_GetSoundGroup(this, (FMOD_SOUNDGROUP **)soundgroup); } + + // Synchronization point API. These points can come from markers embedded in wav files, and can also generate channel callbacks. + FMOD_RESULT getNumSyncPoints (int *numsyncpoints) { return FMOD_Sound_GetNumSyncPoints(this, numsyncpoints); } + FMOD_RESULT getSyncPoint (int index, FMOD_SYNCPOINT **point) { return FMOD_Sound_GetSyncPoint(this, index, point); } + FMOD_RESULT getSyncPointInfo (FMOD_SYNCPOINT *point, char *name, int namelen, unsigned int *offset, FMOD_TIMEUNIT offsettype) { return FMOD_Sound_GetSyncPointInfo(this, point, name, namelen, offset, offsettype); } + FMOD_RESULT addSyncPoint (unsigned int offset, FMOD_TIMEUNIT offsettype, const char *name, FMOD_SYNCPOINT **point) { return FMOD_Sound_AddSyncPoint(this, offset, offsettype, name, point); } + FMOD_RESULT deleteSyncPoint (FMOD_SYNCPOINT *point) { return FMOD_Sound_DeleteSyncPoint(this, point); } + + // Functions also in Channel class but here they are the 'default' to save having to change it in Channel all the time. + FMOD_RESULT setMode (FMOD_MODE mode) { return FMOD_Sound_SetMode(this, mode); } + FMOD_RESULT getMode (FMOD_MODE *mode) { return FMOD_Sound_GetMode(this, mode); } + FMOD_RESULT setLoopCount (int loopcount) { return FMOD_Sound_SetLoopCount(this, loopcount); } + FMOD_RESULT getLoopCount (int *loopcount) { return FMOD_Sound_GetLoopCount(this, loopcount); } + FMOD_RESULT setLoopPoints (unsigned int loopstart, FMOD_TIMEUNIT loopstarttype, unsigned int loopend, FMOD_TIMEUNIT loopendtype) { return FMOD_Sound_SetLoopPoints(this, loopstart, loopstarttype, loopend, loopendtype); } + FMOD_RESULT getLoopPoints (unsigned int *loopstart, FMOD_TIMEUNIT loopstarttype, unsigned int *loopend, FMOD_TIMEUNIT loopendtype) { return FMOD_Sound_GetLoopPoints(this, loopstart, loopstarttype, loopend, loopendtype); } + + // Userdata set/get. + FMOD_RESULT setUserData (void *userdata) { return FMOD_Sound_SetUserData(this, userdata); } + FMOD_RESULT getUserData (void **userdata) { return FMOD_Sound_GetUserData(this, userdata); } + }; + + /* + 'Channel' API. + */ + class Channel : FMOD_CHANNEL + { + private: + + Channel(); /* Constructor made private so user cannot statically instance a Channel class. + Appropriate Channel creation or retrieval function must be used. */ + public: + + FMOD_RESULT getSystemObject (System **system) { return FMOD_Channel_GetSystemObject(this, (FMOD_SYSTEM **)system); } + + FMOD_RESULT stop () { return FMOD_Channel_Stop(this); } + FMOD_RESULT setPaused (bool paused) { return FMOD_Channel_SetPaused(this, paused); } + FMOD_RESULT getPaused (bool *paused) { FMOD_BOOL b; FMOD_RESULT res = FMOD_Channel_GetPaused(this, &b); *paused = b; return res; } + FMOD_RESULT setVolume (float volume) { return FMOD_Channel_SetVolume(this, volume); } + FMOD_RESULT getVolume (float *volume) { return FMOD_Channel_GetVolume(this, volume); } + FMOD_RESULT setFrequency (float frequency) { return FMOD_Channel_SetFrequency(this, frequency); } + FMOD_RESULT getFrequency (float *frequency) { return FMOD_Channel_GetFrequency(this, frequency); } + FMOD_RESULT setPan (float pan) { return FMOD_Channel_SetPan(this, pan); } + FMOD_RESULT getPan (float *pan) { return FMOD_Channel_GetPan(this, pan); } + FMOD_RESULT setDelay (unsigned int startdelay, unsigned int enddelay) { return FMOD_Channel_SetDelay(this, startdelay, enddelay); } + FMOD_RESULT getDelay (unsigned int *startdelay, unsigned int *enddelay) { return FMOD_Channel_GetDelay(this, startdelay, enddelay); } + FMOD_RESULT setSpeakerMix (float frontleft, float frontright, float center, float lfe, float backleft, float backright, float sideleft, float sideright) { return FMOD_Channel_SetSpeakerMix(this, frontleft, frontright, center, lfe, backleft, backright, sideleft, sideright); } + FMOD_RESULT getSpeakerMix (float *frontleft, float *frontright, float *center, float *lfe, float *backleft, float *backright, float *sideleft, float *sideright) { return FMOD_Channel_GetSpeakerMix(this, frontleft, frontright, center, lfe, backleft, backright, sideleft, sideright); } + FMOD_RESULT setSpeakerLevels (FMOD_SPEAKER speaker, float *levels, int numlevels) { return FMOD_Channel_SetSpeakerLevels(this, speaker, levels, numlevels); } + FMOD_RESULT getSpeakerLevels (FMOD_SPEAKER speaker, float *levels, int numlevels) { return FMOD_Channel_GetSpeakerLevels(this, speaker, levels, numlevels); } + FMOD_RESULT setInputChannelMix (float *levels, int numlevels) { return FMOD_Channel_SetInputChannelMix(this, levels, numlevels); } + FMOD_RESULT getInputChannelMix (float *levels, int numlevels) { return FMOD_Channel_GetInputChannelMix(this, levels, numlevels); } + FMOD_RESULT setMute (bool mute) { return FMOD_Channel_SetMute(this, mute); } + FMOD_RESULT getMute (bool *mute) { FMOD_BOOL b; FMOD_RESULT res = FMOD_Channel_GetMute(this, &b); *mute = b; return res; } + FMOD_RESULT setPriority (int priority) { return FMOD_Channel_SetPriority(this, priority); } + FMOD_RESULT getPriority (int *priority) { return FMOD_Channel_GetPriority(this, priority); } + FMOD_RESULT setPosition (unsigned int position, FMOD_TIMEUNIT postype) { return FMOD_Channel_SetPosition(this, position, postype); } + FMOD_RESULT getPosition (unsigned int *position, FMOD_TIMEUNIT postype) { return FMOD_Channel_GetPosition(this, position, postype); } + FMOD_RESULT setReverbProperties (const FMOD_REVERB_CHANNELPROPERTIES *prop) { return FMOD_Channel_SetReverbProperties(this, prop); } + FMOD_RESULT getReverbProperties (FMOD_REVERB_CHANNELPROPERTIES *prop) { return FMOD_Channel_GetReverbProperties(this, prop); } + + FMOD_RESULT setChannelGroup (ChannelGroup *channelgroup) { return FMOD_Channel_SetChannelGroup(this, (FMOD_CHANNELGROUP *)channelgroup); } + FMOD_RESULT getChannelGroup (ChannelGroup **channelgroup) { return FMOD_Channel_GetChannelGroup(this, (FMOD_CHANNELGROUP **)channelgroup); } + FMOD_RESULT setCallback (FMOD_CHANNEL_CALLBACKTYPE type, FMOD_CHANNEL_CALLBACK callback, int command) { return FMOD_Channel_SetCallback(this, type, callback, command); } + + // 3D functionality. + FMOD_RESULT set3DAttributes (const FMOD_VECTOR *pos, const FMOD_VECTOR *vel) { return FMOD_Channel_Set3DAttributes(this, pos, vel); } + FMOD_RESULT get3DAttributes (FMOD_VECTOR *pos, FMOD_VECTOR *vel) { return FMOD_Channel_Get3DAttributes(this, pos, vel); } + FMOD_RESULT set3DMinMaxDistance (float mindistance, float maxdistance) { return FMOD_Channel_Set3DMinMaxDistance(this, mindistance, maxdistance); } + FMOD_RESULT get3DMinMaxDistance (float *mindistance, float *maxdistance) { return FMOD_Channel_Get3DMinMaxDistance(this, mindistance, maxdistance); } + FMOD_RESULT set3DConeSettings (float insideconeangle, float outsideconeangle, float outsidevolume) { return FMOD_Channel_Set3DConeSettings(this, insideconeangle, outsideconeangle, outsidevolume); } + FMOD_RESULT get3DConeSettings (float *insideconeangle, float *outsideconeangle, float *outsidevolume) { return FMOD_Channel_Get3DConeSettings(this, insideconeangle, outsideconeangle, outsidevolume); } + FMOD_RESULT set3DConeOrientation (FMOD_VECTOR *orientation) { return FMOD_Channel_Set3DConeOrientation(this, orientation); } + FMOD_RESULT get3DConeOrientation (FMOD_VECTOR *orientation) { return FMOD_Channel_Get3DConeOrientation(this, orientation); } + FMOD_RESULT set3DCustomRolloff (FMOD_VECTOR *points, int numpoints) { return FMOD_Channel_Set3DCustomRolloff(this, points, numpoints); } + FMOD_RESULT get3DCustomRolloff (FMOD_VECTOR **points, int *numpoints) { return FMOD_Channel_Get3DCustomRolloff(this, points, numpoints); } + FMOD_RESULT set3DOcclusion (float directocclusion, float reverbocclusion) { return FMOD_Channel_Set3DOcclusion(this, directocclusion, reverbocclusion); } + FMOD_RESULT get3DOcclusion (float *directocclusion, float *reverbocclusion) { return FMOD_Channel_Get3DOcclusion(this, directocclusion, reverbocclusion); } + FMOD_RESULT set3DSpread (float angle) { return FMOD_Channel_Set3DSpread(this, angle); } + FMOD_RESULT get3DSpread (float *angle) { return FMOD_Channel_Get3DSpread(this, angle); } + FMOD_RESULT set3DPanLevel (float level) { return FMOD_Channel_Set3DPanLevel(this, level); } + FMOD_RESULT get3DPanLevel (float *level) { return FMOD_Channel_Get3DPanLevel(this, level); } + FMOD_RESULT set3DDopplerLevel (float level) { return FMOD_Channel_Set3DDopplerLevel(this, level); } + FMOD_RESULT get3DDopplerLevel (float *level) { return FMOD_Channel_Get3DDopplerLevel(this, level); } + + // DSP functionality only for channels playing sounds created with FMOD_SOFTWARE. + FMOD_RESULT getDSPHead (DSP **dsp) { return FMOD_Channel_GetDSPHead(this, (FMOD_DSP **)dsp); } + FMOD_RESULT addDSP (DSP *dsp) { return FMOD_Channel_AddDSP(this, (FMOD_DSP *)dsp); } + + // Information only functions. + FMOD_RESULT isPlaying (bool *isplaying) { FMOD_BOOL b; FMOD_RESULT res = FMOD_Channel_IsPlaying(this, &b); *isplaying = b; return res; } + FMOD_RESULT isVirtual (bool *isvirtual) { FMOD_BOOL b; FMOD_RESULT res = FMOD_Channel_IsVirtual(this, &b); *isvirtual = b; return res; } + FMOD_RESULT getAudibility (float *audibility) { return FMOD_Channel_GetAudibility(this, audibility); } + FMOD_RESULT getCurrentSound (Sound **sound) { return FMOD_Channel_GetCurrentSound(this, (FMOD_SOUND **)sound); } + FMOD_RESULT getSpectrum (float *spectrumarray, int numvalues, int channeloffset, FMOD_DSP_FFT_WINDOW windowtype) { return FMOD_Channel_GetSpectrum(this, spectrumarray, numvalues, channeloffset, windowtype); } + FMOD_RESULT getWaveData (float *wavearray, int numvalues, int channeloffset) { return FMOD_Channel_GetWaveData(this, wavearray, numvalues, channeloffset); } + FMOD_RESULT getIndex (int *index) { return FMOD_Channel_GetIndex(this, index); } + + // Functions also found in Sound class but here they can be set per channel. + FMOD_RESULT setMode (FMOD_MODE mode) { return FMOD_Channel_SetMode(this, mode); } + FMOD_RESULT getMode (FMOD_MODE *mode) { return FMOD_Channel_GetMode(this, mode); } + FMOD_RESULT setLoopCount (int loopcount) { return FMOD_Channel_SetLoopCount(this, loopcount); } + FMOD_RESULT getLoopCount (int *loopcount) { return FMOD_Channel_GetLoopCount(this, loopcount); } + FMOD_RESULT setLoopPoints (unsigned int loopstart, FMOD_TIMEUNIT loopstarttype, unsigned int loopend, FMOD_TIMEUNIT loopendtype) { return FMOD_Channel_SetLoopPoints(this, loopstart, loopstarttype, loopend, loopendtype); } + FMOD_RESULT getLoopPoints (unsigned int *loopstart, FMOD_TIMEUNIT loopstarttype, unsigned int *loopend, FMOD_TIMEUNIT loopendtype) { return FMOD_Channel_GetLoopPoints(this, loopstart, loopstarttype, loopend, loopendtype); } + + // Userdata set/get. + FMOD_RESULT setUserData (void *userdata) { return FMOD_Channel_SetUserData(this, userdata); } + FMOD_RESULT getUserData (void **userdata) { return FMOD_Channel_GetUserData(this, userdata); } + }; + + /* + 'ChannelGroup' API + */ + class ChannelGroup : FMOD_CHANNELGROUP + { + private: + + ChannelGroup(); /* Constructor made private so user cannot statically instance a ChannelGroup class. + Appropriate ChannelGroup creation or retrieval function must be used. */ + public: + + FMOD_RESULT release () { return FMOD_ChannelGroup_Release(this); } + FMOD_RESULT getSystemObject (System **system) { return FMOD_ChannelGroup_GetSystemObject(this, (FMOD_SYSTEM **)system); } + + // Channelgroup scale values. (changes attributes relative to the channels, doesn't overwrite them) + FMOD_RESULT setVolume (float volume) { return FMOD_ChannelGroup_SetVolume(this, volume); } + FMOD_RESULT getVolume (float *volume) { return FMOD_ChannelGroup_GetVolume(this, volume); } + FMOD_RESULT setPitch (float pitch) { return FMOD_ChannelGroup_SetPitch(this, pitch); } + FMOD_RESULT getPitch (float *pitch) { return FMOD_ChannelGroup_GetPitch(this, pitch); } + FMOD_RESULT set3DOcclusion (float directocclusion, float reverbocclusion) { return FMOD_ChannelGroup_Set3DOcclusion(this, directocclusion, reverbocclusion); } + FMOD_RESULT get3DOcclusion (float *directocclusion, float *reverbocclusion) { return FMOD_ChannelGroup_Get3DOcclusion(this, directocclusion, reverbocclusion); } + FMOD_RESULT setPaused (bool paused) { return FMOD_ChannelGroup_SetPaused(this, paused); } + FMOD_RESULT getPaused (bool *paused) { FMOD_BOOL b; FMOD_RESULT res = FMOD_ChannelGroup_GetPaused(this, &b); *paused = b; return res; } + FMOD_RESULT setMute (bool mute) { return FMOD_ChannelGroup_SetMute(this, mute); } + FMOD_RESULT getMute (bool *mute) { FMOD_BOOL b; FMOD_RESULT res = FMOD_ChannelGroup_GetMute(this, &b); *mute = b; return res; } + + // Channelgroup override values. (recursively overwrites whatever settings the channels had) + FMOD_RESULT stop () { return FMOD_ChannelGroup_Stop(this); } + FMOD_RESULT overrideVolume (float volume) { return FMOD_ChannelGroup_OverrideVolume(this, volume); } + FMOD_RESULT overrideFrequency (float frequency) { return FMOD_ChannelGroup_OverrideFrequency(this, frequency); } + FMOD_RESULT overridePan (float pan) { return FMOD_ChannelGroup_OverridePan(this, pan); } + FMOD_RESULT overrideReverbProperties(const FMOD_REVERB_CHANNELPROPERTIES *prop) { return FMOD_ChannelGroup_OverrideReverbProperties(this, prop); } + FMOD_RESULT override3DAttributes (const FMOD_VECTOR *pos, const FMOD_VECTOR *vel) { return FMOD_ChannelGroup_Override3DAttributes(this, pos, vel); } + FMOD_RESULT overrideSpeakerMix (float frontleft, float frontright, float center, float lfe, float backleft, float backright, float sideleft, float sideright) { return FMOD_ChannelGroup_OverrideSpeakerMix(this, frontleft, frontright, center, lfe, backleft, backright, sideleft, sideright); } + + // Nested channel groups. + FMOD_RESULT addGroup (ChannelGroup *group) { return FMOD_ChannelGroup_AddGroup(this, group); } + FMOD_RESULT getNumGroups (int *numgroups) { return FMOD_ChannelGroup_GetNumGroups(this, numgroups); } + FMOD_RESULT getGroup (int index, ChannelGroup **group) { return FMOD_ChannelGroup_GetGroup(this, index, (FMOD_CHANNELGROUP **)group); } + FMOD_RESULT getParentGroup (ChannelGroup **group) { return FMOD_ChannelGroup_GetParentGroup(this, (FMOD_CHANNELGROUP **)group); } + + // DSP functionality only for channel groups playing sounds created with FMOD_SOFTWARE. + FMOD_RESULT getDSPHead (DSP **dsp) { return FMOD_ChannelGroup_GetDSPHead(this, (FMOD_DSP **)dsp); } + FMOD_RESULT addDSP (DSP *dsp) { return FMOD_ChannelGroup_AddDSP(this, (FMOD_DSP *)dsp); } + + // Information only functions. + FMOD_RESULT getName (char *name, int namelen) { return FMOD_ChannelGroup_GetName(this, name, namelen); } + FMOD_RESULT getNumChannels (int *numchannels) { return FMOD_ChannelGroup_GetNumChannels(this, numchannels); } + FMOD_RESULT getChannel (int index, Channel **channel) { return FMOD_ChannelGroup_GetChannel(this, index, (FMOD_CHANNEL **)channel); } + FMOD_RESULT getSpectrum (float *spectrumarray, int numvalues, int channeloffset, FMOD_DSP_FFT_WINDOW windowtype) { return FMOD_ChannelGroup_GetSpectrum(this, spectrumarray, numvalues, channeloffset, windowtype); } + FMOD_RESULT getWaveData (float *wavearray, int numvalues, int channeloffset) { return FMOD_ChannelGroup_GetWaveData(this, wavearray, numvalues, channeloffset) ;} + + // Userdata set/get. + FMOD_RESULT setUserData (void *userdata) { return FMOD_ChannelGroup_SetUserData(this, userdata); } + FMOD_RESULT getUserData (void **userdata) { return FMOD_ChannelGroup_GetUserData(this, userdata); } + }; + + /* + 'SoundGroup' API + */ + class SoundGroup : FMOD_SOUNDGROUP + { + private: + + SoundGroup(); /* Constructor made private so user cannot statically instance a SoundGroup class. + Appropriate SoundGroup creation or retrieval function must be used. */ + public: + + FMOD_RESULT release () { return FMOD_SoundGroup_Release(this); } + FMOD_RESULT getSystemObject (System **system) { return FMOD_SoundGroup_GetSystemObject(this, (FMOD_SYSTEM **)system); } + + // SoundGroup control functions. + FMOD_RESULT setMaxAudible (int maxaudible) { return FMOD_SoundGroup_SetMaxAudible(this, maxaudible); } + FMOD_RESULT getMaxAudible (int *maxaudible) { return FMOD_SoundGroup_GetMaxAudible(this, maxaudible); } + FMOD_RESULT setMaxAudibleBehavior (FMOD_SOUNDGROUP_BEHAVIOR behavior) { return FMOD_SoundGroup_SetMaxAudibleBehavior(this, behavior); } + FMOD_RESULT getMaxAudibleBehavior (FMOD_SOUNDGROUP_BEHAVIOR *behavior) { return FMOD_SoundGroup_GetMaxAudibleBehavior(this, behavior); } + FMOD_RESULT setMuteFadeSpeed (float speed) { return FMOD_SoundGroup_SetMuteFadeSpeed(this, speed); } + FMOD_RESULT getMuteFadeSpeed (float *speed) { return FMOD_SoundGroup_GetMuteFadeSpeed(this, speed); } + FMOD_RESULT setVolume (float volume) { return FMOD_SoundGroup_SetVolume(this, volume); } + FMOD_RESULT getVolume (float *volume) { return FMOD_SoundGroup_GetVolume(this, volume); } + FMOD_RESULT stop () { return FMOD_SoundGroup_Stop(this); } + + // Information only functions. + FMOD_RESULT getName (char *name, int namelen) { return FMOD_SoundGroup_GetName(this, name, namelen); } + FMOD_RESULT getNumSounds (int *numsounds) { return FMOD_SoundGroup_GetNumSounds(this, numsounds); } + FMOD_RESULT getSound (int index, Sound **sound) { return FMOD_SoundGroup_GetSound(this, index, (FMOD_SOUND **)sound); } + FMOD_RESULT getNumPlaying (int *numplaying) { return FMOD_SoundGroup_GetNumPlaying(this, numplaying); } + + // Userdata set/get. + FMOD_RESULT setUserData (void *userdata) { return FMOD_SoundGroup_SetUserData(this, userdata); } + FMOD_RESULT getUserData (void **userdata) { return FMOD_SoundGroup_GetUserData(this, userdata); } + }; + + /* + 'DSP' API + */ + class DSP : FMOD_DSP + { + private: + + DSP(); /* Constructor made private so user cannot statically instance a DSP class. + Appropriate DSP creation or retrieval function must be used. */ + public: + + FMOD_RESULT release () { return FMOD_DSP_Release(this); } + FMOD_RESULT getSystemObject (System **system) { return FMOD_DSP_GetSystemObject(this, (FMOD_SYSTEM **)system); } + + // Connection / disconnection / input and output enumeration. + FMOD_RESULT addInput (DSP *target) { return FMOD_DSP_AddInput(this, target); } + FMOD_RESULT disconnectFrom (DSP *target) { return FMOD_DSP_DisconnectFrom(this, target); } + FMOD_RESULT disconnectAll (bool inputs, bool outputs) { return FMOD_DSP_DisconnectAll(this, inputs, outputs); } + FMOD_RESULT remove () { return FMOD_DSP_Remove(this); } + FMOD_RESULT getNumInputs (int *numinputs) { return FMOD_DSP_GetNumInputs(this, numinputs); } + FMOD_RESULT getNumOutputs (int *numoutputs) { return FMOD_DSP_GetNumOutputs(this, numoutputs); } + FMOD_RESULT getInput (int index, DSP **input) { return FMOD_DSP_GetInput(this, index, (FMOD_DSP **)input); } + FMOD_RESULT getOutput (int index, DSP **output) { return FMOD_DSP_GetOutput(this, index, (FMOD_DSP **)output); } + FMOD_RESULT setInputMix (int index, float volume) { return FMOD_DSP_SetInputMix(this, index, volume); } + FMOD_RESULT getInputMix (int index, float *volume) { return FMOD_DSP_GetInputMix(this, index, volume); } + FMOD_RESULT setInputLevels (int index, FMOD_SPEAKER speaker, float *levels, int numlevels) { return FMOD_DSP_SetInputLevels(this, index, speaker, levels, numlevels); } + FMOD_RESULT getInputLevels (int index, FMOD_SPEAKER speaker, float *levels, int numlevels) { return FMOD_DSP_GetInputLevels(this, index, speaker, levels, numlevels); } + FMOD_RESULT setOutputMix (int index, float volume) { return FMOD_DSP_SetOutputMix(this, index, volume); } + FMOD_RESULT getOutputMix (int index, float *volume) { return FMOD_DSP_GetOutputMix(this, index, volume); } + FMOD_RESULT setOutputLevels (int index, FMOD_SPEAKER speaker, float *levels, int numlevels) { return FMOD_DSP_SetOutputLevels(this, index, speaker, levels, numlevels); } + FMOD_RESULT getOutputLevels (int index, FMOD_SPEAKER speaker, float *levels, int numlevels) { return FMOD_DSP_GetOutputLevels(this, index, speaker, levels, numlevels); } + + // DSP unit control. + FMOD_RESULT setActive (bool active) { return FMOD_DSP_SetActive(this, active); } + FMOD_RESULT getActive (bool *active) { FMOD_BOOL b; FMOD_RESULT res = FMOD_DSP_GetActive(this, &b); *active = b; return res; } + FMOD_RESULT setBypass (bool bypass) { return FMOD_DSP_SetBypass(this, bypass); } + FMOD_RESULT getBypass (bool *bypass) { FMOD_BOOL b; FMOD_RESULT res = FMOD_DSP_GetBypass(this, &b); *bypass = b; return res; } + FMOD_RESULT reset () { return FMOD_DSP_Reset(this); } + + // DSP parameter control. + FMOD_RESULT setParameter (int index, float value) { return FMOD_DSP_SetParameter(this, index, value); } + FMOD_RESULT getParameter (int index, float *value, char *valuestr, int valuestrlen) { return FMOD_DSP_GetParameter(this, index, value, valuestr, valuestrlen); } + FMOD_RESULT getNumParameters (int *numparams) { return FMOD_DSP_GetNumParameters(this, numparams); } + FMOD_RESULT getParameterInfo (int index, char *name, char *label, char *description, int descriptionlen, float *min, float *max) { return FMOD_DSP_GetParameterInfo(this, index, name, label, description, descriptionlen, min, max); } + FMOD_RESULT showConfigDialog (void *hwnd, bool show) { return FMOD_DSP_ShowConfigDialog(this, hwnd, show); } + + // DSP attributes. + FMOD_RESULT getInfo (char *name, unsigned int *version, int *channels, int *configwidth, int *configheight) { return FMOD_DSP_GetInfo(this, name, version, channels, configwidth, configheight); } + FMOD_RESULT getType (FMOD_DSP_TYPE *type) { return FMOD_DSP_GetType(this, type); } + FMOD_RESULT setDefaults (float frequency, float volume, float pan, int priority) { return FMOD_DSP_SetDefaults(this, frequency, volume, pan, priority); } + FMOD_RESULT getDefaults (float *frequency, float *volume, float *pan, int *priority) { return FMOD_DSP_GetDefaults(this, frequency, volume, pan, priority) ;} + + // Userdata set/get. + FMOD_RESULT setUserData (void *userdata) { return FMOD_DSP_SetUserData(this, userdata); } + FMOD_RESULT getUserData (void **userdata) { return FMOD_DSP_GetUserData(this, userdata); } + }; + + + /* + 'Geometry' API + */ + class Geometry : FMOD_GEOMETRY + { + private: + + Geometry(); /* Constructor made private so user cannot statically instance a Geometry class. + Appropriate Geometry creation or retrieval function must be used. */ + + public: + + FMOD_RESULT release () { return FMOD_Geometry_Release(this); } + + // Polygon manipulation. + FMOD_RESULT addPolygon (float directocclusion, float reverbocclusion, bool doublesided, int numvertices, const FMOD_VECTOR *vertices, int *polygonindex) { return FMOD_Geometry_AddPolygon(this, directocclusion, reverbocclusion, doublesided, numvertices, vertices, polygonindex); } + FMOD_RESULT getNumPolygons (int *numpolygons) { return FMOD_Geometry_GetNumPolygons(this, numpolygons); } + FMOD_RESULT getMaxPolygons (int *maxpolygons, int *maxvertices) { return FMOD_Geometry_GetMaxPolygons(this, maxpolygons, maxvertices); } + FMOD_RESULT getPolygonNumVertices (int index, int *numvertices) { return FMOD_Geometry_GetPolygonNumVertices(this, index, numvertices); } + FMOD_RESULT setPolygonVertex (int index, int vertexindex, const FMOD_VECTOR *vertex) { return FMOD_Geometry_SetPolygonVertex(this, index, vertexindex, vertex); } + FMOD_RESULT getPolygonVertex (int index, int vertexindex, FMOD_VECTOR *vertex) { return FMOD_Geometry_GetPolygonVertex(this, index, vertexindex, vertex); } + FMOD_RESULT setPolygonAttributes (int index, float directocclusion, float reverbocclusion, bool doublesided) { return FMOD_Geometry_SetPolygonAttributes(this, index, directocclusion, reverbocclusion, doublesided); } + FMOD_RESULT getPolygonAttributes (int index, float *directocclusion, float *reverbocclusion, bool *doublesided) { FMOD_BOOL b; FMOD_RESULT res = FMOD_Geometry_GetPolygonAttributes(this, index, directocclusion, reverbocclusion, &b); *doublesided = b; return res; } + + // Object manipulation. + FMOD_RESULT setActive (bool active) { return FMOD_Geometry_SetActive(this, active); } + FMOD_RESULT getActive (bool *active) { FMOD_BOOL b; FMOD_RESULT res = FMOD_Geometry_GetActive(this, &b); *active = b; return res; } + FMOD_RESULT setRotation (const FMOD_VECTOR *forward, const FMOD_VECTOR *up) { return FMOD_Geometry_SetRotation(this, forward, up); } + FMOD_RESULT getRotation (FMOD_VECTOR *forward, FMOD_VECTOR *up) { return FMOD_Geometry_GetRotation(this, forward, up); } + FMOD_RESULT setPosition (const FMOD_VECTOR *position) { return FMOD_Geometry_SetPosition(this, position); } + FMOD_RESULT getPosition (FMOD_VECTOR *position) { return FMOD_Geometry_GetPosition(this, position); } + FMOD_RESULT setScale (const FMOD_VECTOR *scale) { return FMOD_Geometry_SetScale(this, scale); } + FMOD_RESULT getScale (FMOD_VECTOR *scale) { return FMOD_Geometry_GetScale(this, scale); } + FMOD_RESULT save (void *data, int *datasize) { return FMOD_Geometry_Save(this, data, datasize); } + + // Userdata set/get. + FMOD_RESULT setUserData (void *userdata) { return FMOD_Geometry_SetUserData(this, userdata); } + FMOD_RESULT getUserData (void **userdata) { return FMOD_Geometry_GetUserData(this, userdata); } + }; + + + /* + 'Reverb' API + */ + class Reverb : FMOD_REVERB + { + private: + + Reverb(); /* Constructor made private so user cannot statically instance a Reverb class. + Appropriate Reverb creation or retrieval function must be used. */ + + public: + + FMOD_RESULT release () { return FMOD_Reverb_Release(this); } + + // Reverb manipulation. + FMOD_RESULT set3DAttributes (const FMOD_VECTOR *position, float mindistance, float maxdistance) { return FMOD_Reverb_Set3DAttributes(this, position, mindistance, maxdistance); } + FMOD_RESULT get3DAttributes (FMOD_VECTOR *position, float *mindistance, float *maxdistance) { return FMOD_Reverb_Get3DAttributes(this, position, mindistance, maxdistance); } + FMOD_RESULT setProperties (const FMOD_REVERB_PROPERTIES *properties) { return FMOD_Reverb_SetProperties(this, properties); } + FMOD_RESULT getProperties (FMOD_REVERB_PROPERTIES *properties) { return FMOD_Reverb_GetProperties(this, properties); } + FMOD_RESULT setActive (bool active) { return FMOD_Reverb_SetActive(this, active); } + FMOD_RESULT getActive (bool *active) { FMOD_BOOL b; FMOD_RESULT res = FMOD_Reverb_GetActive(this, &b); *active = b; return res; } + + // Userdata set/get. + FMOD_RESULT setUserData (void *userdata) { return FMOD_Reverb_SetUserData(this, userdata); } + FMOD_RESULT getUserData (void **userdata) { return FMOD_Reverb_GetUserData(this, userdata); } + }; +} + +#endif +#endif diff --git a/src/sound/fmodsound.cpp b/src/sound/fmodsound.cpp index fe526fdc4..01b4efda8 100644 --- a/src/sound/fmodsound.cpp +++ b/src/sound/fmodsound.cpp @@ -32,6 +32,8 @@ ** */ +// HEADER FILES ------------------------------------------------------------ + #ifdef _WIN32 #define WIN32_LEAN_AND_MEAN #include @@ -52,299 +54,424 @@ extern HWND Window; #include "actor.h" #include "r_state.h" #include "w_wad.h" -#include "sample_flac.h" #include "i_music.h" #include "i_musicinterns.h" +#include "v_text.h" + +// MACROS ------------------------------------------------------------------ // killough 2/21/98: optionally use varying pitched sounds -#define PITCH(f,x) (snd_pitched ? ((f)*(x))/128 : (f)) +#define PITCH(freq,pitch) (snd_pitched ? ((freq)*(pitch))/128.f : float(freq)) + +// Just some extra for music and whatever +#define NUM_EXTRA_SOFTWARE_CHANNELS 8 + +#define ERRCHECK(x) + + +// TYPES ------------------------------------------------------------------- + +struct FEnumList +{ + const char *Name; + int Value; +}; + +// EXTERNAL FUNCTION PROTOTYPES -------------------------------------------- + +// PUBLIC FUNCTION PROTOTYPES ---------------------------------------------- + +// PRIVATE FUNCTION PROTOTYPES --------------------------------------------- + +static int Enum_NumForName(const FEnumList *list, const char *name); +static const char *Enum_NameForNum(const FEnumList *list, int num); + +// EXTERNAL DATA DECLARATIONS ---------------------------------------------- extern int MAX_SND_DIST; -const int S_CLIPPING_DIST = 1200; -const int S_CLOSE_DIST = 160; - -CVAR (Int, snd_driver, 0, CVAR_ARCHIVE|CVAR_GLOBALCONFIG) -CVAR (Bool, snd_3d, false, CVAR_ARCHIVE|CVAR_GLOBALCONFIG) -CVAR (Bool, snd_waterreverb, true, CVAR_ARCHIVE|CVAR_GLOBALCONFIG) -CVAR (Bool, snd_fpumixer, false, CVAR_ARCHIVE|CVAR_GLOBALCONFIG) EXTERN_CVAR (String, snd_output) EXTERN_CVAR (Float, snd_musicvolume) EXTERN_CVAR (Int, snd_buffersize) EXTERN_CVAR (Int, snd_samplerate) EXTERN_CVAR (Bool, snd_pitched) +EXTERN_CVAR (Int, snd_channels) + +// PUBLIC DATA DEFINITIONS ------------------------------------------------- -static const ReverbContainer *PrevEnvironment; ReverbContainer *ForcedEnvironment; -static const char *OutputNames[] = +CVAR (Int, snd_driver, 0, CVAR_ARCHIVE|CVAR_GLOBALCONFIG) +CVAR (Bool, snd_3d, true, CVAR_ARCHIVE|CVAR_GLOBALCONFIG) +CVAR (Bool, snd_hw3d, false, CVAR_ARCHIVE|CVAR_GLOBALCONFIG) +CVAR (Bool, snd_waterreverb, true, CVAR_ARCHIVE|CVAR_GLOBALCONFIG) +CVAR (String, snd_resampler, "Linear", CVAR_ARCHIVE|CVAR_GLOBALCONFIG) +CVAR (String, snd_speakermode, "Auto", CVAR_ARCHIVE|CVAR_GLOBALCONFIG) +CVAR (String, snd_output_format, "PCM-16", CVAR_ARCHIVE|CVAR_GLOBALCONFIG) +CVAR (Bool, snd_dspnet, false, 0) + +// PRIVATE DATA DEFINITIONS ------------------------------------------------ + +static const int S_CLIPPING_DIST = 1200; +static const int S_CLOSE_DIST = 160; +static const ReverbContainer *PrevEnvironment; + +// In the below lists, duplicate entries are for user selection. When +// queried, only the first one for the particular value is shown. +static const FEnumList OutputNames[] = { - "No sound", - "Windows Multimedia", - "DirectSound", - "A3D", - "OSS (Open Sound System)", - "ESD (Enlightenment Sound Daemon)", - "ALSA (Advanced Linux Sound Architecture)" -}; -static const char *MixerNames[] = -{ - "Auto", - "Non-MMX blendmode", - "Pentium MMX", - "PPro MMX", - "Quality auto", - "Quality FPU", - "Quality Pentium MMX", - "Quality PPro MMX" + { "Auto", FMOD_OUTPUTTYPE_AUTODETECT }, + { "Default", FMOD_OUTPUTTYPE_AUTODETECT }, + { "No sound", FMOD_OUTPUTTYPE_NOSOUND }, + + // Windows + { "DirectSound", FMOD_OUTPUTTYPE_DSOUND }, + { "DSound", FMOD_OUTPUTTYPE_DSOUND }, + { "Windows Multimedia", FMOD_OUTPUTTYPE_WINMM }, + { "WinMM", FMOD_OUTPUTTYPE_WINMM }, + { "WaveOut", FMOD_OUTPUTTYPE_WINMM }, + { "OpenAL", FMOD_OUTPUTTYPE_OPENAL }, + { "WASAPI", FMOD_OUTPUTTYPE_WASAPI }, + { "ASIO", FMOD_OUTPUTTYPE_ASIO }, + + // Linux + { "OSS", FMOD_OUTPUTTYPE_OSS }, + { "ALSA", FMOD_OUTPUTTYPE_ALSA }, + { "ESD", FMOD_OUTPUTTYPE_ESD }, + + // Mac + { "Sound Manager", FMOD_OUTPUTTYPE_SOUNDMANAGER }, + { "Core Audio", FMOD_OUTPUTTYPE_COREAUDIO }, + + { NULL, 0 } }; -static char FModLog (char success) +static const FEnumList SpeakerModeNames[] = { - if (success) + { "Mono", FMOD_SPEAKERMODE_MONO }, + { "Stereo", FMOD_SPEAKERMODE_STEREO }, + { "Quad", FMOD_SPEAKERMODE_QUAD }, + { "Surround", FMOD_SPEAKERMODE_SURROUND }, + { "5.1", FMOD_SPEAKERMODE_5POINT1 }, + { "7.1", FMOD_SPEAKERMODE_7POINT1 }, + { "Prologic", FMOD_SPEAKERMODE_PROLOGIC }, + { "1", FMOD_SPEAKERMODE_MONO }, + { "2", FMOD_SPEAKERMODE_STEREO }, + { "4", FMOD_SPEAKERMODE_QUAD }, + { NULL, 0 } +}; + +static const FEnumList ResamplerNames[] = +{ + { "No Interpolation", FMOD_DSP_RESAMPLER_NOINTERP }, + { "NoInterp", FMOD_DSP_RESAMPLER_NOINTERP }, + { "Linear", FMOD_DSP_RESAMPLER_LINEAR }, + { "Cubic", FMOD_DSP_RESAMPLER_CUBIC }, + { "Spline", FMOD_DSP_RESAMPLER_SPLINE }, + { NULL, 0 } +}; + +static const FEnumList SoundFormatNames[] = +{ + { "None", FMOD_SOUND_FORMAT_NONE }, + { "PCM-8", FMOD_SOUND_FORMAT_PCM8 }, + { "PCM-16", FMOD_SOUND_FORMAT_PCM16 }, + { "PCM-24", FMOD_SOUND_FORMAT_PCM24 }, + { "PCM-32", FMOD_SOUND_FORMAT_PCM32 }, + { "PCM-Float", FMOD_SOUND_FORMAT_PCMFLOAT }, + { "GCADPCM", FMOD_SOUND_FORMAT_GCADPCM }, + { "IMAADPCM", FMOD_SOUND_FORMAT_IMAADPCM }, + { "VAG", FMOD_SOUND_FORMAT_VAG }, + { "XMA", FMOD_SOUND_FORMAT_XMA }, + { "MPEG", FMOD_SOUND_FORMAT_MPEG }, + { NULL, 0 } +}; + +// CODE -------------------------------------------------------------------- + +//========================================================================== +// +// Enum_NumForName +// +// Returns the value of an enum name, or -1 if not found. +// +//========================================================================== + +static int Enum_NumForName(const FEnumList *list, const char *name) +{ + while (list->Name != NULL) { - Printf (" succeeded\n"); + if (stricmp(list->Name, name) == 0) + { + return list->Value; + } + list++; } - else - { - Printf (" failed (error %d)\n", FSOUND_GetError()); - } - return success; + return -1; } -//=========================================================================== +//========================================================================== +// +// Enum_NameForNum +// +// Returns the name of an enum value. If there is more than one name for a +// value, on the first one in the list is returned. Returns NULL if there +// was no match. +// +//========================================================================== + +static const char *Enum_NameForNum(const FEnumList *list, int num) +{ + while (list->Name != NULL) + { + if (list->Value == num) + { + return list->Name; + } + list++; + } + return NULL; +} + +//========================================================================== // // The container for a FSOUND_STREAM. // -//=========================================================================== +//========================================================================== class FMODStreamCapsule : public SoundStream { public: - FMODStreamCapsule (FSOUND_STREAM *stream) - : Stream(stream), UserData(NULL), Callback(NULL), Channel(-1) {} + FMODStreamCapsule(FMOD::Sound *stream, FMODSoundRenderer *owner) + : Owner(owner), Stream(stream), Channel(NULL), DSP(NULL), + UserData(NULL), Callback(NULL) + {} - FMODStreamCapsule (void *udata, SoundStreamCallback callback) - : Stream(NULL), UserData(udata), Callback(callback), Channel(-1) {} + FMODStreamCapsule(void *udata, SoundStreamCallback callback, FMODSoundRenderer *owner) + : Owner(owner), Stream(NULL), Channel(NULL), DSP(NULL), + UserData(udata), Callback(callback) + {} - ~FMODStreamCapsule () + ~FMODStreamCapsule() { - if (Stream != NULL) FSOUND_Stream_Close (Stream); + if (Stream != NULL) + { + Stream->release(); + } } - void SetStream (FSOUND_STREAM *stream) + void SetStream(FMOD::Sound *stream) { Stream = stream; } - bool Play (bool looping, float volume) + bool Play(bool looping, float volume, bool normalize) { - FSOUND_Stream_SetMode(Stream, looping? FSOUND_LOOP_NORMAL : FSOUND_LOOP_OFF); - Channel = FSOUND_Stream_PlayEx (FSOUND_FREE, Stream, NULL, true); - if (Channel != -1) + FMOD_RESULT result; + + Stream->setMode(looping ? FMOD_LOOP_NORMAL : FMOD_LOOP_OFF); + result = Owner->Sys->playSound(FMOD_CHANNEL_FREE, Stream, true, &Channel); + if (result != FMOD_OK) { - FSOUND_SetVolumeAbsolute (Channel, clamp((int)(volume * relative_volume * 255), 0, 255)); - FSOUND_SetPan (Channel, FSOUND_STEREOPAN); - FSOUND_SetPaused (Channel, false); - return true; + return false; + } + Channel->setChannelGroup(Owner->MusicGroup); + Channel->setVolume(volume); + Channel->setPaused(false); + + if (normalize) + { // Attach a normalizer DSP unit to the channel. + result = Owner->Sys->createDSPByType(FMOD_DSP_TYPE_NORMALIZE, &DSP); + if (result == FMOD_OK) + { + Channel->addDSP(DSP); + } + } + return true; + } + + void Stop() + { + if (Channel != NULL) + { + Channel->stop(); + Channel = NULL; + } + if (DSP != NULL) + { + DSP->release(); + DSP = NULL; + } + } + + bool SetPaused(bool paused) + { + if (Channel != NULL) + { + return FMOD_OK == Channel->setPaused(paused); } return false; } - void Stop () + unsigned int GetPosition() { - FSOUND_Stream_Stop (Stream); - } + unsigned int pos; - bool SetPaused (bool paused) - { - if (Channel != -1) + if (Channel != NULL && FMOD_OK == Channel->getPosition(&pos, FMOD_TIMEUNIT_MS)) { - return !!FSOUND_SetPaused (Channel, paused); + return pos; } - return false; + return 0; } - unsigned int GetPosition () + void SetVolume(float volume) { - return FSOUND_Stream_GetPosition (Stream); - } - - void SetVolume (float volume) - { - if (Channel != -1) + if (Channel != NULL) { - FSOUND_SetVolumeAbsolute (Channel, clamp((int)(volume * relative_volume * 255), 0, 255)); + Channel->setVolume(volume); } } - static signed char F_CALLBACKAPI MyCallback (FSOUND_STREAM *stream, void *buff, int len, void *userdata) + // Sets the current order number for a MOD-type song, or the position in ms + // for anything else. + bool SetPosition(int pos) { - FMODStreamCapsule *me = (FMODStreamCapsule *)userdata; - if (me->Callback == NULL) + FMOD_SOUND_TYPE type; + + if (FMOD_OK == Stream->getFormat(&type, NULL, NULL, NULL) && + (type == FMOD_SOUND_TYPE_IT || + type == FMOD_SOUND_TYPE_MOD || + type == FMOD_SOUND_TYPE_S3M || + type == FMOD_SOUND_TYPE_XM)) { - return FALSE; + return FMOD_OK == Channel->setPosition(pos, FMOD_TIMEUNIT_MODORDER); } - return me->Callback (me, buff, len, me->UserData); + return FMOD_OK == Channel->setPosition(pos, FMOD_TIMEUNIT_MS); + } + + static FMOD_RESULT F_CALLBACK PCMReadCallback(FMOD_SOUND *sound, void *data, unsigned int datalen) + { + FMOD_RESULT result; + FMODStreamCapsule *self; + + result = ((FMOD::Sound *)sound)->getUserData((void **)&self); + if (result != FMOD_OK || self == NULL || self->Callback == NULL) + { + return FMOD_ERR_INVALID_PARAM; + } + if (self->Callback(self, data, datalen, self->UserData)) + { + return FMOD_OK; + } + else + { + return FMOD_ERR_FILE_EOF; + } + } + + static FMOD_RESULT F_CALLBACK PCMSetPosCallback(FMOD_SOUND *sound, int subsound, unsigned int position, FMOD_TIMEUNIT postype) + { + // This is useful if the user calls Channel::setPosition and you want + // to seek your data accordingly. + return FMOD_OK; } private: - FSOUND_STREAM *Stream; + FMODSoundRenderer *Owner; + FMOD::Sound *Stream; + FMOD::Channel *Channel; + FMOD::DSP *DSP; void *UserData; SoundStreamCallback Callback; - int Channel; }; -//=========================================================================== -// -// The container for a FMUSIC_MODULE. -// -//=========================================================================== - -class FMUSICCapsule : public SoundTrackerModule -{ -public: - FMUSICCapsule (FMUSIC_MODULE *mod) : Module (mod) {} - ~FMUSICCapsule () { FMUSIC_FreeSong (Module); } - - bool Play () - { - return !!FMUSIC_PlaySong (Module); - } - - void Stop () - { - FMUSIC_StopSong (Module); - } - - void SetVolume (float volume) - { - FMUSIC_SetMasterVolume (Module, clamp((int)(volume * relative_volume * 256), 0, 256)); - } - - bool SetPaused (bool paused) - { - return !!FMUSIC_SetPaused (Module, paused); - } - - bool IsPlaying () - { - return !!FMUSIC_IsPlaying (Module); - } - - bool IsFinished () - { - return !!FMUSIC_IsFinished (Module); - } - - bool SetOrder (int order) - { - return !!FMUSIC_SetOrder (Module, order); - } - -private: - FMUSIC_MODULE *Module; -}; - -//=========================================================================== +//========================================================================== // // The interface the game uses to talk to FMOD. // -//=========================================================================== +//========================================================================== -FMODSoundRenderer::FMODSoundRenderer () +FMODSoundRenderer::FMODSoundRenderer() { - DidInit = Init (); + Init(); } -FMODSoundRenderer::~FMODSoundRenderer () +FMODSoundRenderer::~FMODSoundRenderer() { - Shutdown (); + Shutdown(); } -bool FMODSoundRenderer::IsValid () +bool FMODSoundRenderer::IsValid() { - return DidInit; + return Sys != NULL; } -bool FMODSoundRenderer::Init () +bool FMODSoundRenderer::Init() { -#ifdef _WIN32 - static const FSOUND_OUTPUTTYPES outtypes[2] = - { FSOUND_OUTPUT_DSOUND, FSOUND_OUTPUT_WINMM }; - const int maxtrynum = 2; -#else - static const FSOUND_OUTPUTTYPES outtypes[3] = - { FSOUND_OUTPUT_OSS, FSOUND_OUTPUT_ALSA, FSOUND_OUTPUT_ESD }; - const int maxtrynum = 3; -#endif -#if 0 - bool trya3d = false; -#endif + FMOD_RESULT result; + unsigned int version; + FMOD_SPEAKERMODE speakermode; + FMOD_SOUND_FORMAT format; + FMOD_DSP_RESAMPLER resampler; + FMOD_INITFLAGS initflags; - int outindex; - int trynum; - bool nosound = false; + int eval; ChannelMap = NULL; NumChannels = 0; + SFXPaused = false; + MusicGroup = NULL; + SfxGroup = NULL; + PausableSfx = NULL; PrevEnvironment = DefaultEnvironments[0]; -#ifdef _WIN32 - if (stricmp (snd_output, "dsound") == 0 || stricmp (snd_output, "directsound") == 0) - { - outindex = 0; - } - else if (stricmp (snd_output, "winmm") == 0 || stricmp (snd_output, "waveout") == 0) - { - outindex = 1; - } - else - { - // If snd_3d is true, try for a3d output if snd_output was not recognized above. - // However, if running under NT 4.0, a3d will only be tried if specifically requested. - outindex = (OSPlatform == os_WinNT4) ? 1 : 0; -#if 0 - // FMOD 3.6 no longer supports a3d. Keep this code here in case support comes back. - if (stricmp (snd_output, "a3d") == 0 || (outindex == 0 && snd_3d)) - { - trya3d = true; - } -#endif - } -#else - if (stricmp (snd_output, "alsa") == 0) - { - outindex = 1; - } - else if (stricmp (snd_output, "esd") == 0 || - stricmp (snd_output, "esound") == 0) - { - outindex = 2; - } - else - { - outindex = 0; - } -#endif - Printf ("I_InitSound: Initializing FMOD\n"); -#ifdef _WIN32 - FSOUND_SetHWND (Window); -#endif - if (snd_fpumixer) + // Create a System object and initialize. + result = FMOD::System_Create(&Sys); + ERRCHECK(result); + + result = Sys->getVersion(&version); + ERRCHECK(result); + + if (version < FMOD_VERSION) { - FSOUND_SetMixer (FSOUND_MIXER_QUALITY_FPU); + Printf ("Error! You are using an old version of FMOD %08x.\n" + "This program requires %08x\n", version, FMOD_VERSION); + return false; } - else + + result = Sys->getDriverCaps(0, &Driver_Caps, &Driver_MinFrequency, &Driver_MaxFrequency, &speakermode); + ERRCHECK(result); + + // Set the user selected speaker mode. + eval = Enum_NumForName(SpeakerModeNames, snd_speakermode); + if (eval >= 0) { - FSOUND_SetMixer (FSOUND_MIXER_AUTODETECT); + speakermode = FMOD_SPEAKERMODE(eval); } + result = Sys->setSpeakerMode(speakermode); + ERRCHECK(result); + + // Set software format + eval = Enum_NumForName(SoundFormatNames, snd_output_format); + format = eval >= 0 ? FMOD_SOUND_FORMAT(eval) : FMOD_SOUND_FORMAT_PCM16; + eval = Enum_NumForName(ResamplerNames, snd_resampler); + resampler = eval >= 0 ? FMOD_DSP_RESAMPLER(eval) : FMOD_DSP_RESAMPLER_LINEAR; + result = Sys->setSoftwareFormat(snd_samplerate, format, 0, 0, resampler); + ERRCHECK(result); + + // Set software channels according to snd_channels + result = Sys->setSoftwareChannels(snd_channels + NUM_EXTRA_SOFTWARE_CHANNELS); + ERRCHECK(result); #ifdef _WIN32 if (OSPlatform == os_WinNT4) { + // The following was true as of FMOD 3. I don't know if it still + // applies to FMOD Ex, nor do I have an NT 4 install anymore, but + // there's no reason to get rid of it yet. + // // If running Windows NT 4, we need to initialize DirectSound before // using WinMM. If we don't, then FSOUND_Close will corrupt a // heap. This might just be the Audigy's drivers--I don't know why @@ -357,130 +484,124 @@ bool FMODSoundRenderer::Init () // latency as high as 120 ms to avoid crackling--quite the opposite // from the other Windows versions with real DirectSound support. - static bool initedDSound = false; + static bool inited_dsound = false; - if (!initedDSound) + if (!inited_dsound) { - FSOUND_SetOutput (FSOUND_OUTPUT_DSOUND); - if (FSOUND_GetOutput () == FSOUND_OUTPUT_DSOUND) + if (Sys->setOutput(FMOD_OUTPUTTYPE_DSOUND) == FMOD_OK) { - if (FSOUND_Init (snd_samplerate, 64, 0)) + if (Sys->init(1, FMOD_INIT_NORMAL, 0) == FMOD_OK) { - Sleep (50); - FSOUND_Close (); - initedDSound = true; + inited_dsound = true; + Sleep(50); + Sys->close(); } + Sys->setOutput(FMOD_OUTPUTTYPE_WINMM); } } } #endif - while (!nosound) + // Set the user specified output mode. + eval = Enum_NumForName(OutputNames, snd_output); + if (eval >= 0) { - trynum = 0; - while (trynum < maxtrynum) - { - long outtype = /*trya3d ? FSOUND_OUTPUT_A3D :*/ - outtypes[(outindex+trynum) % maxtrynum]; - - Printf (" Setting %s output", OutputNames[outtype]); - FModLog (FSOUND_SetOutput (outtype)); - if (FSOUND_GetOutput() != outtype) - { - Printf (" Got %s output instead.\n", OutputNames[FSOUND_GetOutput()]); -#if 0 - if (trya3d) - { - trya3d = false; - } - else -#endif - { - ++trynum; - } - continue; - } - Printf (" Setting driver %d", *snd_driver); - FModLog (FSOUND_SetDriver (snd_driver)); - if (FSOUND_GetOutput() != outtype) - { - Printf (" Output changed to %s\n Trying driver 0", - OutputNames[FSOUND_GetOutput()]); - FSOUND_SetOutput (outtype); - FModLog (FSOUND_SetDriver (0)); - } - if (snd_buffersize) - { - Printf (" Setting buffer size %d", *snd_buffersize); - FModLog (FSOUND_SetBufferSize (snd_buffersize)); - } - FSOUND_GetDriverCaps (FSOUND_GetDriver(), &DriverCaps); - Printf (" Initialization"); - if (!FModLog (FSOUND_Init (snd_samplerate, 64, FSOUND_INIT_DSOUND_DEFERRED))) - { -#if 0 - if (trya3d) - { - trya3d = false; - } - else -#endif - { - trynum++; - } - } - else - { - break; - } - } - if (trynum < 2) - { // Initialized successfully - break; - } + result = Sys->setOutput(FMOD_OUTPUTTYPE(eval)); + ERRCHECK(result); } - if (!nosound) + if (Driver_Caps & FMOD_CAPS_HARDWARE_EMULATED) + { // The user has the 'Acceleration' slider set to off! + // This is really bad for latency! + Printf ("Warning: The sound acceleration slider has been set to off.\n"); + Printf ("Please turn it back on if you want decent sound.\n"); + result = Sys->setDSPBufferSize(1024, 10); // At 48khz, the latency between issuing an fmod command and hearing it will now be about 213ms. + ERRCHECK(result); + } + + // Try to init + initflags = FMOD_INIT_NORMAL | FMOD_INIT_SOFTWARE_HRTF; + if (snd_dspnet) { - OutputType = FSOUND_GetOutput (); - if (snd_3d) + initflags |= FMOD_INIT_ENABLE_DSPNET; + } + result = Sys->init(100, initflags, 0); + if (result == FMOD_ERR_OUTPUT_CREATEBUFFER) + { // The speaker mode selected isn't supported by this soundcard. Switch it back to stereo. + result = Sys->setSpeakerMode(FMOD_SPEAKERMODE_STEREO); + ERRCHECK(result); + + result = Sys->init(100, FMOD_INIT_NORMAL, 0); + ERRCHECK(result); + } + if (result != FMOD_OK) + { // Initializing FMOD failed. Cry cry. + return false; + } + + // Create channel groups + result = Sys->createChannelGroup("Music", &MusicGroup); + ERRCHECK(result); + + result = Sys->createChannelGroup("SFX", &SfxGroup); + ERRCHECK(result); + + result = Sys->createChannelGroup("Pausable SFX", &PausableSfx); + ERRCHECK(result); + + result = SfxGroup->addGroup(PausableSfx); + ERRCHECK(result); + + if (snd_3d) + { + float rolloff_factor; + + Sound3D = true; + if (gameinfo.gametype == GAME_Doom || gameinfo.gametype == GAME_Strife) + { + rolloff_factor = 1.7f; + } + else if (gameinfo.gametype == GAME_Heretic) { - Sound3D = true; - if (gameinfo.gametype == GAME_Doom || gameinfo.gametype == GAME_Strife) - { - FSOUND_3D_SetRolloffFactor (1.7f); - } - else if (gameinfo.gametype == GAME_Heretic) - { - FSOUND_3D_SetRolloffFactor (1.24f); - } - else - { - FSOUND_3D_SetRolloffFactor (0.96f); - } - FSOUND_3D_SetDopplerFactor (1.f); - FSOUND_3D_SetDistanceFactor (100.f); // Distance factor only effects doppler! - if (!(DriverCaps & FSOUND_CAPS_HARDWARE)) - { - Printf ("Using software 3D sound\n"); - } + rolloff_factor = 1.24f; } else { - Sound3D = false; + rolloff_factor = 0.96f; } - snd_sfxvolume.Callback (); + Sys->set3DSettings(1.f, 100.f, rolloff_factor); + Hardware3D = snd_hw3d; } - return !nosound; + else + { + Sound3D = false; + Hardware3D = false; + } + snd_sfxvolume.Callback (); + return true; } -void FMODSoundRenderer::Shutdown () +void FMODSoundRenderer::Shutdown() { - if (DidInit) + if (Sys != NULL) { unsigned int i; - FSOUND_StopSound (FSOUND_ALL); + if (MusicGroup != NULL) + { + MusicGroup->release(); + MusicGroup = NULL; + } + if (PausableSfx != NULL) + { + PausableSfx->release(); + PausableSfx = NULL; + } + if (SfxGroup != NULL) + { + SfxGroup->release(); + SfxGroup = NULL; + } if (ChannelMap) { delete[] ChannelMap; @@ -491,167 +612,258 @@ void FMODSoundRenderer::Shutdown () // Free all loaded samples for (i = 0; i < S_sfx.Size (); i++) { - S_sfx[i].data = NULL; - S_sfx[i].altdata = NULL; + if (S_sfx[i].data != NULL) + { + ((FMOD::Sound *)S_sfx[i].data)->release(); + S_sfx[i].data = NULL; + } + if (S_sfx[i].altdata != NULL) + { + ((FMOD::Sound *)S_sfx[i].altdata)->release(); + S_sfx[i].altdata = NULL; + } S_sfx[i].bHaveLoop = false; } - FSOUND_Close (); - DidInit = false; + Sys->close(); + Sys = NULL; } } -void FMODSoundRenderer::PrintStatus () +void FMODSoundRenderer::PrintStatus() { - int output = FSOUND_GetOutput (); - int driver = FSOUND_GetDriver (); - int mixer = FSOUND_GetMixer (); + FMOD_OUTPUTTYPE output; + FMOD_SPEAKERMODE speakermode; + FMOD_SOUND_FORMAT format; + FMOD_DSP_RESAMPLER resampler; + int driver; + int samplerate; + int numoutputchannels; int num2d, num3d, total; - Printf ("Output: %s\n", OutputNames[output]); - Printf ("Driver: %d (%s)\n", driver, FSOUND_GetDriverName (driver)); - Printf ("Mixer: %s\n", MixerNames[mixer]); - if (DriverCaps) + if (FMOD_OK == Sys->getOutput(&output)) { - Printf ("Driver caps:"); - if (DriverCaps & FSOUND_CAPS_HARDWARE) Printf (" HARDWARE"); - if (DriverCaps & FSOUND_CAPS_EAX2) Printf (" EAX2"); - if (DriverCaps & FSOUND_CAPS_EAX3) Printf (" EAX3"); - Printf ("\n"); + Printf ("Output type: "TEXTCOLOR_GREEN"%s\n", Enum_NameForNum(OutputNames, output)); } - FSOUND_GetNumHWChannels (&num2d, &num3d, &total); - Printf ("Hardware 2D channels: %d\n", num2d); - Printf ("Hardware 3D channels: %d\n", num3d); - Printf ("Total hardware channels: %d\n", total); + if (FMOD_OK == Sys->getSpeakerMode(&speakermode)) + { + Printf ("Speaker mode: "TEXTCOLOR_GREEN"%s\n", Enum_NameForNum(SpeakerModeNames, speakermode)); + } + if (FMOD_OK == Sys->getDriver(&driver)) + { + char name[256]; + if (FMOD_OK != Sys->getDriverInfo(driver, name, sizeof(name), NULL)) + { + strcpy(name, "Unknown"); + } + Printf ("Driver: "TEXTCOLOR_GREEN"%d"TEXTCOLOR_NORMAL" ("TEXTCOLOR_ORANGE"%s"TEXTCOLOR_NORMAL")\n", driver, name); + DumpDriverCaps(Driver_Caps, Driver_MinFrequency, Driver_MaxFrequency); + } + if (FMOD_OK == Sys->getHardwareChannels(&num2d, &num3d, &total)) + { + Printf (TEXTCOLOR_YELLOW "Hardware 2D channels: "TEXTCOLOR_GREEN"%d\n", num2d); + Printf (TEXTCOLOR_YELLOW "Hardware 3D channels: "TEXTCOLOR_GREEN"%d\n", num3d); + Printf (TEXTCOLOR_YELLOW "Total hardware channels: "TEXTCOLOR_GREEN"%d\n", total); + } + if (FMOD_OK == Sys->getSoftwareFormat(&samplerate, &format, &numoutputchannels, NULL, &resampler, NULL)) + { + Printf (TEXTCOLOR_LIGHTBLUE "Software mixer sample rate: "TEXTCOLOR_GREEN"%d\n", samplerate); + Printf (TEXTCOLOR_LIGHTBLUE "Software mixer format: "TEXTCOLOR_GREEN"%s\n", Enum_NameForNum(SoundFormatNames, format)); + Printf (TEXTCOLOR_LIGHTBLUE "Software mixer channels: "TEXTCOLOR_GREEN"%d\n", numoutputchannels); + Printf (TEXTCOLOR_LIGHTBLUE "Software mixer resampler: "TEXTCOLOR_GREEN"%s\n", Enum_NameForNum(ResamplerNames, resampler)); + } + Printf("Using 3D sound: "TEXTCOLOR_GREEN"%s\n", Sound3D ? "yes" : "no"); if (Sound3D) { - Printf ("\nUsing 3D sound\n"); + Printf("Using hardware 3D sound: "TEXTCOLOR_GREEN"%s\n", Hardware3D ? "yes" : "no"); } } -void FMODSoundRenderer::PrintDriversList () +void FMODSoundRenderer::DumpDriverCaps(FMOD_CAPS caps, int minfrequency, int maxfrequency) { - int numdrivers = FSOUND_GetNumDrivers (); - - for (int i = 0; i < numdrivers; i++) + Printf (TEXTCOLOR_OLIVE " Min. frequency: "TEXTCOLOR_GREEN"%d\n", minfrequency); + Printf (TEXTCOLOR_OLIVE " Max. frequency: "TEXTCOLOR_GREEN"%d\n", maxfrequency); + Printf (" Features:\n"); + if (caps == 0) Printf(TEXTCOLOR_OLIVE " None\n"); + if (caps & FMOD_CAPS_HARDWARE) Printf(TEXTCOLOR_OLIVE " Hardware mixing\n"); + if (caps & FMOD_CAPS_HARDWARE_EMULATED) Printf(TEXTCOLOR_OLIVE " Hardware acceleration is turned off!\n"); + if (caps & FMOD_CAPS_OUTPUT_MULTICHANNEL) Printf(TEXTCOLOR_OLIVE " Multichannel\n"); + if (caps & FMOD_CAPS_OUTPUT_FORMAT_PCM8) Printf(TEXTCOLOR_OLIVE " PCM-8"); + if (caps & FMOD_CAPS_OUTPUT_FORMAT_PCM16) Printf(TEXTCOLOR_OLIVE " PCM-16"); + if (caps & FMOD_CAPS_OUTPUT_FORMAT_PCM24) Printf(TEXTCOLOR_OLIVE " PCM-24"); + if (caps & FMOD_CAPS_OUTPUT_FORMAT_PCM32) Printf(TEXTCOLOR_OLIVE " PCM-32"); + if (caps & FMOD_CAPS_OUTPUT_FORMAT_PCMFLOAT) Printf(TEXTCOLOR_OLIVE " PCM-Float"); + if (caps & (FMOD_CAPS_OUTPUT_FORMAT_PCM8 | FMOD_CAPS_OUTPUT_FORMAT_PCM16 | FMOD_CAPS_OUTPUT_FORMAT_PCM24 | FMOD_CAPS_OUTPUT_FORMAT_PCM32 | FMOD_CAPS_OUTPUT_FORMAT_PCMFLOAT)) { - Printf ("%d. %s\n", i, FSOUND_GetDriverName (i)); + Printf("\n"); + } + if (caps & FMOD_CAPS_REVERB_EAX2) Printf(TEXTCOLOR_OLIVE " EAX2"); + if (caps & FMOD_CAPS_REVERB_EAX3) Printf(TEXTCOLOR_OLIVE " EAX3"); + if (caps & FMOD_CAPS_REVERB_EAX4) Printf(TEXTCOLOR_OLIVE " EAX4"); + if (caps & FMOD_CAPS_REVERB_EAX5) Printf(TEXTCOLOR_OLIVE " EAX5"); + if (caps & FMOD_CAPS_REVERB_I3DL2) Printf(TEXTCOLOR_OLIVE " I3DL2"); + if (caps & (FMOD_CAPS_REVERB_EAX2 | FMOD_CAPS_REVERB_EAX3 | FMOD_CAPS_REVERB_EAX4 | FMOD_CAPS_REVERB_EAX5 | FMOD_CAPS_REVERB_I3DL2)) + { + Printf("\n"); + } + if (caps & FMOD_CAPS_REVERB_LIMITED) Printf("TEXTCOLOR_OLIVE Limited reverb\n"); +} + +void FMODSoundRenderer::PrintDriversList() +{ + int numdrivers; + int i; + char name[256]; + + if (FMOD_OK == Sys->getNumDrivers(&numdrivers)) + { + for (i = 0; i < numdrivers; ++i) + { + if (FMOD_OK == Sys->getDriverInfo(i, name, sizeof(name), NULL)) + { + Printf("%d. %s\n", i, name); + } + } } } -FString FMODSoundRenderer::GatherStats () +FString FMODSoundRenderer::GatherStats() { + int channels; + float dsp, stream, update, total; FString out; - out.Format ("%d channels, %.2f%% CPU", FSOUND_GetChannelsPlaying(), - FSOUND_GetCPUUsage()); + + channels = 0; + total = update = stream = dsp = 0; + Sys->getChannelsPlaying(&channels); + Sys->getCPUUsage(&dsp, &stream, &update, &total); + + out.Format ("%d channels,%5.2f%% CPU (%.2f%% DSP %.2f%% Stream %.2f%% Update)", + channels, total, dsp, stream, update); return out; } -void FMODSoundRenderer::MovieDisableSound () +void FMODSoundRenderer::MovieDisableSound() { - I_ShutdownMusic (); - Shutdown (); + I_ShutdownMusic(); + Shutdown(); } -void FMODSoundRenderer::MovieResumeSound () +void FMODSoundRenderer::MovieResumeSound() { - Init (); - S_Init (); - S_RestartMusic (); + Init(); + S_Init(); + S_RestartMusic(); } -void FMODSoundRenderer::SetSfxVolume (float volume) +void FMODSoundRenderer::SetSfxVolume(float volume) { - FSOUND_SetSFXMasterVolume (int(volume * 255.f)); - // FMOD apparently resets absolute volume channels when setting master vol - snd_musicvolume.Callback (); + SfxGroup->setVolume(volume); } -int FMODSoundRenderer::SetChannels (int numchannels) +void FMODSoundRenderer::SetMusicVolume(float volume) { - int i; + MusicGroup->setVolume(volume); +} - // If using 3D sound, use all the hardware channels available, - // regardless of what the user sets with snd_channels. If there - // are fewer than 8 hardware channels, then force software. - if (Sound3D) +int FMODSoundRenderer::GetNumChannels() +{ + int chancount; + + if (!Hardware3D) { - int hardChans; - - FSOUND_GetNumHWChannels (NULL, &hardChans, NULL); - - if (hardChans < 8) + if (FMOD_OK == Sys->getSoftwareChannels(&chancount)) { - Sound3D = false; - } - else - { - numchannels = hardChans; + chancount = MIN(snd_channels, chancount - NUM_EXTRA_SOFTWARE_CHANNELS); } } + // If hardware, let FMOD deal with the maximum actually supported by the hardware. + chancount = snd_channels; - ChannelMap = new ChanMap[numchannels]; - for (i = 0; i < numchannels; i++) + ChannelMap = new ChanMap[chancount]; + for (int i = 0; i < chancount; i++) { ChannelMap[i].soundID = -1; } - NumChannels = numchannels; - return numchannels; + NumChannels = chancount; + return chancount; } SoundStream *FMODSoundRenderer::CreateStream (SoundStreamCallback callback, int buffbytes, int flags, int samplerate, void *userdata) { - FMODStreamCapsule *capsule = new FMODStreamCapsule (userdata, callback); - unsigned int mode = FSOUND_2D | FSOUND_SIGNED; - mode |= (flags & SoundStream::Mono) ? FSOUND_MONO : FSOUND_STEREO; - mode |= (flags & SoundStream::Bits8) ? FSOUND_8BITS : FSOUND_16BITS; - FSOUND_STREAM *stream = FSOUND_Stream_Create (FMODStreamCapsule::MyCallback, buffbytes, mode, samplerate, capsule); - if (stream != NULL) - { - capsule->SetStream (stream); - return capsule; - } - delete capsule; - return NULL; -} - -SoundStream *FMODSoundRenderer::OpenStream (const char *filename_or_data, int flags, int offset, int length) -{ - unsigned int mode = FSOUND_NORMAL | FSOUND_2D; - if (flags & SoundStream::Loop) mode |= FSOUND_LOOP_NORMAL; - FSOUND_STREAM *stream; + FMODStreamCapsule *capsule; + FMOD::Sound *sound; + FMOD_RESULT result; + FMOD_CREATESOUNDEXINFO exinfo = { sizeof(exinfo), }; + FMOD_MODE mode; + int sample_shift; + int channel_shift; - if (offset==-1) - { - mode |= FSOUND_LOADMEMORY; - offset=0; - } - stream = FSOUND_Stream_Open (filename_or_data, mode, offset, length); + capsule = new FMODStreamCapsule (userdata, callback, this); - if (stream != NULL) + mode = FMOD_2D | FMOD_OPENUSER | FMOD_LOOP_NORMAL | FMOD_SOFTWARE | FMOD_CREATESTREAM; + sample_shift = (flags & SoundStream::Bits8) ? 0 : 1; + channel_shift = (flags & SoundStream::Mono) ? 0 : 1; + + // Chunk size of stream update in samples. This will be the amount of data + // passed to the user callback. + exinfo.decodebuffersize = buffbytes >> (sample_shift + channel_shift); + + // Number of channels in the sound. + exinfo.numchannels = 1 << channel_shift; + + // Length of PCM data in bytes of whole song (for Sound::getLength). + // This pretends it's 5 seconds long. + exinfo.length = (samplerate * 5) << (sample_shift + channel_shift); + + // Default playback rate of sound. */ + exinfo.defaultfrequency = samplerate; + + // Data format of sound. + exinfo.format = (flags & SoundStream::Bits8) ? FMOD_SOUND_FORMAT_PCM8 : FMOD_SOUND_FORMAT_PCM16; + + // User callback for reading. + exinfo.pcmreadcallback = FMODStreamCapsule::PCMReadCallback; + + // User callback for seeking. + exinfo.pcmsetposcallback = FMODStreamCapsule::PCMSetPosCallback; + + // User data to be attached to the sound during creation. Access via Sound::getUserData. + exinfo.userdata = capsule; + + result = Sys->createSound(NULL, mode, &exinfo, &sound); + if (result != FMOD_OK) { - return new FMODStreamCapsule (stream); + delete capsule; + return NULL; } - return NULL; + capsule->SetStream(sound); + return capsule; } -SoundTrackerModule *FMODSoundRenderer::OpenModule (const char *filename_or_data, int offset, int length) +SoundStream *FMODSoundRenderer::OpenStream(const char *filename_or_data, int flags, int offset, int length) { - FMUSIC_MODULE *mod; + FMOD_MODE mode; + FMOD_CREATESOUNDEXINFO exinfo = { sizeof(exinfo), }; + FMOD::Sound *stream; - int mode = FSOUND_LOOP_NORMAL; - if (offset==-1) + mode = FMOD_SOFTWARE | FMOD_2D | FMOD_CREATESTREAM; + if (flags & SoundStream::Loop) { - mode |= FSOUND_LOADMEMORY; - offset=0; + mode |= FMOD_LOOP_NORMAL; } - - mod = FMUSIC_LoadSongEx (filename_or_data, offset, length, mode, NULL, 0); - - if (mod != NULL) + if (offset == -1) { - return new FMUSICCapsule (mod); + mode |= FMOD_OPENMEMORY; + offset = 0; + } + exinfo.length = length; + exinfo.fileoffset = offset; + + if (FMOD_OK == Sys->createSound(filename_or_data, mode, &exinfo, &stream)) + { + return new FMODStreamCapsule(stream, this); } return NULL; } @@ -660,89 +872,77 @@ SoundTrackerModule *FMODSoundRenderer::OpenModule (const char *filename_or_data, // vol range is 0-255 // sep range is 0-255, -1 for surround, -2 for full vol middle // -long FMODSoundRenderer::StartSound (sfxinfo_t *sfx, int vol, int sep, int pitch, int channel, bool looping, bool pauseable) +long FMODSoundRenderer::StartSound(sfxinfo_t *sfx, float vol, float sep, int pitch, int channel, bool looping, bool pausable) { if (!ChannelMap) return 0; int id = int(sfx - &S_sfx[0]); - long volume; - long pan; - long freq; - long chan; + FMOD_RESULT result; + FMOD::Channel *chan; + float freq; - if (sep < 0) - { - pan = 128; // FSOUND_STEREOPAN is too loud relative to everything else - // when we don't want positioned sounds, so use center panning instead. - } - else - { - pan = sep; - } + freq = PITCH(sfx->frequency, pitch); - freq = PITCH(sfx->frequency,pitch); - volume = vol; - - chan = FSOUND_PlaySoundEx (FSOUND_FREE, CheckLooping (sfx, looping), NULL, true); - - if (chan != -1) + result = Sys->playSound(FMOD_CHANNEL_FREE, CheckLooping(sfx, looping), true, &chan); + if (FMOD_OK == result) { - FSOUND_SetSurround (chan, sep == -1 ? TRUE : FALSE); - FSOUND_SetFrequency (chan, freq); - FSOUND_SetVolume (chan, vol); - FSOUND_SetPan (chan, pan); - FSOUND_SetPaused (chan, false); + chan->setChannelGroup((pausable && !SFXPaused) ? PausableSfx : SfxGroup); + chan->setFrequency(freq); + chan->setVolume(vol); + chan->setPan(sep); + chan->setPaused(false); + if (Sound3D) + { + FMOD_MODE mode; + + if (FMOD_OK == chan->getMode(&mode)) + { + mode = (mode & ~FMOD_3D_WORLDRELATIVE) | (FMOD_3D_HEADRELATIVE); + } + } ChannelMap[channel].channelID = chan; ChannelMap[channel].soundID = id; - ChannelMap[channel].bIsLooping = looping ? true : false; - ChannelMap[channel].lastPos = 0; - ChannelMap[channel].bIs3D = false; - ChannelMap[channel].bIsPauseable = pauseable; + ChannelMap[channel].bIsLooping = looping; return channel + 1; } - DPrintf ("Sound %s failed to play: %d\n", sfx->name.GetChars(), FSOUND_GetError ()); + DPrintf ("Sound %s failed to play: %d\n", sfx->name.GetChars(), result); return 0; } -long FMODSoundRenderer::StartSound3D (sfxinfo_t *sfx, float vol, int pitch, int channel, - bool looping, float pos[3], float vel[3], bool pauseable) +long FMODSoundRenderer::StartSound3D(sfxinfo_t *sfx, float vol, int pitch, int channel, + bool looping, float pos[3], float vel[3], bool pausable) { if (!Sound3D || !ChannelMap) return 0; int id = int(sfx - &S_sfx[0]); - long freq; - long chan; + FMOD_RESULT result; + FMOD::Channel *chan; + float freq; - freq = PITCH(sfx->frequency,pitch); + freq = PITCH(sfx->frequency, pitch); - FSOUND_SAMPLE *sample = CheckLooping (sfx, looping); - - chan = FSOUND_PlaySoundEx (FSOUND_FREE, sample, NULL, true); - - if (chan != -1) + result = Sys->playSound(FMOD_CHANNEL_FREE, CheckLooping(sfx, looping), true, &chan); + if (FMOD_OK == result) { - //FSOUND_SetReserved (chan, TRUE); - FSOUND_SetFrequency (chan, freq); - FSOUND_SetVolume (chan, (int)(vol * 255.f)); - FSOUND_3D_SetAttributes (chan, pos, vel); - FSOUND_SetPaused (chan, false); + chan->setChannelGroup((pausable && !SFXPaused) ? PausableSfx : SfxGroup); + chan->setFrequency(freq); + chan->setVolume(vol); + chan->set3DAttributes((FMOD_VECTOR *)pos, (FMOD_VECTOR *)vel); + chan->setPaused(false); ChannelMap[channel].channelID = chan; ChannelMap[channel].soundID = id; - ChannelMap[channel].bIsLooping = looping ? true : false; - ChannelMap[channel].lastPos = 0; - ChannelMap[channel].bIs3D = true; - ChannelMap[channel].bIsPauseable = pauseable; + ChannelMap[channel].bIsLooping = looping; return channel + 1; } - DPrintf ("Sound %s failed to play: %d (%d)\n", sfx->name.GetChars(), FSOUND_GetError (), FSOUND_GetChannelsPlaying ()); + DPrintf ("Sound %s failed to play: %d\n", sfx->name.GetChars(), result); return 0; } -void FMODSoundRenderer::StopSound (long handle) +void FMODSoundRenderer::StopSound(long handle) { if (!handle || !ChannelMap) return; @@ -750,213 +950,152 @@ void FMODSoundRenderer::StopSound (long handle) handle--; if (ChannelMap[handle].soundID != -1) { - FSOUND_StopSound (ChannelMap[handle].channelID); - //FSOUND_SetReserved (ChannelMap[handle].channelID, FALSE); - UncheckSound (&S_sfx[ChannelMap[handle].soundID], ChannelMap[handle].bIsLooping); - ChannelMap[handle].soundID = -1; + ChannelMap[handle].channelID->stop(); + UncheckSound(&S_sfx[ChannelMap[handle].soundID], ChannelMap[handle].bIsLooping); + ChannelMap[handle].soundID = NULL; } } -void FMODSoundRenderer::StopAllChannels () +void FMODSoundRenderer::StopAllChannels() { - for (long i = 1; i <= NumChannels; ++i) + for (int i = 1; i <= NumChannels; ++i) { - StopSound (i); + StopSound(i); } } -void FMODSoundRenderer::SetSfxPaused (bool paused) +void FMODSoundRenderer::SetSfxPaused(bool paused) { - for (int i = 0; i < NumChannels; ++i) + if (SFXPaused != paused) { - if (ChannelMap[i].soundID != -1) - { - if (ChannelMap[i].bIsPauseable) - { - FSOUND_SetPaused (ChannelMap[i].channelID, paused); - } - } + PausableSfx->setPaused(paused); + SFXPaused = paused; } } -bool FMODSoundRenderer::IsPlayingSound (long handle) +bool FMODSoundRenderer::IsPlayingSound(long handle) { if (!handle || !ChannelMap) return false; handle--; - if (ChannelMap[handle].soundID == -1) - { - return false; - } - else + if (ChannelMap[handle].channelID != NULL) { bool is; - // FSOUND_IsPlaying does not work with A3D - if (OutputType != FSOUND_OUTPUT_A3D) + if (FMOD_OK == ChannelMap[handle].channelID->isPlaying(&is)) { - is = !!FSOUND_IsPlaying (ChannelMap[handle].channelID); + return is; } - else - { - unsigned int pos; - if (ChannelMap[handle].bIsLooping) - { - is = true; - } - else - { - pos = FSOUND_GetCurrentPosition (ChannelMap[handle].channelID); - is = pos >= ChannelMap[handle].lastPos && - pos <= S_sfx[ChannelMap[handle].soundID].length; - ChannelMap[handle].lastPos = pos; - } - } - if (!is) - { - //FSOUND_SetReserved (ChannelMap[handle].channelID, FALSE); - UncheckSound (&S_sfx[ChannelMap[handle].soundID], ChannelMap[handle].bIsLooping); - ChannelMap[handle].soundID = -1; - } - return is; } + return false; } -void FMODSoundRenderer::UpdateSoundParams (long handle, int vol, int sep, int pitch) +void FMODSoundRenderer::UpdateSoundParams(long handle, float vol, float sep, int pitch) { if (!handle || !ChannelMap) return; handle--; - if (ChannelMap[handle].soundID == -1) + if (ChannelMap[handle].soundID == -1 || ChannelMap[handle].channelID == NULL) return; - long volume; - long pan; - long freq; + float freq = PITCH(S_sfx[ChannelMap[handle].soundID].frequency, pitch); + FMOD::Channel *chan = ChannelMap[handle].channelID; - freq = PITCH(S_sfx[ChannelMap[handle].soundID].frequency, pitch); - volume = vol; - - if (sep < 0) - { - pan = 128; //FSOUND_STEREOPAN - } - else - { - pan = sep; - } - - FSOUND_SetSurround (ChannelMap[handle].channelID, sep == -1 ? TRUE : FALSE); - FSOUND_SetPan (ChannelMap[handle].channelID, pan); - FSOUND_SetVolume (ChannelMap[handle].channelID, volume); - FSOUND_SetFrequency (ChannelMap[handle].channelID, freq); + chan->setPan(sep); + chan->setVolume(vol); + chan->setFrequency(freq); } -void FMODSoundRenderer::UpdateSoundParams3D (long handle, float pos[3], float vel[3]) +void FMODSoundRenderer::UpdateSoundParams3D(long handle, float pos[3], float vel[3]) { if (!handle || !ChannelMap) return; handle--; - if (ChannelMap[handle].soundID == -1) + if (ChannelMap[handle].soundID == -1 || ChannelMap[handle].channelID == NULL) return; - FSOUND_3D_SetAttributes (ChannelMap[handle].channelID, pos, vel); + ChannelMap[handle].channelID->set3DAttributes((FMOD_VECTOR *)pos, (FMOD_VECTOR *)vel); } -void FMODSoundRenderer::ResetEnvironment () +void FMODSoundRenderer::ResetEnvironment() { PrevEnvironment = NULL; } -void FMODSoundRenderer::UpdateListener (AActor *listener) +void FMODSoundRenderer::UpdateListener(AActor *listener) { float angle; - float pos[3], vel[3]; - float lpos[3]; + FMOD_VECTOR pos, vel; + FMOD_VECTOR forward; + FMOD_VECTOR up; - if (Sound3D && ChannelMap) + if(Sound3D && ChannelMap) { - vel[0] = listener->momx * (TICRATE/65536.f); - vel[1] = listener->momy * (TICRATE/65536.f); - vel[2] = listener->momz * (TICRATE/65536.f); - pos[0] = listener->x / 65536.f; - pos[2] = listener->y / 65536.f; - pos[1] = listener->z / 65536.f; - - // Move sounds that are not meant to be heard in 3D so - // that they remain on top of the listener. - - for (int i = 0; i < NumChannels; i++) - { - if (ChannelMap[i].soundID != -1 && !ChannelMap[i].bIs3D) - { - FSOUND_3D_SetAttributes (ChannelMap[i].channelID, pos, vel); - } - } - - // Sounds that are right on top of the listener can produce - // weird results depending on the environment, so position - // the listener back slightly from its true location. + vel.x = listener->momx * (TICRATE/65536.f); + vel.y = listener->momz * (TICRATE/65536.f); + vel.z = listener->momy * (TICRATE/65536.f); + pos.x = listener->x / 65536.f; + pos.y = listener->z / 65536.f; + pos.z = listener->y / 65536.f; angle = (float)(listener->angle) * ((float)PI / 2147483648.f); - lpos[0] = pos[0] - .5f * cosf (angle); - lpos[2] = pos[2] - .5f * sinf (angle); - lpos[1] = pos[1]; + forward.x = cosf(angle); + forward.y = 0; + forward.z = sinf(angle); - FSOUND_3D_Listener_SetAttributes (lpos, vel, - cosf (angle), 0.f, sinf (angle), 0.f, 1.f, 0.f); + up.x = 0; + up.y = 1; + up.z = 0; - //if (DriverCaps & (FSOUND_CAPS_EAX2|FSOUND_CAPS_EAX3)) + Sys->set3DListenerAttributes(0, &pos, &vel, &forward, &up); + + bool underwater; + const ReverbContainer *env; + + if (ForcedEnvironment) { - bool underwater; - const ReverbContainer *env; - - if (ForcedEnvironment) + env = ForcedEnvironment; + } + else + { + underwater = (listener->waterlevel == 3 && snd_waterreverb); + assert (zones != NULL); + env = zones[listener->Sector->ZoneNumber].Environment; + if (env == NULL) { - env = ForcedEnvironment; + env = DefaultEnvironments[0]; } - else + if (env == DefaultEnvironments[0] && underwater) { - underwater = (listener->waterlevel == 3 && snd_waterreverb); - assert (zones != NULL); - env = zones[listener->Sector->ZoneNumber].Environment; - if (env == NULL) - { - env = DefaultEnvironments[0]; - } - if (env == DefaultEnvironments[0] && underwater) - { - env = DefaultEnvironments[22]; - } - } - if (env != PrevEnvironment || env->Modified) - { - DPrintf ("Reverb Environment %s\n", env->Name); - const_cast(env)->Modified = false; - FSOUND_Reverb_SetProperties ((FSOUND_REVERB_PROPERTIES *)(&env->Properties)); - PrevEnvironment = env; + env = DefaultEnvironments[22]; } } - - FSOUND_Update (); + if (env != PrevEnvironment || env->Modified) + { + DPrintf ("Reverb Environment %s\n", env->Name); + const_cast(env)->Modified = false; + Sys->setReverbProperties((FMOD_REVERB_PROPERTIES *)(&env->Properties)); + PrevEnvironment = env; + } } + Sys->update(); } -void FMODSoundRenderer::LoadSound (sfxinfo_t *sfx) +void FMODSoundRenderer::LoadSound(sfxinfo_t *sfx) { if (!sfx->data) { - DPrintf ("loading sound \"%s\" (%d) ", sfx->name.GetChars(), sfx - &S_sfx[0]); - getsfx (sfx); + DPrintf("loading sound \"%s\" (%d) ", sfx->name.GetChars(), sfx - &S_sfx[0]); + getsfx(sfx); } } -void FMODSoundRenderer::UnloadSound (sfxinfo_t *sfx) +void FMODSoundRenderer::UnloadSound(sfxinfo_t *sfx) { if (sfx->data == NULL) return; @@ -966,23 +1105,23 @@ void FMODSoundRenderer::UnloadSound (sfxinfo_t *sfx) sfx->looping = 0; if (sfx->altdata != NULL) { - FSOUND_Sample_Free ((FSOUND_SAMPLE *)sfx->altdata); + ((FMOD::Sound *)sfx->altdata)->release(); sfx->altdata = NULL; } if (sfx->data != NULL) { - FSOUND_Sample_Free ((FSOUND_SAMPLE *)sfx->data); + ((FMOD::Sound *)sfx->data)->release(); sfx->data = NULL; } - DPrintf ("Unloaded sound \"%s\" (%d)\n", sfx->name.GetChars(), sfx - &S_sfx[0]); + DPrintf("Unloaded sound \"%s\" (%d)\n", sfx->name.GetChars(), sfx - &S_sfx[0]); } // FSOUND_Sample_Upload seems to mess up the signedness of sound data when // uploading to hardware buffers. The pattern is not particularly predictable, // so this is a replacement for it that loads the data manually. Source data // is mono, unsigned, 8-bit. Output is mono, signed, 8- or 16-bit. - +#if 0 int FMODSoundRenderer::PutSampleData (FSOUND_SAMPLE *sample, const BYTE *data, int len, unsigned int mode) { /*if (mode & FSOUND_2D) @@ -1050,15 +1189,21 @@ int FMODSoundRenderer::PutSampleData (FSOUND_SAMPLE *sample, const BYTE *data, i } } } +#endif -void FMODSoundRenderer::DoLoad (void **slot, sfxinfo_t *sfx) +void FMODSoundRenderer::DoLoad(void **slot, sfxinfo_t *sfx) { BYTE *sfxdata; + BYTE *sfxstart; int size; int errcount; - unsigned long samplemode; + FMOD_RESULT result; + FMOD_MODE samplemode; + FMOD_CREATESOUNDEXINFO exinfo = { sizeof(exinfo), }; + FMOD::Sound *sample; - samplemode = Sound3D ? FSOUND_HW3D : FSOUND_2D; + samplemode = (Sound3D ? FMOD_3D : FMOD_2D) | FMOD_OPENMEMORY; + samplemode |= Hardware3D ? FMOD_HARDWARE : FMOD_SOFTWARE; sfxdata = NULL; errcount = 0; @@ -1071,18 +1216,18 @@ void FMODSoundRenderer::DoLoad (void **slot, sfxinfo_t *sfx) } if (errcount) - sfx->lumpnum = Wads.GetNumForName ("dsempty", ns_sounds); + sfx->lumpnum = Wads.GetNumForName("dsempty", ns_sounds); - size = Wads.LumpLength (sfx->lumpnum); + size = Wads.LumpLength(sfx->lumpnum); if (size == 0) { errcount++; continue; } - FWadLump wlump = Wads.OpenLumpNum (sfx->lumpnum); - sfxdata = new BYTE[size]; - wlump.Read (sfxdata, size); + FWadLump wlump = Wads.OpenLumpNum(sfx->lumpnum); + sfxstart = sfxdata = new BYTE[size]; + wlump.Read(sfxdata, size); SDWORD len = ((SDWORD *)sfxdata)[1]; // If the sound is raw, just load it as such. @@ -1091,15 +1236,10 @@ void FMODSoundRenderer::DoLoad (void **slot, sfxinfo_t *sfx) if (sfx->bLoadRAW || (((BYTE *)sfxdata)[0] == 3 && ((BYTE *)sfxdata)[1] == 0 && len <= size - 8)) { - FSOUND_SAMPLE *sample; - const BYTE *sfxstart; - unsigned int bits; - if (sfx->bLoadRAW) { len = Wads.LumpLength (sfx->lumpnum); sfx->frequency = (sfx->bForce22050 ? 22050 : 11025); - sfxstart = sfxdata; } else { @@ -1110,82 +1250,76 @@ void FMODSoundRenderer::DoLoad (void **slot, sfxinfo_t *sfx) } sfxstart = sfxdata + 8; } - sfx->ms = sfx->length = len; + sfx->length = len; - bits = FSOUND_8BITS; - do - { - sample = FSOUND_Sample_Alloc (FSOUND_FREE, len, - samplemode|bits|FSOUND_LOOP_OFF|FSOUND_MONO, - sfx->frequency, 255, FSOUND_STEREOPAN, 255); - } while (sample == NULL && (bits <<= 1) == FSOUND_16BITS); + exinfo.length = len; + exinfo.numchannels = 1; + exinfo.defaultfrequency = sfx->frequency; + exinfo.format = FMOD_SOUND_FORMAT_PCM8; - if (sample == NULL) + samplemode |= FMOD_OPENRAW; + + // Need to convert sample data from unsigned to signed. + for (int i = 0; i < len; ++i) { - DPrintf ("Failed to allocate sample: %d\n", FSOUND_GetError ()); - errcount++; - continue; + sfxstart[i] = sfxstart[i] - 128; } - - if (!PutSampleData (sample, sfxstart, len, samplemode)) - { - DPrintf ("Failed to upload sample: %d\n", FSOUND_GetError ()); - FSOUND_Sample_Free (sample); - sample = NULL; - errcount++; - continue; - } - *slot = sample; } else { - if (((BYTE *)sfxdata)[0] == 'f' && ((BYTE *)sfxdata)[1] == 'L' && - ((BYTE *)sfxdata)[2] == 'a' && ((BYTE *)sfxdata)[3] == 'C') - { - FLACSampleLoader loader (sfx); - *slot = loader.LoadSample (samplemode); - if (*slot == NULL && FSOUND_GetError() == FMOD_ERR_CREATEBUFFER && samplemode == FSOUND_HW3D) - { - DPrintf ("Trying to fall back to software sample\n"); - *slot = FSOUND_Sample_Load (FSOUND_FREE, (char *)sfxdata, FSOUND_2D, 0, size); - } - } - else - { - *slot = FSOUND_Sample_Load (FSOUND_FREE, (char *)sfxdata, - samplemode|FSOUND_LOADMEMORY, 0, size); - if (*slot == NULL && FSOUND_GetError() == FMOD_ERR_CREATEBUFFER && samplemode == FSOUND_HW3D) - { - DPrintf ("Trying to fall back to software sample\n"); - *slot = FSOUND_Sample_Load (FSOUND_FREE, (char *)sfxdata, FSOUND_2D|FSOUND_LOADMEMORY, 0, size); - } - } - if (*slot != NULL) - { - int probe; + exinfo.length = size; + } + result = Sys->createSound((char *)sfxstart, samplemode, &exinfo, &sample); + if (result == FMOD_ERR_OUTPUT_CREATEBUFFER && !(samplemode & FMOD_SOFTWARE)) + { + DPrintf("Trying to fall back to software sample\n"); + samplemode = (samplemode & ~FMOD_HARDWARE) | FMOD_SOFTWARE; + result = Sys->createSound((char *)sfxstart, samplemode, &exinfo, &sample); + } + if (result != FMOD_OK) + { + DPrintf("Failed to allocate sample: %d\n", result); + errcount++; + continue; + } + *slot = sample; + // Get frequency and length for sounds FMOD handled for us. + if (!(samplemode & FMOD_OPENRAW)) + { + float freq; - FSOUND_Sample_GetDefaults ((FSOUND_SAMPLE *)sfx->data, &probe, - NULL, NULL, NULL); - - sfx->frequency = probe; - sfx->ms = FSOUND_Sample_GetLength ((FSOUND_SAMPLE *)sfx->data); - sfx->length = sfx->ms; + result = sample->getDefaults(&freq, NULL, NULL, NULL); + if (result != FMOD_OK) + { + DPrintf("Failed getting default sound frequency, assuming 11025Hz\n"); + freq = 11025; } + sfx->frequency = (unsigned int)freq; + + result = sample->getLength(&sfx->length, FMOD_TIMEUNIT_PCM); + if (result != FMOD_OK) + { + DPrintf("Failed getting sample length\n"); + } + } + + // Get sample length in milliseconds. I think this is only used for ambient sounds? + if (FMOD_OK != sample->getLength(&sfx->ms, FMOD_TIMEUNIT_MS)) + { + sfx->ms = (sfx->length * 1000) / sfx->frequency; } break; } if (sfx->data) { - sfx->ms = (sfx->ms * 1000) / (sfx->frequency); DPrintf ("[%d Hz %d samples]\n", sfx->frequency, sfx->length); if (Sound3D) { // Match s_sound.cpp min distance. // Max distance is irrelevant. - FSOUND_Sample_SetMinMaxDistance ((FSOUND_SAMPLE *)sfx->data, - (float)S_CLOSE_DIST, (float)MAX_SND_DIST*2); + sample->set3DMinMaxDistance(float(S_CLOSE_DIST), float(MAX_SND_DIST)*2); } } @@ -1195,7 +1329,7 @@ void FMODSoundRenderer::DoLoad (void **slot, sfxinfo_t *sfx) } } -void FMODSoundRenderer::getsfx (sfxinfo_t *sfx) +void FMODSoundRenderer::getsfx(sfxinfo_t *sfx) { unsigned int i; @@ -1228,36 +1362,39 @@ void FMODSoundRenderer::getsfx (sfxinfo_t *sfx) } -// Right now, FMOD's biggest shortcoming compared to MIDAS is that it does not -// support multiple samples with the same sample data. Thus, if we want to -// play a looped and non-looped version of the same sound, we need to create -// two copies of it. Fortunately, most sounds will either be played looped or -// not, but not both at the same time, so this really isn't too much of a -// problem. This function juggles the sample between looping and non-looping, -// creating a copy if necessary. It also increments the appropriate use -// counter. +//=========================================================================== // -// Update: FMOD 3.3 added FSOUND_SetLoopMode to set a channel's looping status, -// but that only works with software channels. So I think I will continue to -// do this even though I don't *have* to anymore. +// FMODSoundRenderer :: CheckLooping +// +// Hardware sounds do not support arbitrarily changing the loop mode for +// different playing copies of the same sound, so if we need to play both +// a looping and an unlooping version of the same sound, then we need two +// copies of the sound. +// +// Fortunately, most sounds will be played as one or the other and not both. +// This function juggles the sample between looping and non-looping, creating +// a copy if necessary, and increasing the appropriate use counter. +// +// Note that software sounds do not have this restriction, but since there's +// no way for the engine to designate that a sound should always be software, +// this function is used for them too, just to keep things simpler for me. +// +//=========================================================================== -FSOUND_SAMPLE *FMODSoundRenderer::CheckLooping (sfxinfo_t *sfx, bool looped) +FMOD::Sound *FMODSoundRenderer::CheckLooping(sfxinfo_t *sfx, bool looped) { if (looped) { sfx->looping++; if (sfx->bHaveLoop) { - return (FSOUND_SAMPLE *)(sfx->altdata ? sfx->altdata : sfx->data); + return (FMOD::Sound *)(sfx->altdata ? sfx->altdata : sfx->data); } - else + else if (sfx->normal == 0) { - if (sfx->normal == 0) - { - sfx->bHaveLoop = true; - FSOUND_Sample_SetMode ((FSOUND_SAMPLE *)sfx->data, FSOUND_LOOP_NORMAL); - return (FSOUND_SAMPLE *)sfx->data; - } + sfx->bHaveLoop = true; + ((FMOD::Sound *)sfx->data)->setLoopCount(-1); + return (FMOD::Sound *)sfx->data; } } else @@ -1265,28 +1402,25 @@ FSOUND_SAMPLE *FMODSoundRenderer::CheckLooping (sfxinfo_t *sfx, bool looped) sfx->normal++; if (sfx->altdata || !sfx->bHaveLoop) { - return (FSOUND_SAMPLE *)sfx->data; + return (FMOD::Sound *)sfx->data; } - else + else if (sfx->looping == 0) { - if (sfx->looping == 0) - { - sfx->bHaveLoop = false; - FSOUND_Sample_SetMode ((FSOUND_SAMPLE *)sfx->data, FSOUND_LOOP_OFF); - return (FSOUND_SAMPLE *)sfx->data; - } + sfx->bHaveLoop = false; + ((FMOD::Sound *)sfx->data)->setLoopCount(0); + return (FMOD::Sound *)sfx->data; } } // If we get here, we need to create an alternate version of the sample. - FSOUND_Sample_SetMode ((FSOUND_SAMPLE *)sfx->data, FSOUND_LOOP_OFF); - DoLoad (&sfx->altdata, sfx); - FSOUND_Sample_SetMode ((FSOUND_SAMPLE *)sfx->altdata, FSOUND_LOOP_NORMAL); + ((FMOD::Sound *)sfx->data)->setLoopCount(0); + DoLoad(&sfx->altdata, sfx); + ((FMOD::Sound *)sfx->altdata)->setLoopCount(-1); sfx->bHaveLoop = true; - return (FSOUND_SAMPLE *)(looped ? sfx->altdata : sfx->data); + return (FMOD::Sound *)(looped ? sfx->altdata : sfx->data); } -void FMODSoundRenderer::UncheckSound (sfxinfo_t *sfx, bool looped) +void FMODSoundRenderer::UncheckSound(sfxinfo_t *sfx, bool looped) { if (looped) { diff --git a/src/sound/fmodsound.h b/src/sound/fmodsound.h index a63a5b71b..3e3046dcc 100644 --- a/src/sound/fmodsound.h +++ b/src/sound/fmodsound.h @@ -2,7 +2,7 @@ #define FMODSOUND_H #include "i_sound.h" -#include +#include "fmod_wrap.h" class FMODSoundRenderer : public SoundRenderer { @@ -12,7 +12,8 @@ public: bool IsValid (); void SetSfxVolume (float volume); - int SetChannels (int numchans); + void SetMusicVolume (float volume); + int GetNumChannels (); void LoadSound (sfxinfo_t *sfx); void UnloadSound (sfxinfo_t *sfx); @@ -22,11 +23,8 @@ public: long PlayStream (SoundStream *stream, int volume); void StopStream (SoundStream *stream); - // Tracker modules. - SoundTrackerModule *OpenModule (const char *file, int offset, int length); - // Starts a sound in a particular sound channel. - long StartSound (sfxinfo_t *sfx, int vol, int sep, int pitch, int channel, bool looping, bool pauseable); + long StartSound (sfxinfo_t *sfx, float vol, float sep, int pitch, int channel, bool looping, bool pauseable); long StartSound3D (sfxinfo_t *sfx, float vol, int pitch, int channel, bool looping, float pos[3], float vel[3], bool pauseable); // Stops a sound channel. @@ -42,7 +40,7 @@ public: bool IsPlayingSound (long handle); // Updates the volume, separation, and pitch of a sound channel. - void UpdateSoundParams (long handle, int vol, int sep, int pitch); + void UpdateSoundParams (long handle, float vol, float sep, int pitch); void UpdateSoundParams3D (long handle, float pos[3], float vel[3]); // For use by I_PlayMovie @@ -61,26 +59,36 @@ private: struct ChanMap { int soundID; // sfx playing on this channel - long channelID; + FMOD::Channel *channelID; bool bIsLooping; - bool bIs3D; - bool bIsPauseable; - unsigned int lastPos; } *ChannelMap; int NumChannels; unsigned int DriverCaps; int OutputType; - bool DidInit; + bool Hardware3D; + bool SFXPaused; - int PutSampleData (FSOUND_SAMPLE *sample, const BYTE *data, int len, unsigned int mode); +// int PutSampleData (FSOUND_SAMPLE *sample, const BYTE *data, int len, unsigned int mode); void DoLoad (void **slot, sfxinfo_t *sfx); void getsfx (sfxinfo_t *sfx); - FSOUND_SAMPLE *CheckLooping (sfxinfo_t *sfx, bool looped); + FMOD::Sound *CheckLooping (sfxinfo_t *sfx, bool looped); void UncheckSound (sfxinfo_t *sfx, bool looped); bool Init (); void Shutdown (); + void DumpDriverCaps(FMOD_CAPS caps, int minfrequency, int maxfrequency); + + FMOD::System *Sys; + FMOD::ChannelGroup *SfxGroup, *PausableSfx; + FMOD::ChannelGroup *MusicGroup; + + // Just for snd_status display + int Driver_MinFrequency; + int Driver_MaxFrequency; + FMOD_CAPS Driver_Caps; + + friend class FMODStreamCapsule; }; #endif diff --git a/src/sound/i_music.cpp b/src/sound/i_music.cpp index 34328eb8e..198aa7ca6 100644 --- a/src/sound/i_music.cpp +++ b/src/sound/i_music.cpp @@ -72,13 +72,9 @@ extern void ChildSigHandler (int signum); #include -EXTERN_CVAR (Float, snd_midivolume) EXTERN_CVAR (Int, snd_samplerate) EXTERN_CVAR (Int, snd_mididevice) -void Enable_FSOUND_IO_Loader (); -void Disable_FSOUND_IO_Loader (); - static bool MusicDown = true; MusInfo *currSong; @@ -99,8 +95,18 @@ CUSTOM_CVAR (Float, snd_musicvolume, 0.3f, CVAR_ARCHIVE|CVAR_GLOBALCONFIG) self = 0.f; else if (self > 1.f) self = 1.f; - else if (currSong != NULL && !currSong->IsMIDI ()) - currSong->SetVolume (clamp (self * relative_volume, 0.f, 1.f)); + else if (GSnd != NULL) + { + // Set general music volume. + GSnd->SetMusicVolume(clamp(self * relative_volume, 0, 1)); + + // For music not implemented through the digital sound system, + // let them know about the changed. + if (currSong != NULL) + { + currSong->MusicVolumeChanged(); + } + } } MusInfo::~MusInfo () @@ -116,6 +122,14 @@ void MusInfo::Update () { } +void MusInfo::MusicVolumeChanged() +{ +} + +void MusInfo::TimidityVolumeChanged() +{ +} + void I_InitMusic (void) { static bool setatterm = false; @@ -335,12 +349,6 @@ void *I_RegisterSong (const char *filename, char * musiccache, int offset, int l } } #endif - // Check for FLAC format - else if (id == MAKE_ID('f','L','a','C')) - { - info = new FLACSong (file, musiccache, len); - file = NULL; - } // Check for RDosPlay raw OPL format else if (id == MAKE_ID('R','A','W','A') && len >= 12) { @@ -421,14 +429,12 @@ void *I_RegisterSong (const char *filename, char * musiccache, int offset, int l if (info == NULL && GSnd != NULL && len >= 1024) { // First try loading it as MOD, then as a stream - if (file != NULL) fclose (file); - file = NULL; - info = new MODSong (offset>=0? filename : musiccache, offset, len); - if (!info->IsValid ()) + if (file != NULL) { - delete info; - info = new StreamSong (offset>=0? filename : musiccache, offset, len); + fclose (file); + file = NULL; } + info = new StreamSong (offset >=0 ? filename : musiccache, offset, len); } } @@ -487,9 +493,6 @@ void I_SetMusicVolume (float factor) { factor = clamp(factor, 0, 2.0f); relative_volume = saved_relative_volume * factor; -#ifdef _WIN32 - snd_midivolume.Callback(); -#endif snd_musicvolume.Callback(); } @@ -498,9 +501,6 @@ CCMD(testmusicvol) if (argv.argc() > 1) { relative_volume = (float)strtod(argv[1], NULL); -#ifdef _WIN32 - snd_midivolume.Callback(); -#endif snd_musicvolume.Callback(); } } diff --git a/src/sound/i_musicinterns.h b/src/sound/i_musicinterns.h index 928a6c265..51dde57ca 100644 --- a/src/sound/i_musicinterns.h +++ b/src/sound/i_musicinterns.h @@ -33,7 +33,8 @@ class MusInfo public: MusInfo () : m_Status(STATE_Stopped) {} virtual ~MusInfo (); - virtual void SetVolume (float volume) = 0; + virtual void MusicVolumeChanged(); // snd_musicvolume changed + virtual void TimidityVolumeChanged(); // timidity_mastervolume changed virtual void Play (bool looping) = 0; virtual void Pause () = 0; virtual void Resume () = 0; @@ -116,7 +117,7 @@ public: MIDIStreamer(); ~MIDIStreamer(); - void SetVolume(float volume); + void MusicVolumeChanged(); void Play(bool looping); void Pause(); void Resume(); @@ -171,6 +172,7 @@ protected: int Tempo; int InitialTempo; BYTE ChannelVolumes[16]; + DWORD Volume; }; // MUS file played with a MIDI stream --------------------------------------- @@ -225,37 +227,13 @@ protected: #endif /* _WIN32 */ -// MOD file played with FMOD ------------------------------------------------ - -class MODSong : public MusInfo -{ -public: - MODSong (const char *file, int offset, int length); - ~MODSong (); - void SetVolume (float volume); - void Play (bool looping); - void Pause (); - void Resume (); - void Stop (); - bool IsPlaying (); - bool IsMIDI () const { return false; } - bool IsValid () const { return m_Module != NULL; } - bool SetPosition (int order); - -protected: - MODSong () {} - - SoundTrackerModule *m_Module; -}; - -// OGG/MP3/WAV/other format streamed through FMOD --------------------------- +// Anything supported by FMOD out of the box -------------------------------- class StreamSong : public MusInfo { public: StreamSong (const char *file, int offset, int length); ~StreamSong (); - void SetVolume (float volume); void Play (bool looping); void Pause (); void Resume (); @@ -263,6 +241,7 @@ public: bool IsPlaying (); bool IsMIDI () const { return false; } bool IsValid () const { return m_Stream != NULL; } + bool SetPosition (int order); protected: StreamSong () : m_Stream(NULL), m_LastPos(0) {} @@ -328,7 +307,7 @@ public: void Stop (); bool IsPlaying (); bool IsValid () const { return CommandLine.Len() > 0; } - void SetVolume (float volume); + void TimidityVolumeChanged(); protected: void PrepTimidity (); @@ -373,25 +352,6 @@ protected: OPLmusicBlock *Music; }; -// FLAC file streamed through FMOD (You should probably use Vorbis instead) - - -class FLACSong : public StreamSong -{ -public: - FLACSong (FILE *file, char * musiccache, int length); - ~FLACSong (); - void Play (bool looping); - bool IsPlaying (); - bool IsValid () const; - -protected: - static bool FillStream (SoundStream *stream, void *buff, int len, void *userdata); - - class FLACStreamer; - - FLACStreamer *State; -}; - // CD track/disk played through the multimedia system ----------------------- class CDSong : public MusInfo @@ -399,7 +359,6 @@ class CDSong : public MusInfo public: CDSong (int track, int id); ~CDSong (); - void SetVolume (float volume) {} void Play (bool looping); void Pause (); void Resume (); diff --git a/src/sound/i_sound.cpp b/src/sound/i_sound.cpp index 112660f17..4cf4a4956 100644 --- a/src/sound/i_sound.cpp +++ b/src/sound/i_sound.cpp @@ -54,9 +54,6 @@ extern HINSTANCE g_hInst; #include #include "fmodsound.h" -#ifdef _WIN32 -#include "altsound.h" -#endif #include "m_swap.h" #include "stats.h" @@ -76,7 +73,7 @@ extern HINSTANCE g_hInst; #include "doomdef.h" EXTERN_CVAR (Float, snd_sfxvolume) -CVAR (Int, snd_samplerate, 44100, CVAR_ARCHIVE|CVAR_GLOBALCONFIG) +CVAR (Int, snd_samplerate, 48000, CVAR_ARCHIVE|CVAR_GLOBALCONFIG) CVAR (Int, snd_buffersize, 0, CVAR_ARCHIVE|CVAR_GLOBALCONFIG) CVAR (String, snd_output, "default", CVAR_ARCHIVE|CVAR_GLOBALCONFIG) @@ -172,23 +169,7 @@ void I_InitSound () snd_samplerate = 65535; } -#ifdef _WIN32 - if (stricmp (snd_output, "alternate") == 0) - { - GSnd = new AltSoundRenderer; - } - else - { - GSnd = new FMODSoundRenderer; - if (!GSnd->IsValid ()) - { - delete GSnd; - GSnd = new AltSoundRenderer; - } - } -#else GSnd = new FMODSoundRenderer; -#endif if (!GSnd->IsValid ()) { @@ -269,11 +250,6 @@ SoundRenderer::~SoundRenderer () { } -SoundTrackerModule *SoundRenderer::OpenModule (const char *file, int offset, int length) -{ - return NULL; -} - long SoundRenderer::StartSound3D (sfxinfo_t *sfx, float vol, int pitch, int channel, bool looping, float pos[3], float vel[3], bool pauseable) { return 0; @@ -304,7 +280,7 @@ SoundStream::~SoundStream () { } -SoundTrackerModule::~SoundTrackerModule () +bool SoundStream::SetPosition(int pos) { + return false; } - diff --git a/src/sound/i_sound.h b/src/sound/i_sound.h index 2f5a2599b..996f9910d 100644 --- a/src/sound/i_sound.h +++ b/src/sound/i_sound.h @@ -51,25 +51,12 @@ public: Loop = 4 }; - virtual bool Play (bool looping, float volume) = 0; + virtual bool Play (bool looping, float volume, bool normalize) = 0; virtual void Stop () = 0; virtual void SetVolume (float volume) = 0; virtual bool SetPaused (bool paused) = 0; virtual unsigned int GetPosition () = 0; -}; - -class SoundTrackerModule -{ -public: - virtual ~SoundTrackerModule (); - - virtual bool Play () = 0; - virtual void Stop () = 0; - virtual void SetVolume (float volume) = 0; - virtual bool SetPaused (bool paused) = 0; - virtual bool IsPlaying () = 0; - virtual bool IsFinished () = 0; - virtual bool SetOrder (int order) = 0; + virtual bool SetPosition (int pos); }; typedef bool (*SoundStreamCallback)(SoundStream *stream, void *buff, int len, void *userdata); @@ -81,7 +68,8 @@ public: virtual ~SoundRenderer (); virtual void SetSfxVolume (float volume) = 0; - virtual int SetChannels (int numchans) = 0; // Initialize channels + virtual void SetMusicVolume (float volume) = 0; + virtual int GetNumChannels () = 0; // Initialize channels virtual void LoadSound (sfxinfo_t *sfx) = 0; // load a sound from disk virtual void UnloadSound (sfxinfo_t *sfx) = 0; // unloads a sound from memory @@ -89,11 +77,8 @@ public: virtual SoundStream *CreateStream (SoundStreamCallback callback, int buffbytes, int flags, int samplerate, void *userdata) = 0; virtual SoundStream *OpenStream (const char *filename, int flags, int offset, int length) = 0; - // Tracker modules. - virtual SoundTrackerModule *OpenModule (const char *file, int offset, int length); - // Starts a sound in a particular sound channel. - virtual long StartSound (sfxinfo_t *sfx, int vol, int sep, int pitch, int channel, bool looping, bool pauseable) = 0; + virtual long StartSound (sfxinfo_t *sfx, float vol, float sep, int pitch, int channel, bool looping, bool pauseable) = 0; virtual long StartSound3D (sfxinfo_t *sfx, float vol, int pitch, int channel, bool looping, float pos[3], float vel[3], bool pauseable); // Stops a sound channel. @@ -109,7 +94,7 @@ public: virtual bool IsPlayingSound (long handle) = 0; // Updates the volume, separation, and pitch of a sound channel. - virtual void UpdateSoundParams (long handle, int vol, int sep, int pitch) = 0; + virtual void UpdateSoundParams (long handle, float vol, float sep, int pitch) = 0; virtual void UpdateSoundParams3D (long handle, float pos[3], float vel[3]); // For use by I_PlayMovie diff --git a/src/sound/music_flac.cpp b/src/sound/music_flac.cpp deleted file mode 100644 index 5a1e46a70..000000000 --- a/src/sound/music_flac.cpp +++ /dev/null @@ -1,323 +0,0 @@ -#include "i_musicinterns.h" -#include "templates.h" - -#include - -#define FLAC__NO_DLL -#include - -class FLACSong::FLACStreamer : protected FLAC::Decoder::Stream -{ -public: - FLACStreamer (FILE *file, char * musiccache, int length); - ~FLACStreamer (); - - bool ServiceStream (void *buff, int len, bool loop); - - unsigned NumChannels, SampleBits, SampleRate; - -protected: - virtual ::FLAC__StreamDecoderReadStatus read_callback(FLAC__byte buffer[], unsigned *bytes); - virtual ::FLAC__StreamDecoderWriteStatus write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[]); - virtual void metadata_callback(const ::FLAC__StreamMetadata *metadata); - virtual void error_callback(::FLAC__StreamDecoderErrorStatus status); - - void CopyToStream (void *&sbuff, FLAC__int32 **buffer, size_t ofs, size_t samples); - - FileReader *File; - long StartPos, EndPos; - - FLAC__int32 *SamplePool[2]; - size_t PoolSize; - size_t PoolUsed; - size_t PoolPos; - - void *SBuff; - size_t SLen; -}; - -FLACSong::FLACSong (FILE *file, char * musiccache, int length) - : State (NULL) -{ - State = new FLACStreamer (file, musiccache, length); - - if (State->NumChannels > 0 && State->SampleBits > 0 && State->SampleRate > 0) - { - int flags = 0; - int buffsize = State->SampleRate / 5; - - if (State->SampleBits <= 8) - { - flags |= SoundStream::Bits8; - } - else - { - buffsize <<= 1; - } - if (State->NumChannels == 1) - { - flags |= SoundStream::Mono; - } - else - { - buffsize <<= 1; - } - m_Stream = GSnd->CreateStream (FillStream, buffsize, flags, State->SampleRate, this); - if (m_Stream == NULL) - { - Printf (PRINT_BOLD, "Could not create music stream.\n"); - return; - } - } -} - -FLACSong::~FLACSong () -{ - Stop (); - if (State != NULL) - { - delete State; - } -} - -bool FLACSong::IsValid () const -{ - return m_Stream != NULL; -} - -bool FLACSong::IsPlaying () -{ - return m_Status == STATE_Playing; -} - -void FLACSong::Play (bool looping) -{ - m_Status = STATE_Stopped; - m_Looping = looping; - - if (m_Stream->Play (true, snd_musicvolume)) - { - m_Status = STATE_Playing; - } -} - -bool FLACSong::FillStream (SoundStream *stream, void *buff, int len, void *userdata) -{ - FLACSong *song = (FLACSong *)userdata; - return song->State->ServiceStream (buff, len, song->m_Looping); -} - -FLACSong::FLACStreamer::FLACStreamer (FILE *iofile, char * musiccache, int length) - : NumChannels (0), - SampleBits (0), - SampleRate (0), - PoolSize (0), - PoolUsed (0), - PoolPos (0) -{ - if (iofile != NULL) - { - File = new FileReader (iofile, length); - } - else - { - File = new MemoryReader(musiccache, length); - } - - StartPos = File->Tell(); - EndPos = StartPos + File->GetLength(); - init (); - process_until_end_of_metadata (); -} - -FLACSong::FLACStreamer::~FLACStreamer () -{ - if (PoolSize > 0 && SamplePool[0] != NULL) - { - delete[] SamplePool[0]; - SamplePool[0] = NULL; - } - if (File->GetFile()!=NULL) fclose (File->GetFile()); - delete File; -} - -bool FLACSong::FLACStreamer::ServiceStream (void *buff1, int len, bool loop) -{ - void *buff = buff1; - size_t samples = len; - size_t poolAvail; - size_t poolGrab; - - if (NumChannels == 2) - { - samples >>= 1; - } - if (SampleBits > 8) - { - samples >>= 1; - } - - poolAvail = PoolUsed - PoolPos; - if (poolAvail > 0) - { - poolGrab = MIN (samples, poolAvail); - CopyToStream (buff, SamplePool, PoolPos, poolGrab); - PoolPos += poolGrab; - if (PoolPos == PoolUsed) - { - PoolPos = PoolUsed = 0; - } - samples -= poolGrab; - } - - SBuff = buff; - SLen = samples; - - while (SLen > 0) - { - if (get_state() == FLAC__STREAM_DECODER_END_OF_STREAM) - { - if (!loop) - { - return FALSE; - } - File->Seek (StartPos, SEEK_SET); - reset (); - } - - if (!process_single ()) - { - return FALSE; - } - } - - return TRUE; -} - -void FLACSong::FLACStreamer::CopyToStream (void *&sbuff, FLAC__int32 **buffer, size_t ofs, size_t len) -{ - size_t i; - - if (SampleBits == 16) - { - SWORD *buff = (SWORD *)sbuff; - if (NumChannels == 1) - { - for (i = 0; i < len; ++i) - { - buff[i] = SWORD(buffer[0][i + ofs]); - } - sbuff = buff + i; - } - else - { - for (i = 0; i < len; ++i) - { - buff[i*2] = SWORD(buffer[0][i + ofs]); - buff[i*2+1] = SWORD(buffer[1][i + ofs]); - } - sbuff = buff + i*2; - } - } - else if (SampleBits == 8) - { - SBYTE *buff = (SBYTE *)sbuff; - if (NumChannels == 1) - { - for (i = 0; i < len; ++i) - { - buff[i] = SBYTE(buffer[0][i + ofs]); - } - sbuff = buff + i; - } - else - { - for (i = 0; i < len; ++i) - { - buff[i*2] = SBYTE(buffer[0][i + ofs]); - buff[i*2+1] = SBYTE(buffer[1][i + ofs]); - } - sbuff = buff + i*2; - } - } -} - -::FLAC__StreamDecoderReadStatus FLACSong::FLACStreamer::read_callback(FLAC__byte buffer[], unsigned *bytes) -{ - if (*bytes > 0) - { - long here = File->Tell(); - if (here == EndPos) - { - return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM; - } - else - { - if (*bytes > (size_t)(EndPos - here)) - { - *bytes = EndPos - here; - } - File->Read (buffer, *bytes); - return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; - } - } - else - { - return FLAC__STREAM_DECODER_READ_STATUS_ABORT; - } -} - -::FLAC__StreamDecoderWriteStatus FLACSong::FLACStreamer::write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[]) -{ - size_t blockSize = frame->header.blocksize; - size_t blockGrab = 0; - size_t buffAvail; - size_t blockOfs; - - buffAvail = SLen; - blockGrab = MIN (SLen, blockSize); - CopyToStream (SBuff, (FLAC__int32 **)buffer, 0, blockGrab); - blockSize -= blockGrab; - SLen -= blockGrab; - blockOfs = blockGrab; - - if (blockSize > 0) - { - buffAvail = PoolSize - PoolUsed; - blockGrab = MIN (buffAvail, blockSize); - memcpy (SamplePool[0] + PoolUsed, buffer[0] + blockOfs, sizeof(buffer[0])*blockGrab); - if (NumChannels > 1) - { - memcpy (SamplePool[1] + PoolUsed, buffer[1] + blockOfs, sizeof(buffer[1])*blockGrab); - } - PoolUsed += blockGrab; - } - - return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE; -} - -void FLACSong::FLACStreamer::metadata_callback(const ::FLAC__StreamMetadata *metadata) -{ - if (metadata->type == FLAC__METADATA_TYPE_STREAMINFO && PoolSize == 0) - { - switch (metadata->data.stream_info.bits_per_sample) - { - case 8: case 16: break; - default: - Printf ("Only FLACs with 8 or 16 bits per sample are supported\n"); - return; - } - SampleRate = metadata->data.stream_info.sample_rate; - NumChannels = (unsigned int)MIN (2u, metadata->data.stream_info.channels); - SampleBits = metadata->data.stream_info.bits_per_sample; - PoolSize = metadata->data.stream_info.max_blocksize * 2; - - SamplePool[0] = new FLAC__int32[PoolSize * NumChannels]; - SamplePool[1] = SamplePool[0] + PoolSize; - } -} - -void FLACSong::FLACStreamer::error_callback(::FLAC__StreamDecoderErrorStatus status) -{ - status = status; -} diff --git a/src/sound/music_midi_base.cpp b/src/sound/music_midi_base.cpp index fd8fe6418..ea9bc871f 100644 --- a/src/sound/music_midi_base.cpp +++ b/src/sound/music_midi_base.cpp @@ -9,34 +9,8 @@ static DWORD nummididevices; static bool nummididevicesset; - DWORD midivolume; UINT mididevice; -//========================================================================== -// -// CVAR snd_midivolume -// -// Maximum volume of MIDI/MUS music through the MM subsystem. -//========================================================================== - -CUSTOM_CVAR (Float, snd_midivolume, 0.5f, CVAR_ARCHIVE|CVAR_GLOBALCONFIG) -{ - if (self < 0.f) - self = 0.f; - else if (self > 1.f) - self = 1.f; - else - { - float realvolume = clamp(self * relative_volume, 0.f, 1.f); - DWORD onechanvol = clamp((DWORD)(realvolume * 65535.f), 0, 65535); - midivolume = (onechanvol << 16) | onechanvol; - if (currSong && currSong->IsMIDI ()) - { - currSong->SetVolume (realvolume); - } - } -} - CVAR (Bool, snd_midiprecache, true, CVAR_ARCHIVE|CVAR_GLOBALCONFIG); CUSTOM_CVAR (Int, snd_mididevice, -1, CVAR_ARCHIVE|CVAR_GLOBALCONFIG) @@ -48,13 +22,13 @@ CUSTOM_CVAR (Int, snd_mididevice, -1, CVAR_ARCHIVE|CVAR_GLOBALCONFIG) if ((self >= (signed)nummididevices) || (self < -2)) { - Printf ("ID out of range. Using MIDI mapper.\n"); - self = -1; + Printf ("ID out of range. Using default device.\n"); + self = 0; return; } else if (self < 0) { - mididevice = MIDI_MAPPER; + mididevice = 0; } else { @@ -62,7 +36,7 @@ CUSTOM_CVAR (Int, snd_mididevice, -1, CVAR_ARCHIVE|CVAR_GLOBALCONFIG) } // If a song is playing, move it to the new device. - if (oldmididev != mididevice && currSong) + if (oldmididev != mididevice && currSong != NULL && currSong->IsMIDI()) { MusInfo *song = currSong; if (song->m_Status == MusInfo::STATE_Playing) diff --git a/src/sound/music_midi_midiout.cpp b/src/sound/music_midi_midiout.cpp index b089b7707..1993a2c4b 100644 --- a/src/sound/music_midi_midiout.cpp +++ b/src/sound/music_midi_midiout.cpp @@ -87,9 +87,6 @@ struct MIDISong2::TrackInfo // EXTERNAL DATA DECLARATIONS ---------------------------------------------- -EXTERN_CVAR (Float, snd_midivolume) - -extern DWORD midivolume; extern UINT mididevice; // PRIVATE DATA DEFINITIONS ------------------------------------------------ diff --git a/src/sound/music_midi_timidity.cpp b/src/sound/music_midi_timidity.cpp index f969a16fd..e3578f1e0 100644 --- a/src/sound/music_midi_timidity.cpp +++ b/src/sound/music_midi_timidity.cpp @@ -64,8 +64,8 @@ CUSTOM_CVAR (Float, timidity_mastervolume, 1.0f, CVAR_ARCHIVE|CVAR_GLOBALCONFIG) self = 0.f; else if (self > 4.f) self = 4.f; - else if (currSong != NULL && !currSong->IsMIDI ()) - currSong->SetVolume (clamp (snd_musicvolume, 0.f, 1.f)); + else if (currSong != NULL) + currSong->TimidityVolumeChanged(); } @@ -94,7 +94,7 @@ void TimiditySong::Play (bool looping) { if (m_Stream != NULL) { - if (m_Stream->Play (true, timidity_mastervolume * snd_musicvolume)) + if (m_Stream->Play (true, timidity_mastervolume, false)) { m_Status = STATE_Playing; } @@ -594,9 +594,12 @@ bool TimiditySong::FillStream (SoundStream *stream, void *buff, int len, void *u return true; } -void TimiditySong::SetVolume (float volume) +void TimiditySong::TimidityVolumeChanged() { - if (m_Stream!=NULL) m_Stream->SetVolume (volume*timidity_mastervolume); + if (m_Stream != NULL) + { + m_Stream->SetVolume(timidity_mastervolume); + } } bool TimiditySong::IsPlaying () diff --git a/src/sound/music_midistream.cpp b/src/sound/music_midistream.cpp index 264bde4a4..58117710e 100644 --- a/src/sound/music_midistream.cpp +++ b/src/sound/music_midistream.cpp @@ -53,9 +53,8 @@ // EXTERNAL DATA DECLARATIONS ---------------------------------------------- -EXTERN_CVAR (Float, snd_midivolume) +EXTERN_CVAR(Float, snd_musicvolume) -extern DWORD midivolume; extern UINT mididevice; // PRIVATE DATA DEFINITIONS ------------------------------------------------ @@ -184,8 +183,7 @@ void MIDIStreamer::Play (bool looping) return; } - snd_midivolume.Callback(); // set volume to current music's properties - OutputVolume (midivolume & 0xffff); + MusicVolumeChanged(); // set volume to current music's properties ResetEvent(ExitEvent); ResetEvent(BufferDoneEvent); @@ -290,7 +288,7 @@ void MIDIStreamer::Resume () { if (m_Status == STATE_Paused) { - OutputVolume(midivolume & 0xffff); + OutputVolume(Volume); m_Status = STATE_Playing; } } @@ -341,13 +339,22 @@ bool MIDIStreamer::IsPlaying () //========================================================================== // -// MIDIStreamer :: SetVolume +// MIDIStreamer :: MusicVolumeChanged +// +// WinMM MIDI doesn't go through the sound system, so the normal volume +// changing procedure doesn't work for it. // //========================================================================== -void MIDIStreamer::SetVolume (float volume) +void MIDIStreamer::MusicVolumeChanged () { - OutputVolume(midivolume & 0xffff); + float realvolume = clamp(snd_musicvolume * relative_volume, 0.f, 1.f); + DWORD onechanvol = clamp((DWORD)(realvolume * 65535.f), 0, 65535); + Volume = onechanvol; + if (m_Status == STATE_Playing) + { + OutputVolume(onechanvol); + } } //========================================================================== @@ -377,7 +384,7 @@ void MIDIStreamer::OutputVolume (DWORD volume) int MIDIStreamer::VolumeControllerChange(int channel, int volume) { ChannelVolumes[channel] = volume; - return ((volume + 1) * (midivolume & 0xffff)) >> 16; + return ((volume + 1) * Volume) >> 16; } //========================================================================== diff --git a/src/sound/music_mod.cpp b/src/sound/music_mod.cpp deleted file mode 100644 index f17915ec5..000000000 --- a/src/sound/music_mod.cpp +++ /dev/null @@ -1,91 +0,0 @@ -#include "i_musicinterns.h" - -void MODSong::SetVolume (float volume) -{ - if (m_Module) - { - m_Module->SetVolume (volume); - } -} - -void MODSong::Play (bool looping) -{ - m_Status = STATE_Stopped; - m_Looping = looping; - - if (m_Module->Play ()) - { - m_Module->SetVolume (snd_musicvolume); - m_Status = STATE_Playing; - } -} - -void MODSong::Pause () -{ - if (m_Status == STATE_Playing) - { - if (m_Module->SetPaused (true)) - m_Status = STATE_Paused; - } -} - -void MODSong::Resume () -{ - if (m_Status == STATE_Paused) - { - if (m_Module->SetPaused (false)) - m_Status = STATE_Playing; - } -} - -void MODSong::Stop () -{ - if (m_Status != STATE_Stopped && m_Module) - { - m_Module->Stop (); - } - m_Status = STATE_Stopped; -} - -MODSong::~MODSong () -{ - Stop (); - if (m_Module != NULL) - { - delete m_Module; - m_Module = NULL; - } -} - -MODSong::MODSong (const char *file_or_data, int offset, int length) -{ - m_Module = GSnd->OpenModule (file_or_data, offset, length); -} - -bool MODSong::IsPlaying () -{ - if (m_Status != STATE_Stopped) - { - if (m_Module->IsPlaying ()) - { - if (!m_Looping && m_Module->IsFinished ()) - { - Stop (); - return false; - } - return true; - } - else if (m_Looping) - { - Play (true); - return m_Status != STATE_Stopped; - } - } - return false; -} - -bool MODSong::SetPosition (int order) -{ - return m_Module->SetOrder (order); -} - diff --git a/src/sound/music_mus_midiout.cpp b/src/sound/music_mus_midiout.cpp index c31189612..3fb8b9d9c 100644 --- a/src/sound/music_mus_midiout.cpp +++ b/src/sound/music_mus_midiout.cpp @@ -54,11 +54,8 @@ // EXTERNAL DATA DECLARATIONS ---------------------------------------------- -extern DWORD midivolume; extern UINT mididevice; -EXTERN_CVAR (Float, snd_midivolume) - // PRIVATE DATA DEFINITIONS ------------------------------------------------ static const BYTE CtrlTranslate[15] = diff --git a/src/sound/music_mus_opl.cpp b/src/sound/music_mus_opl.cpp index d62b18dd7..ae0000da0 100644 --- a/src/sound/music_mus_opl.cpp +++ b/src/sound/music_mus_opl.cpp @@ -72,7 +72,7 @@ void OPLMUSSong::Play (bool looping) Music->SetLooping (looping); Music->Restart (); - if (m_Stream->Play (true, snd_musicvolume)) + if (m_Stream->Play (true, snd_musicvolume, true)) { m_Status = STATE_Playing; } diff --git a/src/sound/music_spc.cpp b/src/sound/music_spc.cpp index bf7c90e65..485df2dfe 100644 --- a/src/sound/music_spc.cpp +++ b/src/sound/music_spc.cpp @@ -171,7 +171,7 @@ void SPCSong::Play (bool looping) m_Status = STATE_Stopped; m_Looping = true; - if (m_Stream->Play (true, snd_musicvolume)) + if (m_Stream->Play (true, snd_musicvolume, false)) { m_Status = STATE_Playing; } diff --git a/src/sound/music_stream.cpp b/src/sound/music_stream.cpp index 3abbd3fa2..bddbfaf84 100644 --- a/src/sound/music_stream.cpp +++ b/src/sound/music_stream.cpp @@ -1,16 +1,11 @@ #include "i_musicinterns.h" -void StreamSong::SetVolume (float volume) -{ - if (m_Stream!=NULL) m_Stream->SetVolume (volume); -} - void StreamSong::Play (bool looping) { m_Status = STATE_Stopped; m_Looping = looping; - if (m_Stream->Play (m_Looping, snd_musicvolume)) + if (m_Stream->Play (m_Looping, 1, false)) { m_Status = STATE_Playing; m_LastPos = 0; @@ -80,3 +75,13 @@ bool StreamSong::IsPlaying () return false; } +// +// StreamSong :: SetPosition +// +// Sets the current order number for a MOD-type song, or the position in ms +// for anything else. + +bool StreamSong::SetPosition(int order) +{ + return m_Stream->SetPosition(order); +} diff --git a/src/sound/sample_flac.cpp b/src/sound/sample_flac.cpp deleted file mode 100644 index 9957fadaf..000000000 --- a/src/sound/sample_flac.cpp +++ /dev/null @@ -1,300 +0,0 @@ -#include - -#include "sample_flac.h" -#include "w_wad.h" -#include "templates.h" - -FLACSampleLoader::FLACSampleLoader (sfxinfo_t *sfx) - : NumChannels (0), - SampleBits (0), - SampleRate (0), - NumSamples (0), - File (Wads.OpenLumpNum (sfx->lumpnum)), - Sfx (sfx) -{ - StartPos = File.Tell(); - EndPos = StartPos + File.GetLength(); - - init (); - process_until_end_of_metadata (); -} - -FSOUND_SAMPLE *FLACSampleLoader::LoadSample (unsigned int samplemode) -{ - if (NumSamples == 0) - { - DPrintf ("FLAC has unknown number of samples\n"); - return NULL; - } - - FSOUND_SAMPLE *sample; - unsigned int mode; - unsigned int bits; - - Sfx->frequency = SampleRate; - bits = (SampleBits <= 8) ? FSOUND_8BITS : FSOUND_16BITS; - mode = FSOUND_SIGNED | FSOUND_MONO | FSOUND_LOOP_OFF; - - sample = FSOUND_Sample_Alloc (FSOUND_FREE, NumSamples, - samplemode | bits | mode, SampleRate, 255, FSOUND_STEREOPAN, 255); - if (sample == NULL) - { - bits ^= FSOUND_8BITS | FSOUND_16BITS; - sample = FSOUND_Sample_Alloc (FSOUND_FREE, NumSamples, - samplemode | bits | mode, SampleRate, 255, FSOUND_STEREOPAN, 255); - if (sample == NULL) - { - return sample; - } - } - - void *ptr1, *ptr2; - unsigned int len1, len2; - - if (!FSOUND_Sample_Lock (sample, 0, - bits & FSOUND_8BITS ? NumSamples : NumSamples * 2, - &ptr1, &ptr2, &len1, &len2)) - { - FSOUND_Sample_Free (sample); - DPrintf ("Failed locking FLAC sample\n"); - return NULL; - } - - SBuff = ptr1; - SBuff2 = ptr2; - SLen = len1; - SLen2 = len2; - Dest8 = (bits & FSOUND_8BITS) ? true : false; - - if (!process_until_end_of_stream ()) - { - FSOUND_Sample_Unlock (sample, ptr1, ptr2, len1, len2); - FSOUND_Sample_Free (sample); - DPrintf ("Failed loading FLAC sample\n"); - return NULL; - } - - FSOUND_Sample_Unlock (sample, ptr1, ptr2, len1, len2); - return sample; -} - -BYTE *FLACSampleLoader::ReadSample (SDWORD *numbytes) -{ - if (NumSamples == 0) - { - DPrintf ("FLAC has unknown number of samples\n"); - return NULL; - } - - BYTE *sfxdata; - - Sfx->frequency = SampleRate; - Sfx->b16bit = (SampleBits > 8); - - sfxdata = new BYTE[NumSamples << Sfx->b16bit]; - - SBuff = sfxdata; - SBuff2 = NULL; - SLen = NumSamples; - SLen2 = 0; - Dest8 = !Sfx->b16bit; - *numbytes = NumSamples << Sfx->b16bit; - - if (!process_until_end_of_stream ()) - { - DPrintf ("Failed loading FLAC sample\n"); - delete[] sfxdata; - return NULL; - } - - return sfxdata; -} - -FLACSampleLoader::~FLACSampleLoader () -{ -} - -void FLACSampleLoader::CopyToSample (size_t ofs, FLAC__int32 **buffer, size_t ilen) -{ - size_t i; - size_t len = MIN (ilen, SLen); - FLAC__int32 *buffer0 = buffer[0] + ofs; - - if (SampleBits == 16) - { - if (!Dest8) - { - SWORD *buff = (SWORD *)SBuff; - - if (NumChannels == 1) - { - // Mono16 -> Mono16 - for (i = 0; i < len; ++i) - { - buff[i] = SWORD(buffer0[i]); - } - } - else - { - // Stereo16 -> Mono16 - FLAC__int32 *buffer1 = buffer[1] + ofs; - - for (i = 0; i < len; ++i) - { - buff[i] = (buffer0[i] + buffer1[i]) / 2; - } - } - SBuff = buff + i; - } - else - { - SBYTE *buff = (SBYTE *)SBuff; - - if (NumChannels == 1) - { - // Mono16 -> Mono8 - for (i = 0; i < len; ++i) - { - buff[i] = buffer0[i] / 256; - } - } - else - { - // Stereo16 -> Mono8 - FLAC__int32 *buffer1 = buffer[1] + ofs; - - for (i = 0; i < len; ++i) - { - buff[i] = (buffer0[i] + buffer1[i]) / 512; - } - } - SBuff = buff + i; - } - } - else if (SampleBits == 8) - { - if (Dest8) - { - SBYTE *buff = (SBYTE *)SBuff; - - if (NumChannels == 1) - { - // Mono8 -> Mono8 - for (i = 0; i < len; ++i) - { - buff[i] = SBYTE(buffer0[i]); - } - } - else - { - // Stereo8 -> Mono8 - FLAC__int32 *buffer1 = buffer[1] + ofs; - - for (i = 0; i < len; ++i) - { - buff[i*2] = SBYTE(buffer0[i]); - buff[i*2+1] = SBYTE(buffer1[i]); - } - } - SBuff = buff + i; - } - else - { - SWORD *buff = (SWORD *)SBuff; - - if (NumChannels == 1) - { - // Mono8 -> Mono16 - for (i = 0; i < len; ++i) - { - buff[i] = buffer0[i] * 257; - } - } - else - { - // Stereo8 -> Mono16 - FLAC__int32 *buffer1 = buffer[1] + ofs; - - for (i = 0; i < len; ++i) - { - buff[i*2] = ((buffer0[i] + buffer1[i]) / 2) * 257; - } - } - SBuff = buff + i; - } - } - - // If the lock was split into two buffers, do the other one next. - // (Will this ever happen? Don't think so, but...) - if (ilen > len) - { - SBuff = SBuff2; - SLen = SLen2; - SBuff2 = NULL; - SLen2 = 0; - if (SLen > 0) - { - CopyToSample (len, buffer, ilen - len); - } - } -} - -::FLAC__StreamDecoderReadStatus FLACSampleLoader::read_callback(FLAC__byte buffer[], unsigned *bytes) -{ - if (*bytes > 0) - { - long here = File.Tell(); - - if (here == EndPos) - { - return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM; - } - else - { - if (*bytes > (size_t)(EndPos - here)) - { - *bytes = EndPos - here; - } - File.Read (buffer, *bytes); - return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; - } - } - else - { - return FLAC__STREAM_DECODER_READ_STATUS_ABORT; - } -} - -::FLAC__StreamDecoderWriteStatus FLACSampleLoader::write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[]) -{ - CopyToSample (0, (FLAC__int32 **)buffer, frame->header.blocksize); - - return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE; -} - -void FLACSampleLoader::metadata_callback(const ::FLAC__StreamMetadata *metadata) -{ - if (metadata->type == FLAC__METADATA_TYPE_STREAMINFO) - { - switch (metadata->data.stream_info.bits_per_sample) - { - case 8: case 16: break; - default: - DPrintf ("Only FLACs with 8 or 16 bits per sample are supported\n"); - return; - } - if (metadata->data.stream_info.total_samples > 0xFFFFFFFF) - { - DPrintf ("FLAC is too long\n"); - } - SampleRate = metadata->data.stream_info.sample_rate; - NumChannels = (unsigned int)MIN (2u, metadata->data.stream_info.channels); - SampleBits = metadata->data.stream_info.bits_per_sample; - NumSamples = (unsigned int)metadata->data.stream_info.total_samples; - } -} - -void FLACSampleLoader::error_callback(::FLAC__StreamDecoderErrorStatus status) -{ - status = status; -} diff --git a/src/sound/sample_flac.h b/src/sound/sample_flac.h deleted file mode 100644 index dfe16aa65..000000000 --- a/src/sound/sample_flac.h +++ /dev/null @@ -1,34 +0,0 @@ -#define FLAC__NO_DLL -#include -#include -#include "s_sound.h" -#include "files.h" -#include "w_wad.h" - -class FLACSampleLoader : protected FLAC::Decoder::Stream -{ -public: - FLACSampleLoader (sfxinfo_t *sfx); - ~FLACSampleLoader (); - - FSOUND_SAMPLE *LoadSample (unsigned int samplemode); - BYTE *ReadSample (SDWORD *numbytes); - - unsigned NumChannels, SampleBits, SampleRate, NumSamples; - -protected: - virtual ::FLAC__StreamDecoderReadStatus read_callback(FLAC__byte buffer[], unsigned *bytes); - virtual ::FLAC__StreamDecoderWriteStatus write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[]); - virtual void metadata_callback(const ::FLAC__StreamMetadata *metadata); - virtual void error_callback(::FLAC__StreamDecoderErrorStatus status); - - void CopyToSample (size_t ofs, FLAC__int32 **buffer, size_t samples); - - FWadLump File; - long StartPos, EndPos; - - void *SBuff, *SBuff2; - unsigned int SLen, SLen2; - sfxinfo_t *Sfx; - bool Dest8; -}; diff --git a/zdoom.sln b/zdoom.sln index 5b5ba175f..5487c2d6c 100644 --- a/zdoom.sln +++ b/zdoom.sln @@ -5,15 +5,12 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "zdoom", "zdoom.vcproj", "{8 {F9D9E7D4-E1A2-4866-9E85-B1B14137EE63} = {F9D9E7D4-E1A2-4866-9E85-B1B14137EE63} {6077B7D6-349F-4077-B552-3BC302EF5859} = {6077B7D6-349F-4077-B552-3BC302EF5859} {667D2EE7-C357-49E2-9BAB-0A4A45F0F76E} = {667D2EE7-C357-49E2-9BAB-0A4A45F0F76E} - {873F2EEA-24DF-454C-B245-CB9738BA993E} = {873F2EEA-24DF-454C-B245-CB9738BA993E} {DA47396F-60C1-4BDE-A977-7F7DE461CF77} = {DA47396F-60C1-4BDE-A977-7F7DE461CF77} {AC3F5340-40CB-4C3A-8AA7-CB7158DB4466} = {AC3F5340-40CB-4C3A-8AA7-CB7158DB4466} EndProjectSection EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "zlib", "zlib\zlib.vcproj", "{F9D9E7D4-E1A2-4866-9E85-B1B14137EE63}" EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "FLAC", "FLAC\FLAC.vcproj", "{873F2EEA-24DF-454C-B245-CB9738BA993E}" -EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "lemon", "tools\lemon\lemon.vcproj", "{0F80ACBF-460E-44F0-B28E-B3272D1774A7}" EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "re2c", "tools\re2c\re2c.vcproj", "{667D2EE7-C357-49E2-9BAB-0A4A45F0F76E}" @@ -68,14 +65,6 @@ Global {F9D9E7D4-E1A2-4866-9E85-B1B14137EE63}.Release|Win32.Build.0 = Release|Win32 {F9D9E7D4-E1A2-4866-9E85-B1B14137EE63}.Release|x64.ActiveCfg = Debug|x64 {F9D9E7D4-E1A2-4866-9E85-B1B14137EE63}.Release|x64.Build.0 = Debug|x64 - {873F2EEA-24DF-454C-B245-CB9738BA993E}.Debug|Win32.ActiveCfg = Debug|Win32 - {873F2EEA-24DF-454C-B245-CB9738BA993E}.Debug|Win32.Build.0 = Debug|Win32 - {873F2EEA-24DF-454C-B245-CB9738BA993E}.Debug|x64.ActiveCfg = Debug|x64 - {873F2EEA-24DF-454C-B245-CB9738BA993E}.Debug|x64.Build.0 = Debug|x64 - {873F2EEA-24DF-454C-B245-CB9738BA993E}.Release|Win32.ActiveCfg = Release|Win32 - {873F2EEA-24DF-454C-B245-CB9738BA993E}.Release|Win32.Build.0 = Release|Win32 - {873F2EEA-24DF-454C-B245-CB9738BA993E}.Release|x64.ActiveCfg = Debug|x64 - {873F2EEA-24DF-454C-B245-CB9738BA993E}.Release|x64.Build.0 = Debug|x64 {0F80ACBF-460E-44F0-B28E-B3272D1774A7}.Debug|Win32.ActiveCfg = Debug|Win32 {0F80ACBF-460E-44F0-B28E-B3272D1774A7}.Debug|Win32.Build.0 = Debug|Win32 {0F80ACBF-460E-44F0-B28E-B3272D1774A7}.Debug|x64.ActiveCfg = Release|Win32 diff --git a/zdoom.vcproj b/zdoom.vcproj index c63377ff6..fdecd5ee1 100644 --- a/zdoom.vcproj +++ b/zdoom.vcproj @@ -317,7 +317,7 @@ - - - - - - @@ -9250,10 +9238,6 @@ RelativePath="src\sound\music_cd.cpp" > - - @@ -9270,10 +9254,6 @@ RelativePath=".\src\sound\music_midistream.cpp" > - - @@ -9294,14 +9274,6 @@ RelativePath=".\src\sound\music_win_mididevice.cpp" > - - - -