summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xALACMagicCookieDescription.txt241
-rwxr-xr-xLICENSE53
-rwxr-xr-xReadMe.txt44
-rwxr-xr-xcodec/ALACAudioTypes.h201
-rwxr-xr-xcodec/ALACBitUtilities.c260
-rwxr-xr-xcodec/ALACBitUtilities.h108
-rw-r--r--codec/ALACCodec/ALACCodec.xcodeproj/project.pbxproj241
-rwxr-xr-xcodec/ALACDecoder.cpp730
-rwxr-xr-xcodec/ALACDecoder.h69
-rwxr-xr-xcodec/ALACEncoder.cpp1425
-rwxr-xr-xcodec/ALACEncoder.h96
-rwxr-xr-xcodec/APPLE_LICENSE.txt335
-rwxr-xr-xcodec/EndianPortable.c175
-rwxr-xr-xcodec/EndianPortable.h63
-rwxr-xr-xcodec/ReadMe.txt22
-rwxr-xr-xcodec/ag_dec.c362
-rwxr-xr-xcodec/ag_enc.c370
-rwxr-xr-xcodec/aglib.h85
-rwxr-xr-xcodec/alac.sln20
-rwxr-xr-xcodec/alac.vcproj377
-rwxr-xr-xcodec/dp_dec.c381
-rwxr-xr-xcodec/dp_enc.c386
-rwxr-xr-xcodec/dplib.h65
-rwxr-xr-xcodec/makefile79
-rwxr-xr-xcodec/matrix_dec.c390
-rwxr-xr-xcodec/matrix_enc.c342
-rwxr-xr-xcodec/matrixlib.h84
-rwxr-xr-xcodec/msstdint.h222
-rwxr-xr-xconvert-utility/CAFFileALAC.cpp456
-rwxr-xr-xconvert-utility/CAFFileALAC.h209
30 files changed, 7891 insertions, 0 deletions
diff --git a/ALACMagicCookieDescription.txt b/ALACMagicCookieDescription.txt
new file mode 100755
index 0000000..e924542
--- /dev/null
+++ b/ALACMagicCookieDescription.txt
@@ -0,0 +1,241 @@
+__________________________________________________________________________________________________________________________________
+__________________________________________________________________________________________________________________________________
+Apple Lossless Format "Magic Cookie" Description
+__________________________________________________________________________________________________________________________________
+__________________________________________________________________________________________________________________________________
+
+Many encoded formats for audio require additional, codec specific configuration information in order to operate successfully.
+This codec specific information is often called a 'magic cookie'. The Apple Lossless codec's 'magic cookie' contains the
+ALACSpecificConfig and optional ALACChannelLayoutInfo (both described below).
+
+The 'magic cookie' must accompany the bitstream when stored in any file container (M4A/MP4, CAF) so that it may be provided to the
+decoder when decoding the bitstream. From the caller's perspective, the 'magic cookie' is opaque and should be stored in the file
+and presented to the decoder exactly as it is vended from the encoder (and consequently stored in the file).
+
+The ALAC 'magic cookie' as stored in a file has all fields described in big-endian order (regardless of file format).
+
+The layout of the 'magic cookie' is as follows:
+
+
+---------------- ALAC Specific Info (24 bytes) (mandatory) ---------------------------
+(ALACSpecificConfig) Decoder Info
+
+---------------- Channel Layout Info (24 bytes) (optional) ----------------------------
+(ALAC Channel Layout Info) Channel Layout Info
+
+
+If the channel layout is absent from the cookie, then the following assumptions are made:
+1 channel - mono
+2 channels - stereo in left, right order
+> 2 channels - no specific channel designation or role.
+
+
+__________________________________________________________________________________________________________________________________
+* ALAC Specific Info (24 bytes) (mandatory)
+__________________________________________________________________________________________________________________________________
+
+The Apple Lossless codec stores specific information about the encoded stream in the ALACSpecificConfig. This
+info is vended by the encoder and is used to setup the decoder for a given encoded bitstream.
+
+When read from and written to a file, the fields of this struct must be in big-endian order.
+When vended by the encoder (and received by the decoder) the struct values will be in big-endian order.
+
+/*
+ struct ALACSpecificConfig (defined in ALACAudioTypes.h)
+ abstract This struct is used to describe codec provided information about the encoded Apple Lossless bitstream.
+ It must accompany the encoded stream in the containing audio file and be provided to the decoder.
+
+ field frameLength uint32_t indicating the frames per packet when no explicit frames per packet setting is
+ present in the packet header. The encoder frames per packet can be explicitly set
+ but for maximum compatibility, the default encoder setting of 4096 should be used.
+
+ field compatibleVersion uint8_t indicating compatible version,
+ value must be set to 0
+
+ field bitDepth uint8_t describes the bit depth of the source PCM data (maximum value = 32)
+
+ field pb uint8_t currently unused tuning parameter.
+ value should be set to 40
+
+ field mb uint8_t currently unused tuning parameter.
+ value should be set to 14
+
+ field kb uint8_t currently unused tuning parameter.
+ value should be set to 10
+
+ field numChannels uint8_t describes the channel count (1 = mono, 2 = stereo, etc...)
+ when channel layout info is not provided in the 'magic cookie', a channel count > 2
+ describes a set of discreet channels with no specific ordering
+
+ field maxRun uint16_t currently unused.
+ value should be set to 255
+
+ field maxFrameBytes uint32_t the maximum size of an Apple Lossless packet within the encoded stream.
+ value of 0 indicates unknown
+
+ field avgBitRate uint32_t the average bit rate in bits per second of the Apple Lossless stream.
+ value of 0 indicates unknown
+
+ field sampleRate uint32_t sample rate of the encoded stream
+ */
+
+typedef struct ALACSpecificConfig
+{
+ uint32_t frameLength;
+ uint8_t compatibleVersion;
+ uint8_t bitDepth;
+ uint8_t pb;
+ uint8_t mb;
+ uint8_t kb;
+ uint8_t numChannels;
+ uint16_t maxRun;
+ uint32_t maxFrameBytes;
+ uint32_t avgBitRate;
+ uint32_t sampleRate;
+
+} ALACSpecificConfig;
+
+
+__________________________________________________________________________________________________________________________________
+Channel Layout Info (24 bytes) (optional)
+__________________________________________________________________________________________________________________________________
+
+The Apple Lossless codec can support a specific set of channel layouts. When channel information is vended
+by the encoder (in the 'magic cookie'), it is formatted in the the ALACChannelLayoutInfo.
+
+When read from and written to a file, the fields of this struct must be in big-endian order.
+When vended by the encoder (and received by the decoder) the struct values will be in big-endian order.
+
+/*
+ struct ALACChannelLayoutInfo (defined in ALACAudioTypes.h)
+ abstract This struct is used to specify particular channel orderings or configurations.
+ It is an optional portion of the 'magic cookie', being required to describe specific channel layouts (see below)
+ of more than 2 channels.
+
+ field channelLayoutInfoSize uint32_t indicates the size of the channel layout data
+ value should be set to 24
+
+ field channelLayoutInfoID uint32_t identifier indicating that channel layout info is present
+ value = 'chan'
+
+ field versionFlags uint32_t version flags
+ value should be set to 0
+
+ field channelLayoutTag uint32_t channel layout type
+ from defined list in ALACAudioTypes.h (see below)
+
+ field reserved1 uint32_t currently unused field
+ value should be set to 0
+
+ field reserved2 uint32_t currently unused field
+ value should be set to 0
+*/
+
+typedef struct ALACChannelLayoutInfo
+{
+ uint32_t channelLayoutInfoSize;
+ uint32_t channelLayoutInfoID;
+ uint32_t versionFlags;
+ uint32_t channelLayoutTag;
+ uint32_t reserved1;
+ uint32_t reserved2;
+} ALACChannelLayoutInfo;
+
+
+* Channel Layout Tags
+
+These constants will be used to describe the bitstream's channel layout. (defined in ALACAudioTypes.h)
+
+enum
+{
+ kALACChannelLayoutTag_Mono = (100<<16) | 1, // C
+ kALACChannelLayoutTag_Stereo = (101<<16) | 2, // L R
+ kALACChannelLayoutTag_MPEG_3_0_B = (113<<16) | 3, // C L R
+ kALACChannelLayoutTag_MPEG_4_0_B = (116<<16) | 4, // C L R Cs
+ kALACChannelLayoutTag_MPEG_5_0_D = (120<<16) | 5, // C L R Ls Rs
+ kALACChannelLayoutTag_MPEG_5_1_D = (124<<16) | 6, // C L R Ls Rs LFE
+ kALACChannelLayoutTag_AAC_6_1 = (142<<16) | 7, // C L R Ls Rs Cs LFE
+ kALACChannelLayoutTag_MPEG_7_1_B = (127<<16) | 8 // C Lc Rc L R Ls Rs LFE (doc: IS-13818-7 MPEG2-AAC)
+};
+
+
+__________________________________________________________________________________________________________________________________
+__________________________________________________________________________________________________________________________________
+* Storing Apple Lossless Magic Cookie in Audio Files
+__________________________________________________________________________________________________________________________________
+__________________________________________________________________________________________________________________________________
+
+The Apple Lossless Magic Cookie is treated as opaque by file parsing code. The 'magic cookie' vended by the encoder
+is placed without modification into the audio file and the read from that file and passed (unmodified) to the decoder.
+
+__________________________________________________________________________________________________________________________________
+* CAF File
+
+In a CAF file (Core Audio File), the 'magic cookie' is stored in CAF's Magic Cookie chunk ('kuki').
+
+__________________________________________________________________________________________________________________________________
+* MP4/M4A File
+
+In an MP4/M4A file, the 'magic cookie' is encapsulated in the AudioSample entry of a Sound Description box ('stsd').
+An ISO style full box header to describe the ALACSpecificConfig portion is appended to the AudioSampleEntry, followed by the
+'magic cookie' as it is vended by the encoder.
+
+(All fields are stored in big-endian order: see ISO/IEC 14496-12 for a full description of the SoundDescription and AudioSampleEntry boxes, etc.)
+
+---------------- SoundDescriptionBox (FullBox) ----------------------------
+
+ SampleEntrySize // = sizeof(SoundDescriptionBox)(16) + sizeof (AudioSampleEntry)(AudioSampleEntry.SampleEntrySize)
+ SampleEntryType // = 'stsd'
+ VersionFlags // = 0
+ EntryCount // = 1
+
+---------------- Audio Sample Entry (REQUIRED) -----------------------------
+
+ SampleEntrySize // sizeof(AudioSampleEntry)(36) + sizeof(full ISO box header)(12) + sizeof(Apple Lossless Magic Cookie)
+ SampleEntryType // = 'alac', specifies that the AudioSampleEntry describes an Apple Lossless bitstream
+ mReserved[6] // = 0
+ dref index // = 1
+ reserved[2] // = 0
+ channel count // = number of channels as a uint_16 value
+ sample size // = source pcm bitdepth (example = 16bit source pcm)
+ predefined // = 0
+ reserved // = 0
+ sample rate // sample rate as a uint_32 value
+
+ Appended to AudioSampleEntry:
+
+ ALAC Specific Info Size // uint_32 value, = 36 (12 + sizeof(ALACSpecificConfig))
+ ALAC Specific Info ID // uint_32 value, = 'alac', format ID which matches the Audio Sample Entry SampleEntryType field
+ Version Flags // uint_32 value, = 0
+
+ Apple Lossless Magic Cookie // 'magic cookie' vended from ALAC encoder (24 or 48 Bytes)
+
+__________________________________________________________________________________________________________________________________
+__________________________________________________________________________________________________________________________________
+* Compatibility
+__________________________________________________________________________________________________________________________________
+__________________________________________________________________________________________________________________________________
+
+Previous versions of the Apple Lossless encoder vended a different 'magic cookie'. To ensure compatibility, the Apple Lossless decoder
+must be prepared to parse a 'magic cookie' in the format described below. Note that the 'magic cookie' defined above is
+encapsulated in the following method and can be extracted as a contiguous set of bytes.
+
+
+---------------- Format Atom (12 bytes) --------------------------------
+(uint_32) Format Atom Size // = 12
+(uint_32) Channel Layout Info ID // = 'frma'
+(uint_32) Format Type // = 'alac'
+
+---------------- ALAC Specific Info (36 bytes) (required) --------------
+(uint_32) ALAC Specific Info Size // = 36 (12 + sizeof(ALACSpecificConfig))
+(uint_32) ALAC Specific Info ID // = 'alac', format ID which matches the Audio Sample Entry SampleEntryType field
+(uint_32) Version Flags // = 0
+
+ [ Apple Lossless Magic Cookie (see above) ]
+
+---------------- Terminator Atom (8 bytes) -----------------------------
+(uint_32) Channel Layout Info Size // = 8
+(uint_32) Channel Layout Info ID // = 0
+
+
+
diff --git a/LICENSE b/LICENSE
new file mode 100755
index 0000000..c9500ba
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,53 @@
+Apache License
+
+Version 2.0, January 2004
+
+http://www.apache.org/licenses/
+
+TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+1. Definitions.
+
+"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
+
+"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
+
+"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
+
+"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
+
+"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
+
+"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
+
+"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
+
+"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
+
+"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
+
+"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
+
+2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
+
+3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
+
+4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
+
+You must give any other recipients of the Work or Derivative Works a copy of this License; and
+
+You must cause any modified files to carry prominent notices stating that You changed the files; and
+
+You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
+
+If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
+
+5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
+
+6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
+
+7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
+
+8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
+
+9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
diff --git a/ReadMe.txt b/ReadMe.txt
new file mode 100755
index 0000000..375255c
--- /dev/null
+++ b/ReadMe.txt
@@ -0,0 +1,44 @@
+- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+The Apple Lossless Format
+- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+
+Apple Lossless supports the following features. Not all of these are implemented in alacconvert, though they are in the codec code provided.
+
+1. Bit depths 16, 20, 24 and 32 bits.
+2. Any arbitrary integer sample rate from 1 to 384,000 Hz. In theory rates up to 4,294,967,295 (2^32 - 1) Hz could be supported.
+3. From one to eight channels are supported. Channel orders for the supported formats are described as:
+ Num Chan Order
+ 1 mono
+ 2 stereo (Left, Right)
+ 3 MPEG 3.0 B (Center, Left, Right)
+ 4 MPEG 4.0 B (Center, Left, Right, Center Surround)
+ 5 MPEG 5.0 D (Center, Left, Right, Left Surround, Right Surround)
+ 6 MPEG 5.1 D (Center, Left, Right, Left Surround, Right Surround, Low Frequency Effects)
+ 7 Apple AAC 6.1 (Center, Left, Right, Left Surround, Right Surround, Center Surround, Low Frequency Effects)
+ 8 MPEG 7.1 B (Center, Left Center, Right Center, Left, Right, Left Surround, Right Surround, Low Frequency Effects)
+4. Packet size defaults to 4096 sample frames of audio per packet. Other packet sizes are certainly possible. However, non-default packet sizes are not guaranteed to work properly on all hardware devices that support Apple Lossless. Packets above 16,384 sample frames are not supported.
+
+
+
+- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+This package contains the sources for the Apple Lossless (ALAC) encoder and decoder.
+
+The "codec" directory contains all the sources necessary for a functioning codec. Also includes is a makefile that will build libalac.a on a UNIX/Linux machine.
+
+- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+ALACconvert
+
+The convert-utility directory contains sources to build alacconvert which is a simple utility that demonstrates how to use the included ALAC encoder and decoder.
+
+alacconvert supports the following formats:
+
+1. 16- or 24-bit mono or stereo .wav files where the data is little endian integer. Extended WAVE format chunks are not handled.
+2. 16- or 24-bit mono or stereo .caf (Core Audio Format) files as well as certain multi-channel configurations where the data is big or little endian integer. It does no channel order manipulation.
+3. ALAC .caf files.
+
+Three project are provided to build a command line utility called alacconvert that converts cpm data to ALAC or vice versa. A Mac OS X Xcode project, A Windows Visual Studio project, and a generic UNIX/Linux make file.
+
+Note: When building on Windows, if you are using a version of Visual Studio before Visual Studio 2010, <stdint.h> is not installed. You will need to acquire this file on your own. It can be put in the same directory as the project.
+
+
+
diff --git a/codec/ALACAudioTypes.h b/codec/ALACAudioTypes.h
new file mode 100755
index 0000000..0e90595
--- /dev/null
+++ b/codec/ALACAudioTypes.h
@@ -0,0 +1,201 @@
+/*
+ * Copyright (c) 2011 Apple Inc. All rights reserved.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_START@
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_END@
+ */
+
+/*
+ File: ALACAudioTypes.h
+*/
+
+#ifndef ALACAUDIOTYPES_H
+#define ALACAUDIOTYPES_H
+
+#if PRAGMA_ONCE
+#pragma once
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#if PRAGMA_STRUCT_ALIGN
+ #pragma options align=mac68k
+#elif PRAGMA_STRUCT_PACKPUSH
+ #pragma pack(push, 2)
+#elif PRAGMA_STRUCT_PACK
+ #pragma pack(2)
+#endif
+
+#ifdef _MSC_VER
+#include <msstdint.h>
+#else
+#include <stdint.h>
+#endif
+
+#if defined(__ppc__)
+#define TARGET_RT_BIG_ENDIAN 1
+#elif defined(__ppc64__)
+#define TARGET_RT_BIG_ENDIAN 1
+#endif
+
+#define kChannelAtomSize 12
+
+enum
+{
+ kALAC_UnimplementedError = -4,
+ kALAC_FileNotFoundError = -43,
+ kALAC_ParamError = -50,
+ kALAC_MemFullError = -108
+};
+
+enum
+{
+ kALACFormatAppleLossless = 'alac',
+ kALACFormatLinearPCM = 'lpcm'
+};
+
+enum
+{
+ kALACMaxChannels = 8,
+ kALACMaxEscapeHeaderBytes = 8,
+ kALACMaxSearches = 16,
+ kALACMaxCoefs = 16,
+ kALACDefaultFramesPerPacket = 4096
+};
+
+typedef uint32_t ALACChannelLayoutTag;
+
+enum
+{
+ kALACFormatFlagIsFloat = (1 << 0), // 0x1
+ kALACFormatFlagIsBigEndian = (1 << 1), // 0x2
+ kALACFormatFlagIsSignedInteger = (1 << 2), // 0x4
+ kALACFormatFlagIsPacked = (1 << 3), // 0x8
+ kALACFormatFlagIsAlignedHigh = (1 << 4), // 0x10
+};
+
+enum
+{
+#if TARGET_RT_BIG_ENDIAN
+ kALACFormatFlagsNativeEndian = kALACFormatFlagIsBigEndian
+#else
+ kALACFormatFlagsNativeEndian = 0
+#endif
+};
+
+// this is required to be an IEEE 64bit float
+typedef double alac_float64_t;
+
+// These are the Channel Layout Tags used in the Channel Layout Info portion of the ALAC magic cookie
+enum
+{
+ kALACChannelLayoutTag_Mono = (100<<16) | 1, // C
+ kALACChannelLayoutTag_Stereo = (101<<16) | 2, // L R
+ kALACChannelLayoutTag_MPEG_3_0_B = (113<<16) | 3, // C L R
+ kALACChannelLayoutTag_MPEG_4_0_B = (116<<16) | 4, // C L R Cs
+ kALACChannelLayoutTag_MPEG_5_0_D = (120<<16) | 5, // C L R Ls Rs
+ kALACChannelLayoutTag_MPEG_5_1_D = (124<<16) | 6, // C L R Ls Rs LFE
+ kALACChannelLayoutTag_AAC_6_1 = (142<<16) | 7, // C L R Ls Rs Cs LFE
+ kALACChannelLayoutTag_MPEG_7_1_B = (127<<16) | 8 // C Lc Rc L R Ls Rs LFE (doc: IS-13818-7 MPEG2-AAC)
+};
+
+// ALAC currently only utilizes these channels layouts. There is a one for one correspondance between a
+// given number of channels and one of these layout tags
+static const ALACChannelLayoutTag ALACChannelLayoutTags[kALACMaxChannels] =
+{
+ kALACChannelLayoutTag_Mono, // C
+ kALACChannelLayoutTag_Stereo, // L R
+ kALACChannelLayoutTag_MPEG_3_0_B, // C L R
+ kALACChannelLayoutTag_MPEG_4_0_B, // C L R Cs
+ kALACChannelLayoutTag_MPEG_5_0_D, // C L R Ls Rs
+ kALACChannelLayoutTag_MPEG_5_1_D, // C L R Ls Rs LFE
+ kALACChannelLayoutTag_AAC_6_1, // C L R Ls Rs Cs LFE
+ kALACChannelLayoutTag_MPEG_7_1_B // C Lc Rc L R Ls Rs LFE (doc: IS-13818-7 MPEG2-AAC)
+};
+
+// AudioChannelLayout from CoreAudioTypes.h. We never need the AudioChannelDescription so we remove it
+struct ALACAudioChannelLayout
+{
+ ALACChannelLayoutTag mChannelLayoutTag;
+ uint32_t mChannelBitmap;
+ uint32_t mNumberChannelDescriptions;
+};
+typedef struct ALACAudioChannelLayout ALACAudioChannelLayout;
+
+struct AudioFormatDescription
+{
+ alac_float64_t mSampleRate;
+ uint32_t mFormatID;
+ uint32_t mFormatFlags;
+ uint32_t mBytesPerPacket;
+ uint32_t mFramesPerPacket;
+ uint32_t mBytesPerFrame;
+ uint32_t mChannelsPerFrame;
+ uint32_t mBitsPerChannel;
+ uint32_t mReserved;
+};
+typedef struct AudioFormatDescription AudioFormatDescription;
+
+/* Lossless Definitions */
+
+enum
+{
+ kALACCodecFormat = 'alac',
+ kALACVersion = 0,
+ kALACCompatibleVersion = kALACVersion,
+ kALACDefaultFrameSize = 4096
+};
+
+// note: this struct is wrapped in an 'alac' atom in the sample description extension area
+// note: in QT movies, it will be further wrapped in a 'wave' atom surrounded by 'frma' and 'term' atoms
+typedef struct ALACSpecificConfig
+{
+ uint32_t frameLength;
+ uint8_t compatibleVersion;
+ uint8_t bitDepth; // max 32
+ uint8_t pb; // 0 <= pb <= 255
+ uint8_t mb;
+ uint8_t kb;
+ uint8_t numChannels;
+ uint16_t maxRun;
+ uint32_t maxFrameBytes;
+ uint32_t avgBitRate;
+ uint32_t sampleRate;
+
+} ALACSpecificConfig;
+
+
+// The AudioChannelLayout atom type is not exposed yet so define it here
+enum
+{
+ AudioChannelLayoutAID = 'chan'
+};
+
+#if PRAGMA_STRUCT_ALIGN
+ #pragma options align=reset
+#elif PRAGMA_STRUCT_PACKPUSH
+ #pragma pack(pop)
+#elif PRAGMA_STRUCT_PACK
+ #pragma pack()
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* ALACAUDIOTYPES_H */
diff --git a/codec/ALACBitUtilities.c b/codec/ALACBitUtilities.c
new file mode 100755
index 0000000..9414889
--- /dev/null
+++ b/codec/ALACBitUtilities.c
@@ -0,0 +1,260 @@
+/*
+ * Copyright (c) 2011 Apple Inc. All rights reserved.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_START@
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_END@
+ */
+
+/*=============================================================================
+ File: ALACBitUtilities.c
+
+ $NoKeywords: $
+=============================================================================*/
+
+#include <stdio.h>
+#include "ALACBitUtilities.h"
+
+// BitBufferInit
+//
+void BitBufferInit( BitBuffer * bits, uint8_t * buffer, uint32_t byteSize )
+{
+ bits->cur = buffer;
+ bits->end = bits->cur + byteSize;
+ bits->bitIndex = 0;
+ bits->byteSize = byteSize;
+}
+
+// BitBufferRead
+//
+uint32_t BitBufferRead( BitBuffer * bits, uint8_t numBits )
+{
+ uint32_t returnBits;
+
+ //Assert( numBits <= 16 );
+
+ returnBits = ((uint32_t)bits->cur[0] << 16) | ((uint32_t)bits->cur[1] << 8) | ((uint32_t)bits->cur[2]);
+ returnBits = returnBits << bits->bitIndex;
+ returnBits &= 0x00FFFFFF;
+
+ bits->bitIndex += numBits;
+
+ returnBits = returnBits >> (24 - numBits);
+
+ bits->cur += (bits->bitIndex >> 3);
+ bits->bitIndex &= 7;
+
+ //Assert( bits->cur <= bits->end );
+
+ return returnBits;
+}
+
+// BitBufferReadSmall
+//
+// Reads up to 8 bits
+uint8_t BitBufferReadSmall( BitBuffer * bits, uint8_t numBits )
+{
+ uint16_t returnBits;
+
+ //Assert( numBits <= 8 );
+
+ returnBits = (bits->cur[0] << 8) | bits->cur[1];
+ returnBits = returnBits << bits->bitIndex;
+
+ bits->bitIndex += numBits;
+
+ returnBits = returnBits >> (16 - numBits);
+
+ bits->cur += (bits->bitIndex >> 3);
+ bits->bitIndex &= 7;
+
+ //Assert( bits->cur <= bits->end );
+
+ return (uint8_t)returnBits;
+}
+
+// BitBufferReadOne
+//
+// Reads one byte
+uint8_t BitBufferReadOne( BitBuffer * bits )
+{
+ uint8_t returnBits;
+
+ returnBits = (bits->cur[0] >> (7 - bits->bitIndex)) & 1;
+
+ bits->bitIndex++;
+
+ bits->cur += (bits->bitIndex >> 3);
+ bits->bitIndex &= 7;
+
+ //Assert( bits->cur <= bits->end );
+
+ return returnBits;
+}
+
+// BitBufferPeek
+//
+uint32_t BitBufferPeek( BitBuffer * bits, uint8_t numBits )
+{
+ return ((((((uint32_t) bits->cur[0] << 16) | ((uint32_t) bits->cur[1] << 8) |
+ ((uint32_t) bits->cur[2])) << bits->bitIndex) & 0x00FFFFFF) >> (24 - numBits));
+}
+
+// BitBufferPeekOne
+//
+uint32_t BitBufferPeekOne( BitBuffer * bits )
+{
+ return ((bits->cur[0] >> (7 - bits->bitIndex)) & 1);
+}
+
+// BitBufferUnpackBERSize
+//
+uint32_t BitBufferUnpackBERSize( BitBuffer * bits )
+{
+ uint32_t size;
+ uint8_t tmp;
+
+ for ( size = 0, tmp = 0x80u; tmp &= 0x80u; size = (size << 7u) | (tmp & 0x7fu) )
+ tmp = (uint8_t) BitBufferReadSmall( bits, 8 );
+
+ return size;
+}
+
+// BitBufferGetPosition
+//
+uint32_t BitBufferGetPosition( BitBuffer * bits )
+{
+ uint8_t * begin;
+
+ begin = bits->end - bits->byteSize;
+
+ return ((uint32_t)(bits->cur - begin) * 8) + bits->bitIndex;
+}
+
+// BitBufferByteAlign
+//
+void BitBufferByteAlign( BitBuffer * bits, int32_t addZeros )
+{
+ // align bit buffer to next byte boundary, writing zeros if requested
+ if ( bits->bitIndex == 0 )
+ return;
+
+ if ( addZeros )
+ BitBufferWrite( bits, 0, 8 - bits->bitIndex );
+ else
+ BitBufferAdvance( bits, 8 - bits->bitIndex );
+}
+
+// BitBufferAdvance
+//
+void BitBufferAdvance( BitBuffer * bits, uint32_t numBits )
+{
+ if ( numBits )
+ {
+ bits->bitIndex += numBits;
+ bits->cur += (bits->bitIndex >> 3);
+ bits->bitIndex &= 7;
+ }
+}
+
+// BitBufferRewind
+//
+void BitBufferRewind( BitBuffer * bits, uint32_t numBits )
+{
+ uint32_t numBytes;
+
+ if ( numBits == 0 )
+ return;
+
+ if ( bits->bitIndex >= numBits )
+ {
+ bits->bitIndex -= numBits;
+ return;
+ }
+
+ numBits -= bits->bitIndex;
+ bits->bitIndex = 0;
+
+ numBytes = numBits / 8;
+ numBits = numBits % 8;
+
+ bits->cur -= numBytes;
+
+ if ( numBits > 0 )
+ {
+ bits->bitIndex = 8 - numBits;
+ bits->cur--;
+ }
+
+ if ( bits->cur < (bits->end - bits->byteSize) )
+ {
+ //DebugCMsg("BitBufferRewind: Rewound too far.");
+
+ bits->cur = (bits->end - bits->byteSize);
+ bits->bitIndex = 0;
+ }
+}
+
+// BitBufferWrite
+//
+void BitBufferWrite( BitBuffer * bits, uint32_t bitValues, uint32_t numBits )
+{
+ uint32_t invBitIndex;
+
+ RequireAction( bits != nil, return; );
+ RequireActionSilent( numBits > 0, return; );
+
+ invBitIndex = 8 - bits->bitIndex;
+
+ while ( numBits > 0 )
+ {
+ uint32_t tmp;
+ uint8_t shift;
+ uint8_t mask;
+ uint32_t curNum;
+
+ curNum = MIN( invBitIndex, numBits );
+
+ tmp = bitValues >> (numBits - curNum);
+
+ shift = (uint8_t)(invBitIndex - curNum);
+ mask = 0xffu >> (8 - curNum); // must be done in two steps to avoid compiler sequencing ambiguity
+ mask <<= shift;
+
+ bits->cur[0] = (bits->cur[0] & ~mask) | (((uint8_t) tmp << shift) & mask);
+ numBits -= curNum;
+
+ // increment to next byte if need be
+ invBitIndex -= curNum;
+ if ( invBitIndex == 0 )
+ {
+ invBitIndex = 8;
+ bits->cur++;
+ }
+ }
+
+ bits->bitIndex = 8 - invBitIndex;
+}
+
+void BitBufferReset( BitBuffer * bits )
+//void BitBufferInit( BitBuffer * bits, uint8_t * buffer, uint32_t byteSize )
+{
+ bits->cur = bits->end - bits->byteSize;
+ bits->bitIndex = 0;
+}
+
+#if PRAGMA_MARK
+#pragma mark -
+#endif
diff --git a/codec/ALACBitUtilities.h b/codec/ALACBitUtilities.h
new file mode 100755
index 0000000..64d3dca
--- /dev/null
+++ b/codec/ALACBitUtilities.h
@@ -0,0 +1,108 @@
+/*
+ * Copyright (c) 2011 Apple Inc. All rights reserved.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_START@
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_END@
+ */
+
+/*=============================================================================
+ File: ALACBitUtilities.h
+
+ $NoKeywords: $
+=============================================================================*/
+
+#ifndef __ALACBITUTILITIES_H
+#define __ALACBITUTILITIES_H
+
+#ifdef _MSC_VER
+#include <msstdint.h>
+#else
+#include <stdint.h>
+#endif
+
+#ifndef MIN
+#define MIN(x, y) ( (x)<(y) ?(x) :(y) )
+#endif //MIN
+#ifndef MAX
+#define MAX(x, y) ( (x)>(y) ?(x): (y) )
+#endif //MAX
+
+#ifndef nil
+#define nil NULL
+#endif
+
+#define RequireAction(condition, action) if (!(condition)) { action }
+#define RequireActionSilent(condition, action) if (!(condition)) { action }
+#define RequireNoErr(condition, action) if ((condition)) { action }
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+enum
+{
+ ALAC_noErr = 0
+};
+
+
+typedef enum
+{
+
+ ID_SCE = 0, /* Single Channel Element */
+ ID_CPE = 1, /* Channel Pair Element */
+ ID_CCE = 2, /* Coupling Channel Element */
+ ID_LFE = 3, /* LFE Channel Element */
+ ID_DSE = 4, /* not yet supported */
+ ID_PCE = 5,
+ ID_FIL = 6,
+ ID_END = 7
+} ELEMENT_TYPE;
+
+// types
+typedef struct BitBuffer
+{
+ uint8_t * cur;
+ uint8_t * end;
+ uint32_t bitIndex;
+ uint32_t byteSize;
+
+} BitBuffer;
+
+/*
+ BitBuffer routines
+ - these routines take a fixed size buffer and read/write to it
+ - bounds checking must be done by the client
+*/
+void BitBufferInit( BitBuffer * bits, uint8_t * buffer, uint32_t byteSize );
+uint32_t BitBufferRead( BitBuffer * bits, uint8_t numBits ); // note: cannot read more than 16 bits at a time
+uint8_t BitBufferReadSmall( BitBuffer * bits, uint8_t numBits );
+uint8_t BitBufferReadOne( BitBuffer * bits );
+uint32_t BitBufferPeek( BitBuffer * bits, uint8_t numBits ); // note: cannot read more than 16 bits at a time
+uint32_t BitBufferPeekOne( BitBuffer * bits );
+uint32_t BitBufferUnpackBERSize( BitBuffer * bits );
+uint32_t BitBufferGetPosition( BitBuffer * bits );
+void BitBufferByteAlign( BitBuffer * bits, int32_t addZeros );
+void BitBufferAdvance( BitBuffer * bits, uint32_t numBits );
+void BitBufferRewind( BitBuffer * bits, uint32_t numBits );
+void BitBufferWrite( BitBuffer * bits, uint32_t value, uint32_t numBits );
+void BitBufferReset( BitBuffer * bits);
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __BITUTILITIES_H */
diff --git a/codec/ALACCodec/ALACCodec.xcodeproj/project.pbxproj b/codec/ALACCodec/ALACCodec.xcodeproj/project.pbxproj
new file mode 100644
index 0000000..9a93243
--- /dev/null
+++ b/codec/ALACCodec/ALACCodec.xcodeproj/project.pbxproj
@@ -0,0 +1,241 @@
+// !$*UTF8*$!
+{
+ archiveVersion = 1;
+ classes = {
+ };
+ objectVersion = 45;
+ objects = {
+
+/* Begin PBXBuildFile section */
+ 89D087EB14884F1D00C955C4 /* ag_dec.c in Sources */ = {isa = PBXBuildFile; fileRef = 89D087E114884F1D00C955C4 /* ag_dec.c */; };
+ 89D087EC14884F1D00C955C4 /* ag_enc.c in Sources */ = {isa = PBXBuildFile; fileRef = 89D087E214884F1D00C955C4 /* ag_enc.c */; };
+ 89D087ED14884F1D00C955C4 /* ALACBitUtilities.c in Sources */ = {isa = PBXBuildFile; fileRef = 89D087E314884F1D00C955C4 /* ALACBitUtilities.c */; };
+ 89D087EE14884F1D00C955C4 /* ALACDecoder.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 89D087E414884F1D00C955C4 /* ALACDecoder.cpp */; };
+ 89D087EF14884F1D00C955C4 /* ALACEncoder.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 89D087E514884F1D00C955C4 /* ALACEncoder.cpp */; };
+ 89D087F014884F1D00C955C4 /* dp_dec.c in Sources */ = {isa = PBXBuildFile; fileRef = 89D087E614884F1D00C955C4 /* dp_dec.c */; };
+ 89D087F114884F1D00C955C4 /* dp_enc.c in Sources */ = {isa = PBXBuildFile; fileRef = 89D087E714884F1D00C955C4 /* dp_enc.c */; };
+ 89D087F214884F1D00C955C4 /* EndianPortable.c in Sources */ = {isa = PBXBuildFile; fileRef = 89D087E814884F1D00C955C4 /* EndianPortable.c */; };
+ 89D087F314884F1D00C955C4 /* matrix_dec.c in Sources */ = {isa = PBXBuildFile; fileRef = 89D087E914884F1D00C955C4 /* matrix_dec.c */; };
+ 89D087F414884F1D00C955C4 /* matrix_enc.c in Sources */ = {isa = PBXBuildFile; fileRef = 89D087EA14884F1D00C955C4 /* matrix_enc.c */; };
+ 89D087FC14884F4100C955C4 /* aglib.h in Headers */ = {isa = PBXBuildFile; fileRef = 89D087F514884F4100C955C4 /* aglib.h */; };
+ 89D087FD14884F4100C955C4 /* ALACBitUtilities.h in Headers */ = {isa = PBXBuildFile; fileRef = 89D087F614884F4100C955C4 /* ALACBitUtilities.h */; };
+ 89D087FE14884F4100C955C4 /* ALACDecoder.h in Headers */ = {isa = PBXBuildFile; fileRef = 89D087F714884F4100C955C4 /* ALACDecoder.h */; };
+ 89D087FF14884F4100C955C4 /* ALACEncoder.h in Headers */ = {isa = PBXBuildFile; fileRef = 89D087F814884F4100C955C4 /* ALACEncoder.h */; };
+ 89D0880014884F4100C955C4 /* dplib.h in Headers */ = {isa = PBXBuildFile; fileRef = 89D087F914884F4100C955C4 /* dplib.h */; };
+ 89D0880114884F4100C955C4 /* EndianPortable.h in Headers */ = {isa = PBXBuildFile; fileRef = 89D087FA14884F4100C955C4 /* EndianPortable.h */; };
+ 89D0880214884F4100C955C4 /* matrixlib.h in Headers */ = {isa = PBXBuildFile; fileRef = 89D087FB14884F4100C955C4 /* matrixlib.h */; };
+/* End PBXBuildFile section */
+
+/* Begin PBXFileReference section */
+ 89D087E114884F1D00C955C4 /* ag_dec.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = ag_dec.c; path = ../ag_dec.c; sourceTree = SOURCE_ROOT; };
+ 89D087E214884F1D00C955C4 /* ag_enc.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = ag_enc.c; path = ../ag_enc.c; sourceTree = SOURCE_ROOT; };
+ 89D087E314884F1D00C955C4 /* ALACBitUtilities.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = ALACBitUtilities.c; path = ../ALACBitUtilities.c; sourceTree = SOURCE_ROOT; };
+ 89D087E414884F1D00C955C4 /* ALACDecoder.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ALACDecoder.cpp; path = ../ALACDecoder.cpp; sourceTree = SOURCE_ROOT; };
+ 89D087E514884F1D00C955C4 /* ALACEncoder.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ALACEncoder.cpp; path = ../ALACEncoder.cpp; sourceTree = SOURCE_ROOT; };
+ 89D087E614884F1D00C955C4 /* dp_dec.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = dp_dec.c; path = ../dp_dec.c; sourceTree = SOURCE_ROOT; };
+ 89D087E714884F1D00C955C4 /* dp_enc.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = dp_enc.c; path = ../dp_enc.c; sourceTree = SOURCE_ROOT; };
+ 89D087E814884F1D00C955C4 /* EndianPortable.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = EndianPortable.c; path = ../EndianPortable.c; sourceTree = SOURCE_ROOT; };
+ 89D087E914884F1D00C955C4 /* matrix_dec.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = matrix_dec.c; path = ../matrix_dec.c; sourceTree = SOURCE_ROOT; };
+ 89D087EA14884F1D00C955C4 /* matrix_enc.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = matrix_enc.c; path = ../matrix_enc.c; sourceTree = SOURCE_ROOT; };
+ 89D087F514884F4100C955C4 /* aglib.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = aglib.h; path = ../aglib.h; sourceTree = SOURCE_ROOT; };
+ 89D087F614884F4100C955C4 /* ALACBitUtilities.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ALACBitUtilities.h; path = ../ALACBitUtilities.h; sourceTree = SOURCE_ROOT; };
+ 89D087F714884F4100C955C4 /* ALACDecoder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ALACDecoder.h; path = ../ALACDecoder.h; sourceTree = SOURCE_ROOT; };
+ 89D087F814884F4100C955C4 /* ALACEncoder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ALACEncoder.h; path = ../ALACEncoder.h; sourceTree = SOURCE_ROOT; };
+ 89D087F914884F4100C955C4 /* dplib.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = dplib.h; path = ../dplib.h; sourceTree = SOURCE_ROOT; };
+ 89D087FA14884F4100C955C4 /* EndianPortable.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = EndianPortable.h; path = ../EndianPortable.h; sourceTree = SOURCE_ROOT; };
+ 89D087FB14884F4100C955C4 /* matrixlib.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = matrixlib.h; path = ../matrixlib.h; sourceTree = SOURCE_ROOT; };
+ 89D088671488568E00C955C4 /* MacConfigExternalDebug.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; name = MacConfigExternalDebug.xcconfig; path = ../../../MacConfigExternalDebug.xcconfig; sourceTree = SOURCE_ROOT; };
+ 89D088681488568E00C955C4 /* MacConfigExternalRelease.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; name = MacConfigExternalRelease.xcconfig; path = ../../../MacConfigExternalRelease.xcconfig; sourceTree = SOURCE_ROOT; };
+ D2AAC046055464E500DB518D /* libALACCodec.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libALACCodec.a; sourceTree = BUILT_PRODUCTS_DIR; };
+/* End PBXFileReference section */
+
+/* Begin PBXFrameworksBuildPhase section */
+ D289987405E68DCB004EDB86 /* Frameworks */ = {
+ isa = PBXFrameworksBuildPhase;
+ buildActionMask = 2147483647;
+ files = (
+ );
+ runOnlyForDeploymentPostprocessing = 0;
+ };
+/* End PBXFrameworksBuildPhase section */
+
+/* Begin PBXGroup section */
+ 08FB7794FE84155DC02AAC07 /* ALACCodec */ = {
+ isa = PBXGroup;
+ children = (
+ 89D088671488568E00C955C4 /* MacConfigExternalDebug.xcconfig */,
+ 89D088681488568E00C955C4 /* MacConfigExternalRelease.xcconfig */,
+ 08FB7795FE84155DC02AAC07 /* Source */,
+ C6A0FF2B0290797F04C91782 /* Documentation */,
+ 1AB674ADFE9D54B511CA2CBB /* Products */,
+ );
+ name = ALACCodec;
+ sourceTree = "<group>";
+ };
+ 08FB7795FE84155DC02AAC07 /* Source */ = {
+ isa = PBXGroup;
+ children = (
+ 89D087F514884F4100C955C4 /* aglib.h */,
+ 89D087F614884F4100C955C4 /* ALACBitUtilities.h */,
+ 89D087F714884F4100C955C4 /* ALACDecoder.h */,
+ 89D087F814884F4100C955C4 /* ALACEncoder.h */,
+ 89D087F914884F4100C955C4 /* dplib.h */,
+ 89D087FA14884F4100C955C4 /* EndianPortable.h */,
+ 89D087FB14884F4100C955C4 /* matrixlib.h */,
+ 89D087E114884F1D00C955C4 /* ag_dec.c */,
+ 89D087E214884F1D00C955C4 /* ag_enc.c */,
+ 89D087E314884F1D00C955C4 /* ALACBitUtilities.c */,
+ 89D087E414884F1D00C955C4 /* ALACDecoder.cpp */,
+ 89D087E514884F1D00C955C4 /* ALACEncoder.cpp */,
+ 89D087E614884F1D00C955C4 /* dp_dec.c */,
+ 89D087E714884F1D00C955C4 /* dp_enc.c */,
+ 89D087E814884F1D00C955C4 /* EndianPortable.c */,
+ 89D087E914884F1D00C955C4 /* matrix_dec.c */,
+ 89D087EA14884F1D00C955C4 /* matrix_enc.c */,
+ );
+ name = Source;
+ sourceTree = "<group>";
+ };
+ 1AB674ADFE9D54B511CA2CBB /* Products */ = {
+ isa = PBXGroup;
+ children = (
+ D2AAC046055464E500DB518D /* libALACCodec.a */,
+ );
+ name = Products;
+ sourceTree = "<group>";
+ };
+ C6A0FF2B0290797F04C91782 /* Documentation */ = {
+ isa = PBXGroup;
+ children = (
+ );
+ name = Documentation;
+ sourceTree = "<group>";
+ };
+/* End PBXGroup section */
+
+/* Begin PBXHeadersBuildPhase section */
+ D2AAC043055464E500DB518D /* Headers */ = {
+ isa = PBXHeadersBuildPhase;
+ buildActionMask = 2147483647;
+ files = (
+ 89D087FC14884F4100C955C4 /* aglib.h in Headers */,
+ 89D087FD14884F4100C955C4 /* ALACBitUtilities.h in Headers */,
+ 89D087FE14884F4100C955C4 /* ALACDecoder.h in Headers */,
+ 89D087FF14884F4100C955C4 /* ALACEncoder.h in Headers */,
+ 89D0880014884F4100C955C4 /* dplib.h in Headers */,
+ 89D0880114884F4100C955C4 /* EndianPortable.h in Headers */,
+ 89D0880214884F4100C955C4 /* matrixlib.h in Headers */,
+ );
+ runOnlyForDeploymentPostprocessing = 0;
+ };
+/* End PBXHeadersBuildPhase section */
+
+/* Begin PBXNativeTarget section */
+ D2AAC045055464E500DB518D /* ALACCodec */ = {
+ isa = PBXNativeTarget;
+ buildConfigurationList = 1DEB91EB08733DB70010E9CD /* Build configuration list for PBXNativeTarget "ALACCodec" */;
+ buildPhases = (
+ D2AAC043055464E500DB518D /* Headers */,
+ D2AAC044055464E500DB518D /* Sources */,
+ D289987405E68DCB004EDB86 /* Frameworks */,
+ );
+ buildRules = (
+ );
+ dependencies = (
+ );
+ name = ALACCodec;
+ productName = ALACCodec;
+ productReference = D2AAC046055464E500DB518D /* libALACCodec.a */;
+ productType = "com.apple.product-type.library.static";
+ };
+/* End PBXNativeTarget section */
+
+/* Begin PBXProject section */
+ 08FB7793FE84155DC02AAC07 /* Project object */ = {
+ isa = PBXProject;
+ buildConfigurationList = 1DEB91EF08733DB70010E9CD /* Build configuration list for PBXProject "ALACCodec" */;
+ compatibilityVersion = "Xcode 3.1";
+ hasScannedForEncodings = 1;
+ mainGroup = 08FB7794FE84155DC02AAC07 /* ALACCodec */;
+ projectDirPath = "";
+ projectRoot = "";
+ targets = (
+ D2AAC045055464E500DB518D /* ALACCodec */,
+ );
+ };
+/* End PBXProject section */
+
+/* Begin PBXSourcesBuildPhase section */
+ D2AAC044055464E500DB518D /* Sources */ = {
+ isa = PBXSourcesBuildPhase;
+ buildActionMask = 2147483647;
+ files = (
+ 89D087EB14884F1D00C955C4 /* ag_dec.c in Sources */,
+ 89D087EC14884F1D00C955C4 /* ag_enc.c in Sources */,
+ 89D087ED14884F1D00C955C4 /* ALACBitUtilities.c in Sources */,
+ 89D087EE14884F1D00C955C4 /* ALACDecoder.cpp in Sources */,
+ 89D087EF14884F1D00C955C4 /* ALACEncoder.cpp in Sources */,
+ 89D087F014884F1D00C955C4 /* dp_dec.c in Sources */,
+ 89D087F114884F1D00C955C4 /* dp_enc.c in Sources */,
+ 89D087F214884F1D00C955C4 /* EndianPortable.c in Sources */,
+ 89D087F314884F1D00C955C4 /* matrix_dec.c in Sources */,
+ 89D087F414884F1D00C955C4 /* matrix_enc.c in Sources */,
+ );
+ runOnlyForDeploymentPostprocessing = 0;
+ };
+/* End PBXSourcesBuildPhase section */
+
+/* Begin XCBuildConfiguration section */
+ 1DEB91EC08733DB70010E9CD /* Debug */ = {
+ isa = XCBuildConfiguration;
+ buildSettings = {
+ PRODUCT_NAME = ALACCodec;
+ };
+ name = Debug;
+ };
+ 1DEB91ED08733DB70010E9CD /* Release */ = {
+ isa = XCBuildConfiguration;
+ buildSettings = {
+ PRODUCT_NAME = ALACCodec;
+ };
+ name = Release;
+ };
+ 1DEB91F008733DB70010E9CD /* Debug */ = {
+ isa = XCBuildConfiguration;
+ baseConfigurationReference = 89D088671488568E00C955C4 /* MacConfigExternalDebug.xcconfig */;
+ buildSettings = {
+ };
+ name = Debug;
+ };
+ 1DEB91F108733DB70010E9CD /* Release */ = {
+ isa = XCBuildConfiguration;
+ baseConfigurationReference = 89D088681488568E00C955C4 /* MacConfigExternalRelease.xcconfig */;
+ buildSettings = {
+ };
+ name = Release;
+ };
+/* End XCBuildConfiguration section */
+
+/* Begin XCConfigurationList section */
+ 1DEB91EB08733DB70010E9CD /* Build configuration list for PBXNativeTarget "ALACCodec" */ = {
+ isa = XCConfigurationList;
+ buildConfigurations = (
+ 1DEB91EC08733DB70010E9CD /* Debug */,
+ 1DEB91ED08733DB70010E9CD /* Release */,
+ );
+ defaultConfigurationIsVisible = 0;
+ defaultConfigurationName = Release;
+ };
+ 1DEB91EF08733DB70010E9CD /* Build configuration list for PBXProject "ALACCodec" */ = {
+ isa = XCConfigurationList;
+ buildConfigurations = (
+ 1DEB91F008733DB70010E9CD /* Debug */,
+ 1DEB91F108733DB70010E9CD /* Release */,
+ );
+ defaultConfigurationIsVisible = 0;
+ defaultConfigurationName = Release;
+ };
+/* End XCConfigurationList section */
+ };
+ rootObject = 08FB7793FE84155DC02AAC07 /* Project object */;
+}
diff --git a/codec/ALACDecoder.cpp b/codec/ALACDecoder.cpp
new file mode 100755
index 0000000..faf870a
--- /dev/null
+++ b/codec/ALACDecoder.cpp
@@ -0,0 +1,730 @@
+/*
+ * Copyright (c) 2011 Apple Inc. All rights reserved.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_START@
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_END@
+ */
+
+/*
+ File: ALACDecoder.cpp
+*/
+
+#include <stdlib.h>
+#include <string.h>
+
+#include "ALACDecoder.h"
+
+#include "dplib.h"
+#include "aglib.h"
+#include "matrixlib.h"
+
+#include "ALACBitUtilities.h"
+#include "EndianPortable.h"
+
+// constants/data
+const uint32_t kMaxBitDepth = 32; // max allowed bit depth is 32
+
+
+// prototypes
+static void Zero16( int16_t * buffer, uint32_t numItems, uint32_t stride );
+static void Zero24( uint8_t * buffer, uint32_t numItems, uint32_t stride );
+static void Zero32( int32_t * buffer, uint32_t numItems, uint32_t stride );
+
+/*
+ Constructor
+*/
+ALACDecoder::ALACDecoder() :
+ mMixBufferU( nil ),
+ mMixBufferV( nil ),
+ mPredictor( nil ),
+ mShiftBuffer( nil )
+{
+ memset( &mConfig, 0, sizeof(mConfig) );
+}
+
+/*
+ Destructor
+*/
+ALACDecoder::~ALACDecoder()
+{
+ // delete the matrix mixing buffers
+ if ( mMixBufferU )
+ {
+ free(mMixBufferU);
+ mMixBufferU = NULL;
+ }
+ if ( mMixBufferV )
+ {
+ free(mMixBufferV);
+ mMixBufferV = NULL;
+ }
+
+ // delete the dynamic predictor's "corrector" buffer
+ // - note: mShiftBuffer shares memory with this buffer
+ if ( mPredictor )
+ {
+ free(mPredictor);
+ mPredictor = NULL;
+ }
+}
+
+/*
+ Init()
+ - initialize the decoder with the given configuration
+*/
+int32_t ALACDecoder::Init( void * inMagicCookie, uint32_t inMagicCookieSize )
+{
+ int32_t status = ALAC_noErr;
+ ALACSpecificConfig theConfig;
+ uint8_t * theActualCookie = (uint8_t *)inMagicCookie;
+ uint32_t theCookieBytesRemaining = inMagicCookieSize;
+
+ // For historical reasons the decoder needs to be resilient to magic cookies vended by older encoders.
+ // As specified in the ALACMagicCookieDescription.txt document, there may be additional data encapsulating
+ // the ALACSpecificConfig. This would consist of format ('frma') and 'alac' atoms which precede the
+ // ALACSpecificConfig.
+ // See ALACMagicCookieDescription.txt for additional documentation concerning the 'magic cookie'
+
+ // skip format ('frma') atom if present
+ if (theActualCookie[4] == 'f' && theActualCookie[5] == 'r' && theActualCookie[6] == 'm' && theActualCookie[7] == 'a')
+ {
+ theActualCookie += 12;
+ theCookieBytesRemaining -= 12;
+ }
+
+ // skip 'alac' atom header if present
+ if (theActualCookie[4] == 'a' && theActualCookie[5] == 'l' && theActualCookie[6] == 'a' && theActualCookie[7] == 'c')
+ {
+ theActualCookie += 12;
+ theCookieBytesRemaining -= 12;
+ }
+
+ // read the ALACSpecificConfig
+ if (theCookieBytesRemaining >= sizeof(ALACSpecificConfig))
+ {
+ theConfig.frameLength = Swap32BtoN(((ALACSpecificConfig *)theActualCookie)->frameLength);
+ theConfig.compatibleVersion = ((ALACSpecificConfig *)theActualCookie)->compatibleVersion;
+ theConfig.bitDepth = ((ALACSpecificConfig *)theActualCookie)->bitDepth;
+ theConfig.pb = ((ALACSpecificConfig *)theActualCookie)->pb;
+ theConfig.mb = ((ALACSpecificConfig *)theActualCookie)->mb;
+ theConfig.kb = ((ALACSpecificConfig *)theActualCookie)->kb;
+ theConfig.numChannels = ((ALACSpecificConfig *)theActualCookie)->numChannels;
+ theConfig.maxRun = Swap16BtoN(((ALACSpecificConfig *)theActualCookie)->maxRun);
+ theConfig.maxFrameBytes = Swap32BtoN(((ALACSpecificConfig *)theActualCookie)->maxFrameBytes);
+ theConfig.avgBitRate = Swap32BtoN(((ALACSpecificConfig *)theActualCookie)->avgBitRate);
+ theConfig.sampleRate = Swap32BtoN(((ALACSpecificConfig *)theActualCookie)->sampleRate);
+
+ mConfig = theConfig;
+
+ RequireAction( mConfig.compatibleVersion <= kALACVersion, return kALAC_ParamError; );
+
+ // allocate mix buffers
+ mMixBufferU = (int32_t *) calloc( mConfig.frameLength * sizeof(int32_t), 1 );
+ mMixBufferV = (int32_t *) calloc( mConfig.frameLength * sizeof(int32_t), 1 );
+
+ // allocate dynamic predictor buffer
+ mPredictor = (int32_t *) calloc( mConfig.frameLength * sizeof(int32_t), 1 );
+
+ // "shift off" buffer shares memory with predictor buffer
+ mShiftBuffer = (uint16_t *) mPredictor;
+
+ RequireAction( (mMixBufferU != nil) && (mMixBufferV != nil) && (mPredictor != nil),
+ status = kALAC_MemFullError; goto Exit; );
+ }
+ else
+ {
+ status = kALAC_ParamError;
+ }
+
+ // skip to Channel Layout Info
+ // theActualCookie += sizeof(ALACSpecificConfig);
+
+ // Currently, the Channel Layout Info portion of the magic cookie (as defined in the
+ // ALACMagicCookieDescription.txt document) is unused by the decoder.
+
+Exit:
+ return status;
+}
+
+/*
+ Decode()
+ - the decoded samples are interleaved into the output buffer in the order they arrive in
+ the bitstream
+*/
+int32_t ALACDecoder::Decode( BitBuffer * bits, uint8_t * sampleBuffer, uint32_t numSamples, uint32_t numChannels, uint32_t * outNumSamples )
+{
+ BitBuffer shiftBits;
+ uint32_t bits1, bits2;
+ uint8_t tag;
+ uint8_t elementInstanceTag;
+ AGParamRec agParams;
+ uint32_t channelIndex;
+ int16_t coefsU[32]; // max possible size is 32 although NUMCOEPAIRS is the current limit
+ int16_t coefsV[32];
+ uint8_t numU, numV;
+ uint8_t mixBits;
+ int8_t mixRes;
+ uint16_t unusedHeader;
+ uint8_t escapeFlag;
+ uint32_t chanBits;
+ uint8_t bytesShifted;
+ uint32_t shift;
+ uint8_t modeU, modeV;
+ uint32_t denShiftU, denShiftV;
+ uint16_t pbFactorU, pbFactorV;
+ uint16_t pb;
+ int16_t * samples;
+ int16_t * out16;
+ uint8_t * out20;
+ uint8_t * out24;
+ int32_t * out32;
+ uint8_t headerByte;
+ uint8_t partialFrame;
+ uint32_t extraBits;
+ int32_t val;
+ uint32_t i, j;
+ int32_t status;
+
+ RequireAction( (bits != nil) && (sampleBuffer != nil) && (outNumSamples != nil), return kALAC_ParamError; );
+ RequireAction( numChannels > 0, return kALAC_ParamError; );
+
+ mActiveElements = 0;
+ channelIndex = 0;
+
+ samples = (int16_t *) sampleBuffer;
+
+ status = ALAC_noErr;
+ *outNumSamples = numSamples;
+
+ while ( status == ALAC_noErr )
+ {
+ // bail if we ran off the end of the buffer
+ RequireAction( bits->cur < bits->end, status = kALAC_ParamError; goto Exit; );
+
+ // copy global decode params for this element
+ pb = mConfig.pb;
+
+ // read element tag
+ tag = BitBufferReadSmall( bits, 3 );
+ switch ( tag )
+ {
+ case ID_SCE:
+ case ID_LFE:
+ {
+ // mono/LFE channel
+ elementInstanceTag = BitBufferReadSmall( bits, 4 );
+ mActiveElements |= (1u << elementInstanceTag);
+
+ // read the 12 unused header bits
+ unusedHeader = (uint16_t) BitBufferRead( bits, 12 );
+ RequireAction( unusedHeader == 0, status = kALAC_ParamError; goto Exit; );
+
+ // read the 1-bit "partial frame" flag, 2-bit "shift-off" flag & 1-bit "escape" flag
+ headerByte = (uint8_t) BitBufferRead( bits, 4 );
+
+ partialFrame = headerByte >> 3;
+
+ bytesShifted = (headerByte >> 1) & 0x3u;
+ RequireAction( bytesShifted != 3, status = kALAC_ParamError; goto Exit; );
+
+ shift = bytesShifted * 8;
+
+ escapeFlag = headerByte & 0x1;
+
+ chanBits = mConfig.bitDepth - (bytesShifted * 8);
+
+ // check for partial frame to override requested numSamples
+ if ( partialFrame != 0 )
+ {
+ numSamples = BitBufferRead( bits, 16 ) << 16;
+ numSamples |= BitBufferRead( bits, 16 );
+ }
+
+ if ( escapeFlag == 0 )
+ {
+ // compressed frame, read rest of parameters
+ mixBits = (uint8_t) BitBufferRead( bits, 8 );
+ mixRes = (int8_t) BitBufferRead( bits, 8 );
+ //Assert( (mixBits == 0) && (mixRes == 0) ); // no mixing for mono
+
+ headerByte = (uint8_t) BitBufferRead( bits, 8 );
+ modeU = headerByte >> 4;
+ denShiftU = headerByte & 0xfu;
+
+ headerByte = (uint8_t) BitBufferRead( bits, 8 );
+ pbFactorU = headerByte >> 5;
+ numU = headerByte & 0x1fu;
+
+ for ( i = 0; i < numU; i++ )
+ coefsU[i] = (int16_t) BitBufferRead( bits, 16 );
+
+ // if shift active, skip the the shift buffer but remember where it starts
+ if ( bytesShifted != 0 )
+ {
+ shiftBits = *bits;
+ BitBufferAdvance( bits, (bytesShifted * 8) * numSamples );
+ }
+
+ // decompress
+ set_ag_params( &agParams, mConfig.mb, (pb * pbFactorU) / 4, mConfig.kb, numSamples, numSamples, mConfig.maxRun );
+ status = dyn_decomp( &agParams, bits, mPredictor, numSamples, chanBits, &bits1 );
+ RequireNoErr( status, goto Exit; );
+
+ if ( modeU == 0 )
+ {
+ unpc_block( mPredictor, mMixBufferU, numSamples, &coefsU[0], numU, chanBits, denShiftU );
+ }
+ else
+ {
+ // the special "numActive == 31" mode can be done in-place
+ unpc_block( mPredictor, mPredictor, numSamples, nil, 31, chanBits, 0 );
+ unpc_block( mPredictor, mMixBufferU, numSamples, &coefsU[0], numU, chanBits, denShiftU );
+ }
+ }
+ else
+ {
+ //Assert( bytesShifted == 0 );
+
+ // uncompressed frame, copy data into the mix buffer to use common output code
+ shift = 32 - chanBits;
+ if ( chanBits <= 16 )
+ {
+ for ( i = 0; i < numSamples; i++ )
+ {
+ val = (int32_t) BitBufferRead( bits, (uint8_t) chanBits );
+ val = (val << shift) >> shift;
+ mMixBufferU[i] = val;
+ }
+ }
+ else
+ {
+ // BitBufferRead() can't read more than 16 bits at a time so break up the reads
+ extraBits = chanBits - 16;
+ for ( i = 0; i < numSamples; i++ )
+ {
+ val = (int32_t) BitBufferRead( bits, 16 );
+ val = (val << 16) >> shift;
+ mMixBufferU[i] = val | BitBufferRead( bits, (uint8_t) extraBits );
+ }
+ }
+
+ mixBits = mixRes = 0;
+ bits1 = chanBits * numSamples;
+ bytesShifted = 0;
+ }
+
+ // now read the shifted values into the shift buffer
+ if ( bytesShifted != 0 )
+ {
+ shift = bytesShifted * 8;
+ //Assert( shift <= 16 );
+
+ for ( i = 0; i < numSamples; i++ )
+ mShiftBuffer[i] = (uint16_t) BitBufferRead( &shiftBits, (uint8_t) shift );
+ }
+
+ // convert 32-bit integers into output buffer
+ switch ( mConfig.bitDepth )
+ {
+ case 16:
+ out16 = &((int16_t *)sampleBuffer)[channelIndex];
+ for ( i = 0, j = 0; i < numSamples; i++, j += numChannels )
+ out16[j] = (int16_t) mMixBufferU[i];
+ break;
+ case 20:
+ out20 = (uint8_t *)sampleBuffer + (channelIndex * 3);
+ copyPredictorTo20( mMixBufferU, out20, numChannels, numSamples );
+ break;
+ case 24:
+ out24 = (uint8_t *)sampleBuffer + (channelIndex * 3);
+ if ( bytesShifted != 0 )
+ copyPredictorTo24Shift( mMixBufferU, mShiftBuffer, out24, numChannels, numSamples, bytesShifted );
+ else
+ copyPredictorTo24( mMixBufferU, out24, numChannels, numSamples );
+ break;
+ case 32:
+ out32 = &((int32_t *)sampleBuffer)[channelIndex];
+ if ( bytesShifted != 0 )
+ copyPredictorTo32Shift( mMixBufferU, mShiftBuffer, out32, numChannels, numSamples, bytesShifted );
+ else
+ copyPredictorTo32( mMixBufferU, out32, numChannels, numSamples);
+ break;
+ }
+
+ channelIndex += 1;
+ *outNumSamples = numSamples;
+ break;
+ }
+
+ case ID_CPE:
+ {
+ // if decoding this pair would take us over the max channels limit, bail
+ if ( (channelIndex + 2) > numChannels )
+ goto NoMoreChannels;
+
+ // stereo channel pair
+ elementInstanceTag = BitBufferReadSmall( bits, 4 );
+ mActiveElements |= (1u << elementInstanceTag);
+
+ // read the 12 unused header bits
+ unusedHeader = (uint16_t) BitBufferRead( bits, 12 );
+ RequireAction( unusedHeader == 0, status = kALAC_ParamError; goto Exit; );
+
+ // read the 1-bit "partial frame" flag, 2-bit "shift-off" flag & 1-bit "escape" flag
+ headerByte = (uint8_t) BitBufferRead( bits, 4 );
+
+ partialFrame = headerByte >> 3;
+
+ bytesShifted = (headerByte >> 1) & 0x3u;
+ RequireAction( bytesShifted != 3, status = kALAC_ParamError; goto Exit; );
+
+ shift = bytesShifted * 8;
+
+ escapeFlag = headerByte & 0x1;
+
+ chanBits = mConfig.bitDepth - (bytesShifted * 8) + 1;
+
+ // check for partial frame length to override requested numSamples
+ if ( partialFrame != 0 )
+ {
+ numSamples = BitBufferRead( bits, 16 ) << 16;
+ numSamples |= BitBufferRead( bits, 16 );
+ }
+
+ if ( escapeFlag == 0 )
+ {
+ // compressed frame, read rest of parameters
+ mixBits = (uint8_t) BitBufferRead( bits, 8 );
+ mixRes = (int8_t) BitBufferRead( bits, 8 );
+
+ headerByte = (uint8_t) BitBufferRead( bits, 8 );
+ modeU = headerByte >> 4;
+ denShiftU = headerByte & 0xfu;
+
+ headerByte = (uint8_t) BitBufferRead( bits, 8 );
+ pbFactorU = headerByte >> 5;
+ numU = headerByte & 0x1fu;
+ for ( i = 0; i < numU; i++ )
+ coefsU[i] = (int16_t) BitBufferRead( bits, 16 );
+
+ headerByte = (uint8_t) BitBufferRead( bits, 8 );
+ modeV = headerByte >> 4;
+ denShiftV = headerByte & 0xfu;
+
+ headerByte = (uint8_t) BitBufferRead( bits, 8 );
+ pbFactorV = headerByte >> 5;
+ numV = headerByte & 0x1fu;
+ for ( i = 0; i < numV; i++ )
+ coefsV[i] = (int16_t) BitBufferRead( bits, 16 );
+
+ // if shift active, skip the interleaved shifted values but remember where they start
+ if ( bytesShifted != 0 )
+ {
+ shiftBits = *bits;
+ BitBufferAdvance( bits, (bytesShifted * 8) * 2 * numSamples );
+ }
+
+ // decompress and run predictor for "left" channel
+ set_ag_params( &agParams, mConfig.mb, (pb * pbFactorU) / 4, mConfig.kb, numSamples, numSamples, mConfig.maxRun );
+ status = dyn_decomp( &agParams, bits, mPredictor, numSamples, chanBits, &bits1 );
+ RequireNoErr( status, goto Exit; );
+
+ if ( modeU == 0 )
+ {
+ unpc_block( mPredictor, mMixBufferU, numSamples, &coefsU[0], numU, chanBits, denShiftU );
+ }
+ else
+ {
+ // the special "numActive == 31" mode can be done in-place
+ unpc_block( mPredictor, mPredictor, numSamples, nil, 31, chanBits, 0 );
+ unpc_block( mPredictor, mMixBufferU, numSamples, &coefsU[0], numU, chanBits, denShiftU );
+ }
+
+ // decompress and run predictor for "right" channel
+ set_ag_params( &agParams, mConfig.mb, (pb * pbFactorV) / 4, mConfig.kb, numSamples, numSamples, mConfig.maxRun );
+ status = dyn_decomp( &agParams, bits, mPredictor, numSamples, chanBits, &bits2 );
+ RequireNoErr( status, goto Exit; );
+
+ if ( modeV == 0 )
+ {
+ unpc_block( mPredictor, mMixBufferV, numSamples, &coefsV[0], numV, chanBits, denShiftV );
+ }
+ else
+ {
+ // the special "numActive == 31" mode can be done in-place
+ unpc_block( mPredictor, mPredictor, numSamples, nil, 31, chanBits, 0 );
+ unpc_block( mPredictor, mMixBufferV, numSamples, &coefsV[0], numV, chanBits, denShiftV );
+ }
+ }
+ else
+ {
+ //Assert( bytesShifted == 0 );
+
+ // uncompressed frame, copy data into the mix buffers to use common output code
+ chanBits = mConfig.bitDepth;
+ shift = 32 - chanBits;
+ if ( chanBits <= 16 )
+ {
+ for ( i = 0; i < numSamples; i++ )
+ {
+ val = (int32_t) BitBufferRead( bits, (uint8_t) chanBits );
+ val = (val << shift) >> shift;
+ mMixBufferU[i] = val;
+
+ val = (int32_t) BitBufferRead( bits, (uint8_t) chanBits );
+ val = (val << shift) >> shift;
+ mMixBufferV[i] = val;
+ }
+ }
+ else
+ {
+ // BitBufferRead() can't read more than 16 bits at a time so break up the reads
+ extraBits = chanBits - 16;
+ for ( i = 0; i < numSamples; i++ )
+ {
+ val = (int32_t) BitBufferRead( bits, 16 );
+ val = (val << 16) >> shift;
+ mMixBufferU[i] = val | BitBufferRead( bits, (uint8_t)extraBits );
+
+ val = (int32_t) BitBufferRead( bits, 16 );
+ val = (val << 16) >> shift;
+ mMixBufferV[i] = val | BitBufferRead( bits, (uint8_t)extraBits );
+ }
+ }
+
+ bits1 = chanBits * numSamples;
+ bits2 = chanBits * numSamples;
+ mixBits = mixRes = 0;
+ bytesShifted = 0;
+ }
+
+ // now read the shifted values into the shift buffer
+ if ( bytesShifted != 0 )
+ {
+ shift = bytesShifted * 8;
+ //Assert( shift <= 16 );
+
+ for ( i = 0; i < (numSamples * 2); i += 2 )
+ {
+ mShiftBuffer[i + 0] = (uint16_t) BitBufferRead( &shiftBits, (uint8_t) shift );
+ mShiftBuffer[i + 1] = (uint16_t) BitBufferRead( &shiftBits, (uint8_t) shift );
+ }
+ }
+
+ // un-mix the data and convert to output format
+ // - note that mixRes = 0 means just interleave so we use that path for uncompressed frames
+ switch ( mConfig.bitDepth )
+ {
+ case 16:
+ out16 = &((int16_t *)sampleBuffer)[channelIndex];
+ unmix16( mMixBufferU, mMixBufferV, out16, numChannels, numSamples, mixBits, mixRes );
+ break;
+ case 20:
+ out20 = (uint8_t *)sampleBuffer + (channelIndex * 3);
+ unmix20( mMixBufferU, mMixBufferV, out20, numChannels, numSamples, mixBits, mixRes );
+ break;
+ case 24:
+ out24 = (uint8_t *)sampleBuffer + (channelIndex * 3);
+ unmix24( mMixBufferU, mMixBufferV, out24, numChannels, numSamples,
+ mixBits, mixRes, mShiftBuffer, bytesShifted );
+ break;
+ case 32:
+ out32 = &((int32_t *)sampleBuffer)[channelIndex];
+ unmix32( mMixBufferU, mMixBufferV, out32, numChannels, numSamples,
+ mixBits, mixRes, mShiftBuffer, bytesShifted );
+ break;
+ }
+
+ channelIndex += 2;
+ *outNumSamples = numSamples;
+ break;
+ }
+
+ case ID_CCE:
+ case ID_PCE:
+ {
+ // unsupported element, bail
+ //AssertNoErr( tag );
+ status = kALAC_ParamError;
+ break;
+ }
+
+ case ID_DSE:
+ {
+ // data stream element -- parse but ignore
+ status = this->DataStreamElement( bits );
+ break;
+ }
+
+ case ID_FIL:
+ {
+ // fill element -- parse but ignore
+ status = this->FillElement( bits );
+ break;
+ }
+
+ case ID_END:
+ {
+ // frame end, all done so byte align the frame and check for overruns
+ BitBufferByteAlign( bits, false );
+ //Assert( bits->cur == bits->end );
+ goto Exit;
+ }
+ }
+
+#if 0 //! DEBUG
+ // if we've decoded all of our channels, bail (but not in debug b/c we want to know if we're seeing bad bits)
+ // - this also protects us if the config does not match the bitstream or crap data bits follow the audio bits
+ if ( channelIndex >= numChannels )
+ break;
+#endif
+ }
+
+NoMoreChannels:
+
+ // if we get here and haven't decoded all of the requested channels, fill the remaining channels with zeros
+ for ( ; channelIndex < numChannels; channelIndex++ )
+ {
+ switch ( mConfig.bitDepth )
+ {
+ case 16:
+ {
+ int16_t * fill16 = &((int16_t *)sampleBuffer)[channelIndex];
+ Zero16( fill16, numSamples, numChannels );
+ break;
+ }
+ case 24:
+ {
+ uint8_t * fill24 = (uint8_t *)sampleBuffer + (channelIndex * 3);
+ Zero24( fill24, numSamples, numChannels );
+ break;
+ }
+ case 32:
+ {
+ int32_t * fill32 = &((int32_t *)sampleBuffer)[channelIndex];
+ Zero32( fill32, numSamples, numChannels );
+ break;
+ }
+ }
+ }
+
+Exit:
+ return status;
+}
+
+#if PRAGMA_MARK
+#pragma mark -
+#endif
+
+/*
+ FillElement()
+ - they're just filler so we don't need 'em
+*/
+int32_t ALACDecoder::FillElement( BitBuffer * bits )
+{
+ int16_t count;
+
+ // 4-bit count or (4-bit + 8-bit count) if 4-bit count == 15
+ // - plus this weird -1 thing I still don't fully understand
+ count = BitBufferReadSmall( bits, 4 );
+ if ( count == 15 )
+ count += (int16_t) BitBufferReadSmall( bits, 8 ) - 1;
+
+ BitBufferAdvance( bits, count * 8 );
+
+ RequireAction( bits->cur <= bits->end, return kALAC_ParamError; );
+
+ return ALAC_noErr;
+}
+
+/*
+ DataStreamElement()
+ - we don't care about data stream elements so just skip them
+*/
+int32_t ALACDecoder::DataStreamElement( BitBuffer * bits )
+{
+ uint8_t element_instance_tag;
+ int32_t data_byte_align_flag;
+ uint16_t count;
+
+ // the tag associates this data stream element with a given audio element
+ element_instance_tag = BitBufferReadSmall( bits, 4 );
+
+ data_byte_align_flag = BitBufferReadOne( bits );
+
+ // 8-bit count or (8-bit + 8-bit count) if 8-bit count == 255
+ count = BitBufferReadSmall( bits, 8 );
+ if ( count == 255 )
+ count += BitBufferReadSmall( bits, 8 );
+
+ // the align flag means the bitstream should be byte-aligned before reading the following data bytes
+ if ( data_byte_align_flag )
+ BitBufferByteAlign( bits, false );
+
+ // skip the data bytes
+ BitBufferAdvance( bits, count * 8 );
+
+ RequireAction( bits->cur <= bits->end, return kALAC_ParamError; );
+
+ return ALAC_noErr;
+}
+
+/*
+ ZeroN()
+ - helper routines to clear out output channel buffers when decoding fewer channels than requested
+*/
+static void Zero16( int16_t * buffer, uint32_t numItems, uint32_t stride )
+{
+ if ( stride == 1 )
+ {
+ memset( buffer, 0, numItems * sizeof(int16_t) );
+ }
+ else
+ {
+ for ( uint32_t index = 0; index < (numItems * stride); index += stride )
+ buffer[index] = 0;
+ }
+}
+
+static void Zero24( uint8_t * buffer, uint32_t numItems, uint32_t stride )
+{
+ if ( stride == 1 )
+ {
+ memset( buffer, 0, numItems * 3 );
+ }
+ else
+ {
+ for ( uint32_t index = 0; index < (numItems * stride * 3); index += (stride * 3) )
+ {
+ buffer[index + 0] = 0;
+ buffer[index + 1] = 0;
+ buffer[index + 2] = 0;
+ }
+ }
+}
+
+static void Zero32( int32_t * buffer, uint32_t numItems, uint32_t stride )
+{
+ if ( stride == 1 )
+ {
+ memset( buffer, 0, numItems * sizeof(int32_t) );
+ }
+ else
+ {
+ for ( uint32_t index = 0; index < (numItems * stride); index += stride )
+ buffer[index] = 0;
+ }
+}
diff --git a/codec/ALACDecoder.h b/codec/ALACDecoder.h
new file mode 100755
index 0000000..a308715
--- /dev/null
+++ b/codec/ALACDecoder.h
@@ -0,0 +1,69 @@
+/*
+ * Copyright (c) 2011 Apple Inc. All rights reserved.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_START@
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_END@
+ */
+
+/*
+ File: ALACDecoder.h
+*/
+
+#ifndef _ALACDECODER_H
+#define _ALACDECODER_H
+
+#if PRAGMA_ONCE
+#pragma once
+#endif
+
+#ifdef _MSC_VER
+#include <msstdint.h>
+#else
+#include <stdint.h>
+#endif
+
+#include "ALACAudioTypes.h"
+
+struct BitBuffer;
+
+class ALACDecoder
+{
+ public:
+ ALACDecoder();
+ ~ALACDecoder();
+
+ int32_t Init( void * inMagicCookie, uint32_t inMagicCookieSize );
+ int32_t Decode( struct BitBuffer * bits, uint8_t * sampleBuffer, uint32_t numSamples, uint32_t numChannels, uint32_t * outNumSamples );
+
+ public:
+ // decoding parameters (public for use in the analyzer)
+ ALACSpecificConfig mConfig;
+
+ protected:
+ int32_t FillElement( struct BitBuffer * bits );
+ int32_t DataStreamElement( struct BitBuffer * bits );
+
+ uint16_t mActiveElements;
+
+ // decoding buffers
+ int32_t * mMixBufferU;
+ int32_t * mMixBufferV;
+ int32_t * mPredictor;
+ uint16_t * mShiftBuffer; // note: this points to mPredictor's memory but different
+ // variable for clarity and type difference
+};
+
+#endif /* _ALACDECODER_H */
diff --git a/codec/ALACEncoder.cpp b/codec/ALACEncoder.cpp
new file mode 100755
index 0000000..1b71c29
--- /dev/null
+++ b/codec/ALACEncoder.cpp
@@ -0,0 +1,1425 @@
+/*
+ * Copyright (c) 2011 Apple Inc. All rights reserved.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_START@
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_END@
+ */
+
+/*
+ File: ALACEncoder.cpp
+*/
+
+// build stuff
+#define VERBOSE_DEBUG 0
+
+// headers
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "ALACEncoder.h"
+
+#include "aglib.h"
+#include "dplib.h"
+#include "matrixlib.h"
+
+#include "ALACBitUtilities.h"
+#include "ALACAudioTypes.h"
+#include "EndianPortable.h"
+
+// Note: in C you can't typecast to a 2-dimensional array pointer but that's what we need when
+// picking which coefs to use so we declare this typedef b/c we *can* typecast to this type
+typedef int16_t (*SearchCoefs)[kALACMaxCoefs];
+
+// defines/constants
+const uint32_t kALACEncoderMagic = 'dpge';
+const uint32_t kMaxSampleSize = 32; // max allowed bit width is 32
+const uint32_t kDefaultMixBits = 2;
+const uint32_t kDefaultMixRes = 0;
+const uint32_t kMaxRes = 4;
+const uint32_t kDefaultNumUV = 8;
+const uint32_t kMinUV = 4;
+const uint32_t kMaxUV = 8;
+
+// static functions
+#if VERBOSE_DEBUG
+static void AddFiller( BitBuffer * bits, int32_t numBytes );
+#endif
+
+
+/*
+ Map Format: 3-bit field per channel which is the same as the "element tag" that should be placed
+ at the beginning of the frame for that channel. Indicates whether SCE, CPE, or LFE.
+ Each particular field is accessed via the current channel index. Note that the channel
+ index increments by two for channel pairs.
+
+ For example:
+
+ C L R 3-channel input = (ID_CPE << 3) | (ID_SCE)
+ index 0 value = (map & (0x7ul << (0 * 3))) >> (0 * 3)
+ index 1 value = (map & (0x7ul << (1 * 3))) >> (1 * 3)
+
+ C L R Ls Rs LFE 5.1-channel input = (ID_LFE << 15) | (ID_CPE << 9) | (ID_CPE << 3) | (ID_SCE)
+ index 0 value = (map & (0x7ul << (0 * 3))) >> (0 * 3)
+ index 1 value = (map & (0x7ul << (1 * 3))) >> (1 * 3)
+ index 3 value = (map & (0x7ul << (3 * 3))) >> (3 * 3)
+ index 5 value = (map & (0x7ul << (5 * 3))) >> (5 * 3)
+ index 7 value = (map & (0x7ul << (7 * 3))) >> (7 * 3)
+*/
+static const uint32_t sChannelMaps[kALACMaxChannels] =
+{
+ ID_SCE,
+ ID_CPE,
+ (ID_CPE << 3) | (ID_SCE),
+ (ID_SCE << 9) | (ID_CPE << 3) | (ID_SCE),
+ (ID_CPE << 9) | (ID_CPE << 3) | (ID_SCE),
+ (ID_SCE << 15) | (ID_CPE << 9) | (ID_CPE << 3) | (ID_SCE),
+ (ID_SCE << 18) | (ID_SCE << 15) | (ID_CPE << 9) | (ID_CPE << 3) | (ID_SCE),
+ (ID_SCE << 21) | (ID_CPE << 15) | (ID_CPE << 9) | (ID_CPE << 3) | (ID_SCE)
+};
+
+static const uint32_t sSupportediPodSampleRates[] =
+{
+ 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000
+};
+
+/*
+ Constructor
+*/
+ALACEncoder::ALACEncoder() :
+ mBitDepth( 0 ),
+ mFastMode( 0 ),
+ mMixBufferU( nil ),
+ mMixBufferV( nil ),
+ mPredictorU( nil ),
+ mPredictorV( nil ),
+ mShiftBufferUV( nil ),
+ mWorkBuffer( nil ),
+
+
+ mTotalBytesGenerated( 0 ),
+ mAvgBitRate( 0 ),
+ mMaxFrameBytes( 0 )
+{
+ // overrides
+ mFrameSize = kALACDefaultFrameSize;
+}
+
+/*
+ Destructor
+*/
+ALACEncoder::~ALACEncoder()
+{
+ // delete the matrix mixing buffers
+ if ( mMixBufferU )
+ {
+ free(mMixBufferU);
+ mMixBufferU = NULL;
+ }
+ if ( mMixBufferV )
+ {
+ free(mMixBufferV);
+ mMixBufferV = NULL;
+ }
+
+ // delete the dynamic predictor's "corrector" buffers
+ if ( mPredictorU )
+ {
+ free(mPredictorU);
+ mPredictorU = NULL;
+ }
+ if ( mPredictorV )
+ {
+ free(mPredictorV);
+ mPredictorV = NULL;
+ }
+
+ // delete the unused byte shift buffer
+ if ( mShiftBufferUV )
+ {
+ free(mShiftBufferUV);
+ mShiftBufferUV = NULL;
+ }
+
+ // delete the work buffer
+ if ( mWorkBuffer )
+ {
+ free(mWorkBuffer);
+ mWorkBuffer = NULL;
+ }
+}
+
+#if PRAGMA_MARK
+#pragma mark -
+#endif
+
+/*
+ HEADER SPECIFICATION
+
+ For every segment we adopt the following header:
+
+ 1 byte reserved (always 0)
+ 1 byte flags (see below)
+ [4 byte frame length] (optional, see below)
+ ---Next, the per-segment ALAC parameters---
+ 1 byte mixBits (middle-side parameter)
+ 1 byte mixRes (middle-side parameter, interpreted as signed char)
+
+ 1 byte shiftU (4 bits modeU, 4 bits denShiftU)
+ 1 byte filterU (3 bits pbFactorU, 5 bits numU)
+ (numU) shorts (signed DP coefficients for V channel)
+ ---Next, 2nd-channel ALAC parameters in case of stereo mode---
+ 1 byte shiftV (4 bits modeV, 4 bits denShiftV)
+ 1 byte filterV (3 bits pbFactorV, 5 bits numV)
+ (numV) shorts (signed DP coefficients for V channel)
+ ---After this come the shift-off bytes for (>= 24)-bit data (n-byte shift) if indicated---
+ ---Then comes the AG-compressor bitstream---
+
+
+ FLAGS
+ -----
+
+ The presence of certain flag bits changes the header format such that the parameters might
+ not even be sent. The currently defined flags format is:
+
+ 0000psse
+
+ where 0 = reserved, must be 0
+ p = 1-bit field "partial frame" flag indicating 32-bit frame length follows this byte
+ ss = 2-bit field indicating "number of shift-off bytes ignored by compression"
+ e = 1-bit field indicating "escape"
+
+ The "partial frame" flag means that the following segment is not equal to the frame length specified
+ in the out-of-band decoder configuration. This allows the decoder to deal with end-of-file partial
+ segments without incurring the 32-bit overhead for each segment.
+
+ The "shift-off" field indicates the number of bytes at the bottom of the word that were passed through
+ uncompressed. The reason for this is that the entropy inherent in the LS bytes of >= 24-bit words
+ quite often means that the frame would have to be "escaped" b/c the compressed size would be >= the
+ uncompressed size. However, by shifting the input values down and running the remaining bits through
+ the normal compression algorithm, a net win can be achieved. If this field is non-zero, it means that
+ the shifted-off bytes follow after the parameter section of the header and before the compressed
+ bitstream. Note that doing this also allows us to use matrixing on 32-bit inputs after one or more
+ bytes are shifted off the bottom which helps the eventual compression ratio. For stereo channels,
+ the shifted off bytes are interleaved.
+
+ The "escape" flag means that this segment was not compressed b/c the compressed size would be
+ >= uncompressed size. In that case, the audio data was passed through uncompressed after the header.
+ The other header parameter bytes will not be sent.
+
+
+ PARAMETERS
+ ----------
+
+ If the segment is not a partial or escape segment, the total header size (in bytes) is given exactly by:
+
+ 4 + (2 + 2 * numU) (mono mode)
+ 4 + (2 + 2 * numV) + (2 + 2 * numV) (stereo mode)
+
+ where the ALAC filter-lengths numU, numV are bounded by a
+ constant (in the current source, numU, numV <= NUMCOEPAIRS), and
+ this forces an absolute upper bound on header size.
+
+ Each segment-decode process loads up these bytes from the front of the
+ local stream, in the above order, then follows with the entropy-encoded
+ bits for the given segment.
+
+ To generalize middle-side, there are various mixing modes including middle-side, each lossless,
+ as embodied in the mix() and unmix() functions. These functions exploit a generalized middle-side
+ transformation:
+
+ u := [(rL + (m-r)R)/m];
+ v := L - R;
+
+ where [ ] denotes integer floor. The (lossless) inverse is
+
+ L = u + v - [rV/m];
+ R = L - v;
+
+ In the segment header, m and r are encoded in mixBits and mixRes.
+ Classical "middle-side" is obtained with m = 2, r = 1, but now
+ we have more generalized mixes.
+
+ NOTES
+ -----
+ The relevance of the ALAC coefficients is explained in detail
+ in patent documents.
+*/
+
+/*
+ EncodeStereo()
+ - encode a channel pair
+*/
+int32_t ALACEncoder::EncodeStereo( BitBuffer * bitstream, void * inputBuffer, uint32_t stride, uint32_t channelIndex, uint32_t numSamples )
+{
+ BitBuffer workBits;
+ BitBuffer startBits = *bitstream; // squirrel away copy of current state in case we need to go back and do an escape packet
+ AGParamRec agParams;
+ uint32_t bits1, bits2;
+ uint32_t dilate;
+ int32_t mixBits, mixRes, maxRes;
+ uint32_t minBits, minBits1, minBits2;
+ uint32_t numU, numV;
+ uint32_t mode;
+ uint32_t pbFactor;
+ uint32_t chanBits;
+ uint32_t denShift;
+ uint8_t bytesShifted;
+ SearchCoefs coefsU;
+ SearchCoefs coefsV;
+ uint32_t index;
+ uint8_t partialFrame;
+ uint32_t escapeBits;
+ bool doEscape;
+ int32_t status = ALAC_noErr;
+
+ // make sure we handle this bit-depth before we get going
+ RequireAction( (mBitDepth == 16) || (mBitDepth == 20) || (mBitDepth == 24) || (mBitDepth == 32), return kALAC_ParamError; );
+
+ // reload coefs pointers for this channel pair
+ // - note that, while you might think they should be re-initialized per block, retaining state across blocks
+ // actually results in better overall compression
+ // - strangely, re-using the same coefs for the different passes of the "mixRes" search loop instead of using
+ // different coefs for the different passes of "mixRes" results in even better compression
+ coefsU = (SearchCoefs) mCoefsU[channelIndex];
+ coefsV = (SearchCoefs) mCoefsV[channelIndex];
+
+ // matrix encoding adds an extra bit but 32-bit inputs cannot be matrixed b/c 33 is too many
+ // so enable 16-bit "shift off" and encode in 17-bit mode
+ // - in addition, 24-bit mode really improves with one byte shifted off
+ if ( mBitDepth == 32 )
+ bytesShifted = 2;
+ else if ( mBitDepth >= 24 )
+ bytesShifted = 1;
+ else
+ bytesShifted = 0;
+
+ chanBits = mBitDepth - (bytesShifted * 8) + 1;
+
+ // flag whether or not this is a partial frame
+ partialFrame = (numSamples == mFrameSize) ? 0 : 1;
+
+ // brute-force encode optimization loop
+ // - run over variations of the encoding params to find the best choice
+ mixBits = kDefaultMixBits;
+ maxRes = kMaxRes;
+ numU = numV = kDefaultNumUV;
+ denShift = DENSHIFT_DEFAULT;
+ mode = 0;
+ pbFactor = 4;
+ dilate = 8;
+
+ minBits = minBits1 = minBits2 = 1ul << 31;
+
+ int32_t bestRes = mLastMixRes[channelIndex];
+
+ for ( mixRes = 0; mixRes <= maxRes; mixRes++ )
+ {
+ // mix the stereo inputs
+ switch ( mBitDepth )
+ {
+ case 16:
+ mix16( (int16_t *) inputBuffer, stride, mMixBufferU, mMixBufferV, numSamples/dilate, mixBits, mixRes );
+ break;
+ case 20:
+ mix20( (uint8_t *) inputBuffer, stride, mMixBufferU, mMixBufferV, numSamples/dilate, mixBits, mixRes );
+ break;
+ case 24:
+ // includes extraction of shifted-off bytes
+ mix24( (uint8_t *) inputBuffer, stride, mMixBufferU, mMixBufferV, numSamples/dilate,
+ mixBits, mixRes, mShiftBufferUV, bytesShifted );
+ break;
+ case 32:
+ // includes extraction of shifted-off bytes
+ mix32( (int32_t *) inputBuffer, stride, mMixBufferU, mMixBufferV, numSamples/dilate,
+ mixBits, mixRes, mShiftBufferUV, bytesShifted );
+ break;
+ }
+
+ BitBufferInit( &workBits, mWorkBuffer, mMaxOutputBytes );
+
+ // run the dynamic predictors
+ pc_block( mMixBufferU, mPredictorU, numSamples/dilate, coefsU[numU - 1], numU, chanBits, DENSHIFT_DEFAULT );
+ pc_block( mMixBufferV, mPredictorV, numSamples/dilate, coefsV[numV - 1], numV, chanBits, DENSHIFT_DEFAULT );
+
+ // run the lossless compressor on each channel
+ set_ag_params( &agParams, MB0, (pbFactor * PB0) / 4, KB0, numSamples/dilate, numSamples/dilate, MAX_RUN_DEFAULT );
+ status = dyn_comp( &agParams, mPredictorU, &workBits, numSamples/dilate, chanBits, &bits1 );
+ RequireNoErr( status, goto Exit; );
+
+ set_ag_params( &agParams, MB0, (pbFactor * PB0) / 4, KB0, numSamples/dilate, numSamples/dilate, MAX_RUN_DEFAULT );
+ status = dyn_comp( &agParams, mPredictorV, &workBits, numSamples/dilate, chanBits, &bits2 );
+ RequireNoErr( status, goto Exit; );
+
+ // look for best match
+ if ( (bits1 + bits2) < minBits1 )
+ {
+ minBits1 = bits1 + bits2;
+ bestRes = mixRes;
+ }
+ }
+
+ mLastMixRes[channelIndex] = (int16_t)bestRes;
+
+ // mix the stereo inputs with the current best mixRes
+ mixRes = mLastMixRes[channelIndex];
+ switch ( mBitDepth )
+ {
+ case 16:
+ mix16( (int16_t *) inputBuffer, stride, mMixBufferU, mMixBufferV, numSamples, mixBits, mixRes );
+ break;
+ case 20:
+ mix20( (uint8_t *) inputBuffer, stride, mMixBufferU, mMixBufferV, numSamples, mixBits, mixRes );
+ break;
+ case 24:
+ // also extracts the shifted off bytes into the shift buffers
+ mix24( (uint8_t *) inputBuffer, stride, mMixBufferU, mMixBufferV, numSamples,
+ mixBits, mixRes, mShiftBufferUV, bytesShifted );
+ break;
+ case 32:
+ // also extracts the shifted off bytes into the shift buffers
+ mix32( (int32_t *) inputBuffer, stride, mMixBufferU, mMixBufferV, numSamples,
+ mixBits, mixRes, mShiftBufferUV, bytesShifted );
+ break;
+ }
+
+ // now it's time for the predictor coefficient search loop
+ numU = numV = kMinUV;
+ minBits1 = minBits2 = 1ul << 31;
+
+ for ( uint32_t numUV = kMinUV; numUV <= kMaxUV; numUV += 4 )
+ {
+ BitBufferInit( &workBits, mWorkBuffer, mMaxOutputBytes );
+
+ dilate = 32;
+
+ // run the predictor over the same data multiple times to help it converge
+ for ( uint32_t converge = 0; converge < 8; converge++ )
+ {
+ pc_block( mMixBufferU, mPredictorU, numSamples/dilate, coefsU[numUV-1], numUV, chanBits, DENSHIFT_DEFAULT );
+ pc_block( mMixBufferV, mPredictorV, numSamples/dilate, coefsV[numUV-1], numUV, chanBits, DENSHIFT_DEFAULT );
+ }
+
+ dilate = 8;
+
+ set_ag_params( &agParams, MB0, (pbFactor * PB0)/4, KB0, numSamples/dilate, numSamples/dilate, MAX_RUN_DEFAULT );
+ status = dyn_comp( &agParams, mPredictorU, &workBits, numSamples/dilate, chanBits, &bits1 );
+
+ if ( (bits1 * dilate + 16 * numUV) < minBits1 )
+ {
+ minBits1 = bits1 * dilate + 16 * numUV;
+ numU = numUV;
+ }
+
+ set_ag_params( &agParams, MB0, (pbFactor * PB0)/4, KB0, numSamples/dilate, numSamples/dilate, MAX_RUN_DEFAULT );
+ status = dyn_comp( &agParams, mPredictorV, &workBits, numSamples/dilate, chanBits, &bits2 );
+
+ if ( (bits2 * dilate + 16 * numUV) < minBits2 )
+ {
+ minBits2 = bits2 * dilate + 16 * numUV;
+ numV = numUV;
+ }
+ }
+
+ // test for escape hatch if best calculated compressed size turns out to be more than the input size
+ minBits = minBits1 + minBits2 + (8 /* mixRes/maxRes/etc. */ * 8) + ((partialFrame == true) ? 32 : 0);
+ if ( bytesShifted != 0 )
+ minBits += (numSamples * (bytesShifted * 8) * 2);
+
+ escapeBits = (numSamples * mBitDepth * 2) + ((partialFrame == true) ? 32 : 0) + (2 * 8); /* 2 common header bytes */
+
+ doEscape = (minBits >= escapeBits) ? true : false;
+
+ if ( doEscape == false )
+ {
+ // write bitstream header and coefs
+ BitBufferWrite( bitstream, 0, 12 );
+ BitBufferWrite( bitstream, (partialFrame << 3) | (bytesShifted << 1), 4 );
+ if ( partialFrame )
+ BitBufferWrite( bitstream, numSamples, 32 );
+ BitBufferWrite( bitstream, mixBits, 8 );
+ BitBufferWrite( bitstream, mixRes, 8 );
+
+ //Assert( (mode < 16) && (DENSHIFT_DEFAULT < 16) );
+ //Assert( (pbFactor < 8) && (numU < 32) );
+ //Assert( (pbFactor < 8) && (numV < 32) );
+
+ BitBufferWrite( bitstream, (mode << 4) | DENSHIFT_DEFAULT, 8 );
+ BitBufferWrite( bitstream, (pbFactor << 5) | numU, 8 );
+ for ( index = 0; index < numU; index++ )
+ BitBufferWrite( bitstream, coefsU[numU - 1][index], 16 );
+
+ BitBufferWrite( bitstream, (mode << 4) | DENSHIFT_DEFAULT, 8 );
+ BitBufferWrite( bitstream, (pbFactor << 5) | numV, 8 );
+ for ( index = 0; index < numV; index++ )
+ BitBufferWrite( bitstream, coefsV[numV - 1][index], 16 );
+
+ // if shift active, write the interleaved shift buffers
+ if ( bytesShifted != 0 )
+ {
+ uint32_t bitShift = bytesShifted * 8;
+
+ //Assert( bitShift <= 16 );
+
+ for ( index = 0; index < (numSamples * 2); index += 2 )
+ {
+ uint32_t shiftedVal;
+
+ shiftedVal = ((uint32_t)mShiftBufferUV[index + 0] << bitShift) | (uint32_t)mShiftBufferUV[index + 1];
+ BitBufferWrite( bitstream, shiftedVal, bitShift * 2 );
+ }
+ }
+
+ // run the dynamic predictor and lossless compression for the "left" channel
+ // - note: to avoid allocating more buffers, we're mixing and matching between the available buffers instead
+ // of only using "U" buffers for the U-channel and "V" buffers for the V-channel
+ if ( mode == 0 )
+ {
+ pc_block( mMixBufferU, mPredictorU, numSamples, coefsU[numU - 1], numU, chanBits, DENSHIFT_DEFAULT );
+ }
+ else
+ {
+ pc_block( mMixBufferU, mPredictorV, numSamples, coefsU[numU - 1], numU, chanBits, DENSHIFT_DEFAULT );
+ pc_block( mPredictorV, mPredictorU, numSamples, nil, 31, chanBits, 0 );
+ }
+
+ set_ag_params( &agParams, MB0, (pbFactor * PB0) / 4, KB0, numSamples, numSamples, MAX_RUN_DEFAULT );
+ status = dyn_comp( &agParams, mPredictorU, bitstream, numSamples, chanBits, &bits1 );
+ RequireNoErr( status, goto Exit; );
+
+ // run the dynamic predictor and lossless compression for the "right" channel
+ if ( mode == 0 )
+ {
+ pc_block( mMixBufferV, mPredictorV, numSamples, coefsV[numV - 1], numV, chanBits, DENSHIFT_DEFAULT );
+ }
+ else
+ {
+ pc_block( mMixBufferV, mPredictorU, numSamples, coefsV[numV - 1], numV, chanBits, DENSHIFT_DEFAULT );
+ pc_block( mPredictorU, mPredictorV, numSamples, nil, 31, chanBits, 0 );
+ }
+
+ set_ag_params( &agParams, MB0, (pbFactor * PB0) / 4, KB0, numSamples, numSamples, MAX_RUN_DEFAULT );
+ status = dyn_comp( &agParams, mPredictorV, bitstream, numSamples, chanBits, &bits2 );
+ RequireNoErr( status, goto Exit; );
+
+ /* if we happened to create a compressed packet that was actually bigger than an escape packet would be,
+ chuck it and do an escape packet
+ */
+ minBits = BitBufferGetPosition( bitstream ) - BitBufferGetPosition( &startBits );
+ if ( minBits >= escapeBits )
+ {
+ *bitstream = startBits; // reset bitstream state
+ doEscape = true;
+ printf( "compressed frame too big: %u vs. %u \n", minBits, escapeBits );
+ }
+ }
+
+ if ( doEscape == true )
+ {
+ /* escape */
+ status = this->EncodeStereoEscape( bitstream, inputBuffer, stride, numSamples );
+
+#if VERBOSE_DEBUG
+ DebugMsg( "escape!: %lu vs %lu", minBits, escapeBits );
+#endif
+ }
+
+Exit:
+ return status;
+}
+
+/*
+ EncodeStereoFast()
+ - encode a channel pair without the search loop for maximum possible speed
+*/
+int32_t ALACEncoder::EncodeStereoFast( BitBuffer * bitstream, void * inputBuffer, uint32_t stride, uint32_t channelIndex, uint32_t numSamples )
+{
+ BitBuffer startBits = *bitstream; // squirrel away current bit position in case we decide to use escape hatch
+ AGParamRec agParams;
+ uint32_t bits1, bits2;
+ int32_t mixBits, mixRes;
+ uint32_t minBits, minBits1, minBits2;
+ uint32_t numU, numV;
+ uint32_t mode;
+ uint32_t pbFactor;
+ uint32_t chanBits;
+ uint32_t denShift;
+ uint8_t bytesShifted;
+ SearchCoefs coefsU;
+ SearchCoefs coefsV;
+ uint32_t index;
+ uint8_t partialFrame;
+ uint32_t escapeBits;
+ bool doEscape;
+ int32_t status;
+
+ // make sure we handle this bit-depth before we get going
+ RequireAction( (mBitDepth == 16) || (mBitDepth == 20) || (mBitDepth == 24) || (mBitDepth == 32), return kALAC_ParamError; );
+
+ // reload coefs pointers for this channel pair
+ // - note that, while you might think they should be re-initialized per block, retaining state across blocks
+ // actually results in better overall compression
+ // - strangely, re-using the same coefs for the different passes of the "mixRes" search loop instead of using
+ // different coefs for the different passes of "mixRes" results in even better compression
+ coefsU = (SearchCoefs) mCoefsU[channelIndex];
+ coefsV = (SearchCoefs) mCoefsV[channelIndex];
+
+ // matrix encoding adds an extra bit but 32-bit inputs cannot be matrixed b/c 33 is too many
+ // so enable 16-bit "shift off" and encode in 17-bit mode
+ // - in addition, 24-bit mode really improves with one byte shifted off
+ if ( mBitDepth == 32 )
+ bytesShifted = 2;
+ else if ( mBitDepth >= 24 )
+ bytesShifted = 1;
+ else
+ bytesShifted = 0;
+
+ chanBits = mBitDepth - (bytesShifted * 8) + 1;
+
+ // flag whether or not this is a partial frame
+ partialFrame = (numSamples == mFrameSize) ? 0 : 1;
+
+ // set up default encoding parameters for "fast" mode
+ mixBits = kDefaultMixBits;
+ mixRes = kDefaultMixRes;
+ numU = numV = kDefaultNumUV;
+ denShift = DENSHIFT_DEFAULT;
+ mode = 0;
+ pbFactor = 4;
+
+ minBits = minBits1 = minBits2 = 1ul << 31;
+
+ // mix the stereo inputs with default mixBits/mixRes
+ switch ( mBitDepth )
+ {
+ case 16:
+ mix16( (int16_t *) inputBuffer, stride, mMixBufferU, mMixBufferV, numSamples, mixBits, mixRes );
+ break;
+ case 20:
+ mix20( (uint8_t *) inputBuffer, stride, mMixBufferU, mMixBufferV, numSamples, mixBits, mixRes );
+ break;
+ case 24:
+ // also extracts the shifted off bytes into the shift buffers
+ mix24( (uint8_t *) inputBuffer, stride, mMixBufferU, mMixBufferV, numSamples,
+ mixBits, mixRes, mShiftBufferUV, bytesShifted );
+ break;
+ case 32:
+ // also extracts the shifted off bytes into the shift buffers
+ mix32( (int32_t *) inputBuffer, stride, mMixBufferU, mMixBufferV, numSamples,
+ mixBits, mixRes, mShiftBufferUV, bytesShifted );
+ break;
+ }
+
+ /* speculatively write the bitstream assuming the compressed version will be smaller */
+
+ // write bitstream header and coefs
+ BitBufferWrite( bitstream, 0, 12 );
+ BitBufferWrite( bitstream, (partialFrame << 3) | (bytesShifted << 1), 4 );
+ if ( partialFrame )
+ BitBufferWrite( bitstream, numSamples, 32 );
+ BitBufferWrite( bitstream, mixBits, 8 );
+ BitBufferWrite( bitstream, mixRes, 8 );
+
+ //Assert( (mode < 16) && (DENSHIFT_DEFAULT < 16) );
+ //Assert( (pbFactor < 8) && (numU < 32) );
+ //Assert( (pbFactor < 8) && (numV < 32) );
+
+ BitBufferWrite( bitstream, (mode << 4) | DENSHIFT_DEFAULT, 8 );
+ BitBufferWrite( bitstream, (pbFactor << 5) | numU, 8 );
+ for ( index = 0; index < numU; index++ )
+ BitBufferWrite( bitstream, coefsU[numU - 1][index], 16 );
+
+ BitBufferWrite( bitstream, (mode << 4) | DENSHIFT_DEFAULT, 8 );
+ BitBufferWrite( bitstream, (pbFactor << 5) | numV, 8 );
+ for ( index = 0; index < numV; index++ )
+ BitBufferWrite( bitstream, coefsV[numV - 1][index], 16 );
+
+ // if shift active, write the interleaved shift buffers
+ if ( bytesShifted != 0 )
+ {
+ uint32_t bitShift = bytesShifted * 8;
+
+ //Assert( bitShift <= 16 );
+
+ for ( index = 0; index < (numSamples * 2); index += 2 )
+ {
+ uint32_t shiftedVal;
+
+ shiftedVal = ((uint32_t)mShiftBufferUV[index + 0] << bitShift) | (uint32_t)mShiftBufferUV[index + 1];
+ BitBufferWrite( bitstream, shiftedVal, bitShift * 2 );
+ }
+ }
+
+ // run the dynamic predictor and lossless compression for the "left" channel
+ // - note: we always use mode 0 in the "fast" path so we don't need the code for mode != 0
+ pc_block( mMixBufferU, mPredictorU, numSamples, coefsU[numU - 1], numU, chanBits, DENSHIFT_DEFAULT );
+
+ set_ag_params( &agParams, MB0, (pbFactor * PB0) / 4, KB0, numSamples, numSamples, MAX_RUN_DEFAULT );
+ status = dyn_comp( &agParams, mPredictorU, bitstream, numSamples, chanBits, &bits1 );
+ RequireNoErr( status, goto Exit; );
+
+ // run the dynamic predictor and lossless compression for the "right" channel
+ pc_block( mMixBufferV, mPredictorV, numSamples, coefsV[numV - 1], numV, chanBits, DENSHIFT_DEFAULT );
+
+ set_ag_params( &agParams, MB0, (pbFactor * PB0) / 4, KB0, numSamples, numSamples, MAX_RUN_DEFAULT );
+ status = dyn_comp( &agParams, mPredictorV, bitstream, numSamples, chanBits, &bits2 );
+ RequireNoErr( status, goto Exit; );
+
+ // do bit requirement calculations
+ minBits1 = bits1 + (numU * sizeof(int16_t) * 8);
+ minBits2 = bits2 + (numV * sizeof(int16_t) * 8);
+
+ // test for escape hatch if best calculated compressed size turns out to be more than the input size
+ minBits = minBits1 + minBits2 + (8 /* mixRes/maxRes/etc. */ * 8) + ((partialFrame == true) ? 32 : 0);
+ if ( bytesShifted != 0 )
+ minBits += (numSamples * (bytesShifted * 8) * 2);
+
+ escapeBits = (numSamples * mBitDepth * 2) + ((partialFrame == true) ? 32 : 0) + (2 * 8); /* 2 common header bytes */
+
+ doEscape = (minBits >= escapeBits) ? true : false;
+
+ if ( doEscape == false )
+ {
+ /* if we happened to create a compressed packet that was actually bigger than an escape packet would be,
+ chuck it and do an escape packet
+ */
+ minBits = BitBufferGetPosition( bitstream ) - BitBufferGetPosition( &startBits );
+ if ( minBits >= escapeBits )
+ {
+ doEscape = true;
+ printf( "compressed frame too big: %u vs. %u\n", minBits, escapeBits );
+ }
+
+ }
+
+ if ( doEscape == true )
+ {
+ /* escape */
+
+ // reset bitstream position since we speculatively wrote the compressed version
+ *bitstream = startBits;
+
+ // write escape frame
+ status = this->EncodeStereoEscape( bitstream, inputBuffer, stride, numSamples );
+
+#if VERBOSE_DEBUG
+ DebugMsg( "escape!: %u vs %u", minBits, (numSamples * mBitDepth * 2) );
+#endif
+ }
+
+Exit:
+ return status;
+}
+
+/*
+ EncodeStereoEscape()
+ - encode stereo escape frame
+*/
+int32_t ALACEncoder::EncodeStereoEscape( BitBuffer * bitstream, void * inputBuffer, uint32_t stride, uint32_t numSamples )
+{
+ int16_t * input16;
+ int32_t * input32;
+ uint8_t partialFrame;
+ uint32_t index;
+
+ // flag whether or not this is a partial frame
+ partialFrame = (numSamples == mFrameSize) ? 0 : 1;
+
+ // write bitstream header
+ BitBufferWrite( bitstream, 0, 12 );
+ BitBufferWrite( bitstream, (partialFrame << 3) | 1, 4 ); // LSB = 1 means "frame not compressed"
+ if ( partialFrame )
+ BitBufferWrite( bitstream, numSamples, 32 );
+
+ // just copy the input data to the output buffer
+ switch ( mBitDepth )
+ {
+ case 16:
+ input16 = (int16_t *) inputBuffer;
+
+ for ( index = 0; index < (numSamples * stride); index += stride )
+ {
+ BitBufferWrite( bitstream, input16[index + 0], 16 );
+ BitBufferWrite( bitstream, input16[index + 1], 16 );
+ }
+ break;
+ case 20:
+ // mix20() with mixres param = 0 means de-interleave so use it to simplify things
+ mix20( (uint8_t *) inputBuffer, stride, mMixBufferU, mMixBufferV, numSamples, 0, 0 );
+ for ( index = 0; index < numSamples; index++ )
+ {
+ BitBufferWrite( bitstream, mMixBufferU[index], 20 );
+ BitBufferWrite( bitstream, mMixBufferV[index], 20 );
+ }
+ break;
+ case 24:
+ // mix24() with mixres param = 0 means de-interleave so use it to simplify things
+ mix24( (uint8_t *) inputBuffer, stride, mMixBufferU, mMixBufferV, numSamples, 0, 0, mShiftBufferUV, 0 );
+ for ( index = 0; index < numSamples; index++ )
+ {
+ BitBufferWrite( bitstream, mMixBufferU[index], 24 );
+ BitBufferWrite( bitstream, mMixBufferV[index], 24 );
+ }
+ break;
+ case 32:
+ input32 = (int32_t *) inputBuffer;
+
+ for ( index = 0; index < (numSamples * stride); index += stride )
+ {
+ BitBufferWrite( bitstream, input32[index + 0], 32 );
+ BitBufferWrite( bitstream, input32[index + 1], 32 );
+ }
+ break;
+ }
+
+ return ALAC_noErr;
+}
+
+/*
+ EncodeMono()
+ - encode a mono input buffer
+*/
+int32_t ALACEncoder::EncodeMono( BitBuffer * bitstream, void * inputBuffer, uint32_t stride, uint32_t channelIndex, uint32_t numSamples )
+{
+ BitBuffer startBits = *bitstream; // squirrel away copy of current state in case we need to go back and do an escape packet
+ AGParamRec agParams;
+ uint32_t bits1;
+ uint32_t numU;
+ SearchCoefs coefsU;
+ uint32_t dilate;
+ uint32_t minBits, bestU;
+ uint32_t minU, maxU;
+ uint32_t index, index2;
+ uint8_t bytesShifted;
+ uint32_t shift;
+ uint32_t mask;
+ uint32_t chanBits;
+ uint8_t pbFactor;
+ uint8_t partialFrame;
+ int16_t * input16;
+ int32_t * input32;
+ uint32_t escapeBits;
+ bool doEscape;
+ int32_t status;
+
+ // make sure we handle this bit-depth before we get going
+ RequireAction( (mBitDepth == 16) || (mBitDepth == 20) || (mBitDepth == 24) || (mBitDepth == 32), return kALAC_ParamError; );
+
+ status = ALAC_noErr;
+
+ // reload coefs array from previous frame
+ coefsU = (SearchCoefs) mCoefsU[channelIndex];
+
+ // pick bit depth for actual encoding
+ // - we lop off the lower byte(s) for 24-/32-bit encodings
+ if ( mBitDepth == 32 )
+ bytesShifted = 2;
+ else if ( mBitDepth >= 24 )
+ bytesShifted = 1;
+ else
+ bytesShifted = 0;
+
+ shift = bytesShifted * 8;
+ mask = (1ul << shift) - 1;
+ chanBits = mBitDepth - (bytesShifted * 8);
+
+ // flag whether or not this is a partial frame
+ partialFrame = (numSamples == mFrameSize) ? 0 : 1;
+
+ // convert N-bit data to 32-bit for predictor
+ switch ( mBitDepth )
+ {
+ case 16:
+ {
+ // convert 16-bit data to 32-bit for predictor
+ input16 = (int16_t *) inputBuffer;
+ for ( index = 0, index2 = 0; index < numSamples; index++, index2 += stride )
+ mMixBufferU[index] = (int32_t) input16[index2];
+ break;
+ }
+ case 20:
+ // convert 20-bit data to 32-bit for predictor
+ copy20ToPredictor( (uint8_t *) inputBuffer, stride, mMixBufferU, numSamples );
+ break;
+ case 24:
+ // convert 24-bit data to 32-bit for the predictor and extract the shifted off byte(s)
+ copy24ToPredictor( (uint8_t *) inputBuffer, stride, mMixBufferU, numSamples );
+ for ( index = 0; index < numSamples; index++ )
+ {
+ mShiftBufferUV[index] = (uint16_t)(mMixBufferU[index] & mask);
+ mMixBufferU[index] >>= shift;
+ }
+ break;
+ case 32:
+ {
+ // just copy the 32-bit input data for the predictor and extract the shifted off byte(s)
+ input32 = (int32_t *) inputBuffer;
+
+ for ( index = 0, index2 = 0; index < numSamples; index++, index2 += stride )
+ {
+ int32_t val = input32[index2];
+
+ mShiftBufferUV[index] = (uint16_t)(val & mask);
+ mMixBufferU[index] = val >> shift;
+ }
+ break;
+ }
+ }
+
+ // brute-force encode optimization loop (implied "encode depth" of 0 if comparing to cmd line tool)
+ // - run over variations of the encoding params to find the best choice
+ minU = 4;
+ maxU = 8;
+ minBits = 1ul << 31;
+ pbFactor = 4;
+
+ minBits = 1ul << 31;
+ bestU = minU;
+
+ for ( numU = minU; numU <= maxU; numU += 4 )
+ {
+ BitBuffer workBits;
+ uint32_t numBits;
+
+ BitBufferInit( &workBits, mWorkBuffer, mMaxOutputBytes );
+
+ dilate = 32;
+ for ( uint32_t converge = 0; converge < 7; converge++ )
+ pc_block( mMixBufferU, mPredictorU, numSamples/dilate, coefsU[numU-1], numU, chanBits, DENSHIFT_DEFAULT );
+
+ dilate = 8;
+ pc_block( mMixBufferU, mPredictorU, numSamples/dilate, coefsU[numU-1], numU, chanBits, DENSHIFT_DEFAULT );
+
+ set_ag_params( &agParams, MB0, (pbFactor * PB0) / 4, KB0, numSamples/dilate, numSamples/dilate, MAX_RUN_DEFAULT );
+ status = dyn_comp( &agParams, mPredictorU, &workBits, numSamples/dilate, chanBits, &bits1 );
+ RequireNoErr( status, goto Exit; );
+
+ numBits = (dilate * bits1) + (16 * numU);
+ if ( numBits < minBits )
+ {
+ bestU = numU;
+ minBits = numBits;
+ }
+ }
+
+ // test for escape hatch if best calculated compressed size turns out to be more than the input size
+ // - first, add bits for the header bytes mixRes/maxRes/shiftU/filterU
+ minBits += (4 /* mixRes/maxRes/etc. */ * 8) + ((partialFrame == true) ? 32 : 0);
+ if ( bytesShifted != 0 )
+ minBits += (numSamples * (bytesShifted * 8));
+
+ escapeBits = (numSamples * mBitDepth) + ((partialFrame == true) ? 32 : 0) + (2 * 8); /* 2 common header bytes */
+
+ doEscape = (minBits >= escapeBits) ? true : false;
+
+ if ( doEscape == false )
+ {
+ // write bitstream header
+ BitBufferWrite( bitstream, 0, 12 );
+ BitBufferWrite( bitstream, (partialFrame << 3) | (bytesShifted << 1), 4 );
+ if ( partialFrame )
+ BitBufferWrite( bitstream, numSamples, 32 );
+ BitBufferWrite( bitstream, 0, 16 ); // mixBits = mixRes = 0
+
+ // write the params and predictor coefs
+ numU = bestU;
+ BitBufferWrite( bitstream, (0 << 4) | DENSHIFT_DEFAULT, 8 ); // modeU = 0
+ BitBufferWrite( bitstream, (pbFactor << 5) | numU, 8 );
+ for ( index = 0; index < numU; index++ )
+ BitBufferWrite( bitstream, coefsU[numU-1][index], 16 );
+
+ // if shift active, write the interleaved shift buffers
+ if ( bytesShifted != 0 )
+ {
+ for ( index = 0; index < numSamples; index++ )
+ BitBufferWrite( bitstream, mShiftBufferUV[index], shift );
+ }
+
+ // run the dynamic predictor with the best result
+ pc_block( mMixBufferU, mPredictorU, numSamples, coefsU[numU-1], numU, chanBits, DENSHIFT_DEFAULT );
+
+ // do lossless compression
+ set_standard_ag_params( &agParams, numSamples, numSamples );
+ status = dyn_comp( &agParams, mPredictorU, bitstream, numSamples, chanBits, &bits1 );
+ //AssertNoErr( status );
+
+
+ /* if we happened to create a compressed packet that was actually bigger than an escape packet would be,
+ chuck it and do an escape packet
+ */
+ minBits = BitBufferGetPosition( bitstream ) - BitBufferGetPosition( &startBits );
+ if ( minBits >= escapeBits )
+ {
+ *bitstream = startBits; // reset bitstream state
+ doEscape = true;
+ printf( "compressed frame too big: %u vs. %u\n", minBits, escapeBits );
+ }
+ }
+
+ if ( doEscape == true )
+ {
+ // write bitstream header and coefs
+ BitBufferWrite( bitstream, 0, 12 );
+ BitBufferWrite( bitstream, (partialFrame << 3) | 1, 4 ); // LSB = 1 means "frame not compressed"
+ if ( partialFrame )
+ BitBufferWrite( bitstream, numSamples, 32 );
+
+ // just copy the input data to the output buffer
+ switch ( mBitDepth )
+ {
+ case 16:
+ input16 = (int16_t *) inputBuffer;
+ for ( index = 0; index < (numSamples * stride); index += stride )
+ BitBufferWrite( bitstream, input16[index], 16 );
+ break;
+ case 20:
+ // convert 20-bit data to 32-bit for simplicity
+ copy20ToPredictor( (uint8_t *) inputBuffer, stride, mMixBufferU, numSamples );
+ for ( index = 0; index < numSamples; index++ )
+ BitBufferWrite( bitstream, mMixBufferU[index], 20 );
+ break;
+ case 24:
+ // convert 24-bit data to 32-bit for simplicity
+ copy24ToPredictor( (uint8_t *) inputBuffer, stride, mMixBufferU, numSamples );
+ for ( index = 0; index < numSamples; index++ )
+ BitBufferWrite( bitstream, mMixBufferU[index], 24 );
+ break;
+ case 32:
+ input32 = (int32_t *) inputBuffer;
+ for ( index = 0; index < (numSamples * stride); index += stride )
+ BitBufferWrite( bitstream, input32[index], 32 );
+ break;
+ }
+#if VERBOSE_DEBUG
+ DebugMsg( "escape!: %lu vs %lu", minBits, (numSamples * mBitDepth) );
+#endif
+ }
+
+Exit:
+ return status;
+}
+
+#if PRAGMA_MARK
+#pragma mark -
+#endif
+
+/*
+ Encode()
+ - encode the next block of samples
+*/
+int32_t ALACEncoder::Encode(AudioFormatDescription theInputFormat, AudioFormatDescription theOutputFormat,
+ unsigned char * theReadBuffer, unsigned char * theWriteBuffer, int32_t * ioNumBytes)
+{
+ uint32_t numFrames;
+ uint32_t outputSize;
+ BitBuffer bitstream;
+ int32_t status;
+
+ numFrames = *ioNumBytes/theInputFormat.mBytesPerPacket;
+
+ // create a bit buffer structure pointing to our output buffer
+ BitBufferInit( &bitstream, theWriteBuffer, mMaxOutputBytes );
+
+ if ( theInputFormat.mChannelsPerFrame == 2 )
+ {
+ // add 3-bit frame start tag ID_CPE = channel pair & 4-bit element instance tag = 0
+ BitBufferWrite( &bitstream, ID_CPE, 3 );
+ BitBufferWrite( &bitstream, 0, 4 );
+
+ // encode stereo input buffer
+ if ( mFastMode == false )
+ status = this->EncodeStereo( &bitstream, theReadBuffer, 2, 0, numFrames );
+ else
+ status = this->EncodeStereoFast( &bitstream, theReadBuffer, 2, 0, numFrames );
+ RequireNoErr( status, goto Exit; );
+ }
+ else if ( theInputFormat.mChannelsPerFrame == 1 )
+ {
+ // add 3-bit frame start tag ID_SCE = mono channel & 4-bit element instance tag = 0
+ BitBufferWrite( &bitstream, ID_SCE, 3 );
+ BitBufferWrite( &bitstream, 0, 4 );
+
+ // encode mono input buffer
+ status = this->EncodeMono( &bitstream, theReadBuffer, 1, 0, numFrames );
+ RequireNoErr( status, goto Exit; );
+ }
+ else
+ {
+ char * inputBuffer;
+ uint32_t tag;
+ uint32_t channelIndex;
+ uint32_t inputIncrement;
+ uint8_t stereoElementTag;
+ uint8_t monoElementTag;
+ uint8_t lfeElementTag;
+
+ inputBuffer = (char *) theReadBuffer;
+ inputIncrement = ((mBitDepth + 7) / 8);
+
+ stereoElementTag = 0;
+ monoElementTag = 0;
+ lfeElementTag = 0;
+
+ for ( channelIndex = 0; channelIndex < theInputFormat.mChannelsPerFrame; )
+ {
+ tag = (sChannelMaps[theInputFormat.mChannelsPerFrame - 1] & (0x7ul << (channelIndex * 3))) >> (channelIndex * 3);
+
+ BitBufferWrite( &bitstream, tag, 3 );
+ switch ( tag )
+ {
+ case ID_SCE:
+ // mono
+ BitBufferWrite( &bitstream, monoElementTag, 4 );
+
+ status = this->EncodeMono( &bitstream, inputBuffer, theInputFormat.mChannelsPerFrame, channelIndex, numFrames );
+
+ inputBuffer += inputIncrement;
+ channelIndex++;
+ monoElementTag++;
+ break;
+
+ case ID_CPE:
+ // stereo
+ BitBufferWrite( &bitstream, stereoElementTag, 4 );
+
+ status = this->EncodeStereo( &bitstream, inputBuffer, theInputFormat.mChannelsPerFrame, channelIndex, numFrames );
+
+ inputBuffer += (inputIncrement * 2);
+ channelIndex += 2;
+ stereoElementTag++;
+ break;
+
+ case ID_LFE:
+ // LFE channel (subwoofer)
+ BitBufferWrite( &bitstream, lfeElementTag, 4 );
+
+ status = this->EncodeMono( &bitstream, inputBuffer, theInputFormat.mChannelsPerFrame, channelIndex, numFrames );
+
+ inputBuffer += inputIncrement;
+ channelIndex++;
+ lfeElementTag++;
+ break;
+
+ default:
+ printf( "That ain't right! (%u)\n", tag );
+ status = kALAC_ParamError;
+ goto Exit;
+ }
+
+ RequireNoErr( status, goto Exit; );
+ }
+ }
+
+#if VERBOSE_DEBUG
+{
+ // if there is room left in the output buffer, add some random fill data to test decoder
+ int32_t bitsLeft;
+ int32_t bytesLeft;
+
+ bitsLeft = BitBufferGetPosition( &bitstream ) - 3; // - 3 for ID_END tag
+ bytesLeft = bitstream.byteSize - ((bitsLeft + 7) / 8);
+
+ if ( (bytesLeft > 20) && ((bytesLeft & 0x4u) != 0) )
+ AddFiller( &bitstream, bytesLeft );
+}
+#endif
+
+ // add 3-bit frame end tag: ID_END
+ BitBufferWrite( &bitstream, ID_END, 3 );
+
+ // byte-align the output data
+ BitBufferByteAlign( &bitstream, true );
+
+ outputSize = BitBufferGetPosition( &bitstream ) / 8;
+ //Assert( outputSize <= mMaxOutputBytes );
+
+
+ // all good, let iTunes know what happened and remember the total number of input sample frames
+ *ioNumBytes = outputSize;
+ //mEncodedFrames += encodeMsg->numInputSamples;
+
+ // gather encoding stats
+ mTotalBytesGenerated += outputSize;
+ mMaxFrameBytes = MAX( mMaxFrameBytes, outputSize );
+
+ status = ALAC_noErr;
+
+Exit:
+ return status;
+}
+
+/*
+ Finish()
+ - drain out any leftover samples
+*/
+
+int32_t ALACEncoder::Finish()
+{
+/* // finalize bit rate statistics
+ if ( mSampleSize.numEntries != 0 )
+ mAvgBitRate = (uint32_t)( (((float)mTotalBytesGenerated * 8.0f) / (float)mSampleSize.numEntries) * ((float)mSampleRate / (float)mFrameSize) );
+ else
+ mAvgBitRate = 0;
+*/
+ return ALAC_noErr;
+}
+
+#if PRAGMA_MARK
+#pragma mark -
+#endif
+
+/*
+ GetConfig()
+*/
+void ALACEncoder::GetConfig( ALACSpecificConfig & config )
+{
+ config.frameLength = Swap32NtoB(mFrameSize);
+ config.compatibleVersion = (uint8_t) kALACCompatibleVersion;
+ config.bitDepth = (uint8_t) mBitDepth;
+ config.pb = (uint8_t) PB0;
+ config.kb = (uint8_t) KB0;
+ config.mb = (uint8_t) MB0;
+ config.numChannels = (uint8_t) mNumChannels;
+ config.maxRun = Swap16NtoB((uint16_t) MAX_RUN_DEFAULT);
+ config.maxFrameBytes = Swap32NtoB(mMaxFrameBytes);
+ config.avgBitRate = Swap32NtoB(mAvgBitRate);
+ config.sampleRate = Swap32NtoB(mOutputSampleRate);
+}
+
+uint32_t ALACEncoder::GetMagicCookieSize(uint32_t inNumChannels)
+{
+ if (inNumChannels > 2)
+ {
+ return sizeof(ALACSpecificConfig) + kChannelAtomSize + sizeof(ALACAudioChannelLayout);
+ }
+ else
+ {
+ return sizeof(ALACSpecificConfig);
+ }
+}
+
+void ALACEncoder::GetMagicCookie(void * outCookie, uint32_t * ioSize)
+{
+ ALACSpecificConfig theConfig = {0};
+ ALACAudioChannelLayout theChannelLayout = {0};
+ uint8_t theChannelAtom[kChannelAtomSize] = {0, 0, 0, 0, 'c', 'h', 'a', 'n', 0, 0, 0, 0};
+ uint32_t theCookieSize = sizeof(ALACSpecificConfig);
+ uint8_t * theCookiePointer = (uint8_t *)outCookie;
+
+ GetConfig(theConfig);
+ if (theConfig.numChannels > 2)
+ {
+ theChannelLayout.mChannelLayoutTag = ALACChannelLayoutTags[theConfig.numChannels - 1];
+ theCookieSize += (sizeof(ALACAudioChannelLayout) + kChannelAtomSize);
+ }
+ if (*ioSize >= theCookieSize)
+ {
+ memcpy(theCookiePointer, &theConfig, sizeof(ALACSpecificConfig));
+ theChannelAtom[3] = (sizeof(ALACAudioChannelLayout) + kChannelAtomSize);
+ if (theConfig.numChannels > 2)
+ {
+ theCookiePointer += sizeof(ALACSpecificConfig);
+ memcpy(theCookiePointer, theChannelAtom, kChannelAtomSize);
+ theCookiePointer += kChannelAtomSize;
+ memcpy(theCookiePointer, &theChannelLayout, sizeof(ALACAudioChannelLayout));
+ }
+ *ioSize = theCookieSize;
+ }
+ else
+ {
+ *ioSize = 0; // no incomplete cookies
+ }
+}
+
+/*
+ InitializeEncoder()
+ - initialize the encoder component with the current config
+*/
+int32_t ALACEncoder::InitializeEncoder(AudioFormatDescription theOutputFormat)
+{
+ int32_t status;
+
+ mOutputSampleRate = theOutputFormat.mSampleRate;
+ mNumChannels = theOutputFormat.mChannelsPerFrame;
+ switch(theOutputFormat.mFormatFlags)
+ {
+ case 1:
+ mBitDepth = 16;
+ break;
+ case 2:
+ mBitDepth = 20;
+ break;
+ case 3:
+ mBitDepth = 24;
+ break;
+ case 4:
+ mBitDepth = 32;
+ break;
+ default:
+ break;
+ }
+
+ // set up default encoding parameters and state
+ // - note: mFrameSize is set in the constructor or via SetFrameSize() which must be called before this routine
+ for ( uint32_t index = 0; index < kALACMaxChannels; index++ )
+ mLastMixRes[index] = kDefaultMixRes;
+
+ // the maximum output frame size can be no bigger than (samplesPerBlock * numChannels * ((10 + sampleSize)/8) + 1)
+ // but note that this can be bigger than the input size!
+ // - since we don't yet know what our input format will be, use our max allowed sample size in the calculation
+ mMaxOutputBytes = mFrameSize * mNumChannels * ((10 + kMaxSampleSize) / 8) + 1;
+
+ // allocate mix buffers
+ mMixBufferU = (int32_t *) calloc( mFrameSize * sizeof(int32_t), 1 );
+ mMixBufferV = (int32_t *) calloc( mFrameSize * sizeof(int32_t), 1 );
+
+ // allocate dynamic predictor buffers
+ mPredictorU = (int32_t *) calloc( mFrameSize * sizeof(int32_t), 1 );
+ mPredictorV = (int32_t *) calloc( mFrameSize * sizeof(int32_t), 1 );
+
+ // allocate combined shift buffer
+ mShiftBufferUV = (uint16_t *) calloc( mFrameSize * 2 * sizeof(uint16_t),1 );
+
+ // allocate work buffer for search loop
+ mWorkBuffer = (uint8_t *) calloc( mMaxOutputBytes, 1 );
+
+ RequireAction( (mMixBufferU != nil) && (mMixBufferV != nil) &&
+ (mPredictorU != nil) && (mPredictorV != nil) &&
+ (mShiftBufferUV != nil) && (mWorkBuffer != nil ),
+ status = kALAC_MemFullError; goto Exit; );
+
+ status = ALAC_noErr;
+
+
+ // initialize coefs arrays once b/c retaining state across blocks actually improves the encode ratio
+ for ( int32_t channel = 0; channel < (int32_t)mNumChannels; channel++ )
+ {
+ for ( int32_t search = 0; search < kALACMaxSearches; search++ )
+ {
+ init_coefs( mCoefsU[channel][search], DENSHIFT_DEFAULT, kALACMaxCoefs );
+ init_coefs( mCoefsV[channel][search], DENSHIFT_DEFAULT, kALACMaxCoefs );
+ }
+ }
+
+Exit:
+ return status;
+}
+
+/*
+ GetSourceFormat()
+ - given the input format, return one of our supported formats
+*/
+void ALACEncoder::GetSourceFormat( const AudioFormatDescription * source, AudioFormatDescription * output )
+{
+ // default is 16-bit native endian
+ // - note: for float input we assume that's coming from one of our decoders (mp3, aac) so it only makes sense
+ // to encode to 16-bit since the source was lossy in the first place
+ // - note: if not a supported bit depth, find the closest supported bit depth to the input one
+ if ( (source->mFormatID != kALACFormatLinearPCM) || ((source->mFormatFlags & kALACFormatFlagIsFloat) != 0) ||
+ ( source->mBitsPerChannel <= 16 ) )
+ mBitDepth = 16;
+ else if ( source->mBitsPerChannel <= 20 )
+ mBitDepth = 20;
+ else if ( source->mBitsPerChannel <= 24 )
+ mBitDepth = 24;
+ else
+ mBitDepth = 32;
+
+ // we support 16/20/24/32-bit integer data at any sample rate and our target number of channels
+ // and sample rate were specified when we were configured
+ /*
+ MakeUncompressedAudioFormat( mNumChannels, (float) mOutputSampleRate, mBitDepth, kAudioFormatFlagsNativeIntegerPacked, output );
+ */
+}
+
+
+
+#if VERBOSE_DEBUG
+
+#if PRAGMA_MARK
+#pragma mark -
+#endif
+
+/*
+ AddFiller()
+ - add fill and data stream elements to the bitstream to test the decoder
+*/
+static void AddFiller( BitBuffer * bits, int32_t numBytes )
+{
+ uint8_t tag;
+ uint32_t index;
+
+ // out of lameness, subtract 6 bytes to deal with header + alignment as required for fill/data elements
+ numBytes -= 6;
+ if ( numBytes <= 0 )
+ return;
+
+ // randomly pick Fill or Data Stream Element based on numBytes requested
+ tag = (numBytes & 0x8) ? ID_FIL : ID_DSE;
+
+ BitBufferWrite( bits, tag, 3 );
+ if ( tag == ID_FIL )
+ {
+ // can't write more than 269 bytes in a fill element
+ numBytes = (numBytes > 269) ? 269 : numBytes;
+
+ // fill element = 4-bit size unless >= 15 then 4-bit size + 8-bit extension size
+ if ( numBytes >= 15 )
+ {
+ uint16_t extensionSize;
+
+ BitBufferWrite( bits, 15, 4 );
+
+ // 8-bit extension count field is "extra + 1" which is weird but I didn't define the syntax
+ // - otherwise, there's no way to represent 15
+ // - for example, to really mean 15 bytes you must encode extensionSize = 1
+ // - why it's not like data stream elements I have no idea
+ extensionSize = (numBytes - 15) + 1;
+ Assert( extensionSize <= 255 );
+ BitBufferWrite( bits, extensionSize, 8 );
+ }
+ else
+ BitBufferWrite( bits, numBytes, 4 );
+
+ BitBufferWrite( bits, 0x10, 8 ); // extension_type = FILL_DATA = b0001 or'ed with fill_nibble = b0000
+ for ( index = 0; index < (numBytes - 1); index++ )
+ BitBufferWrite( bits, 0xa5, 8 ); // fill_byte = b10100101 = 0xa5
+ }
+ else
+ {
+ // can't write more than 510 bytes in a data stream element
+ numBytes = (numBytes > 510) ? 510 : numBytes;
+
+ BitBufferWrite( bits, 0, 4 ); // element instance tag
+ BitBufferWrite( bits, 1, 1 ); // byte-align flag = true
+
+ // data stream element = 8-bit size unless >= 255 then 8-bit size + 8-bit size
+ if ( numBytes >= 255 )
+ {
+ BitBufferWrite( bits, 255, 8 );
+ BitBufferWrite( bits, numBytes - 255, 8 );
+ }
+ else
+ BitBufferWrite( bits, numBytes, 8 );
+
+ BitBufferByteAlign( bits, true ); // byte-align with zeros
+
+ for ( index = 0; index < numBytes; index++ )
+ BitBufferWrite( bits, 0x5a, 8 );
+ }
+}
+
+#endif /* VERBOSE_DEBUG */
diff --git a/codec/ALACEncoder.h b/codec/ALACEncoder.h
new file mode 100755
index 0000000..19b733a
--- /dev/null
+++ b/codec/ALACEncoder.h
@@ -0,0 +1,96 @@
+/*
+ * Copyright (c) 2011 Apple Inc. All rights reserved.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_START@
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_END@
+ */
+
+/*
+ File: ALACEncoder.h
+*/
+
+#pragma once
+
+#ifdef _MSC_VER
+#include <msstdint.h>
+#else
+#include <stdint.h>
+#endif
+
+#include "ALACAudioTypes.h"
+
+
+struct BitBuffer;
+
+class ALACEncoder
+{
+ public:
+ ALACEncoder();
+ virtual ~ALACEncoder();
+
+ virtual int32_t Encode(AudioFormatDescription theInputFormat, AudioFormatDescription theOutputFormat,
+ unsigned char * theReadBuffer, unsigned char * theWriteBuffer, int32_t * ioNumBytes);
+ virtual int32_t Finish( );
+
+ void SetFastMode( bool fast ) { mFastMode = fast; };
+
+ // this must be called *before* InitializeEncoder()
+ void SetFrameSize( uint32_t frameSize ) { mFrameSize = frameSize; };
+
+ void GetConfig( ALACSpecificConfig & config );
+ uint32_t GetMagicCookieSize(uint32_t inNumChannels);
+ void GetMagicCookie( void * config, uint32_t * ioSize );
+
+ virtual int32_t InitializeEncoder(AudioFormatDescription theOutputFormat);
+
+ protected:
+ virtual void GetSourceFormat( const AudioFormatDescription * source, AudioFormatDescription * output );
+
+ int32_t EncodeStereo( struct BitBuffer * bitstream, void * input, uint32_t stride, uint32_t channelIndex, uint32_t numSamples );
+ int32_t EncodeStereoFast( struct BitBuffer * bitstream, void * input, uint32_t stride, uint32_t channelIndex, uint32_t numSamples );
+ int32_t EncodeStereoEscape( struct BitBuffer * bitstream, void * input, uint32_t stride, uint32_t numSamples );
+ int32_t EncodeMono( struct BitBuffer * bitstream, void * input, uint32_t stride, uint32_t channelIndex, uint32_t numSamples );
+
+
+ // ALAC encoder parameters
+ int16_t mBitDepth;
+ bool mFastMode;
+
+ // encoding state
+ int16_t mLastMixRes[kALACMaxChannels];
+
+ // encoding buffers
+ int32_t * mMixBufferU;
+ int32_t * mMixBufferV;
+ int32_t * mPredictorU;
+ int32_t * mPredictorV;
+ uint16_t * mShiftBufferUV;
+
+ uint8_t * mWorkBuffer;
+
+ // per-channel coefficients buffers
+ int16_t mCoefsU[kALACMaxChannels][kALACMaxSearches][kALACMaxCoefs];
+ int16_t mCoefsV[kALACMaxChannels][kALACMaxSearches][kALACMaxCoefs];
+
+ // encoding statistics
+ uint32_t mTotalBytesGenerated;
+ uint32_t mAvgBitRate;
+ uint32_t mMaxFrameBytes;
+ uint32_t mFrameSize;
+ uint32_t mMaxOutputBytes;
+ uint32_t mNumChannels;
+ uint32_t mOutputSampleRate;
+};
diff --git a/codec/APPLE_LICENSE.txt b/codec/APPLE_LICENSE.txt
new file mode 100755
index 0000000..71fe6fd
--- /dev/null
+++ b/codec/APPLE_LICENSE.txt
@@ -0,0 +1,335 @@
+APPLE PUBLIC SOURCE LICENSE
+Version 2.0 - August 6, 2003
+
+Please read this License carefully before downloading this software. By
+downloading or using this software, you are agreeing to be bound by the terms
+of this License. If you do not or cannot agree to the terms of this License,
+please do not download or use the software.
+
+Apple Note: In January 2007, Apple changed its corporate name from "Apple
+Computer, Inc." to "Apple Inc." This change has been reflected below and
+copyright years updated, but no other changes have been made to the APSL 2.0.
+
+1. General; Definitions. This License applies to any program or other
+work which Apple Inc. ("Apple") makes publicly available and which contains a
+notice placed by Apple identifying such program or work as "Original Code" and
+stating that it is subject to the terms of this Apple Public Source License
+version 2.0 ("License"). As used in this License:
+
+1.1 "Applicable Patent Rights" mean: (a) in the case where Apple is the
+grantor of rights, (i) claims of patents that are now or hereafter acquired,
+owned by or assigned to Apple and (ii) that cover subject matter contained in
+the Original Code, but only to the extent necessary to use, reproduce and/or
+distribute the Original Code without infringement; and (b) in the case where
+You are the grantor of rights, (i) claims of patents that are now or hereafter
+acquired, owned by or assigned to You and (ii) that cover subject matter in
+Your Modifications, taken alone or in combination with Original Code.
+
+1.2 "Contributor" means any person or entity that creates or contributes to
+the creation of Modifications.
+
+1.3 "Covered Code" means the Original Code, Modifications, the combination
+of Original Code and any Modifications, and/or any respective portions thereof.
+
+1.4 "Externally Deploy" means: (a) to sublicense, distribute or otherwise
+make Covered Code available, directly or indirectly, to anyone other than You;
+and/or (b) to use Covered Code, alone or as part of a Larger Work, in any way
+to provide a service, including but not limited to delivery of content, through
+electronic communication with a client other than You.
+
+1.5 "Larger Work" means a work which combines Covered Code or portions
+thereof with code not governed by the terms of this License.
+
+1.6 "Modifications" mean any addition to, deletion from, and/or change to,
+the substance and/or structure of the Original Code, any previous
+Modifications, the combination of Original Code and any previous Modifications,
+and/or any respective portions thereof. When code is released as a series of
+files, a Modification is: (a) any addition to or deletion from the contents of
+a file containing Covered Code; and/or (b) any new file or other representation
+of computer program statements that contains any part of Covered Code.
+
+1.7 "Original Code" means (a) the Source Code of a program or other work as
+originally made available by Apple under this License, including the Source
+Code of any updates or upgrades to such programs or works made available by
+Apple under this License, and that has been expressly identified by Apple as
+such in the header file(s) of such work; and (b) the object code compiled from
+such Source Code and originally made available by Apple under this License
+
+1.8 "Source Code" means the human readable form of a program or other work
+that is suitable for making modifications to it, including all modules it
+contains, plus any associated interface definition files, scripts used to
+control compilation and installation of an executable (object code).
+
+1.9 "You" or "Your" means an individual or a legal entity exercising rights
+under this License. For legal entities, "You" or "Your" includes any entity
+which controls, is controlled by, or is under common control with, You, where
+"control" means (a) the power, direct or indirect, to cause the direction or
+management of such entity, whether by contract or otherwise, or (b) ownership
+of fifty percent (50%) or more of the outstanding shares or beneficial
+ownership of such entity.
+
+2. Permitted Uses; Conditions & Restrictions. Subject to the terms and
+conditions of this License, Apple hereby grants You, effective on the date You
+accept this License and download the Original Code, a world-wide, royalty-free,
+non-exclusive license, to the extent of Apple's Applicable Patent Rights and
+copyrights covering the Original Code, to do the following:
+
+2.1 Unmodified Code. You may use, reproduce, display, perform, internally
+distribute within Your organization, and Externally Deploy verbatim, unmodified
+copies of the Original Code, for commercial or non-commercial purposes,
+provided that in each instance:
+
+(a) You must retain and reproduce in all copies of Original Code the
+copyright and other proprietary notices and disclaimers of Apple as they appear
+in the Original Code, and keep intact all notices in the Original Code that
+refer to this License; and
+
+(b) You must include a copy of this License with every copy of Source Code
+of Covered Code and documentation You distribute or Externally Deploy, and You
+may not offer or impose any terms on such Source Code that alter or restrict
+this License or the recipients' rights hereunder, except as permitted under
+Section 6.
+
+2.2 Modified Code. You may modify Covered Code and use, reproduce,
+display, perform, internally distribute within Your organization, and
+Externally Deploy Your Modifications and Covered Code, for commercial or
+non-commercial purposes, provided that in each instance You also meet all of
+these conditions:
+
+(a) You must satisfy all the conditions of Section 2.1 with respect to the
+Source Code of the Covered Code;
+
+(b) You must duplicate, to the extent it does not already exist, the notice
+in Exhibit A in each file of the Source Code of all Your Modifications, and
+cause the modified files to carry prominent notices stating that You changed
+the files and the date of any change; and
+
+(c) If You Externally Deploy Your Modifications, You must make Source Code
+of all Your Externally Deployed Modifications either available to those to whom
+You have Externally Deployed Your Modifications, or publicly available. Source
+Code of Your Externally Deployed Modifications must be released under the terms
+set forth in this License, including the license grants set forth in Section 3
+below, for as long as you Externally Deploy the Covered Code or twelve (12)
+months from the date of initial External Deployment, whichever is longer. You
+should preferably distribute the Source Code of Your Externally Deployed
+Modifications electronically (e.g. download from a web site).
+
+2.3 Distribution of Executable Versions. In addition, if You Externally
+Deploy Covered Code (Original Code and/or Modifications) in object code,
+executable form only, You must include a prominent notice, in the code itself
+as well as in related documentation, stating that Source Code of the Covered
+Code is available under the terms of this License with information on how and
+where to obtain such Source Code.
+
+2.4 Third Party Rights. You expressly acknowledge and agree that although
+Apple and each Contributor grants the licenses to their respective portions of
+the Covered Code set forth herein, no assurances are provided by Apple or any
+Contributor that the Covered Code does not infringe the patent or other
+intellectual property rights of any other entity. Apple and each Contributor
+disclaim any liability to You for claims brought by any other entity based on
+infringement of intellectual property rights or otherwise. As a condition to
+exercising the rights and licenses granted hereunder, You hereby assume sole
+responsibility to secure any other intellectual property rights needed, if any.
+For example, if a third party patent license is required to allow You to
+distribute the Covered Code, it is Your responsibility to acquire that license
+before distributing the Covered Code.
+
+3. Your Grants. In consideration of, and as a condition to, the licenses
+granted to You under this License, You hereby grant to any person or entity
+receiving or distributing Covered Code under this License a non-exclusive,
+royalty-free, perpetual, irrevocable license, under Your Applicable Patent
+Rights and other intellectual property rights (other than patent) owned or
+controlled by You, to use, reproduce, display, perform, modify, sublicense,
+distribute and Externally Deploy Your Modifications of the same scope and
+extent as Apple's licenses under Sections 2.1 and 2.2 above.
+
+4. Larger Works. You may create a Larger Work by combining Covered Code
+with other code not governed by the terms of this License and distribute the
+Larger Work as a single product. In each such instance, You must make sure the
+requirements of this License are fulfilled for the Covered Code or any portion
+thereof.
+
+5. Limitations on Patent License. Except as expressly stated in Section
+2, no other patent rights, express or implied, are granted by Apple herein.
+Modifications and/or Larger Works may require additional patent licenses from
+Apple which Apple may grant in its sole discretion.
+
+6. Additional Terms. You may choose to offer, and to charge a fee for,
+warranty, support, indemnity or liability obligations and/or other rights
+consistent with the scope of the license granted herein ("Additional Terms") to
+one or more recipients of Covered Code. However, You may do so only on Your own
+behalf and as Your sole responsibility, and not on behalf of Apple or any
+Contributor. You must obtain the recipient's agreement that any such Additional
+Terms are offered by You alone, and You hereby agree to indemnify, defend and
+hold Apple and every Contributor harmless for any liability incurred by or
+claims asserted against Apple or such Contributor by reason of any such
+Additional Terms.
+
+7. Versions of the License. Apple may publish revised and/or new versions
+of this License from time to time. Each version will be given a distinguishing
+version number. Once Original Code has been published under a particular
+version of this License, You may continue to use it under the terms of that
+version. You may also choose to use such Original Code under the terms of any
+subsequent version of this License published by Apple. No one other than Apple
+has the right to modify the terms applicable to Covered Code created under this
+License.
+
+8. NO WARRANTY OR SUPPORT. The Covered Code may contain in whole or in
+part pre-release, untested, or not fully tested works. The Covered Code may
+contain errors that could cause failures or loss of data, and may be incomplete
+or contain inaccuracies. You expressly acknowledge and agree that use of the
+Covered Code, or any portion thereof, is at Your sole and entire risk. THE
+COVERED CODE IS PROVIDED "AS IS" AND WITHOUT WARRANTY, UPGRADES OR SUPPORT OF
+ANY KIND AND APPLE AND APPLE'S LICENSOR(S) (COLLECTIVELY REFERRED TO AS "APPLE"
+FOR THE PURPOSES OF SECTIONS 8 AND 9) AND ALL CONTRIBUTORS EXPRESSLY DISCLAIM
+ALL WARRANTIES AND/OR CONDITIONS, EXPRESS OR IMPLIED, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES AND/OR CONDITIONS OF MERCHANTABILITY, OF
+SATISFACTORY QUALITY, OF FITNESS FOR A PARTICULAR PURPOSE, OF ACCURACY, OF
+QUIET ENJOYMENT, AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. APPLE AND EACH
+CONTRIBUTOR DOES NOT WARRANT AGAINST INTERFERENCE WITH YOUR ENJOYMENT OF THE
+COVERED CODE, THAT THE FUNCTIONS CONTAINED IN THE COVERED CODE WILL MEET YOUR
+REQUIREMENTS, THAT THE OPERATION OF THE COVERED CODE WILL BE UNINTERRUPTED OR
+ERROR-FREE, OR THAT DEFECTS IN THE COVERED CODE WILL BE CORRECTED. NO ORAL OR
+WRITTEN INFORMATION OR ADVICE GIVEN BY APPLE, AN APPLE AUTHORIZED
+REPRESENTATIVE OR ANY CONTRIBUTOR SHALL CREATE A WARRANTY. You acknowledge
+that the Covered Code is not intended for use in the operation of nuclear
+facilities, aircraft navigation, communication systems, or air traffic control
+machines in which case the failure of the Covered Code could lead to death,
+personal injury, or severe physical or environmental damage.
+
+9. LIMITATION OF LIABILITY. TO THE EXTENT NOT PROHIBITED BY LAW, IN NO
+EVENT SHALL APPLE OR ANY CONTRIBUTOR BE LIABLE FOR ANY INCIDENTAL, SPECIAL,
+INDIRECT OR CONSEQUENTIAL DAMAGES ARISING OUT OF OR RELATING TO THIS LICENSE OR
+YOUR USE OR INABILITY TO USE THE COVERED CODE, OR ANY PORTION THEREOF, WHETHER
+UNDER A THEORY OF CONTRACT, WARRANTY, TORT (INCLUDING NEGLIGENCE), PRODUCTS
+LIABILITY OR OTHERWISE, EVEN IF APPLE OR SUCH CONTRIBUTOR HAS BEEN ADVISED OF
+THE POSSIBILITY OF SUCH DAMAGES AND NOTWITHSTANDING THE FAILURE OF ESSENTIAL
+PURPOSE OF ANY REMEDY. SOME JURISDICTIONS DO NOT ALLOW THE LIMITATION OF
+LIABILITY OF INCIDENTAL OR CONSEQUENTIAL DAMAGES, SO THIS LIMITATION MAY NOT
+APPLY TO YOU. In no event shall Apple's total liability to You for all damages
+(other than as may be required by applicable law) under this License exceed the
+amount of fifty dollars ($50.00).
+
+10. Trademarks. This License does not grant any rights to use the
+trademarks or trade names "Apple", "Mac", "Mac OS", "QuickTime", "QuickTime
+Streaming Server" or any other trademarks, service marks, logos or trade names
+belonging to Apple (collectively "Apple Marks") or to any trademark, service
+mark, logo or trade name belonging to any Contributor. You agree not to use
+any Apple Marks in or as part of the name of products derived from the Original
+Code or to endorse or promote products derived from the Original Code other
+than as expressly permitted by and in strict compliance at all times with
+Apple's third party trademark usage guidelines which are posted at
+http://www.apple.com/legal/guidelinesfor3rdparties.html.
+
+11. Ownership. Subject to the licenses granted under this License, each
+Contributor retains all rights, title and interest in and to any Modifications
+made by such Contributor. Apple retains all rights, title and interest in and
+to the Original Code and any Modifications made by or on behalf of Apple
+("Apple Modifications"), and such Apple Modifications will not be automatically
+subject to this License. Apple may, at its sole discretion, choose to license
+such Apple Modifications under this License, or on different terms from those
+contained in this License or may choose not to license them at all.
+
+12. Termination.
+
+12.1 Termination. This License and the rights granted hereunder will
+terminate:
+
+(a) automatically without notice from Apple if You fail to comply with any
+term(s) of this License and fail to cure such breach within 30 days of becoming
+aware of such breach;
+(b) immediately in the event of the circumstances described in Section
+13.5(b); or
+(c) automatically without notice from Apple if You, at any time during the
+term of this License, commence an action for patent infringement against Apple;
+provided that Apple did not first commence an action for patent infringement
+against You in that instance.
+
+12.2 Effect of Termination. Upon termination, You agree to immediately stop
+any further use, reproduction, modification, sublicensing and distribution of
+the Covered Code. All sublicenses to the Covered Code which have been properly
+granted prior to termination shall survive any termination of this License.
+Provisions which, by their nature, should remain in effect beyond the
+termination of this License shall survive, including but not limited to
+Sections 3, 5, 8, 9, 10, 11, 12.2 and 13. No party will be liable to any other
+for compensation, indemnity or damages of any sort solely as a result of
+terminating this License in accordance with its terms, and termination of this
+License will be without prejudice to any other right or remedy of any party.
+
+13. Miscellaneous.
+
+13.1 Government End Users. The Covered Code is a "commercial item" as
+defined in FAR 2.101. Government software and technical data rights in the
+Covered Code include only those rights customarily provided to the public as
+defined in this License. This customary commercial license in technical data
+and software is provided in accordance with FAR 12.211 (Technical Data) and
+12.212 (Computer Software) and, for Department of Defense purchases, DFAR
+252.227-7015 (Technical Data -- Commercial Items) and 227.7202-3 (Rights in
+Commercial Computer Software or Computer Software Documentation). Accordingly,
+all U.S. Government End Users acquire Covered Code with only those rights set
+forth herein.
+
+13.2 Relationship of Parties. This License will not be construed as
+creating an agency, partnership, joint venture or any other form of legal
+association between or among You, Apple or any Contributor, and You will not
+represent to the contrary, whether expressly, by implication, appearance or
+otherwise.
+
+13.3 Independent Development. Nothing in this License will impair Apple's
+right to acquire, license, develop, have others develop for it, market and/or
+distribute technology or products that perform the same or similar functions
+as, or otherwise compete with, Modifications, Larger Works, technology or
+products that You may develop, produce, market or distribute.
+
+13.4 Waiver; Construction. Failure by Apple or any Contributor to enforce
+any provision of this License will not be deemed a waiver of future enforcement
+of that or any other provision. Any law or regulation which provides that the
+language of a contract shall be construed against the drafter will not apply to
+this License.
+
+13.5 Severability. (a) If for any reason a court of competent jurisdiction
+finds any provision of this License, or portion thereof, to be unenforceable,
+that provision of the License will be enforced to the maximum extent
+permissible so as to effect the economic benefits and intent of the parties,
+and the remainder of this License will continue in full force and effect. (b)
+Notwithstanding the foregoing, if applicable law prohibits or restricts You
+from fully and/or specifically complying with Sections 2 and/or 3 or prevents
+the enforceability of either of those Sections, this License will immediately
+terminate and You must immediately discontinue any use of the Covered Code and
+destroy all copies of it that are in your possession or control.
+
+13.6 Dispute Resolution. Any litigation or other dispute resolution between
+You and Apple relating to this License shall take place in the Northern
+District of California, and You and Apple hereby consent to the personal
+jurisdiction of, and venue in, the state and federal courts within that
+District with respect to this License. The application of the United Nations
+Convention on Contracts for the International Sale of Goods is expressly
+excluded.
+
+13.7 Entire Agreement; Governing Law. This License constitutes the entire
+agreement between the parties with respect to the subject matter hereof. This
+License shall be governed by the laws of the United States and the State of
+California, except that body of California law concerning conflicts of law.
+
+Where You are located in the province of Quebec, Canada, the following clause
+applies: The parties hereby confirm that they have requested that this License
+and all related documents be drafted in English. Les parties ont exigé que le
+présent contrat et tous les documents connexes soient rédigés en anglais.
+
+EXHIBIT A.
+
+"Portions Copyright (c) 1999-2007 Apple Inc. All Rights Reserved.
+
+This file contains Original Code and/or Modifications of Original Code as
+defined in and that are subject to the Apple Public Source License Version 2.0
+(the 'License'). You may not use this file except in compliance with the
+License. Please obtain a copy of the License at
+http://www.opensource.apple.com/apsl/ and read it before using this file.
+
+The Original Code and all software distributed under the License are
+distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
+OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
+LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
+PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
+specific language governing rights and limitations under the License."
+
diff --git a/codec/EndianPortable.c b/codec/EndianPortable.c
new file mode 100755
index 0000000..5a7d5b8
--- /dev/null
+++ b/codec/EndianPortable.c
@@ -0,0 +1,175 @@
+/*
+ * Copyright (c) 2011 Apple Inc. All rights reserved.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_START@
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_END@
+ */
+
+//
+// EndianPortable.c
+//
+// Copyright 2011 Apple Inc. All rights reserved.
+//
+
+#include <stdio.h>
+#include "EndianPortable.h"
+
+#define BSWAP16(x) (((x << 8) | ((x >> 8) & 0x00ff)))
+#define BSWAP32(x) (((x << 24) | ((x << 8) & 0x00ff0000) | ((x >> 8) & 0x0000ff00) | ((x >> 24) & 0x000000ff)))
+#define BSWAP64(x) ((((int64_t)x << 56) | (((int64_t)x << 40) & 0x00ff000000000000LL) | \
+ (((int64_t)x << 24) & 0x0000ff0000000000LL) | (((int64_t)x << 8) & 0x000000ff00000000LL) | \
+ (((int64_t)x >> 8) & 0x00000000ff000000LL) | (((int64_t)x >> 24) & 0x0000000000ff0000LL) | \
+ (((int64_t)x >> 40) & 0x000000000000ff00LL) | (((int64_t)x >> 56) & 0x00000000000000ffLL)))
+
+#if defined(__i386__)
+#define TARGET_RT_LITTLE_ENDIAN 1
+#elif defined(__x86_64__)
+#define TARGET_RT_LITTLE_ENDIAN 1
+#elif defined (TARGET_OS_WIN32)
+#define TARGET_RT_LITTLE_ENDIAN 1
+#endif
+
+uint16_t Swap16NtoB(uint16_t inUInt16)
+{
+#if TARGET_RT_LITTLE_ENDIAN
+ return BSWAP16(inUInt16);
+#else
+ return inUInt16;
+#endif
+}
+
+uint16_t Swap16BtoN(uint16_t inUInt16)
+{
+#if TARGET_RT_LITTLE_ENDIAN
+ return BSWAP16(inUInt16);
+#else
+ return inUInt16;
+#endif
+}
+
+uint32_t Swap32NtoB(uint32_t inUInt32)
+{
+#if TARGET_RT_LITTLE_ENDIAN
+ return BSWAP32(inUInt32);
+#else
+ return inUInt32;
+#endif
+}
+
+uint32_t Swap32BtoN(uint32_t inUInt32)
+{
+#if TARGET_RT_LITTLE_ENDIAN
+ return BSWAP32(inUInt32);
+#else
+ return inUInt32;
+#endif
+}
+
+uint64_t Swap64BtoN(uint64_t inUInt64)
+{
+#if TARGET_RT_LITTLE_ENDIAN
+ return BSWAP64(inUInt64);
+#else
+ return inUInt64;
+#endif
+}
+
+uint64_t Swap64NtoB(uint64_t inUInt64)
+{
+#if TARGET_RT_LITTLE_ENDIAN
+ return BSWAP64(inUInt64);
+#else
+ return inUInt64;
+#endif
+}
+
+float SwapFloat32BtoN(float in)
+{
+#if TARGET_RT_LITTLE_ENDIAN
+ union {
+ float f;
+ int32_t i;
+ } x;
+ x.f = in;
+ x.i = BSWAP32(x.i);
+ return x.f;
+#else
+ return in;
+#endif
+}
+
+float SwapFloat32NtoB(float in)
+{
+#if TARGET_RT_LITTLE_ENDIAN
+ union {
+ float f;
+ int32_t i;
+ } x;
+ x.f = in;
+ x.i = BSWAP32(x.i);
+ return x.f;
+#else
+ return in;
+#endif
+}
+
+double SwapFloat64BtoN(double in)
+{
+#if TARGET_RT_LITTLE_ENDIAN
+ union {
+ double f;
+ int64_t i;
+ } x;
+ x.f = in;
+ x.i = BSWAP64(x.i);
+ return x.f;
+#else
+ return in;
+#endif
+}
+
+double SwapFloat64NtoB(double in)
+{
+#if TARGET_RT_LITTLE_ENDIAN
+ union {
+ double f;
+ int64_t i;
+ } x;
+ x.f = in;
+ x.i = BSWAP64(x.i);
+ return x.f;
+#else
+ return in;
+#endif
+}
+
+void Swap16(uint16_t * inUInt16)
+{
+ *inUInt16 = BSWAP16(*inUInt16);
+}
+
+void Swap24(uint8_t * inUInt24)
+{
+ uint8_t tempVal = inUInt24[0];
+ inUInt24[0] = inUInt24[2];
+ inUInt24[2] = tempVal;
+}
+
+void Swap32(uint32_t * inUInt32)
+{
+ *inUInt32 = BSWAP32(*inUInt32);
+}
+
diff --git a/codec/EndianPortable.h b/codec/EndianPortable.h
new file mode 100755
index 0000000..bec8d5c
--- /dev/null
+++ b/codec/EndianPortable.h
@@ -0,0 +1,63 @@
+/*
+ * Copyright (c) 2011 Apple Inc. All rights reserved.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_START@
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_END@
+ */
+
+//
+// EndianPortable.h
+//
+// Copyright 2011 Apple Inc. All rights reserved.
+//
+
+#ifndef _EndianPortable_h
+#define _EndianPortable_h
+
+#ifdef _MSC_VER
+#include <msstdint.h>
+#else
+#include <stdint.h>
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+uint16_t Swap16NtoB(uint16_t inUInt16);
+uint16_t Swap16BtoN(uint16_t inUInt16);
+
+uint32_t Swap32NtoB(uint32_t inUInt32);
+uint32_t Swap32BtoN(uint32_t inUInt32);
+
+uint64_t Swap64BtoN(uint64_t inUInt64);
+uint64_t Swap64NtoB(uint64_t inUInt64);
+
+float SwapFloat32BtoN(float in);
+float SwapFloat32NtoB(float in);
+
+double SwapFloat64BtoN(double in);
+double SwapFloat64NtoB(double in);
+
+void Swap16(uint16_t * inUInt16);
+void Swap24(uint8_t * inUInt24);
+void Swap32(uint32_t * inUInt32);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/codec/ReadMe.txt b/codec/ReadMe.txt
new file mode 100755
index 0000000..da80a84
--- /dev/null
+++ b/codec/ReadMe.txt
@@ -0,0 +1,22 @@
+========================================================================
+ STATIC LIBRARY : alac Project Overview
+========================================================================
+
+AppWizard has created this alac library project for you.
+
+No source files were created as part of your project.
+
+
+alac.vcproj
+ This is the main project file for VC++ projects generated using an Application Wizard.
+ It contains information about the version of Visual C++ that generated the file, and
+ information about the platforms, configurations, and project features selected with the
+ Application Wizard.
+
+/////////////////////////////////////////////////////////////////////////////
+Other notes:
+
+AppWizard uses "TODO:" comments to indicate parts of the source code you
+should add to or customize.
+
+/////////////////////////////////////////////////////////////////////////////
diff --git a/codec/ag_dec.c b/codec/ag_dec.c
new file mode 100755
index 0000000..2214c94
--- /dev/null
+++ b/codec/ag_dec.c
@@ -0,0 +1,362 @@
+/*
+ * Copyright (c) 2011 Apple Inc. All rights reserved.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_START@
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_END@
+ */
+
+/*
+ File: ag_dec.c
+
+ Contains: Adaptive Golomb decode routines.
+
+ Copyright: (c) 2001-2011 Apple, Inc.
+*/
+
+#include "aglib.h"
+#include "ALACBitUtilities.h"
+#include "ALACAudioTypes.h"
+
+#include <math.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#if __GNUC__ && TARGET_OS_MAC
+ #if __POWERPC__
+ #include <ppc_intrinsics.h>
+ #else
+ #include <libkern/OSByteOrder.h>
+ #endif
+#endif
+
+#define CODE_TO_LONG_MAXBITS 32
+#define N_MAX_MEAN_CLAMP 0xffff
+#define N_MEAN_CLAMP_VAL 0xffff
+#define REPORT_VAL 40
+
+#if __GNUC__
+#define ALWAYS_INLINE __attribute__((always_inline))
+#else
+#define ALWAYS_INLINE
+#endif
+
+/* And on the subject of the CodeWarrior x86 compiler and inlining, I reworked a lot of this
+ to help the compiler out. In many cases this required manual inlining or a macro. Sorry
+ if it is ugly but the performance gains are well worth it.
+ - WSK 5/19/04
+*/
+
+void set_standard_ag_params(AGParamRecPtr params, uint32_t fullwidth, uint32_t sectorwidth)
+{
+ /* Use
+ fullwidth = sectorwidth = numOfSamples, for analog 1-dimensional type-short data,
+ but use
+ fullwidth = full image width, sectorwidth = sector (patch) width
+ for such as image (2-dim.) data.
+ */
+ set_ag_params( params, MB0, PB0, KB0, fullwidth, sectorwidth, MAX_RUN_DEFAULT );
+}
+
+void set_ag_params(AGParamRecPtr params, uint32_t m, uint32_t p, uint32_t k, uint32_t f, uint32_t s, uint32_t maxrun)
+{
+ params->mb = params->mb0 = m;
+ params->pb = p;
+ params->kb = k;
+ params->wb = (1u<<params->kb)-1;
+ params->qb = QB-params->pb;
+ params->fw = f;
+ params->sw = s;
+ params->maxrun = maxrun;
+}
+
+#if PRAGMA_MARK
+#pragma mark -
+#endif
+
+
+// note: implementing this with some kind of "count leading zeros" assembly is a big performance win
+static inline int32_t lead( int32_t m )
+{
+ long j;
+ unsigned long c = (1ul << 31);
+
+ for(j=0; j < 32; j++)
+ {
+ if((c & m) != 0)
+ break;
+ c >>= 1;
+ }
+ return (j);
+}
+
+#define arithmin(a, b) ((a) < (b) ? (a) : (b))
+
+static inline int32_t ALWAYS_INLINE lg3a( int32_t x)
+{
+ int32_t result;
+
+ x += 3;
+ result = lead(x);
+
+ return 31 - result;
+}
+
+static inline uint32_t ALWAYS_INLINE read32bit( uint8_t * buffer )
+{
+ // embedded CPUs typically can't read unaligned 32-bit words so just read the bytes
+ uint32_t value;
+
+ value = ((uint32_t)buffer[0] << 24) | ((uint32_t)buffer[1] << 16) |
+ ((uint32_t)buffer[2] << 8) | (uint32_t)buffer[3];
+ return value;
+
+}
+
+#if PRAGMA_MARK
+#pragma mark -
+#endif
+
+#define get_next_fromlong(inlong, suff) ((inlong) >> (32 - (suff)))
+
+
+static inline uint32_t ALWAYS_INLINE
+getstreambits( uint8_t *in, int32_t bitoffset, int32_t numbits )
+{
+ uint32_t load1, load2;
+ uint32_t byteoffset = bitoffset / 8;
+ uint32_t result;
+
+ //Assert( numbits <= 32 );
+
+ load1 = read32bit( in + byteoffset );
+
+ if ( (numbits + (bitoffset & 0x7)) > 32)
+ {
+ int32_t load2shift;
+
+ result = load1 << (bitoffset & 0x7);
+ load2 = (uint32_t) in[byteoffset+4];
+ load2shift = (8-(numbits + (bitoffset & 0x7)-32));
+ load2 >>= load2shift;
+ result >>= (32-numbits);
+ result |= load2;
+ }
+ else
+ {
+ result = load1 >> (32-numbits-(bitoffset & 7));
+ }
+
+ // a shift of >= "the number of bits in the type of the value being shifted" results in undefined
+ // behavior so don't try to shift by 32
+ if ( numbits != (sizeof(result) * 8) )
+ result &= ~(0xfffffffful << numbits);
+
+ return result;
+}
+
+
+static inline int32_t dyn_get(unsigned char *in, uint32_t *bitPos, uint32_t m, uint32_t k)
+{
+ uint32_t tempbits = *bitPos;
+ uint32_t result;
+ uint32_t pre = 0, v;
+ uint32_t streamlong;
+
+ streamlong = read32bit( in + (tempbits >> 3) );
+ streamlong <<= (tempbits & 7);
+
+ /* find the number of bits in the prefix */
+ {
+ uint32_t notI = ~streamlong;
+ pre = lead( notI);
+ }
+
+ if(pre >= MAX_PREFIX_16)
+ {
+ pre = MAX_PREFIX_16;
+ tempbits += pre;
+ streamlong <<= pre;
+ result = get_next_fromlong(streamlong,MAX_DATATYPE_BITS_16);
+ tempbits += MAX_DATATYPE_BITS_16;
+
+ }
+ else
+ {
+ // all of the bits must fit within the long we have loaded
+ //Assert(pre+1+k <= 32);
+
+ tempbits += pre;
+ tempbits += 1;
+ streamlong <<= pre+1;
+ v = get_next_fromlong(streamlong, k);
+ tempbits += k;
+
+ result = pre*m + v-1;
+
+ if(v<2) {
+ result -= (v-1);
+ tempbits -= 1;
+ }
+ }
+
+ *bitPos = tempbits;
+ return result;
+}
+
+
+static inline int32_t dyn_get_32bit( uint8_t * in, uint32_t * bitPos, int32_t m, int32_t k, int32_t maxbits )
+{
+ uint32_t tempbits = *bitPos;
+ uint32_t v;
+ uint32_t streamlong;
+ uint32_t result;
+
+ streamlong = read32bit( in + (tempbits >> 3) );
+ streamlong <<= (tempbits & 7);
+
+ /* find the number of bits in the prefix */
+ {
+ uint32_t notI = ~streamlong;
+ result = lead( notI);
+ }
+
+ if(result >= MAX_PREFIX_32)
+ {
+ result = getstreambits(in, tempbits+MAX_PREFIX_32, maxbits);
+ tempbits += MAX_PREFIX_32 + maxbits;
+ }
+ else
+ {
+ /* all of the bits must fit within the long we have loaded*/
+ //Assert(k<=14);
+ //Assert(result<MAX_PREFIX_32);
+ //Assert(result+1+k <= 32);
+
+ tempbits += result;
+ tempbits += 1;
+
+ if (k != 1)
+ {
+ streamlong <<= result+1;
+ v = get_next_fromlong(streamlong, k);
+ tempbits += k;
+ tempbits -= 1;
+ result = result*m;
+
+ if(v>=2)
+ {
+ result += (v-1);
+ tempbits += 1;
+ }
+ }
+ }
+
+ *bitPos = tempbits;
+
+ return result;
+}
+
+int32_t dyn_decomp( AGParamRecPtr params, BitBuffer * bitstream, int32_t * pc, int32_t numSamples, int32_t maxSize, uint32_t * outNumBits )
+{
+ uint8_t *in;
+ int32_t *outPtr = pc;
+ uint32_t bitPos, startPos, maxPos;
+ uint32_t j, m, k, n, c, mz;
+ int32_t del, zmode;
+ uint32_t mb;
+ uint32_t pb_local = params->pb;
+ uint32_t kb_local = params->kb;
+ uint32_t wb_local = params->wb;
+ int32_t status;
+
+ RequireAction( (bitstream != nil) && (pc != nil) && (outNumBits != nil), return kALAC_ParamError; );
+ *outNumBits = 0;
+
+ in = bitstream->cur;
+ startPos = bitstream->bitIndex;
+ maxPos = bitstream->byteSize * 8;
+ bitPos = startPos;
+
+ mb = params->mb0;
+ zmode = 0;
+
+ c = 0;
+ status = ALAC_noErr;
+
+ while (c < numSamples)
+ {
+ // bail if we've run off the end of the buffer
+ RequireAction( bitPos < maxPos, status = kALAC_ParamError; goto Exit; );
+
+ m = (mb)>>QBSHIFT;
+ k = lg3a(m);
+
+ k = arithmin(k, kb_local);
+ m = (1<<k)-1;
+
+ n = dyn_get_32bit( in, &bitPos, m, k, maxSize );
+
+ // least significant bit is sign bit
+ {
+ uint32_t ndecode = n + zmode;
+ int32_t multiplier = (- (ndecode&1));
+
+ multiplier |= 1;
+ del = ((ndecode+1) >> 1) * (multiplier);
+ }
+
+ *outPtr++ = del;
+
+ c++;
+
+ mb = pb_local*(n+zmode) + mb - ((pb_local*mb)>>QBSHIFT);
+
+ // update mean tracking
+ if (n > N_MAX_MEAN_CLAMP)
+ mb = N_MEAN_CLAMP_VAL;
+
+ zmode = 0;
+
+ if (((mb << MMULSHIFT) < QB) && (c < numSamples))
+ {
+ zmode = 1;
+ k = lead(mb) - BITOFF+((mb+MOFF)>>MDENSHIFT);
+ mz = ((1<<k)-1) & wb_local;
+
+ n = dyn_get(in, &bitPos, mz, k);
+
+ RequireAction(c+n <= numSamples, status = kALAC_ParamError; goto Exit; );
+
+ for(j=0; j < n; j++)
+ {
+ *outPtr++ = 0;
+ ++c;
+ }
+
+ if(n >= 65535)
+ zmode = 0;
+
+ mb = 0;
+ }
+ }
+
+Exit:
+ *outNumBits = (bitPos - startPos);
+ BitBufferAdvance( bitstream, *outNumBits );
+ RequireAction( bitstream->cur <= bitstream->end, status = kALAC_ParamError; );
+
+ return status;
+}
diff --git a/codec/ag_enc.c b/codec/ag_enc.c
new file mode 100755
index 0000000..2dfe999
--- /dev/null
+++ b/codec/ag_enc.c
@@ -0,0 +1,370 @@
+/*
+ * Copyright (c) 2011 Apple Inc. All rights reserved.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_START@
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_END@
+ */
+
+/*
+ File: ag_enc.c
+
+ Contains: Adaptive Golomb encode routines.
+
+ Copyright: (c) 2001-2011 Apple, Inc.
+*/
+
+#include "aglib.h"
+#include "ALACBitUtilities.h"
+#include "EndianPortable.h"
+#include "ALACAudioTypes.h"
+
+#include <math.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#if __GNUC__ && TARGET_OS_MAC
+ #if __POWERPC__
+ #include <ppc_intrinsics.h>
+ #else
+ #include <libkern/OSByteOrder.h>
+ #endif
+#endif
+
+#define CODE_TO_LONG_MAXBITS 32
+#define N_MAX_MEAN_CLAMP 0xffff
+#define N_MEAN_CLAMP_VAL 0xffff
+#define REPORT_VAL 40
+
+#if __GNUC__
+#define ALWAYS_INLINE __attribute__((always_inline))
+#else
+#define ALWAYS_INLINE
+#endif
+
+
+/* And on the subject of the CodeWarrior x86 compiler and inlining, I reworked a lot of this
+ to help the compiler out. In many cases this required manual inlining or a macro. Sorry
+ if it is ugly but the performance gains are well worth it.
+ - WSK 5/19/04
+*/
+
+// note: implementing this with some kind of "count leading zeros" assembly is a big performance win
+static inline int32_t lead( int32_t m )
+{
+ long j;
+ unsigned long c = (1ul << 31);
+
+ for(j=0; j < 32; j++)
+ {
+ if((c & m) != 0)
+ break;
+ c >>= 1;
+ }
+ return (j);
+}
+
+#define arithmin(a, b) ((a) < (b) ? (a) : (b))
+
+static inline int32_t ALWAYS_INLINE lg3a( int32_t x)
+{
+ int32_t result;
+
+ x += 3;
+ result = lead(x);
+
+ return 31 - result;
+}
+
+static inline int32_t ALWAYS_INLINE abs_func( int32_t a )
+{
+ // note: the CW PPC intrinsic __abs() turns into these instructions so no need to try and use it
+ int32_t isneg = a >> 31;
+ int32_t xorval = a ^ isneg;
+ int32_t result = xorval-isneg;
+
+ return result;
+}
+
+static inline uint32_t ALWAYS_INLINE read32bit( uint8_t * buffer )
+{
+ // embedded CPUs typically can't read unaligned 32-bit words so just read the bytes
+ uint32_t value;
+
+ value = ((uint32_t)buffer[0] << 24) | ((uint32_t)buffer[1] << 16) |
+ ((uint32_t)buffer[2] << 8) | (uint32_t)buffer[3];
+ return value;
+}
+
+#if PRAGMA_MARK
+#pragma mark -
+#endif
+
+static inline int32_t dyn_code(int32_t m, int32_t k, int32_t n, uint32_t *outNumBits)
+{
+ uint32_t div, mod, de;
+ uint32_t numBits;
+ uint32_t value;
+
+ //Assert( n >= 0 );
+
+ div = n/m;
+
+ if(div >= MAX_PREFIX_16)
+ {
+ numBits = MAX_PREFIX_16 + MAX_DATATYPE_BITS_16;
+ value = (((1<<MAX_PREFIX_16)-1)<<MAX_DATATYPE_BITS_16) + n;
+ }
+ else
+ {
+ mod = n%m;
+ de = (mod == 0);
+ numBits = div + k + 1 - de;
+ value = (((1<<div)-1)<<(numBits-div)) + mod + 1 - de;
+
+ // if coding this way is bigger than doing escape, then do escape
+ if (numBits > MAX_PREFIX_16 + MAX_DATATYPE_BITS_16)
+ {
+ numBits = MAX_PREFIX_16 + MAX_DATATYPE_BITS_16;
+ value = (((1<<MAX_PREFIX_16)-1)<<MAX_DATATYPE_BITS_16) + n;
+ }
+ }
+
+ *outNumBits = numBits;
+
+ return (int32_t) value;
+}
+
+
+static inline int32_t dyn_code_32bit(int32_t maxbits, uint32_t m, uint32_t k, uint32_t n, uint32_t *outNumBits, uint32_t *outValue, uint32_t *overflow, uint32_t *overflowbits)
+{
+ uint32_t div, mod, de;
+ uint32_t numBits;
+ uint32_t value;
+ int32_t didOverflow = 0;
+
+ div = n/m;
+
+ if (div < MAX_PREFIX_32)
+ {
+ mod = n - (m * div);
+
+ de = (mod == 0);
+ numBits = div + k + 1 - de;
+ value = (((1<<div)-1)<<(numBits-div)) + mod + 1 - de;
+ if (numBits > 25)
+ goto codeasescape;
+ }
+ else
+ {
+codeasescape:
+ numBits = MAX_PREFIX_32;
+ value = (((1<<MAX_PREFIX_32)-1));
+ *overflow = n;
+ *overflowbits = maxbits;
+ didOverflow = 1;
+ }
+
+ *outNumBits = numBits;
+ *outValue = value;
+
+ return didOverflow;
+}
+
+
+static inline void ALWAYS_INLINE dyn_jam_noDeref(unsigned char *out, uint32_t bitPos, uint32_t numBits, uint32_t value)
+{
+ uint32_t *i = (uint32_t *)(out + (bitPos >> 3));
+ uint32_t mask;
+ uint32_t curr;
+ uint32_t shift;
+
+ //Assert( numBits <= 32 );
+
+ curr = *i;
+ curr = Swap32NtoB( curr );
+
+ shift = 32 - (bitPos & 7) - numBits;
+
+ mask = ~0u >> (32 - numBits); // mask must be created in two steps to avoid compiler sequencing ambiguity
+ mask <<= shift;
+
+ value = (value << shift) & mask;
+ value |= curr & ~mask;
+
+ *i = Swap32BtoN( value );
+}
+
+
+static inline void ALWAYS_INLINE dyn_jam_noDeref_large(unsigned char *out, uint32_t bitPos, uint32_t numBits, uint32_t value)
+{
+ uint32_t * i = (uint32_t *)(out + (bitPos>>3));
+ uint32_t w;
+ uint32_t curr;
+ uint32_t mask;
+ int32_t shiftvalue = (32 - (bitPos&7) - numBits);
+
+ //Assert(numBits <= 32);
+
+ curr = *i;
+ curr = Swap32NtoB( curr );
+
+ if (shiftvalue < 0)
+ {
+ uint8_t tailbyte;
+ uint8_t *tailptr;
+
+ w = value >> -shiftvalue;
+ mask = ~0u >> -shiftvalue;
+ w |= (curr & ~mask);
+
+ tailptr = ((uint8_t *)i) + 4;
+ tailbyte = (value << ((8+shiftvalue))) & 0xff;
+ *tailptr = (uint8_t)tailbyte;
+ }
+ else
+ {
+ mask = ~0u >> (32 - numBits);
+ mask <<= shiftvalue; // mask must be created in two steps to avoid compiler sequencing ambiguity
+
+ w = (value << shiftvalue) & mask;
+ w |= curr & ~mask;
+ }
+
+ *i = Swap32BtoN( w );
+}
+
+
+int32_t dyn_comp( AGParamRecPtr params, int32_t * pc, BitBuffer * bitstream, int32_t numSamples, int32_t bitSize, uint32_t * outNumBits )
+{
+ unsigned char * out;
+ uint32_t bitPos, startPos;
+ uint32_t m, k, n, c, mz, nz;
+ uint32_t numBits;
+ uint32_t value;
+ int32_t del, zmode;
+ uint32_t overflow, overflowbits;
+ int32_t status;
+
+ // shadow the variables in params so there's not the dereferencing overhead
+ uint32_t mb, pb, kb, wb;
+ int32_t rowPos = 0;
+ int32_t rowSize = params->sw;
+ int32_t rowJump = (params->fw) - rowSize;
+ int32_t * inPtr = pc;
+
+ *outNumBits = 0;
+ RequireAction( (bitSize >= 1) && (bitSize <= 32), return kALAC_ParamError; );
+
+ out = bitstream->cur;
+ startPos = bitstream->bitIndex;
+ bitPos = startPos;
+
+ mb = params->mb = params->mb0;
+ pb = params->pb;
+ kb = params->kb;
+ wb = params->wb;
+ zmode = 0;
+
+ c=0;
+ status = ALAC_noErr;
+
+ while (c < numSamples)
+ {
+ m = mb >> QBSHIFT;
+ k = lg3a(m);
+ if ( k > kb)
+ {
+ k = kb;
+ }
+ m = (1<<k)-1;
+
+ del = *inPtr++;
+ rowPos++;
+
+ n = (abs_func(del) << 1) - ((del >> 31) & 1) - zmode;
+ //Assert( 32-lead(n) <= bitSize );
+
+ if ( dyn_code_32bit(bitSize, m, k, n, &numBits, &value, &overflow, &overflowbits) )
+ {
+ dyn_jam_noDeref(out, bitPos, numBits, value);
+ bitPos += numBits;
+ dyn_jam_noDeref_large(out, bitPos, overflowbits, overflow);
+ bitPos += overflowbits;
+ }
+ else
+ {
+ dyn_jam_noDeref(out, bitPos, numBits, value);
+ bitPos += numBits;
+ }
+
+ c++;
+ if ( rowPos >= rowSize)
+ {
+ rowPos = 0;
+ inPtr += rowJump;
+ }
+
+ mb = pb * (n + zmode) + mb - ((pb *mb)>>QBSHIFT);
+
+ // update mean tracking if it's overflowed
+ if (n > N_MAX_MEAN_CLAMP)
+ mb = N_MEAN_CLAMP_VAL;
+
+ zmode = 0;
+
+ RequireAction(c <= numSamples, status = kALAC_ParamError; goto Exit; );
+
+ if (((mb << MMULSHIFT) < QB) && (c < numSamples))
+ {
+ zmode = 1;
+ nz = 0;
+
+ while(c<numSamples && *inPtr == 0)
+ {
+ /* Take care of wrap-around globals. */
+ ++inPtr;
+ ++nz;
+ ++c;
+ if ( ++rowPos >= rowSize)
+ {
+ rowPos = 0;
+ inPtr += rowJump;
+ }
+
+ if(nz >= 65535)
+ {
+ zmode = 0;
+ break;
+ }
+ }
+
+ k = lead(mb) - BITOFF+((mb+MOFF)>>MDENSHIFT);
+ mz = ((1<<k)-1) & wb;
+
+ value = dyn_code(mz, k, nz, &numBits);
+ dyn_jam_noDeref(out, bitPos, numBits, value);
+ bitPos += numBits;
+
+ mb = 0;
+ }
+ }
+
+ *outNumBits = (bitPos - startPos);
+ BitBufferAdvance( bitstream, *outNumBits );
+
+Exit:
+ return status;
+}
diff --git a/codec/aglib.h b/codec/aglib.h
new file mode 100755
index 0000000..ef15a11
--- /dev/null
+++ b/codec/aglib.h
@@ -0,0 +1,85 @@
+/*
+ * Copyright (c) 2011 Apple Inc. All rights reserved.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_START@
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_END@
+ */
+
+/*
+ File: aglib.h
+
+ Copyright: (C) 2001-2011 Apple, Inc.
+*/
+
+#ifndef AGLIB_H
+#define AGLIB_H
+
+#ifdef _MSC_VER
+#include <msstdint.h>
+#else
+#include <stdint.h>
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define QBSHIFT 9
+#define QB (1<<QBSHIFT)
+#define PB0 40
+#define MB0 10
+#define KB0 14
+#define MAX_RUN_DEFAULT 255
+
+#define MMULSHIFT 2
+#define MDENSHIFT (QBSHIFT - MMULSHIFT - 1)
+#define MOFF ((1<<(MDENSHIFT-2)))
+
+#define BITOFF 24
+
+/* Max. prefix of 1's. */
+#define MAX_PREFIX_16 9
+#define MAX_PREFIX_TOLONG_16 15
+#define MAX_PREFIX_32 9
+
+/* Max. bits in 16-bit data type */
+#define MAX_DATATYPE_BITS_16 16
+
+typedef struct AGParamRec
+{
+ uint32_t mb, mb0, pb, kb, wb, qb;
+ uint32_t fw, sw;
+
+ uint32_t maxrun;
+
+ // fw = 1, sw = 1;
+
+} AGParamRec, *AGParamRecPtr;
+
+struct BitBuffer;
+
+void set_standard_ag_params(AGParamRecPtr params, uint32_t fullwidth, uint32_t sectorwidth);
+void set_ag_params(AGParamRecPtr params, uint32_t m, uint32_t p, uint32_t k, uint32_t f, uint32_t s, uint32_t maxrun);
+
+int32_t dyn_comp(AGParamRecPtr params, int32_t * pc, struct BitBuffer * bitstream, int32_t numSamples, int32_t bitSize, uint32_t * outNumBits);
+int32_t dyn_decomp(AGParamRecPtr params, struct BitBuffer * bitstream, int32_t * pc, int32_t numSamples, int32_t maxSize, uint32_t * outNumBits);
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif //#ifndef AGLIB_H
diff --git a/codec/alac.sln b/codec/alac.sln
new file mode 100755
index 0000000..da0f049
--- /dev/null
+++ b/codec/alac.sln
@@ -0,0 +1,20 @@
+
+Microsoft Visual Studio Solution File, Format Version 10.00
+# Visual Studio 2008
+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "alac", "alac.vcproj", "{4733A8A1-D7B1-42FA-AA17-EA4128618E1E}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|Win32 = Debug|Win32
+ Release|Win32 = Release|Win32
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {4733A8A1-D7B1-42FA-AA17-EA4128618E1E}.Debug|Win32.ActiveCfg = Debug|Win32
+ {4733A8A1-D7B1-42FA-AA17-EA4128618E1E}.Debug|Win32.Build.0 = Debug|Win32
+ {4733A8A1-D7B1-42FA-AA17-EA4128618E1E}.Release|Win32.ActiveCfg = Release|Win32
+ {4733A8A1-D7B1-42FA-AA17-EA4128618E1E}.Release|Win32.Build.0 = Release|Win32
+ EndGlobalSection
+ GlobalSection(SolutionProperties) = preSolution
+ HideSolutionNode = FALSE
+ EndGlobalSection
+EndGlobal
diff --git a/codec/alac.vcproj b/codec/alac.vcproj
new file mode 100755
index 0000000..67bd062
--- /dev/null
+++ b/codec/alac.vcproj
@@ -0,0 +1,377 @@
+<?xml version="1.0" encoding="Windows-1252"?>
+<VisualStudioProject
+ ProjectType="Visual C++"
+ Version="9.00"
+ Name="alac"
+ ProjectGUID="{4733A8A1-D7B1-42FA-AA17-EA4128618E1E}"
+ RootNamespace="alac"
+ Keyword="Win32Proj"
+ TargetFrameworkVersion="196613"
+ >
+ <Platforms>
+ <Platform
+ Name="Win32"
+ />
+ </Platforms>
+ <ToolFiles>
+ </ToolFiles>
+ <Configurations>
+ <Configuration
+ Name="Debug|Win32"
+ OutputDirectory="$(SolutionDir)$(ConfigurationName)"
+ IntermediateDirectory="$(ConfigurationName)"
+ ConfigurationType="4"
+ CharacterSet="1"
+ >
+ <Tool
+ Name="VCPreBuildEventTool"
+ />
+ <Tool
+ Name="VCCustomBuildTool"
+ />
+ <Tool
+ Name="VCXMLDataGeneratorTool"
+ />
+ <Tool
+ Name="VCWebServiceProxyGeneratorTool"
+ />
+ <Tool
+ Name="VCMIDLTool"
+ />
+ <Tool
+ Name="VCCLCompilerTool"
+ Optimization="0"
+ AdditionalIncludeDirectories=".\"
+ PreprocessorDefinitions="WIN32;_DEBUG;_LIB;TARGET_OS_WIN32"
+ MinimalRebuild="true"
+ BasicRuntimeChecks="3"
+ RuntimeLibrary="3"
+ UsePrecompiledHeader="0"
+ WarningLevel="3"
+ DebugInformationFormat="4"
+ />
+ <Tool
+ Name="VCManagedResourceCompilerTool"
+ />
+ <Tool
+ Name="VCResourceCompilerTool"
+ />
+ <Tool
+ Name="VCPreLinkEventTool"
+ />
+ <Tool
+ Name="VCLibrarianTool"
+ />
+ <Tool
+ Name="VCALinkTool"
+ />
+ <Tool
+ Name="VCXDCMakeTool"
+ />
+ <Tool
+ Name="VCBscMakeTool"
+ />
+ <Tool
+ Name="VCFxCopTool"
+ />
+ <Tool
+ Name="VCPostBuildEventTool"
+ />
+ </Configuration>
+ <Configuration
+ Name="Release|Win32"
+ OutputDirectory="$(SolutionDir)$(ConfigurationName)"
+ IntermediateDirectory="$(ConfigurationName)"
+ ConfigurationType="4"
+ CharacterSet="1"
+ WholeProgramOptimization="1"
+ >
+ <Tool
+ Name="VCPreBuildEventTool"
+ />
+ <Tool
+ Name="VCCustomBuildTool"
+ />
+ <Tool
+ Name="VCXMLDataGeneratorTool"
+ />
+ <Tool
+ Name="VCWebServiceProxyGeneratorTool"
+ />
+ <Tool
+ Name="VCMIDLTool"
+ />
+ <Tool
+ Name="VCCLCompilerTool"
+ Optimization="2"
+ EnableIntrinsicFunctions="true"
+ AdditionalIncludeDirectories=".\"
+ PreprocessorDefinitions="WIN32;NDEBUG;_LIB;TARGET_OS_WIN32"
+ RuntimeLibrary="2"
+ EnableFunctionLevelLinking="true"
+ UsePrecompiledHeader="0"
+ WarningLevel="3"
+ DebugInformationFormat="3"
+ />
+ <Tool
+ Name="VCManagedResourceCompilerTool"
+ />
+ <Tool
+ Name="VCResourceCompilerTool"
+ />
+ <Tool
+ Name="VCPreLinkEventTool"
+ />
+ <Tool
+ Name="VCLibrarianTool"
+ />
+ <Tool
+ Name="VCALinkTool"
+ />
+ <Tool
+ Name="VCXDCMakeTool"
+ />
+ <Tool
+ Name="VCBscMakeTool"
+ />
+ <Tool
+ Name="VCFxCopTool"
+ />
+ <Tool
+ Name="VCPostBuildEventTool"
+ />
+ </Configuration>
+ </Configurations>
+ <References>
+ </References>
+ <Files>
+ <Filter
+ Name="Source Files"
+ Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
+ UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
+ >
+ <File
+ RelativePath=".\ag_dec.c"
+ >
+ <FileConfiguration
+ Name="Debug|Win32"
+ >
+ <Tool
+ Name="VCCLCompilerTool"
+ CompileAs="2"
+ />
+ </FileConfiguration>
+ <FileConfiguration
+ Name="Release|Win32"
+ >
+ <Tool
+ Name="VCCLCompilerTool"
+ CompileAs="2"
+ />
+ </FileConfiguration>
+ </File>
+ <File
+ RelativePath=".\ag_enc.c"
+ >
+ <FileConfiguration
+ Name="Debug|Win32"
+ >
+ <Tool
+ Name="VCCLCompilerTool"
+ CompileAs="2"
+ />
+ </FileConfiguration>
+ <FileConfiguration
+ Name="Release|Win32"
+ >
+ <Tool
+ Name="VCCLCompilerTool"
+ CompileAs="2"
+ />
+ </FileConfiguration>
+ </File>
+ <File
+ RelativePath=".\ALACBitUtilities.c"
+ >
+ <FileConfiguration
+ Name="Debug|Win32"
+ >
+ <Tool
+ Name="VCCLCompilerTool"
+ CompileAs="2"
+ />
+ </FileConfiguration>
+ <FileConfiguration
+ Name="Release|Win32"
+ >
+ <Tool
+ Name="VCCLCompilerTool"
+ CompileAs="2"
+ />
+ </FileConfiguration>
+ </File>
+ <File
+ RelativePath=".\ALACDecoder.cpp"
+ >
+ </File>
+ <File
+ RelativePath=".\ALACEncoder.cpp"
+ >
+ </File>
+ <File
+ RelativePath=".\dp_dec.c"
+ >
+ <FileConfiguration
+ Name="Debug|Win32"
+ >
+ <Tool
+ Name="VCCLCompilerTool"
+ CompileAs="2"
+ />
+ </FileConfiguration>
+ <FileConfiguration
+ Name="Release|Win32"
+ >
+ <Tool
+ Name="VCCLCompilerTool"
+ CompileAs="2"
+ />
+ </FileConfiguration>
+ </File>
+ <File
+ RelativePath=".\dp_enc.c"
+ >
+ <FileConfiguration
+ Name="Debug|Win32"
+ >
+ <Tool
+ Name="VCCLCompilerTool"
+ CompileAs="2"
+ />
+ </FileConfiguration>
+ <FileConfiguration
+ Name="Release|Win32"
+ >
+ <Tool
+ Name="VCCLCompilerTool"
+ CompileAs="2"
+ />
+ </FileConfiguration>
+ </File>
+ <File
+ RelativePath=".\EndianPortable.c"
+ >
+ <FileConfiguration
+ Name="Debug|Win32"
+ >
+ <Tool
+ Name="VCCLCompilerTool"
+ CompileAs="2"
+ />
+ </FileConfiguration>
+ <FileConfiguration
+ Name="Release|Win32"
+ >
+ <Tool
+ Name="VCCLCompilerTool"
+ CompileAs="2"
+ />
+ </FileConfiguration>
+ </File>
+ <File
+ RelativePath=".\matrix_dec.c"
+ >
+ <FileConfiguration
+ Name="Debug|Win32"
+ >
+ <Tool
+ Name="VCCLCompilerTool"
+ CompileAs="2"
+ />
+ </FileConfiguration>
+ <FileConfiguration
+ Name="Release|Win32"
+ >
+ <Tool
+ Name="VCCLCompilerTool"
+ CompileAs="2"
+ />
+ </FileConfiguration>
+ </File>
+ <File
+ RelativePath=".\matrix_enc.c"
+ >
+ <FileConfiguration
+ Name="Debug|Win32"
+ >
+ <Tool
+ Name="VCCLCompilerTool"
+ CompileAs="2"
+ />
+ </FileConfiguration>
+ <FileConfiguration
+ Name="Release|Win32"
+ >
+ <Tool
+ Name="VCCLCompilerTool"
+ CompileAs="2"
+ />
+ </FileConfiguration>
+ </File>
+ </Filter>
+ <Filter
+ Name="Header Files"
+ Filter="h;hpp;hxx;hm;inl;inc;xsd"
+ UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
+ >
+ <File
+ RelativePath=".\aglib.h"
+ >
+ </File>
+ <File
+ RelativePath=".\ALACAudioTypes.h"
+ >
+ </File>
+ <File
+ RelativePath=".\ALACBitUtilities.h"
+ >
+ </File>
+ <File
+ RelativePath=".\ALACDecoder.h"
+ >
+ </File>
+ <File
+ RelativePath=".\ALACEncoder.h"
+ >
+ </File>
+ <File
+ RelativePath=".\dplib.h"
+ >
+ </File>
+ <File
+ RelativePath=".\EndianPortable.h"
+ >
+ </File>
+ <File
+ RelativePath=".\matrixlib.h"
+ >
+ </File>
+ <File
+ RelativePath=".\msstdint.h"
+ >
+ </File>
+ </Filter>
+ <Filter
+ Name="Resource Files"
+ Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
+ UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
+ >
+ </Filter>
+ <File
+ RelativePath=".\ReadMe.txt"
+ >
+ </File>
+ </Files>
+ <Globals>
+ </Globals>
+</VisualStudioProject>
diff --git a/codec/dp_dec.c b/codec/dp_dec.c
new file mode 100755
index 0000000..d153025
--- /dev/null
+++ b/codec/dp_dec.c
@@ -0,0 +1,381 @@
+/*
+ * Copyright (c) 2011 Apple Inc. All rights reserved.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_START@
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_END@
+ */
+
+/*
+ File: dp_dec.c
+
+ Contains: Dynamic Predictor decode routines
+
+ Copyright: (c) 2001-2011 Apple, Inc.
+*/
+
+
+#include "dplib.h"
+#include <string.h>
+
+#if __GNUC__
+#define ALWAYS_INLINE __attribute__((always_inline))
+#else
+#define ALWAYS_INLINE
+#endif
+
+#if TARGET_CPU_PPC && (__MWERKS__ >= 0x3200)
+// align loops to a 16 byte boundary to make the G5 happy
+#pragma function_align 16
+#define LOOP_ALIGN asm { align 16 }
+#else
+#define LOOP_ALIGN
+#endif
+
+static inline int32_t ALWAYS_INLINE sign_of_int( int32_t i )
+{
+ int32_t negishift;
+
+ negishift = ((uint32_t)-i) >> 31;
+ return negishift | (i >> 31);
+}
+
+void unpc_block( int32_t * pc1, int32_t * out, int32_t num, int16_t * coefs, int32_t numactive, uint32_t chanbits, uint32_t denshift )
+{
+ register int16_t a0, a1, a2, a3;
+ register int32_t b0, b1, b2, b3;
+ int32_t j, k, lim;
+ int32_t sum1, sg, sgn, top, dd;
+ int32_t * pout;
+ int32_t del, del0;
+ uint32_t chanshift = 32 - chanbits;
+ int32_t denhalf = 1<<(denshift-1);
+
+ out[0] = pc1[0];
+ if ( numactive == 0 )
+ {
+ // just copy if numactive == 0 (but don't bother if in/out pointers the same)
+ if ( (num > 1) && (pc1 != out) )
+ memcpy( &out[1], &pc1[1], (num - 1) * sizeof(int32_t) );
+ return;
+ }
+ if ( numactive == 31 )
+ {
+ // short-circuit if numactive == 31
+ int32_t prev;
+
+ /* this code is written such that the in/out buffers can be the same
+ to conserve buffer space on embedded devices like the iPod
+
+ (original code)
+ for ( j = 1; j < num; j++ )
+ del = pc1[j] + out[j-1];
+ out[j] = (del << chanshift) >> chanshift;
+ */
+ prev = out[0];
+ for ( j = 1; j < num; j++ )
+ {
+ del = pc1[j] + prev;
+ prev = (del << chanshift) >> chanshift;
+ out[j] = prev;
+ }
+ return;
+ }
+
+ for ( j = 1; j <= numactive; j++ )
+ {
+ del = pc1[j] + out[j-1];
+ out[j] = (del << chanshift) >> chanshift;
+ }
+
+ lim = numactive + 1;
+
+ if ( numactive == 4 )
+ {
+ // optimization for numactive == 4
+ register int16_t a0, a1, a2, a3;
+ register int32_t b0, b1, b2, b3;
+
+ a0 = coefs[0];
+ a1 = coefs[1];
+ a2 = coefs[2];
+ a3 = coefs[3];
+
+ for ( j = lim; j < num; j++ )
+ {
+ LOOP_ALIGN
+
+ top = out[j - lim];
+ pout = out + j - 1;
+
+ b0 = top - pout[0];
+ b1 = top - pout[-1];
+ b2 = top - pout[-2];
+ b3 = top - pout[-3];
+
+ sum1 = (denhalf - a0 * b0 - a1 * b1 - a2 * b2 - a3 * b3) >> denshift;
+
+ del = pc1[j];
+ del0 = del;
+ sg = sign_of_int(del);
+ del += top + sum1;
+
+ out[j] = (del << chanshift) >> chanshift;
+
+ if ( sg > 0 )
+ {
+ sgn = sign_of_int( b3 );
+ a3 -= sgn;
+ del0 -= (4 - 3) * ((sgn * b3) >> denshift);
+ if ( del0 <= 0 )
+ continue;
+
+ sgn = sign_of_int( b2 );
+ a2 -= sgn;
+ del0 -= (4 - 2) * ((sgn * b2) >> denshift);
+ if ( del0 <= 0 )
+ continue;
+
+ sgn = sign_of_int( b1 );
+ a1 -= sgn;
+ del0 -= (4 - 1) * ((sgn * b1) >> denshift);
+ if ( del0 <= 0 )
+ continue;
+
+ a0 -= sign_of_int( b0 );
+ }
+ else if ( sg < 0 )
+ {
+ // note: to avoid unnecessary negations, we flip the value of "sgn"
+ sgn = -sign_of_int( b3 );
+ a3 -= sgn;
+ del0 -= (4 - 3) * ((sgn * b3) >> denshift);
+ if ( del0 >= 0 )
+ continue;
+
+ sgn = -sign_of_int( b2 );
+ a2 -= sgn;
+ del0 -= (4 - 2) * ((sgn * b2) >> denshift);
+ if ( del0 >= 0 )
+ continue;
+
+ sgn = -sign_of_int( b1 );
+ a1 -= sgn;
+ del0 -= (4 - 1) * ((sgn * b1) >> denshift);
+ if ( del0 >= 0 )
+ continue;
+
+ a0 += sign_of_int( b0 );
+ }
+ }
+
+ coefs[0] = a0;
+ coefs[1] = a1;
+ coefs[2] = a2;
+ coefs[3] = a3;
+ }
+ else if ( numactive == 8 )
+ {
+ register int16_t a4, a5, a6, a7;
+ register int32_t b4, b5, b6, b7;
+
+ // optimization for numactive == 8
+ a0 = coefs[0];
+ a1 = coefs[1];
+ a2 = coefs[2];
+ a3 = coefs[3];
+ a4 = coefs[4];
+ a5 = coefs[5];
+ a6 = coefs[6];
+ a7 = coefs[7];
+
+ for ( j = lim; j < num; j++ )
+ {
+ LOOP_ALIGN
+
+ top = out[j - lim];
+ pout = out + j - 1;
+
+ b0 = top - (*pout--);
+ b1 = top - (*pout--);
+ b2 = top - (*pout--);
+ b3 = top - (*pout--);
+ b4 = top - (*pout--);
+ b5 = top - (*pout--);
+ b6 = top - (*pout--);
+ b7 = top - (*pout);
+ pout += 8;
+
+ sum1 = (denhalf - a0 * b0 - a1 * b1 - a2 * b2 - a3 * b3
+ - a4 * b4 - a5 * b5 - a6 * b6 - a7 * b7) >> denshift;
+
+ del = pc1[j];
+ del0 = del;
+ sg = sign_of_int(del);
+ del += top + sum1;
+
+ out[j] = (del << chanshift) >> chanshift;
+
+ if ( sg > 0 )
+ {
+ sgn = sign_of_int( b7 );
+ a7 -= sgn;
+ del0 -= 1 * ((sgn * b7) >> denshift);
+ if ( del0 <= 0 )
+ continue;
+
+ sgn = sign_of_int( b6 );
+ a6 -= sgn;
+ del0 -= 2 * ((sgn * b6) >> denshift);
+ if ( del0 <= 0 )
+ continue;
+
+ sgn = sign_of_int( b5 );
+ a5 -= sgn;
+ del0 -= 3 * ((sgn * b5) >> denshift);
+ if ( del0 <= 0 )
+ continue;
+
+ sgn = sign_of_int( b4 );
+ a4 -= sgn;
+ del0 -= 4 * ((sgn * b4) >> denshift);
+ if ( del0 <= 0 )
+ continue;
+
+ sgn = sign_of_int( b3 );
+ a3 -= sgn;
+ del0 -= 5 * ((sgn * b3) >> denshift);
+ if ( del0 <= 0 )
+ continue;
+
+ sgn = sign_of_int( b2 );
+ a2 -= sgn;
+ del0 -= 6 * ((sgn * b2) >> denshift);
+ if ( del0 <= 0 )
+ continue;
+
+ sgn = sign_of_int( b1 );
+ a1 -= sgn;
+ del0 -= 7 * ((sgn * b1) >> denshift);
+ if ( del0 <= 0 )
+ continue;
+
+ a0 -= sign_of_int( b0 );
+ }
+ else if ( sg < 0 )
+ {
+ // note: to avoid unnecessary negations, we flip the value of "sgn"
+ sgn = -sign_of_int( b7 );
+ a7 -= sgn;
+ del0 -= 1 * ((sgn * b7) >> denshift);
+ if ( del0 >= 0 )
+ continue;
+
+ sgn = -sign_of_int( b6 );
+ a6 -= sgn;
+ del0 -= 2 * ((sgn * b6) >> denshift);
+ if ( del0 >= 0 )
+ continue;
+
+ sgn = -sign_of_int( b5 );
+ a5 -= sgn;
+ del0 -= 3 * ((sgn * b5) >> denshift);
+ if ( del0 >= 0 )
+ continue;
+
+ sgn = -sign_of_int( b4 );
+ a4 -= sgn;
+ del0 -= 4 * ((sgn * b4) >> denshift);
+ if ( del0 >= 0 )
+ continue;
+
+ sgn = -sign_of_int( b3 );
+ a3 -= sgn;
+ del0 -= 5 * ((sgn * b3) >> denshift);
+ if ( del0 >= 0 )
+ continue;
+
+ sgn = -sign_of_int( b2 );
+ a2 -= sgn;
+ del0 -= 6 * ((sgn * b2) >> denshift);
+ if ( del0 >= 0 )
+ continue;
+
+ sgn = -sign_of_int( b1 );
+ a1 -= sgn;
+ del0 -= 7 * ((sgn * b1) >> denshift);
+ if ( del0 >= 0 )
+ continue;
+
+ a0 += sign_of_int( b0 );
+ }
+ }
+
+ coefs[0] = a0;
+ coefs[1] = a1;
+ coefs[2] = a2;
+ coefs[3] = a3;
+ coefs[4] = a4;
+ coefs[5] = a5;
+ coefs[6] = a6;
+ coefs[7] = a7;
+ }
+ else
+ {
+ // general case
+ for ( j = lim; j < num; j++ )
+ {
+ LOOP_ALIGN
+
+ sum1 = 0;
+ pout = out + j - 1;
+ top = out[j-lim];
+
+ for ( k = 0; k < numactive; k++ )
+ sum1 += coefs[k] * (pout[-k] - top);
+
+ del = pc1[j];
+ del0 = del;
+ sg = sign_of_int( del );
+ del += top + ((sum1 + denhalf) >> denshift);
+ out[j] = (del << chanshift) >> chanshift;
+
+ if ( sg > 0 )
+ {
+ for ( k = (numactive - 1); k >= 0; k-- )
+ {
+ dd = top - pout[-k];
+ sgn = sign_of_int( dd );
+ coefs[k] -= sgn;
+ del0 -= (numactive - k) * ((sgn * dd) >> denshift);
+ if ( del0 <= 0 )
+ break;
+ }
+ }
+ else if ( sg < 0 )
+ {
+ for ( k = (numactive - 1); k >= 0; k-- )
+ {
+ dd = top - pout[-k];
+ sgn = sign_of_int( dd );
+ coefs[k] += sgn;
+ del0 -= (numactive - k) * ((-sgn * dd) >> denshift);
+ if ( del0 >= 0 )
+ break;
+ }
+ }
+ }
+ }
+}
diff --git a/codec/dp_enc.c b/codec/dp_enc.c
new file mode 100755
index 0000000..869104c
--- /dev/null
+++ b/codec/dp_enc.c
@@ -0,0 +1,386 @@
+/*
+ * Copyright (c) 2011 Apple Inc. All rights reserved.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_START@
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_END@
+ */
+
+/*
+ File: dp_enc.c
+
+ Contains: Dynamic Predictor encode routines
+
+ Copyright: (c) 2001-2011 Apple, Inc.
+*/
+
+#include "dplib.h"
+#include <string.h>
+
+#if __GNUC__
+#define ALWAYS_INLINE __attribute__((always_inline))
+#else
+#define ALWAYS_INLINE
+#endif
+
+#if TARGET_CPU_PPC && (__MWERKS__ >= 0x3200)
+// align loops to a 16 byte boundary to make the G5 happy
+#pragma function_align 16
+#define LOOP_ALIGN asm { align 16 }
+#else
+#define LOOP_ALIGN
+#endif
+
+void init_coefs( int16_t * coefs, uint32_t denshift, int32_t numPairs )
+{
+ int32_t k;
+ int32_t den = 1 << denshift;
+
+ coefs[0] = (AINIT * den) >> 4;
+ coefs[1] = (BINIT * den) >> 4;
+ coefs[2] = (CINIT * den) >> 4;
+ for ( k = 3; k < numPairs; k++ )
+ coefs[k] = 0;
+}
+
+void copy_coefs( int16_t * srcCoefs, int16_t * dstCoefs, int32_t numPairs )
+{
+ int32_t k;
+
+ for ( k = 0; k < numPairs; k++ )
+ dstCoefs[k] = srcCoefs[k];
+}
+
+static inline int32_t ALWAYS_INLINE sign_of_int( int32_t i )
+{
+ int32_t negishift;
+
+ negishift = ((uint32_t)-i) >> 31;
+ return negishift | (i >> 31);
+}
+
+void pc_block( int32_t * in, int32_t * pc1, int32_t num, int16_t * coefs, int32_t numactive, uint32_t chanbits, uint32_t denshift )
+{
+ register int16_t a0, a1, a2, a3;
+ register int32_t b0, b1, b2, b3;
+ int32_t j, k, lim;
+ int32_t * pin;
+ int32_t sum1, dd;
+ int32_t sg, sgn;
+ int32_t top;
+ int32_t del, del0;
+ uint32_t chanshift = 32 - chanbits;
+ int32_t denhalf = 1 << (denshift - 1);
+
+ pc1[0] = in[0];
+ if ( numactive == 0 )
+ {
+ // just copy if numactive == 0 (but don't bother if in/out pointers the same)
+ if ( (num > 1) && (in != pc1) )
+ memcpy( &pc1[1], &in[1], (num - 1) * sizeof(int32_t) );
+ return;
+ }
+ if ( numactive == 31 )
+ {
+ // short-circuit if numactive == 31
+ for( j = 1; j < num; j++ )
+ {
+ del = in[j] - in[j-1];
+ pc1[j] = (del << chanshift) >> chanshift;
+ }
+ return;
+ }
+
+ for ( j = 1; j <= numactive; j++ )
+ {
+ del = in[j] - in[j-1];
+ pc1[j] = (del << chanshift) >> chanshift;
+ }
+
+ lim = numactive + 1;
+
+ if ( numactive == 4 )
+ {
+ // optimization for numactive == 4
+ a0 = coefs[0];
+ a1 = coefs[1];
+ a2 = coefs[2];
+ a3 = coefs[3];
+
+ for ( j = lim; j < num; j++ )
+ {
+ LOOP_ALIGN
+
+ top = in[j - lim];
+ pin = in + j - 1;
+
+ b0 = top - pin[0];
+ b1 = top - pin[-1];
+ b2 = top - pin[-2];
+ b3 = top - pin[-3];
+
+ sum1 = (denhalf - a0 * b0 - a1 * b1 - a2 * b2 - a3 * b3) >> denshift;
+
+ del = in[j] - top - sum1;
+ del = (del << chanshift) >> chanshift;
+ pc1[j] = del;
+ del0 = del;
+
+ sg = sign_of_int(del);
+ if ( sg > 0 )
+ {
+ sgn = sign_of_int( b3 );
+ a3 -= sgn;
+ del0 -= (4 - 3) * ((sgn * b3) >> denshift);
+ if ( del0 <= 0 )
+ continue;
+
+ sgn = sign_of_int( b2 );
+ a2 -= sgn;
+ del0 -= (4 - 2) * ((sgn * b2) >> denshift);
+ if ( del0 <= 0 )
+ continue;
+
+ sgn = sign_of_int( b1 );
+ a1 -= sgn;
+ del0 -= (4 - 1) * ((sgn * b1) >> denshift);
+ if ( del0 <= 0 )
+ continue;
+
+ a0 -= sign_of_int( b0 );
+ }
+ else if ( sg < 0 )
+ {
+ // note: to avoid unnecessary negations, we flip the value of "sgn"
+ sgn = -sign_of_int( b3 );
+ a3 -= sgn;
+ del0 -= (4 - 3) * ((sgn * b3) >> denshift);
+ if ( del0 >= 0 )
+ continue;
+
+ sgn = -sign_of_int( b2 );
+ a2 -= sgn;
+ del0 -= (4 - 2) * ((sgn * b2) >> denshift);
+ if ( del0 >= 0 )
+ continue;
+
+ sgn = -sign_of_int( b1 );
+ a1 -= sgn;
+ del0 -= (4 - 1) * ((sgn * b1) >> denshift);
+ if ( del0 >= 0 )
+ continue;
+
+ a0 += sign_of_int( b0 );
+ }
+ }
+
+ coefs[0] = a0;
+ coefs[1] = a1;
+ coefs[2] = a2;
+ coefs[3] = a3;
+ }
+ else if ( numactive == 8 )
+ {
+ // optimization for numactive == 8
+ register int16_t a4, a5, a6, a7;
+ register int32_t b4, b5, b6, b7;
+
+ a0 = coefs[0];
+ a1 = coefs[1];
+ a2 = coefs[2];
+ a3 = coefs[3];
+ a4 = coefs[4];
+ a5 = coefs[5];
+ a6 = coefs[6];
+ a7 = coefs[7];
+
+ for ( j = lim; j < num; j++ )
+ {
+ LOOP_ALIGN
+
+ top = in[j - lim];
+ pin = in + j - 1;
+
+ b0 = top - (*pin--);
+ b1 = top - (*pin--);
+ b2 = top - (*pin--);
+ b3 = top - (*pin--);
+ b4 = top - (*pin--);
+ b5 = top - (*pin--);
+ b6 = top - (*pin--);
+ b7 = top - (*pin);
+ pin += 8;
+
+ sum1 = (denhalf - a0 * b0 - a1 * b1 - a2 * b2 - a3 * b3
+ - a4 * b4 - a5 * b5 - a6 * b6 - a7 * b7) >> denshift;
+
+ del = in[j] - top - sum1;
+ del = (del << chanshift) >> chanshift;
+ pc1[j] = del;
+ del0 = del;
+
+ sg = sign_of_int(del);
+ if ( sg > 0 )
+ {
+ sgn = sign_of_int( b7 );
+ a7 -= sgn;
+ del0 -= 1 * ((sgn * b7) >> denshift);
+ if ( del0 <= 0 )
+ continue;
+
+ sgn = sign_of_int( b6 );
+ a6 -= sgn;
+ del0 -= 2 * ((sgn * b6) >> denshift);
+ if ( del0 <= 0 )
+ continue;
+
+ sgn = sign_of_int( b5 );
+ a5 -= sgn;
+ del0 -= 3 * ((sgn * b5) >> denshift);
+ if ( del0 <= 0 )
+ continue;
+
+ sgn = sign_of_int( b4 );
+ a4 -= sgn;
+ del0 -= 4 * ((sgn * b4) >> denshift);
+ if ( del0 <= 0 )
+ continue;
+
+ sgn = sign_of_int( b3 );
+ a3 -= sgn;
+ del0 -= 5 * ((sgn * b3) >> denshift);
+ if ( del0 <= 0 )
+ continue;
+
+ sgn = sign_of_int( b2 );
+ a2 -= sgn;
+ del0 -= 6 * ((sgn * b2) >> denshift);
+ if ( del0 <= 0 )
+ continue;
+
+ sgn = sign_of_int( b1 );
+ a1 -= sgn;
+ del0 -= 7 * ((sgn * b1) >> denshift);
+ if ( del0 <= 0 )
+ continue;
+
+ a0 -= sign_of_int( b0 );
+ }
+ else if ( sg < 0 )
+ {
+ // note: to avoid unnecessary negations, we flip the value of "sgn"
+ sgn = -sign_of_int( b7 );
+ a7 -= sgn;
+ del0 -= 1 * ((sgn * b7) >> denshift);
+ if ( del0 >= 0 )
+ continue;
+
+ sgn = -sign_of_int( b6 );
+ a6 -= sgn;
+ del0 -= 2 * ((sgn * b6) >> denshift);
+ if ( del0 >= 0 )
+ continue;
+
+ sgn = -sign_of_int( b5 );
+ a5 -= sgn;
+ del0 -= 3 * ((sgn * b5) >> denshift);
+ if ( del0 >= 0 )
+ continue;
+
+ sgn = -sign_of_int( b4 );
+ a4 -= sgn;
+ del0 -= 4 * ((sgn * b4) >> denshift);
+ if ( del0 >= 0 )
+ continue;
+
+ sgn = -sign_of_int( b3 );
+ a3 -= sgn;
+ del0 -= 5 * ((sgn * b3) >> denshift);
+ if ( del0 >= 0 )
+ continue;
+
+ sgn = -sign_of_int( b2 );
+ a2 -= sgn;
+ del0 -= 6 * ((sgn * b2) >> denshift);
+ if ( del0 >= 0 )
+ continue;
+
+ sgn = -sign_of_int( b1 );
+ a1 -= sgn;
+ del0 -= 7 * ((sgn * b1) >> denshift);
+ if ( del0 >= 0 )
+ continue;
+
+ a0 += sign_of_int( b0 );
+ }
+ }
+
+ coefs[0] = a0;
+ coefs[1] = a1;
+ coefs[2] = a2;
+ coefs[3] = a3;
+ coefs[4] = a4;
+ coefs[5] = a5;
+ coefs[6] = a6;
+ coefs[7] = a7;
+ }
+ else
+ {
+//pc_block_general:
+ // general case
+ for ( j = lim; j < num; j++ )
+ {
+ LOOP_ALIGN
+
+ top = in[j - lim];
+ pin = in + j - 1;
+
+ sum1 = 0;
+ for ( k = 0; k < numactive; k++ )
+ sum1 -= coefs[k] * (top - pin[-k]);
+
+ del = in[j] - top - ((sum1 + denhalf) >> denshift);
+ del = (del << chanshift) >> chanshift;
+ pc1[j] = del;
+ del0 = del;
+
+ sg = sign_of_int( del );
+ if ( sg > 0 )
+ {
+ for ( k = (numactive - 1); k >= 0; k-- )
+ {
+ dd = top - pin[-k];
+ sgn = sign_of_int( dd );
+ coefs[k] -= sgn;
+ del0 -= (numactive - k) * ((sgn * dd) >> denshift);
+ if ( del0 <= 0 )
+ break;
+ }
+ }
+ else if ( sg < 0 )
+ {
+ for ( k = (numactive - 1); k >= 0; k-- )
+ {
+ dd = top - pin[-k];
+ sgn = sign_of_int( dd );
+ coefs[k] += sgn;
+ del0 -= (numactive - k) * ((-sgn * dd) >> denshift);
+ if ( del0 >= 0 )
+ break;
+ }
+ }
+ }
+ }
+}
diff --git a/codec/dplib.h b/codec/dplib.h
new file mode 100755
index 0000000..d2ad167
--- /dev/null
+++ b/codec/dplib.h
@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2011 Apple Inc. All rights reserved.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_START@
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_END@
+ */
+
+/*
+ File: dplib.h
+
+ Contains: Dynamic Predictor routines
+
+ Copyright: Copyright (C) 2001-2011 Apple, Inc.
+*/
+
+#ifndef __DPLIB_H__
+#define __DPLIB_H__
+
+#ifdef _MSC_VER
+#include <msstdint.h>
+#else
+#include <stdint.h>
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+// defines
+
+#define DENSHIFT_MAX 15
+#define DENSHIFT_DEFAULT 9
+#define AINIT 38
+#define BINIT (-29)
+#define CINIT (-2)
+#define NUMCOEPAIRS 16
+
+// prototypes
+
+void init_coefs( int16_t * coefs, uint32_t denshift, int32_t numPairs );
+void copy_coefs( int16_t * srcCoefs, int16_t * dstCoefs, int32_t numPairs );
+
+// NOTE: these routines read at least "numactive" samples so the i/o buffers must be at least that big
+
+void pc_block( int32_t * in, int32_t * pc, int32_t num, int16_t * coefs, int32_t numactive, uint32_t chanbits, uint32_t denshift );
+void unpc_block( int32_t * pc, int32_t * out, int32_t num, int16_t * coefs, int32_t numactive, uint32_t chanbits, uint32_t denshift );
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __DPLIB_H__ */
diff --git a/codec/makefile b/codec/makefile
new file mode 100755
index 0000000..6d4939f
--- /dev/null
+++ b/codec/makefile
@@ -0,0 +1,79 @@
+# libalac make
+
+# CFLAGS += -c
+LFLAGS = -Wall
+CC = g++
+
+SRCDIR = .
+OBJDIR = ./obj
+INCLUDES = .
+
+HEADERS = \
+$(SRCDIR)/EndianPortable.h \
+$(SRCDIR)/aglib.h \
+$(SRCDIR)/ALACAudioTypes.h \
+$(SRCDIR)/ALACBitUtilities.h\
+$(SRCDIR)/ALACDecoder.h \
+$(SRCDIR)/ALACEncoder.h \
+$(SRCDIR)/dplib.h \
+$(SRCDIR)/matrixlib.h
+
+SOURCES = \
+$(SRCDIR)/EndianPortable.c \
+$(SRCDIR)/ALACBitUtilities.c \
+$(SRCDIR)/ALACDecoder.cpp \
+$(SRCDIR)/ALACEncoder.cpp \
+$(SRCDIR)/ag_dec.c \
+$(SRCDIR)/ag_enc.c \
+$(SRCDIR)/dp_dec.c \
+$(SRCDIR)/dp_enc.c \
+$(SRCDIR)/matrix_dec.c \
+$(SRCDIR)/matrix_enc.c
+
+OBJS = \
+EndianPortable.o \
+ALACBitUtilities.o \
+ALACDecoder.o \
+ALACEncoder.o \
+ag_dec.o \
+ag_enc.o \
+dp_dec.o \
+dp_enc.o \
+matrix_dec.o \
+matrix_enc.o
+
+libalac.a: $(OBJS)
+ ar rcs libalac.a $(OBJS)
+
+EndianPortable.o : EndianPortable.c
+ $(CC) -I $(INCLUDES) $(CFLAGS) EndianPortable.c
+
+ALACBitUtilities.o : ALACBitUtilities.c
+ $(CC) -I $(INCLUDES) $(CFLAGS) ALACBitUtilities.c
+
+ALACDecoder.o : ALACDecoder.cpp
+ $(CC) -I $(INCLUDES) $(CFLAGS) ALACDecoder.cpp
+
+ALACEncoder.o : ALACEncoder.cpp
+ $(CC) -I $(INCLUDES) $(CFLAGS) ALACEncoder.cpp
+
+ag_dec.o : ag_dec.c
+ $(CC) -I $(INCLUDES) $(CFLAGS) ag_dec.c
+
+ag_enc.o : ag_enc.c
+ $(CC) -I $(INCLUDES) $(CFLAGS) ag_enc.c
+
+dp_dec.o : dp_dec.c
+ $(CC) -I $(INCLUDES) $(CFLAGS) dp_dec.c
+
+dp_enc.o : dp_enc.c
+ $(CC) -I $(INCLUDES) $(CFLAGS) dp_enc.c
+
+matrix_dec.o : matrix_dec.c
+ $(CC) -I $(INCLUDES) $(CFLAGS) matrix_dec.c
+
+matrix_enc.o : matrix_enc.c
+ $(CC) -I $(INCLUDES) $(CFLAGS) matrix_enc.c
+
+clean:
+ -rm $(OBJS) libalac.a
diff --git a/codec/matrix_dec.c b/codec/matrix_dec.c
new file mode 100755
index 0000000..b1889b9
--- /dev/null
+++ b/codec/matrix_dec.c
@@ -0,0 +1,390 @@
+/*
+ * Copyright (c) 2011 Apple Inc. All rights reserved.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_START@
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_END@
+ */
+
+/*
+ File: matrix_dec.c
+
+ Contains: ALAC mixing/matrixing decode routines.
+
+ Copyright: (c) 2004-2011 Apple, Inc.
+*/
+
+#include "matrixlib.h"
+#include "ALACAudioTypes.h"
+
+// up to 24-bit "offset" macros for the individual bytes of a 20/24-bit word
+#if TARGET_RT_BIG_ENDIAN
+ #define LBYTE 2
+ #define MBYTE 1
+ #define HBYTE 0
+#else
+ #define LBYTE 0
+ #define MBYTE 1
+ #define HBYTE 2
+#endif
+
+/*
+ There is no plain middle-side option; instead there are various mixing
+ modes including middle-side, each lossless, as embodied in the mix()
+ and unmix() functions. These functions exploit a generalized middle-side
+ transformation:
+
+ u := [(rL + (m-r)R)/m];
+ v := L - R;
+
+ where [ ] denotes integer floor. The (lossless) inverse is
+
+ L = u + v - [rV/m];
+ R = L - v;
+*/
+
+// 16-bit routines
+
+void unmix16( int32_t * u, int32_t * v, int16_t * out, uint32_t stride, int32_t numSamples, int32_t mixbits, int32_t mixres )
+{
+ int16_t * op = out;
+ int32_t j;
+
+ if ( mixres != 0 )
+ {
+ /* matrixed stereo */
+ for ( j = 0; j < numSamples; j++ )
+ {
+ int32_t l, r;
+
+ l = u[j] + v[j] - ((mixres * v[j]) >> mixbits);
+ r = l - v[j];
+
+ op[0] = (int16_t) l;
+ op[1] = (int16_t) r;
+ op += stride;
+ }
+ }
+ else
+ {
+ /* Conventional separated stereo. */
+ for ( j = 0; j < numSamples; j++ )
+ {
+ op[0] = (int16_t) u[j];
+ op[1] = (int16_t) v[j];
+ op += stride;
+ }
+ }
+}
+
+// 20-bit routines
+// - the 20 bits of data are left-justified in 3 bytes of storage but right-aligned for input/output predictor buffers
+
+void unmix20( int32_t * u, int32_t * v, uint8_t * out, uint32_t stride, int32_t numSamples, int32_t mixbits, int32_t mixres )
+{
+ uint8_t * op = out;
+ int32_t j;
+
+ if ( mixres != 0 )
+ {
+ /* matrixed stereo */
+ for ( j = 0; j < numSamples; j++ )
+ {
+ int32_t l, r;
+
+ l = u[j] + v[j] - ((mixres * v[j]) >> mixbits);
+ r = l - v[j];
+
+ l <<= 4;
+ r <<= 4;
+
+ op[HBYTE] = (uint8_t)((l >> 16) & 0xffu);
+ op[MBYTE] = (uint8_t)((l >> 8) & 0xffu);
+ op[LBYTE] = (uint8_t)((l >> 0) & 0xffu);
+ op += 3;
+
+ op[HBYTE] = (uint8_t)((r >> 16) & 0xffu);
+ op[MBYTE] = (uint8_t)((r >> 8) & 0xffu);
+ op[LBYTE] = (uint8_t)((r >> 0) & 0xffu);
+
+ op += (stride - 1) * 3;
+ }
+ }
+ else
+ {
+ /* Conventional separated stereo. */
+ for ( j = 0; j < numSamples; j++ )
+ {
+ int32_t val;
+
+ val = u[j] << 4;
+ op[HBYTE] = (uint8_t)((val >> 16) & 0xffu);
+ op[MBYTE] = (uint8_t)((val >> 8) & 0xffu);
+ op[LBYTE] = (uint8_t)((val >> 0) & 0xffu);
+ op += 3;
+
+ val = v[j] << 4;
+ op[HBYTE] = (uint8_t)((val >> 16) & 0xffu);
+ op[MBYTE] = (uint8_t)((val >> 8) & 0xffu);
+ op[LBYTE] = (uint8_t)((val >> 0) & 0xffu);
+
+ op += (stride - 1) * 3;
+ }
+ }
+}
+
+// 24-bit routines
+// - the 24 bits of data are right-justified in the input/output predictor buffers
+
+void unmix24( int32_t * u, int32_t * v, uint8_t * out, uint32_t stride, int32_t numSamples,
+ int32_t mixbits, int32_t mixres, uint16_t * shiftUV, int32_t bytesShifted )
+{
+ uint8_t * op = out;
+ int32_t shift = bytesShifted * 8;
+ int32_t l, r;
+ int32_t j, k;
+
+ if ( mixres != 0 )
+ {
+ /* matrixed stereo */
+ if ( bytesShifted != 0 )
+ {
+ for ( j = 0, k = 0; j < numSamples; j++, k += 2 )
+ {
+ l = u[j] + v[j] - ((mixres * v[j]) >> mixbits);
+ r = l - v[j];
+
+ l = (l << shift) | (uint32_t) shiftUV[k + 0];
+ r = (r << shift) | (uint32_t) shiftUV[k + 1];
+
+ op[HBYTE] = (uint8_t)((l >> 16) & 0xffu);
+ op[MBYTE] = (uint8_t)((l >> 8) & 0xffu);
+ op[LBYTE] = (uint8_t)((l >> 0) & 0xffu);
+ op += 3;
+
+ op[HBYTE] = (uint8_t)((r >> 16) & 0xffu);
+ op[MBYTE] = (uint8_t)((r >> 8) & 0xffu);
+ op[LBYTE] = (uint8_t)((r >> 0) & 0xffu);
+
+ op += (stride - 1) * 3;
+ }
+ }
+ else
+ {
+ for ( j = 0; j < numSamples; j++ )
+ {
+ l = u[j] + v[j] - ((mixres * v[j]) >> mixbits);
+ r = l - v[j];
+
+ op[HBYTE] = (uint8_t)((l >> 16) & 0xffu);
+ op[MBYTE] = (uint8_t)((l >> 8) & 0xffu);
+ op[LBYTE] = (uint8_t)((l >> 0) & 0xffu);
+ op += 3;
+
+ op[HBYTE] = (uint8_t)((r >> 16) & 0xffu);
+ op[MBYTE] = (uint8_t)((r >> 8) & 0xffu);
+ op[LBYTE] = (uint8_t)((r >> 0) & 0xffu);
+
+ op += (stride - 1) * 3;
+ }
+ }
+ }
+ else
+ {
+ /* Conventional separated stereo. */
+ if ( bytesShifted != 0 )
+ {
+ for ( j = 0, k = 0; j < numSamples; j++, k += 2 )
+ {
+ l = u[j];
+ r = v[j];
+
+ l = (l << shift) | (uint32_t) shiftUV[k + 0];
+ r = (r << shift) | (uint32_t) shiftUV[k + 1];
+
+ op[HBYTE] = (uint8_t)((l >> 16) & 0xffu);
+ op[MBYTE] = (uint8_t)((l >> 8) & 0xffu);
+ op[LBYTE] = (uint8_t)((l >> 0) & 0xffu);
+ op += 3;
+
+ op[HBYTE] = (uint8_t)((r >> 16) & 0xffu);
+ op[MBYTE] = (uint8_t)((r >> 8) & 0xffu);
+ op[LBYTE] = (uint8_t)((r >> 0) & 0xffu);
+
+ op += (stride - 1) * 3;
+ }
+ }
+ else
+ {
+ for ( j = 0; j < numSamples; j++ )
+ {
+ int32_t val;
+
+ val = u[j];
+ op[HBYTE] = (uint8_t)((val >> 16) & 0xffu);
+ op[MBYTE] = (uint8_t)((val >> 8) & 0xffu);
+ op[LBYTE] = (uint8_t)((val >> 0) & 0xffu);
+ op += 3;
+
+ val = v[j];
+ op[HBYTE] = (uint8_t)((val >> 16) & 0xffu);
+ op[MBYTE] = (uint8_t)((val >> 8) & 0xffu);
+ op[LBYTE] = (uint8_t)((val >> 0) & 0xffu);
+
+ op += (stride - 1) * 3;
+ }
+ }
+ }
+}
+
+// 32-bit routines
+// - note that these really expect the internal data width to be < 32 but the arrays are 32-bit
+// - otherwise, the calculations might overflow into the 33rd bit and be lost
+// - therefore, these routines deal with the specified "unused lower" bytes in the "shift" buffers
+
+void unmix32( int32_t * u, int32_t * v, int32_t * out, uint32_t stride, int32_t numSamples,
+ int32_t mixbits, int32_t mixres, uint16_t * shiftUV, int32_t bytesShifted )
+{
+ int32_t * op = out;
+ int32_t shift = bytesShifted * 8;
+ int32_t l, r;
+ int32_t j, k;
+
+ if ( mixres != 0 )
+ {
+ //Assert( bytesShifted != 0 );
+
+ /* matrixed stereo with shift */
+ for ( j = 0, k = 0; j < numSamples; j++, k += 2 )
+ {
+ int32_t lt, rt;
+
+ lt = u[j];
+ rt = v[j];
+
+ l = lt + rt - ((mixres * rt) >> mixbits);
+ r = l - rt;
+
+ op[0] = (l << shift) | (uint32_t) shiftUV[k + 0];
+ op[1] = (r << shift) | (uint32_t) shiftUV[k + 1];
+ op += stride;
+ }
+ }
+ else
+ {
+ if ( bytesShifted == 0 )
+ {
+ /* interleaving w/o shift */
+ for ( j = 0; j < numSamples; j++ )
+ {
+ op[0] = u[j];
+ op[1] = v[j];
+ op += stride;
+ }
+ }
+ else
+ {
+ /* interleaving with shift */
+ for ( j = 0, k = 0; j < numSamples; j++, k += 2 )
+ {
+ op[0] = (u[j] << shift) | (uint32_t) shiftUV[k + 0];
+ op[1] = (v[j] << shift) | (uint32_t) shiftUV[k + 1];
+ op += stride;
+ }
+ }
+ }
+}
+
+// 20/24-bit <-> 32-bit helper routines (not really matrixing but convenient to put here)
+
+void copyPredictorTo24( int32_t * in, uint8_t * out, uint32_t stride, int32_t numSamples )
+{
+ uint8_t * op = out;
+ int32_t j;
+
+ for ( j = 0; j < numSamples; j++ )
+ {
+ int32_t val = in[j];
+
+ op[HBYTE] = (uint8_t)((val >> 16) & 0xffu);
+ op[MBYTE] = (uint8_t)((val >> 8) & 0xffu);
+ op[LBYTE] = (uint8_t)((val >> 0) & 0xffu);
+ op += (stride * 3);
+ }
+}
+
+void copyPredictorTo24Shift( int32_t * in, uint16_t * shift, uint8_t * out, uint32_t stride, int32_t numSamples, int32_t bytesShifted )
+{
+ uint8_t * op = out;
+ int32_t shiftVal = bytesShifted * 8;
+ int32_t j;
+
+ //Assert( bytesShifted != 0 );
+
+ for ( j = 0; j < numSamples; j++ )
+ {
+ int32_t val = in[j];
+
+ val = (val << shiftVal) | (uint32_t) shift[j];
+
+ op[HBYTE] = (uint8_t)((val >> 16) & 0xffu);
+ op[MBYTE] = (uint8_t)((val >> 8) & 0xffu);
+ op[LBYTE] = (uint8_t)((val >> 0) & 0xffu);
+ op += (stride * 3);
+ }
+}
+
+void copyPredictorTo20( int32_t * in, uint8_t * out, uint32_t stride, int32_t numSamples )
+{
+ uint8_t * op = out;
+ int32_t j;
+
+ // 32-bit predictor values are right-aligned but 20-bit output values should be left-aligned
+ // in the 24-bit output buffer
+ for ( j = 0; j < numSamples; j++ )
+ {
+ int32_t val = in[j];
+
+ op[HBYTE] = (uint8_t)((val >> 12) & 0xffu);
+ op[MBYTE] = (uint8_t)((val >> 4) & 0xffu);
+ op[LBYTE] = (uint8_t)((val << 4) & 0xffu);
+ op += (stride * 3);
+ }
+}
+
+void copyPredictorTo32( int32_t * in, int32_t * out, uint32_t stride, int32_t numSamples )
+{
+ int32_t i, j;
+
+ // this is only a subroutine to abstract the "iPod can only output 16-bit data" problem
+ for ( i = 0, j = 0; i < numSamples; i++, j += stride )
+ out[j] = in[i];
+}
+
+void copyPredictorTo32Shift( int32_t * in, uint16_t * shift, int32_t * out, uint32_t stride, int32_t numSamples, int32_t bytesShifted )
+{
+ int32_t * op = out;
+ uint32_t shiftVal = bytesShifted * 8;
+ int32_t j;
+
+ //Assert( bytesShifted != 0 );
+
+ // this is only a subroutine to abstract the "iPod can only output 16-bit data" problem
+ for ( j = 0; j < numSamples; j++ )
+ {
+ op[0] = (in[j] << shiftVal) | (uint32_t) shift[j];
+ op += stride;
+ }
+}
diff --git a/codec/matrix_enc.c b/codec/matrix_enc.c
new file mode 100755
index 0000000..e194330
--- /dev/null
+++ b/codec/matrix_enc.c
@@ -0,0 +1,342 @@
+/*
+ * Copyright (c) 2011 Apple Inc. All rights reserved.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_START@
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_END@
+ */
+
+/*
+ File: matrix_enc.c
+
+ Contains: ALAC mixing/matrixing encode routines.
+
+ Copyright: (c) 2004-2011 Apple, Inc.
+*/
+
+#include "matrixlib.h"
+#include "ALACAudioTypes.h"
+
+// up to 24-bit "offset" macros for the individual bytes of a 20/24-bit word
+#if TARGET_RT_BIG_ENDIAN
+ #define LBYTE 2
+ #define MBYTE 1
+ #define HBYTE 0
+#else
+ #define LBYTE 0
+ #define MBYTE 1
+ #define HBYTE 2
+#endif
+
+/*
+ There is no plain middle-side option; instead there are various mixing
+ modes including middle-side, each lossless, as embodied in the mix()
+ and unmix() functions. These functions exploit a generalized middle-side
+ transformation:
+
+ u := [(rL + (m-r)R)/m];
+ v := L - R;
+
+ where [ ] denotes integer floor. The (lossless) inverse is
+
+ L = u + v - [rV/m];
+ R = L - v;
+*/
+
+// 16-bit routines
+
+void mix16( int16_t * in, uint32_t stride, int32_t * u, int32_t * v, int32_t numSamples, int32_t mixbits, int32_t mixres )
+{
+ int16_t * ip = in;
+ int32_t j;
+
+ if ( mixres != 0 )
+ {
+ int32_t mod = 1 << mixbits;
+ int32_t m2;
+
+ /* matrixed stereo */
+ m2 = mod - mixres;
+ for ( j = 0; j < numSamples; j++ )
+ {
+ int32_t l, r;
+
+ l = (int32_t) ip[0];
+ r = (int32_t) ip[1];
+ ip += stride;
+ u[j] = (mixres * l + m2 * r) >> mixbits;
+ v[j] = l - r;
+ }
+ }
+ else
+ {
+ /* Conventional separated stereo. */
+ for ( j = 0; j < numSamples; j++ )
+ {
+ u[j] = (int32_t) ip[0];
+ v[j] = (int32_t) ip[1];
+ ip += stride;
+ }
+ }
+}
+
+// 20-bit routines
+// - the 20 bits of data are left-justified in 3 bytes of storage but right-aligned for input/output predictor buffers
+
+void mix20( uint8_t * in, uint32_t stride, int32_t * u, int32_t * v, int32_t numSamples, int32_t mixbits, int32_t mixres )
+{
+ int32_t l, r;
+ uint8_t * ip = in;
+ int32_t j;
+
+ if ( mixres != 0 )
+ {
+ /* matrixed stereo */
+ int32_t mod = 1 << mixbits;
+ int32_t m2 = mod - mixres;
+
+ for ( j = 0; j < numSamples; j++ )
+ {
+ l = (int32_t)( ((uint32_t)ip[HBYTE] << 16) | ((uint32_t)ip[MBYTE] << 8) | (uint32_t)ip[LBYTE] );
+ l = (l << 8) >> 12;
+ ip += 3;
+
+ r = (int32_t)( ((uint32_t)ip[HBYTE] << 16) | ((uint32_t)ip[MBYTE] << 8) | (uint32_t)ip[LBYTE] );
+ r = (r << 8) >> 12;
+ ip += (stride - 1) * 3;
+
+ u[j] = (mixres * l + m2 * r) >> mixbits;
+ v[j] = l - r;
+ }
+ }
+ else
+ {
+ /* Conventional separated stereo. */
+ for ( j = 0; j < numSamples; j++ )
+ {
+ l = (int32_t)( ((uint32_t)ip[HBYTE] << 16) | ((uint32_t)ip[MBYTE] << 8) | (uint32_t)ip[LBYTE] );
+ u[j] = (l << 8) >> 12;
+ ip += 3;
+
+ r = (int32_t)( ((uint32_t)ip[HBYTE] << 16) | ((uint32_t)ip[MBYTE] << 8) | (uint32_t)ip[LBYTE] );
+ v[j] = (r << 8) >> 12;
+ ip += (stride - 1) * 3;
+ }
+ }
+}
+
+// 24-bit routines
+// - the 24 bits of data are right-justified in the input/output predictor buffers
+
+void mix24( uint8_t * in, uint32_t stride, int32_t * u, int32_t * v, int32_t numSamples,
+ int32_t mixbits, int32_t mixres, uint16_t * shiftUV, int32_t bytesShifted )
+{
+ int32_t l, r;
+ uint8_t * ip = in;
+ int32_t shift = bytesShifted * 8;
+ uint32_t mask = (1ul << shift) - 1;
+ int32_t j, k;
+
+ if ( mixres != 0 )
+ {
+ /* matrixed stereo */
+ int32_t mod = 1 << mixbits;
+ int32_t m2 = mod - mixres;
+
+ if ( bytesShifted != 0 )
+ {
+ for ( j = 0, k = 0; j < numSamples; j++, k += 2 )
+ {
+ l = (int32_t)( ((uint32_t)ip[HBYTE] << 16) | ((uint32_t)ip[MBYTE] << 8) | (uint32_t)ip[LBYTE] );
+ l = (l << 8) >> 8;
+ ip += 3;
+
+ r = (int32_t)( ((uint32_t)ip[HBYTE] << 16) | ((uint32_t)ip[MBYTE] << 8) | (uint32_t)ip[LBYTE] );
+ r = (r << 8) >> 8;
+ ip += (stride - 1) * 3;
+
+ shiftUV[k + 0] = (uint16_t)(l & mask);
+ shiftUV[k + 1] = (uint16_t)(r & mask);
+
+ l >>= shift;
+ r >>= shift;
+
+ u[j] = (mixres * l + m2 * r) >> mixbits;
+ v[j] = l - r;
+ }
+ }
+ else
+ {
+ for ( j = 0; j < numSamples; j++ )
+ {
+ l = (int32_t)( ((uint32_t)ip[HBYTE] << 16) | ((uint32_t)ip[MBYTE] << 8) | (uint32_t)ip[LBYTE] );
+ l = (l << 8) >> 8;
+ ip += 3;
+
+ r = (int32_t)( ((uint32_t)ip[HBYTE] << 16) | ((uint32_t)ip[MBYTE] << 8) | (uint32_t)ip[LBYTE] );
+ r = (r << 8) >> 8;
+ ip += (stride - 1) * 3;
+
+ u[j] = (mixres * l + m2 * r) >> mixbits;
+ v[j] = l - r;
+ }
+ }
+ }
+ else
+ {
+ /* Conventional separated stereo. */
+ if ( bytesShifted != 0 )
+ {
+ for ( j = 0, k = 0; j < numSamples; j++, k += 2 )
+ {
+ l = (int32_t)( ((uint32_t)ip[HBYTE] << 16) | ((uint32_t)ip[MBYTE] << 8) | (uint32_t)ip[LBYTE] );
+ l = (l << 8) >> 8;
+ ip += 3;
+
+ r = (int32_t)( ((uint32_t)ip[HBYTE] << 16) | ((uint32_t)ip[MBYTE] << 8) | (uint32_t)ip[LBYTE] );
+ r = (r << 8) >> 8;
+ ip += (stride - 1) * 3;
+
+ shiftUV[k + 0] = (uint16_t)(l & mask);
+ shiftUV[k + 1] = (uint16_t)(r & mask);
+
+ l >>= shift;
+ r >>= shift;
+
+ u[j] = l;
+ v[j] = r;
+ }
+ }
+ else
+ {
+ for ( j = 0; j < numSamples; j++ )
+ {
+ l = (int32_t)( ((uint32_t)ip[HBYTE] << 16) | ((uint32_t)ip[MBYTE] << 8) | (uint32_t)ip[LBYTE] );
+ u[j] = (l << 8) >> 8;
+ ip += 3;
+
+ r = (int32_t)( ((uint32_t)ip[HBYTE] << 16) | ((uint32_t)ip[MBYTE] << 8) | (uint32_t)ip[LBYTE] );
+ v[j] = (r << 8) >> 8;
+ ip += (stride - 1) * 3;
+ }
+ }
+ }
+}
+
+// 32-bit routines
+// - note that these really expect the internal data width to be < 32 but the arrays are 32-bit
+// - otherwise, the calculations might overflow into the 33rd bit and be lost
+// - therefore, these routines deal with the specified "unused lower" bytes in the "shift" buffers
+
+void mix32( int32_t * in, uint32_t stride, int32_t * u, int32_t * v, int32_t numSamples,
+ int32_t mixbits, int32_t mixres, uint16_t * shiftUV, int32_t bytesShifted )
+{
+ int32_t * ip = in;
+ int32_t shift = bytesShifted * 8;
+ uint32_t mask = (1ul << shift) - 1;
+ int32_t l, r;
+ int32_t j, k;
+
+ if ( mixres != 0 )
+ {
+ int32_t mod = 1 << mixbits;
+ int32_t m2;
+
+ //Assert( bytesShifted != 0 );
+
+ /* matrixed stereo with shift */
+ m2 = mod - mixres;
+ for ( j = 0, k = 0; j < numSamples; j++, k += 2 )
+ {
+ l = ip[0];
+ r = ip[1];
+ ip += stride;
+
+ shiftUV[k + 0] = (uint16_t)(l & mask);
+ shiftUV[k + 1] = (uint16_t)(r & mask);
+
+ l >>= shift;
+ r >>= shift;
+
+ u[j] = (mixres * l + m2 * r) >> mixbits;
+ v[j] = l - r;
+ }
+ }
+ else
+ {
+ if ( bytesShifted == 0 )
+ {
+ /* de-interleaving w/o shift */
+ for ( j = 0; j < numSamples; j++ )
+ {
+ u[j] = ip[0];
+ v[j] = ip[1];
+ ip += stride;
+ }
+ }
+ else
+ {
+ /* de-interleaving with shift */
+ for ( j = 0, k = 0; j < numSamples; j++, k += 2 )
+ {
+ l = ip[0];
+ r = ip[1];
+ ip += stride;
+
+ shiftUV[k + 0] = (uint16_t)(l & mask);
+ shiftUV[k + 1] = (uint16_t)(r & mask);
+
+ l >>= shift;
+ r >>= shift;
+
+ u[j] = l;
+ v[j] = r;
+ }
+ }
+ }
+}
+
+// 20/24-bit <-> 32-bit helper routines (not really matrixing but convenient to put here)
+
+void copy20ToPredictor( uint8_t * in, uint32_t stride, int32_t * out, int32_t numSamples )
+{
+ uint8_t * ip = in;
+ int32_t j;
+
+ for ( j = 0; j < numSamples; j++ )
+ {
+ int32_t val;
+
+ // 20-bit values are left-aligned in the 24-bit input buffer but right-aligned in the 32-bit output buffer
+ val = (int32_t)( ((uint32_t)ip[HBYTE] << 16) | ((uint32_t)ip[MBYTE] << 8) | (uint32_t)ip[LBYTE] );
+ out[j] = (val << 8) >> 12;
+ ip += stride * 3;
+ }
+}
+
+void copy24ToPredictor( uint8_t * in, uint32_t stride, int32_t * out, int32_t numSamples )
+{
+ uint8_t * ip = in;
+ int32_t j;
+
+ for ( j = 0; j < numSamples; j++ )
+ {
+ int32_t val;
+
+ val = (int32_t)( ((uint32_t)ip[HBYTE] << 16) | ((uint32_t)ip[MBYTE] << 8) | (uint32_t)ip[LBYTE] );
+ out[j] = (val << 8) >> 8;
+ ip += stride * 3;
+ }
+}
diff --git a/codec/matrixlib.h b/codec/matrixlib.h
new file mode 100755
index 0000000..743ec24
--- /dev/null
+++ b/codec/matrixlib.h
@@ -0,0 +1,84 @@
+/*
+ * Copyright (c) 2011 Apple Inc. All rights reserved.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_START@
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_END@
+ */
+
+/*
+ File: matrixlib.h
+
+ Contains: ALAC mixing/matrixing routines to/from 32-bit predictor buffers.
+
+ Copyright: Copyright (C) 2004 to 2011 Apple, Inc.
+*/
+
+#ifndef __MATRIXLIB_H
+#define __MATRIXLIB_H
+
+#pragma once
+
+#ifdef _MSC_VER
+#include <msstdint.h>
+#else
+#include <stdint.h>
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+// 16-bit routines
+void mix16( int16_t * in, uint32_t stride, int32_t * u, int32_t * v, int32_t numSamples, int32_t mixbits, int32_t mixres );
+void unmix16( int32_t * u, int32_t * v, int16_t * out, uint32_t stride, int32_t numSamples, int32_t mixbits, int32_t mixres );
+
+// 20-bit routines
+void mix20( uint8_t * in, uint32_t stride, int32_t * u, int32_t * v, int32_t numSamples, int32_t mixbits, int32_t mixres );
+void unmix20( int32_t * u, int32_t * v, uint8_t * out, uint32_t stride, int32_t numSamples, int32_t mixbits, int32_t mixres );
+
+// 24-bit routines
+// - 24-bit data sometimes compresses better by shifting off the bottom byte so these routines deal with
+// the specified "unused lower bytes" in the combined "shift" buffer
+void mix24( uint8_t * in, uint32_t stride, int32_t * u, int32_t * v, int32_t numSamples,
+ int32_t mixbits, int32_t mixres, uint16_t * shiftUV, int32_t bytesShifted );
+void unmix24( int32_t * u, int32_t * v, uint8_t * out, uint32_t stride, int32_t numSamples,
+ int32_t mixbits, int32_t mixres, uint16_t * shiftUV, int32_t bytesShifted );
+
+// 32-bit routines
+// - note that these really expect the internal data width to be < 32-bit but the arrays are 32-bit
+// - otherwise, the calculations might overflow into the 33rd bit and be lost
+// - therefore, these routines deal with the specified "unused lower" bytes in the combined "shift" buffer
+void mix32( int32_t * in, uint32_t stride, int32_t * u, int32_t * v, int32_t numSamples,
+ int32_t mixbits, int32_t mixres, uint16_t * shiftUV, int32_t bytesShifted );
+void unmix32( int32_t * u, int32_t * v, int32_t * out, uint32_t stride, int32_t numSamples,
+ int32_t mixbits, int32_t mixres, uint16_t * shiftUV, int32_t bytesShifted );
+
+// 20/24/32-bit <-> 32-bit helper routines (not really matrixing but convenient to put here)
+void copy20ToPredictor( uint8_t * in, uint32_t stride, int32_t * out, int32_t numSamples );
+void copy24ToPredictor( uint8_t * in, uint32_t stride, int32_t * out, int32_t numSamples );
+
+void copyPredictorTo24( int32_t * in, uint8_t * out, uint32_t stride, int32_t numSamples );
+void copyPredictorTo24Shift( int32_t * in, uint16_t * shift, uint8_t * out, uint32_t stride, int32_t numSamples, int32_t bytesShifted );
+void copyPredictorTo20( int32_t * in, uint8_t * out, uint32_t stride, int32_t numSamples );
+
+void copyPredictorTo32( int32_t * in, int32_t * out, uint32_t stride, int32_t numSamples );
+void copyPredictorTo32Shift( int32_t * in, uint16_t * shift, int32_t * out, uint32_t stride, int32_t numSamples, int32_t bytesShifted );
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __MATRIXLIB_H */
diff --git a/codec/msstdint.h b/codec/msstdint.h
new file mode 100755
index 0000000..7e200dc
--- /dev/null
+++ b/codec/msstdint.h
@@ -0,0 +1,222 @@
+// ISO C9x compliant stdint.h for Microsoft Visual Studio
+// Based on ISO/IEC 9899:TC2 Committee draft (May 6, 2005) WG14/N1124
+//
+// Copyright (c) 2006 Alexander Chemeris
+//
+// 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 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.
+//
+///////////////////////////////////////////////////////////////////////////////
+
+#ifndef _MSC_VER // [
+#error "Use this header only with Microsoft Visual C++ compilers!"
+#endif // _MSC_VER ]
+
+#ifndef _MSC_STDINT_H_ // [
+#define _MSC_STDINT_H_
+
+#if _MSC_VER > 1000
+#pragma once
+#endif
+
+#include <limits.h>
+
+// For Visual Studio 6 in C++ mode wrap <wchar.h> include with 'extern "C++" {}'
+// or compiler give many errors like this:
+// error C2733: second C linkage of overloaded function 'wmemchr' not allowed
+#if (_MSC_VER < 1300) && defined(__cplusplus)
+ extern "C++" {
+#endif
+# include <wchar.h>
+#if (_MSC_VER < 1300) && defined(__cplusplus)
+ }
+#endif
+
+// 7.18.1 Integer types
+
+// 7.18.1.1 Exact-width integer types
+typedef __int8 int8_t;
+typedef __int16 int16_t;
+typedef __int32 int32_t;
+typedef __int64 int64_t;
+typedef unsigned __int8 uint8_t;
+typedef unsigned __int16 uint16_t;
+typedef unsigned __int32 uint32_t;
+typedef unsigned __int64 uint64_t;
+
+// 7.18.1.2 Minimum-width integer types
+typedef int8_t int_least8_t;
+typedef int16_t int_least16_t;
+typedef int32_t int_least32_t;
+typedef int64_t int_least64_t;
+typedef uint8_t uint_least8_t;
+typedef uint16_t uint_least16_t;
+typedef uint32_t uint_least32_t;
+typedef uint64_t uint_least64_t;
+
+// 7.18.1.3 Fastest minimum-width integer types
+typedef int8_t int_fast8_t;
+typedef int16_t int_fast16_t;
+typedef int32_t int_fast32_t;
+typedef int64_t int_fast64_t;
+typedef uint8_t uint_fast8_t;
+typedef uint16_t uint_fast16_t;
+typedef uint32_t uint_fast32_t;
+typedef uint64_t uint_fast64_t;
+
+// 7.18.1.4 Integer types capable of holding object pointers
+#ifdef _WIN64 // [
+ typedef __int64 intptr_t;
+ typedef unsigned __int64 uintptr_t;
+#else // _WIN64 ][
+ typedef int intptr_t;
+ typedef unsigned int uintptr_t;
+#endif // _WIN64 ]
+
+// 7.18.1.5 Greatest-width integer types
+typedef int64_t intmax_t;
+typedef uint64_t uintmax_t;
+
+
+// 7.18.2 Limits of specified-width integer types
+
+#if !defined(__cplusplus) || defined(__STDC_LIMIT_MACROS) // [ See footnote 220 at page 257 and footnote 221 at page 259
+
+// 7.18.2.1 Limits of exact-width integer types
+#define INT8_MIN ((int8_t)_I8_MIN)
+#define INT8_MAX _I8_MAX
+#define INT16_MIN ((int16_t)_I16_MIN)
+#define INT16_MAX _I16_MAX
+#define INT32_MIN ((int32_t)_I32_MIN)
+#define INT32_MAX _I32_MAX
+#define INT64_MIN ((int64_t)_I64_MIN)
+#define INT64_MAX _I64_MAX
+#define UINT8_MAX _UI8_MAX
+#define UINT16_MAX _UI16_MAX
+#define UINT32_MAX _UI32_MAX
+#define UINT64_MAX _UI64_MAX
+
+// 7.18.2.2 Limits of minimum-width integer types
+#define INT_LEAST8_MIN INT8_MIN
+#define INT_LEAST8_MAX INT8_MAX
+#define INT_LEAST16_MIN INT16_MIN
+#define INT_LEAST16_MAX INT16_MAX
+#define INT_LEAST32_MIN INT32_MIN
+#define INT_LEAST32_MAX INT32_MAX
+#define INT_LEAST64_MIN INT64_MIN
+#define INT_LEAST64_MAX INT64_MAX
+#define UINT_LEAST8_MAX UINT8_MAX
+#define UINT_LEAST16_MAX UINT16_MAX
+#define UINT_LEAST32_MAX UINT32_MAX
+#define UINT_LEAST64_MAX UINT64_MAX
+
+// 7.18.2.3 Limits of fastest minimum-width integer types
+#define INT_FAST8_MIN INT8_MIN
+#define INT_FAST8_MAX INT8_MAX
+#define INT_FAST16_MIN INT16_MIN
+#define INT_FAST16_MAX INT16_MAX
+#define INT_FAST32_MIN INT32_MIN
+#define INT_FAST32_MAX INT32_MAX
+#define INT_FAST64_MIN INT64_MIN
+#define INT_FAST64_MAX INT64_MAX
+#define UINT_FAST8_MAX UINT8_MAX
+#define UINT_FAST16_MAX UINT16_MAX
+#define UINT_FAST32_MAX UINT32_MAX
+#define UINT_FAST64_MAX UINT64_MAX
+
+// 7.18.2.4 Limits of integer types capable of holding object pointers
+#ifdef _WIN64 // [
+# define INTPTR_MIN INT64_MIN
+# define INTPTR_MAX INT64_MAX
+# define UINTPTR_MAX UINT64_MAX
+#else // _WIN64 ][
+# define INTPTR_MIN INT32_MIN
+# define INTPTR_MAX INT32_MAX
+# define UINTPTR_MAX UINT32_MAX
+#endif // _WIN64 ]
+
+// 7.18.2.5 Limits of greatest-width integer types
+#define INTMAX_MIN INT64_MIN
+#define INTMAX_MAX INT64_MAX
+#define UINTMAX_MAX UINT64_MAX
+
+// 7.18.3 Limits of other integer types
+
+#ifdef _WIN64 // [
+# define PTRDIFF_MIN _I64_MIN
+# define PTRDIFF_MAX _I64_MAX
+#else // _WIN64 ][
+# define PTRDIFF_MIN _I32_MIN
+# define PTRDIFF_MAX _I32_MAX
+#endif // _WIN64 ]
+
+#define SIG_ATOMIC_MIN INT_MIN
+#define SIG_ATOMIC_MAX INT_MAX
+
+#ifndef SIZE_MAX // [
+# ifdef _WIN64 // [
+# define SIZE_MAX _UI64_MAX
+# else // _WIN64 ][
+# define SIZE_MAX _UI32_MAX
+# endif // _WIN64 ]
+#endif // SIZE_MAX ]
+
+// WCHAR_MIN and WCHAR_MAX are also defined in <wchar.h>
+#ifndef WCHAR_MIN // [
+# define WCHAR_MIN 0
+#endif // WCHAR_MIN ]
+#ifndef WCHAR_MAX // [
+# define WCHAR_MAX _UI16_MAX
+#endif // WCHAR_MAX ]
+
+#define WINT_MIN 0
+#define WINT_MAX _UI16_MAX
+
+#endif // __STDC_LIMIT_MACROS ]
+
+
+// 7.18.4 Limits of other integer types
+
+#if !defined(__cplusplus) || defined(__STDC_CONSTANT_MACROS) // [ See footnote 224 at page 260
+
+// 7.18.4.1 Macros for minimum-width integer constants
+
+#define INT8_C(val) val##i8
+#define INT16_C(val) val##i16
+#define INT32_C(val) val##i32
+#define INT64_C(val) val##i64
+
+#define UINT8_C(val) val##ui8
+#define UINT16_C(val) val##ui16
+#define UINT32_C(val) val##ui32
+#define UINT64_C(val) val##ui64
+
+// 7.18.4.2 Macros for greatest-width integer constants
+#define INTMAX_C INT64_C
+#define UINTMAX_C UINT64_C
+
+#endif // __STDC_CONSTANT_MACROS ]
+
+
+#endif // _MSC_STDINT_H_ ]
diff --git a/convert-utility/CAFFileALAC.cpp b/convert-utility/CAFFileALAC.cpp
new file mode 100755
index 0000000..e2d0630
--- /dev/null
+++ b/convert-utility/CAFFileALAC.cpp
@@ -0,0 +1,456 @@
+/*
+ Copyright © 2011 Apple Inc. All rights reserved.
+
+ IMPORTANT: This Apple software is supplied to you by Apple Inc. (“Apple”) in consideration of your agreement to the following terms, and your use, installation, modification or redistribution of this Apple software constitutes acceptance of these terms. If you do not agree with these terms, please do not use, install, modify or redistribute this Apple software.
+
+ In consideration of your agreement to abide by the following terms, and subject to these terms, Apple grants you a personal, non-exclusive license, under Apple’s copyrights in this original Apple software (the “Apple Software”), to use, reproduce, modify and redistribute the Apple Software, with or without modifications, in source and/or binary forms; provided that if you redistribute the Apple Software in its entirety and without modifications, you must retain this notice and the following text and disclaimers in all such redistributions of the Apple Software. Neither the name, trademarks, service marks or logos of Apple Inc. may be used to endorse or promote products derived from the Apple Software without specific prior written permission from Apple. Except as expressly stated in this notice, no other rights or licenses, express or implied, are granted by Apple herein, including but not limited to any patent rights that may be infringed by your derivative works or by other works in which the Apple Software may be incorporated.
+
+ The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
+
+ IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+//
+// CAFFileALAC.cpp
+//
+// Copyright 2011 Apple Inc. All rights reserved.
+//
+
+#include <stdio.h>
+#include "CAFFileALAC.h"
+#include "EndianPortable.h"
+
+#define kSizeOfChanAtomPlusChannelLayout 24
+
+int32_t FindCAFFPacketTableStart(FILE * inputFile, int32_t * paktPos, int32_t * paktSize)
+{
+ // returns the absolute position within the file
+ int32_t currentPosition = ftell(inputFile); // record the current position
+ uint8_t theReadBuffer[12];
+ uint32_t chunkType = 0, chunkSize = 0;
+ bool done = false;
+ int32_t bytesRead = 8;
+
+ fseek(inputFile, bytesRead, SEEK_SET); // start at 8!
+ while (!done && bytesRead > 0) // no file size here
+ {
+ bytesRead = fread(theReadBuffer, 1, 12, inputFile);
+ chunkType = ((int32_t)(theReadBuffer[0]) << 24) + ((int32_t)(theReadBuffer[1]) << 16) + ((int32_t)(theReadBuffer[2]) << 8) + theReadBuffer[3];
+ switch(chunkType)
+ {
+ case 'pakt':
+ *paktPos = ftell(inputFile) + kMinCAFFPacketTableHeaderSize;
+ // big endian size
+ *paktSize = ((int32_t)(theReadBuffer[8]) << 24) + ((int32_t)(theReadBuffer[9]) << 16) + ((int32_t)(theReadBuffer[10]) << 8) + theReadBuffer[11];
+ done = true;
+ break;
+ default:
+ chunkSize = ((int32_t)(theReadBuffer[8]) << 24) + ((int32_t)(theReadBuffer[9]) << 16) + ((int32_t)(theReadBuffer[10]) << 8) + theReadBuffer[11];
+ fseek(inputFile, chunkSize, SEEK_CUR);
+ break;
+ }
+ }
+
+ fseek(inputFile, currentPosition, SEEK_SET); // start at 0
+
+ return 0;
+
+}
+
+void WriteCAFFcaffChunk(FILE * outputFile)
+{
+ uint8_t theReadBuffer[8] = {'c', 'a', 'f', 'f', 0, 1, 0, 0};
+
+ fwrite(theReadBuffer, 1, 8, outputFile);
+}
+
+void WriteCAFFdescChunk(FILE * outputFile, AudioFormatDescription theOutputFormat)
+{
+ port_CAFAudioDescription theDescription;
+ uint32_t tempFormatFlags = theOutputFormat.mFormatFlags;
+ uint8_t theReadBuffer[12] = {'d', 'e', 's', 'c', 0, 0, 0, 0, 0, 0, 0, 0};
+
+ if (theOutputFormat.mFormatID == kALACFormatLinearPCM)
+ {
+ if (kALACFormatFlagsNativeEndian > 0) // kALACFormatFlagsNativeEndian is 2 on a big endian machine, 0 on little
+ {
+ tempFormatFlags = 0;
+ }
+ else
+ {
+ tempFormatFlags = k_port_CAFLinearPCMFormatFlagIsLittleEndian;
+ }
+ }
+
+ theDescription.mSampleRate = SwapFloat64NtoB(theOutputFormat.mSampleRate);
+ theDescription.mFormatID = Swap32NtoB(theOutputFormat.mFormatID);
+ theDescription.mFormatFlags = Swap32NtoB(tempFormatFlags);
+ theDescription.mBytesPerPacket = Swap32NtoB(theOutputFormat.mBytesPerPacket);
+ theDescription.mFramesPerPacket = Swap32NtoB(theOutputFormat.mFramesPerPacket);
+ theDescription.mChannelsPerFrame = Swap32NtoB(theOutputFormat.mChannelsPerFrame);
+ theDescription.mBitsPerChannel = Swap32NtoB(theOutputFormat.mBitsPerChannel);
+
+ theReadBuffer[11] = sizeof(port_CAFAudioDescription);
+ fwrite(theReadBuffer, 1, 12, outputFile);
+ fwrite(&theDescription, 1, sizeof(port_CAFAudioDescription), outputFile);
+}
+
+void WriteCAFFdataChunk(FILE * outputFile)
+{
+ uint8_t theReadBuffer[16] = {'d', 'a', 't', 'a', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
+
+ fwrite(theReadBuffer, 1, 16, outputFile);
+}
+
+void WriteCAFFkukiChunk(FILE * outputFile, void * inCookie, uint32_t inCookieSize)
+{
+ uint8_t thekukiHeaderBuffer[12] = {'k', 'u', 'k', 'i', 0, 0, 0, 0, 0, 0, 0, 0};
+
+ thekukiHeaderBuffer[11] = inCookieSize;
+ fwrite(thekukiHeaderBuffer, 1, 12, outputFile);
+ fwrite(inCookie, 1, inCookieSize, outputFile);
+}
+
+void WriteCAFFChunkSize(FILE * outputFile, int64_t numDataBytes)
+{
+ uint8_t theBuffer[8];
+
+ theBuffer[0] = (numDataBytes >> 56) & 0xff;
+ theBuffer[1] = (numDataBytes >> 48) & 0xff;
+ theBuffer[2] = (numDataBytes >> 40) & 0xff;
+ theBuffer[3] = (numDataBytes >> 32) & 0xff;
+ theBuffer[4] = (numDataBytes >> 24) & 0xff;
+ theBuffer[5] = (numDataBytes >> 16) & 0xff;
+ theBuffer[6] = (numDataBytes >> 8) & 0xff;
+ theBuffer[7] = numDataBytes & 0xff;
+ fwrite(theBuffer, 1, 8, outputFile);
+}
+
+void WriteCAFFchanChunk(FILE * outputFile, uint32_t inChannelTag)
+{
+ uint8_t theBuffer[24] = {'c', 'h', 'a', 'n', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
+
+ theBuffer[11] = sizeof(ALACAudioChannelLayout);
+ theBuffer[12] = inChannelTag >> 24;
+ theBuffer[13] = (inChannelTag >> 16) & 0xff;
+ theBuffer[14] = (inChannelTag >> 8) & 0xff;
+ theBuffer[15] = inChannelTag & 0xff;
+
+ fwrite(theBuffer, 1, 24, outputFile);
+}
+
+void WriteCAFFfreeChunk(FILE * outputFile, uint32_t theSize)
+{
+ uint8_t theBuffer[12] = {'f', 'r', 'e', 'e', 0, 0, 0, 0, 0, 0, 0, 0};
+ uint32_t i = 0;
+ uint32_t theAdjustedSize = theSize - sizeof(port_CAFChunkHeader);
+
+ if (theSize > theAdjustedSize) // cause we might have wrapped theAdjustedSize
+ {
+ theBuffer[8] = theAdjustedSize >> 24;
+ theBuffer[9] = (theAdjustedSize >> 16) & 0xff;
+ theBuffer[10] = (theAdjustedSize >> 8) & 0xff;
+ theBuffer[11] = theAdjustedSize & 0xff;
+ fwrite(theBuffer, 1, 12, outputFile);
+
+ for (i = 0; i < theAdjustedSize; ++i)
+ {
+ fwrite(&(theBuffer[4]), 1, 1, outputFile);
+ }
+ }
+}
+
+void WriteCAFFpaktChunkHeader(FILE * outputFile, port_CAFPacketTableHeader * thePacketTableHeader, uint32_t thePacketTableSize)
+{
+ uint8_t theBuffer[12];
+ // Endian swap!
+ thePacketTableHeader->mNumberPackets = Swap64NtoB(thePacketTableHeader->mNumberPackets);
+ thePacketTableHeader->mNumberValidFrames = Swap64NtoB(thePacketTableHeader->mNumberValidFrames);
+ thePacketTableHeader->mPrimingFrames = Swap32NtoB(thePacketTableHeader->mPrimingFrames);
+ thePacketTableHeader->mRemainderFrames = Swap32NtoB(thePacketTableHeader->mRemainderFrames);
+ // write out the pakt chunk -- big endian!
+ theBuffer[0] = 'p';
+ theBuffer[1] = 'a';
+ theBuffer[2] = 'k';
+ theBuffer[3] = 't';
+ theBuffer[4] = 0;
+ theBuffer[5] = 0;
+ theBuffer[6] = 0;
+ theBuffer[7] = 0;
+ theBuffer[8] = thePacketTableSize >> 24;
+ theBuffer[9] = (thePacketTableSize >> 16) & 0xff;
+ theBuffer[10] = (thePacketTableSize >> 8) & 0xff;
+ theBuffer[11] = thePacketTableSize & 0xff;
+ fwrite(theBuffer, 1, 12, outputFile);
+
+ fwrite(thePacketTableHeader, 1, kMinCAFFPacketTableHeaderSize, outputFile);
+}
+
+void GetBERInteger(int32_t theOriginalValue, uint8_t * theBuffer, int32_t * theBERSize)
+{
+ if ((theOriginalValue & 0x7f) == theOriginalValue)
+ {
+ *theBERSize = 1;
+ theBuffer[0] = theOriginalValue;
+ }
+ else if ((theOriginalValue & 0x3fff) == theOriginalValue)
+ {
+ *theBERSize = 2;
+ theBuffer[0] = theOriginalValue >> 7;
+ theBuffer[0] |= 0x80;
+ theBuffer[1] = theOriginalValue & 0x7f;
+ }
+ else if ((theOriginalValue & 0x1fffff) == theOriginalValue)
+ {
+ *theBERSize = 3;
+ theBuffer[0] = theOriginalValue >> 14;
+ theBuffer[0] |= 0x80;
+ theBuffer[1] = (theOriginalValue >> 7) & 0x7f;
+ theBuffer[1] |= 0x80;
+ theBuffer[2] = theOriginalValue & 0x7f;
+ }
+ else if ((theOriginalValue & 0x0fffffff) == theOriginalValue)
+ {
+ *theBERSize = 4;
+ theBuffer[0] = theOriginalValue >> 21;
+ theBuffer[0] |= 0x80;
+ theBuffer[1] = (theOriginalValue >> 14) & 0x7f;
+ theBuffer[1] |= 0x80;
+ theBuffer[2] = (theOriginalValue >> 7) & 0x7f;
+ theBuffer[2] |= 0x80;
+ theBuffer[3] = theOriginalValue & 0x7f;
+ }
+ else
+ {
+ *theBERSize = 5;
+ theBuffer[0] = theOriginalValue >> 28;
+ theBuffer[0] |= 0x80;
+ theBuffer[1] = (theOriginalValue >> 21) & 0x7f;
+ theBuffer[1] |= 0x80;
+ theBuffer[2] = (theOriginalValue >> 14) & 0x7f;
+ theBuffer[2] |= 0x80;
+ theBuffer[3] = (theOriginalValue >> 7) & 0x7f;
+ theBuffer[3] |= 0x80;
+ theBuffer[4] = theOriginalValue & 0x7f;
+ }
+}
+
+uint32_t ReadBERInteger(uint8_t * theInputBuffer, int32_t * ioNumBytes)
+{
+ uint32_t theAnswer = 0;
+ uint8_t theData;
+ int32_t size = 0;
+ do
+ {
+ theData = theInputBuffer[size];
+ theAnswer = (theAnswer << 7) | (theData & 0x7F);
+ if (++size > 5)
+ {
+ size = 0xFFFFFFFF;
+ return 0;
+ }
+ }
+ while(((theData & 0x80) != 0) && (size <= *ioNumBytes));
+
+ *ioNumBytes = size;
+
+ return theAnswer;
+}
+
+int32_t BuildBasePacketTable(AudioFormatDescription theInputFormat, int32_t inputDataSize, int32_t * theMaxPacketTableSize, port_CAFPacketTableHeader * thePacketTableHeader)
+{
+ int32_t theMaxPacketSize = 0, theByteSizeTableEntry = 0;
+
+ // fill out the header
+ thePacketTableHeader->mNumberValidFrames = inputDataSize/((theInputFormat.mBitsPerChannel >> 3) * theInputFormat.mChannelsPerFrame);
+ thePacketTableHeader->mNumberPackets = thePacketTableHeader->mNumberValidFrames/kALACDefaultFramesPerPacket;
+ thePacketTableHeader->mPrimingFrames = 0;
+ thePacketTableHeader->mRemainderFrames = thePacketTableHeader->mNumberValidFrames - thePacketTableHeader->mNumberPackets * kALACDefaultFramesPerPacket;
+ thePacketTableHeader->mRemainderFrames = kALACDefaultFramesPerPacket - thePacketTableHeader->mRemainderFrames;
+ if (thePacketTableHeader->mRemainderFrames) thePacketTableHeader->mNumberPackets += 1;
+
+ // Ok, we have to assume the worst case scenario for packet sizes
+ theMaxPacketSize = (theInputFormat.mBitsPerChannel >> 3) * theInputFormat.mChannelsPerFrame * kALACDefaultFramesPerPacket + kALACMaxEscapeHeaderBytes;
+
+ if (theMaxPacketSize < 16384)
+ {
+ theByteSizeTableEntry = 2;
+ }
+ else
+ {
+ theByteSizeTableEntry = 3;
+ }
+ *theMaxPacketTableSize = theByteSizeTableEntry * thePacketTableHeader->mNumberPackets;
+
+ return 0;
+}
+
+uint32_t GetMagicCookieSizeFromCAFFkuki(FILE * inputFile)
+{
+ // returns to the current absolute position within the file
+ int32_t currentPosition = ftell(inputFile); // record the current position
+ uint8_t theReadBuffer[sizeof(ALACSpecificConfig)];
+ uint32_t chunkType = 0, chunkSize = 0;
+ bool done = false;
+ int32_t bytesRead = sizeof(port_CAFFileHeader);
+ uint32_t theCookieSize = 0;
+
+ fseek(inputFile, bytesRead, SEEK_SET); // start at 8!
+ while (!done && bytesRead > 0) // no file size here
+ {
+ bytesRead = fread(theReadBuffer, 1, 12, inputFile);
+ chunkType = ((int32_t)(theReadBuffer[0]) << 24) + ((int32_t)(theReadBuffer[1]) << 16) + ((int32_t)(theReadBuffer[2]) << 8) + theReadBuffer[3];
+ switch(chunkType)
+ {
+ case 'kuki':
+ {
+ theCookieSize = theReadBuffer[11];
+ done = true;
+ break;
+ }
+ default:
+ chunkSize = ((int32_t)(theReadBuffer[8]) << 24) + ((int32_t)(theReadBuffer[9]) << 16) + ((int32_t)(theReadBuffer[10]) << 8) + theReadBuffer[11];
+ fseek(inputFile, chunkSize, SEEK_CUR);
+ break;
+ }
+ }
+
+ fseek(inputFile, currentPosition, SEEK_SET); // start at 0
+
+ if (!done) return -1;
+
+ return theCookieSize;
+
+}
+// gets the kuki chunk from a caff file
+int32_t GetMagicCookieFromCAFFkuki(FILE * inputFile, uint8_t * outMagicCookie, uint32_t * ioMagicCookieSize)
+{
+ // returns to the current absolute position within the file
+ int32_t currentPosition = ftell(inputFile); // record the current position
+ uint8_t theReadBuffer[12];
+ uint32_t chunkType = 0, chunkSize = 0;
+ bool done = false, cookieFound = false;
+ int32_t bytesRead = sizeof(port_CAFFileHeader);
+ uint32_t theStoredCookieSize = 0;
+
+ fseek(inputFile, bytesRead, SEEK_SET); // start at 8!
+ while (!done && bytesRead > 0) // no file size here
+ {
+ bytesRead = fread(theReadBuffer, 1, 12, inputFile);
+ chunkType = ((int32_t)(theReadBuffer[0]) << 24) + ((int32_t)(theReadBuffer[1]) << 16) + ((int32_t)(theReadBuffer[2]) << 8) + theReadBuffer[3];
+ switch(chunkType)
+ {
+ case 'kuki':
+ {
+ theStoredCookieSize = theReadBuffer[11];
+ if (*ioMagicCookieSize >= theStoredCookieSize)
+ {
+ fread(outMagicCookie, 1, theStoredCookieSize, inputFile);
+ *ioMagicCookieSize = theStoredCookieSize;
+ cookieFound = true;
+ }
+ else
+ {
+ *ioMagicCookieSize = 0;
+ }
+ done = true;
+ break;
+ }
+ default:
+ chunkSize = ((int32_t)(theReadBuffer[8]) << 24) + ((int32_t)(theReadBuffer[9]) << 16) + ((int32_t)(theReadBuffer[10]) << 8) + theReadBuffer[11];
+ fseek(inputFile, chunkSize, SEEK_CUR);
+ break;
+ }
+ }
+
+ fseek(inputFile, currentPosition, SEEK_SET); // start at 0
+
+ if (!done || !cookieFound) return -1;
+
+ return 0;
+}
+
+bool FindCAFFDataStart(FILE * inputFile, int32_t * dataPos, int32_t * dataSize)
+{
+ bool done = false;
+ int32_t bytesRead = 8;
+ uint32_t chunkType = 0, chunkSize = 0;
+ uint8_t theBuffer[12];
+
+ fseek(inputFile, bytesRead, SEEK_SET); // start at 8!
+ while (!done && bytesRead > 0) // no file size here
+ {
+ bytesRead = fread(theBuffer, 1, 12, inputFile);
+ chunkType = ((int32_t)(theBuffer[0]) << 24) + ((int32_t)(theBuffer[1]) << 16) + ((int32_t)(theBuffer[2]) << 8) + theBuffer[3];
+ switch(chunkType)
+ {
+ case 'data':
+ *dataPos = ftell(inputFile) + sizeof(uint32_t); // skip the edits
+ // big endian size
+ *dataSize = ((int32_t)(theBuffer[8]) << 24) + ((int32_t)(theBuffer[9]) << 16) + ((int32_t)(theBuffer[10]) << 8) + theBuffer[11];
+ *dataSize -= 4; // the edits are included in the size
+ done = true;
+ break;
+ default:
+ chunkSize = ((int32_t)(theBuffer[8]) << 24) + ((int32_t)(theBuffer[9]) << 16) + ((int32_t)(theBuffer[10]) << 8) + theBuffer[11];
+ fseek(inputFile, chunkSize, SEEK_CUR);
+ break;
+ }
+ }
+ return done;
+}
+
+bool GetCAFFdescFormat(FILE * inputFile, AudioFormatDescription * theInputFormat)
+{
+ bool done = false;
+ uint32_t theChunkSize = 0, theChunkType = 0;
+ uint8_t theReadBuffer[32];
+
+ fseek(inputFile, 4, SEEK_CUR); // skip 4 bytes
+
+ while (!done)
+ {
+ fread(theReadBuffer, 1, 4, inputFile);
+ theChunkType = ((int32_t)(theReadBuffer[0]) << 24) + ((int32_t)(theReadBuffer[1]) << 16) + ((int32_t)(theReadBuffer[2]) << 8) + theReadBuffer[3];
+ switch (theChunkType)
+ {
+ case 'desc':
+ fseek(inputFile, 8, SEEK_CUR); // skip 8 bytes
+ fread(theReadBuffer, 1, sizeof(port_CAFAudioDescription), inputFile);
+ theInputFormat->mFormatID = Swap32BtoN(((port_CAFAudioDescription *)(theReadBuffer))->mFormatID);
+ theInputFormat->mChannelsPerFrame = Swap32BtoN(((port_CAFAudioDescription *)(theReadBuffer))->mChannelsPerFrame);
+ theInputFormat->mSampleRate = SwapFloat64BtoN(((port_CAFAudioDescription *)(theReadBuffer))->mSampleRate);
+ theInputFormat->mBitsPerChannel = Swap32BtoN(((port_CAFAudioDescription *)(theReadBuffer))->mBitsPerChannel);
+ theInputFormat->mFormatFlags = Swap32BtoN(((port_CAFAudioDescription *)(theReadBuffer))->mFormatFlags);
+ theInputFormat->mBytesPerPacket = Swap32BtoN(((port_CAFAudioDescription *)(theReadBuffer))->mBytesPerPacket);
+ if (theInputFormat->mFormatID == kALACFormatAppleLossless)
+ {
+ theInputFormat->mBytesPerFrame = 0;
+ }
+ else
+ {
+ theInputFormat->mBytesPerFrame = theInputFormat->mBytesPerPacket;
+ if ((theInputFormat->mFormatFlags & 0x02) == 0x02)
+ {
+ theInputFormat->mFormatFlags &= 0xfffffffc;
+ }
+ else
+ {
+ theInputFormat->mFormatFlags |= 0x02;
+ }
+
+ }
+ theInputFormat->mFramesPerPacket = Swap32BtoN(((port_CAFAudioDescription *)(theReadBuffer))->mFramesPerPacket);
+ theInputFormat->mReserved = 0;
+ done = true;
+ break;
+ default:
+ // read the size and skip
+ fread(theReadBuffer, 1, 8, inputFile);
+ theChunkSize = ((int32_t)(theReadBuffer[4]) << 24) + ((int32_t)(theReadBuffer[5]) << 16) + ((int32_t)(theReadBuffer[6]) << 8) + theReadBuffer[7];
+ fseek(inputFile, theChunkSize, SEEK_CUR);
+ break;
+ }
+ }
+ return done;
+}
diff --git a/convert-utility/CAFFileALAC.h b/convert-utility/CAFFileALAC.h
new file mode 100755
index 0000000..576773a
--- /dev/null
+++ b/convert-utility/CAFFileALAC.h
@@ -0,0 +1,209 @@
+/*
+ Copyright © 2011 Apple Inc. All rights reserved.
+
+ IMPORTANT: This Apple software is supplied to you by Apple Inc. (“Apple”) in consideration of your agreement to the following terms, and your use, installation, modification or redistribution of this Apple software constitutes acceptance of these terms. If you do not agree with these terms, please do not use, install, modify or redistribute this Apple software.
+
+ In consideration of your agreement to abide by the following terms, and subject to these terms, Apple grants you a personal, non-exclusive license, under Apple’s copyrights in this original Apple software (the “Apple Software”), to use, reproduce, modify and redistribute the Apple Software, with or without modifications, in source and/or binary forms; provided that if you redistribute the Apple Software in its entirety and without modifications, you must retain this notice and the following text and disclaimers in all such redistributions of the Apple Software. Neither the name, trademarks, service marks or logos of Apple Inc. may be used to endorse or promote products derived from the Apple Software without specific prior written permission from Apple. Except as expressly stated in this notice, no other rights or licenses, express or implied, are granted by Apple herein, including but not limited to any patent rights that may be infringed by your derivative works or by other works in which the Apple Software may be incorporated.
+
+ The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
+
+ IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+//
+// CAFFileALAC.h
+// based CAFFile.h in the CoreAudio headers, ALAC specific
+//
+// Copyright 2011 Apple Inc. All rights reserved.
+//
+
+#ifndef _CAFFilePortable_h
+#define _CAFFilePortable_h
+
+#if TARGET_OS_WIN32
+#define ATTRIBUTE_PACKED
+#pragma pack(push, 1)
+#else
+#define ATTRIBUTE_PACKED __attribute__((__packed__))
+#endif
+
+#include "ALACAudioTypes.h"
+
+#define kMinCAFFPacketTableHeaderSize 24
+
+typedef uint32_t CAFFChannelLayoutTag;
+// These are subset of the channel layout tags listed in CoreAudioTypes.h
+// ALAC and caff both use the same tag values
+enum
+{
+ kCAFFChannelLayoutTag_Mono = (100<<16) | 1, // C
+ kCAFFChannelLayoutTag_Stereo = (101<<16) | 2, // L R
+ kCAFFChannelLayoutTag_MPEG_3_0_B = (113<<16) | 3, // C L R
+ kCAFFChannelLayoutTag_MPEG_4_0_B = (116<<16) | 4, // C L R Cs
+ kCAFFChannelLayoutTag_MPEG_5_0_D = (120<<16) | 5, // C L R Ls Rs
+ kCAFFChannelLayoutTag_MPEG_5_1_D = (124<<16) | 6, // C L R Ls Rs LFE
+ kCAFFChannelLayoutTag_AAC_6_1 = (142<<16) | 7, // C L R Ls Rs Cs LFE
+ kCAFFChannelLayoutTag_MPEG_7_1_B = (127<<16) | 8 // C Lc Rc L R Ls Rs LFE
+};
+
+// ALAC currently only utilizes these channels layouts. CAFF supports all those listed in
+// CoreAudioTypes.h.
+static const CAFFChannelLayoutTag CAFFChannelLayoutTags[kALACMaxChannels] =
+{
+ kCAFFChannelLayoutTag_Mono, // C
+ kCAFFChannelLayoutTag_Stereo, // L R
+ kCAFFChannelLayoutTag_MPEG_3_0_B, // C L R
+ kCAFFChannelLayoutTag_MPEG_4_0_B, // C L R Cs
+ kCAFFChannelLayoutTag_MPEG_5_0_D, // C L R Ls Rs
+ kCAFFChannelLayoutTag_MPEG_5_1_D, // C L R Ls Rs LFE
+ kCAFFChannelLayoutTag_AAC_6_1, // C L R Ls Rs Cs LFE
+ kCAFFChannelLayoutTag_MPEG_7_1_B // C Lc Rc L R Ls Rs LFE (doc: IS-13818-7 MPEG2-AAC)
+};
+
+
+// In a CAF File all of these types' byte order is big endian.
+// When reading or writing these values the program will need to flip byte order to native endian
+
+// CAF File Header
+enum {
+ k_port__port_CAF_FileType = 'caff',
+ k_port_CAF_FileVersion_Initial = 1
+};
+
+// CAF Chunk Types
+enum {
+ k_port_CAF_StreamDescriptionChunkID = 'desc',
+ k_port_CAF_AudioDataChunkID = 'data',
+ k_port_CAF_ChannelLayoutChunkID = 'chan',
+ k_port_CAF_MagicCookieID = 'kuki',
+ k_port_CAF_PacketTableChunkID = 'pakt',
+ k_port_CAF_FreeTableChunkID = 'free'
+};
+
+
+struct port_CAFFileHeader
+{
+ uint32_t mFileType; // 'caff'
+ uint16_t mFileVersion; //initial revision set to 1
+ uint16_t mFileFlags; //initial revision set to 0
+} ATTRIBUTE_PACKED;
+typedef struct CAFFileHeader CAFFileHeader;
+
+
+struct port_CAFChunkHeader
+{
+ uint32_t mChunkType; // four char code
+ int64_t mChunkSize; // size in bytes of the chunk data (not including this header).
+ // mChunkSize is int64_t not uint64_t because negative values for
+ // the data size can have a special meaning
+} ATTRIBUTE_PACKED;
+
+typedef struct port_CAFChunkHeader port_CAFChunkHeader;
+
+// Every file MUST have this chunk. It MUST be the first chunk in the file
+struct port_CAFAudioDescription
+{
+ double mSampleRate;
+ uint32_t mFormatID;
+ uint32_t mFormatFlags;
+ uint32_t mBytesPerPacket;
+ uint32_t mFramesPerPacket;
+ uint32_t mChannelsPerFrame;
+ uint32_t mBitsPerChannel;
+} ATTRIBUTE_PACKED;
+typedef struct port_CAFAudioDescription port_CAFAudioDescription;
+
+// these are the flags if the format ID is 'lpcm'
+// <CoreAudio/CoreAudioTypes.h> declares some of the format constants
+// that can be used as Data Formats in a CAF file
+enum
+{
+ k_port_CAFLinearPCMFormatFlagIsFloat = (1L << 0),
+ k_port_CAFLinearPCMFormatFlagIsLittleEndian = (1L << 1)
+};
+
+
+// 'chan' Optional chunk.
+// struct AudioChannelLayout as defined in CoreAudioTypes.h.
+
+// 'free'
+// this is a padding chunk for reserving space in the file. content is meaningless.
+
+// 'kuki'
+// this is the magic cookie chunk. bag of bytes.
+
+// 'data' Every file MUST have this chunk.
+// actual audio data can be any format as described by the 'asbd' chunk.
+
+// if mChunkSize is < 0 then this is the last chunk in the file and the actual length
+// should be determined from the file size.
+// The motivation for this is to allow writing the files without seeking to update size fields after every
+// write in order to keep the file legal.
+// The program can put a -1 in the mChunkSize field and
+// update it only once at the end of recording.
+// If the program were to crash during recording then the file is still well defined.
+
+// 'pakt' Required if either/or mBytesPerPacket or mFramesPerPacket in the Format Description are zero
+// For formats that are packetized and have variable sized packets.
+// The table is stored as an array of one or two variable length integers.
+// (a) size in bytes of the data of a given packet.
+// (b) number of frames in a given packet.
+// These sizes are encoded as variable length integers
+
+// The packet description entries are either one or two values depending on the format.
+// There are three possibilities
+// (1)
+// If the format has variable bytes per packets (desc.mBytesPerPacket == 0) and constant frames per packet
+// (desc.mFramesPerPacket != 0) then the packet table contains single entries representing the bytes in a given packet
+// (2)
+// If the format is a constant bit rate (desc.mBytesPerPacket != 0) but variable frames per packet
+// (desc.mFramesPerPacket == 0) then the packet table entries contains single entries
+// representing the number of frames in a given packet
+// (3)
+// If the format has variable frames per packet (asbd.mFramesPerPacket == 0) and variable bytes per packet
+// (desc.mBytesPerPacket == 0) then the packet table entries are a duple of two values. The first value
+// is the number of bytes in a given packet, the second value is the number of frames in a given packet
+
+struct port_CAFPacketTableHeader
+{
+ int64_t mNumberPackets;
+ int64_t mNumberValidFrames;
+ int32_t mPrimingFrames;
+ int32_t mRemainderFrames;
+
+ uint8_t mPacketDescriptions[1]; // this is a variable length array of mNumberPackets elements
+} ATTRIBUTE_PACKED;
+typedef struct port_CAFPacketTableHeader port_CAFPacketTableHeader;
+
+struct port_CAFDataChunk
+{
+ uint32_t mEditCount;
+ uint8_t mData[1]; // this is a variable length data field based off the size of the data chunk
+} ATTRIBUTE_PACKED;
+typedef struct port_CAFDataChunk port_CAFDataChunk;
+
+// prototypes
+int32_t FindCAFFPacketTableStart(FILE * inputFile, int32_t * paktPos, int32_t * paktSize);
+void WriteCAFFcaffChunk(FILE * outputFile);
+void WriteCAFFdescChunk(FILE * outputFile, AudioFormatDescription theOutputFormat);
+void WriteCAFFdataChunk(FILE * outputFile);
+void WriteCAFFkukiChunk(FILE * outputFile, void * inCookie, uint32_t inCookieSize);
+void WriteCAFFChunkSize(FILE * outputFile, int64_t numDataBytes);
+void WriteCAFFchanChunk(FILE * outputFile, uint32_t inChannelTag);
+void WriteCAFFfreeChunk(FILE * outputFile, uint32_t theSize);
+void WriteCAFFpaktChunkHeader(FILE * outputFile, port_CAFPacketTableHeader * thePacketTableHeader, uint32_t thePacketTableSize);
+void GetBERInteger(int32_t theOriginalValue, uint8_t * theBuffer, int32_t * theBERSize);
+uint32_t ReadBERInteger(uint8_t * theInputBuffer, int32_t * ioNumBytes);
+int32_t BuildBasePacketTable(AudioFormatDescription theInputFormat, int32_t inputDataSize, int32_t * thePacketTableSize, port_CAFPacketTableHeader * thePacketTableHeader);
+uint32_t GetMagicCookieSizeFromCAFFkuki(FILE * inputFile);
+int32_t GetMagicCookieFromCAFFkuki(FILE * inputFile, uint8_t * outMagicCookie, uint32_t * ioMagicCookieSize);
+bool FindCAFFDataStart(FILE * inputFile, int32_t * dataPos, int32_t * dataSize);
+bool GetCAFFdescFormat(FILE * inputFile, AudioFormatDescription * theInputFormat);
+
+#if TARGET_OS_WIN32
+#pragma pack(pop)
+#endif
+////////////////////////////////////////////////////////////////////////////////////////////////
+
+
+#endif