2 * This file is part of Shairport Sync.
3 * Copyright (c) Mike Brady 2019
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or
11 * sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
29 // these are headers for the ALAC decoder, utilities and endian utilities
30 #include <alac/ALACBitUtilities.h>
31 #include <alac/ALACDecoder.h>
32 #include <alac/EndianPortable.h>
34 #include "apple_alac.h"
37 typedef struct magicCookie
{
38 ALACSpecificConfig config
;
39 ALACAudioChannelLayout channelLayoutInfo
; // seems to be unused
43 ALACDecoder
*theDecoder
;
45 extern "C" int apple_alac_init(int32_t fmtp
[12]) {
47 memset(&cookie
, 0, sizeof(magicCookie
));
49 // create a magic cookie for the decoder from the fmtp information. It seems to be in the same
50 // format as a simple magic cookie
52 cookie
.config
.frameLength
= Swap32NtoB(352);
53 cookie
.config
.compatibleVersion
= fmtp
[2]; // should be zero, uint8_t
54 cookie
.config
.bitDepth
= fmtp
[3]; // uint8_t expected to be 16
55 cookie
.config
.pb
= fmtp
[4]; // uint8_t should be 40;
56 cookie
.config
.mb
= fmtp
[5]; // uint8_t should be 10;
57 cookie
.config
.kb
= fmtp
[6]; // uint8_t should be 14;
58 cookie
.config
.numChannels
= fmtp
[7]; // uint8_t expected to be 2
59 cookie
.config
.maxRun
= Swap16NtoB(fmtp
[8]); // uint16_t expected to be 255
60 cookie
.config
.maxFrameBytes
= Swap32NtoB(fmtp
[9]); // uint32_t should be 0;
61 cookie
.config
.avgBitRate
= Swap32NtoB(fmtp
[10]); // uint32_t should be 0;;
62 cookie
.config
.sampleRate
= Swap32NtoB(fmtp
[11]); // uint32_t expected to be 44100;
64 theDecoder
= new ALACDecoder
;
65 theDecoder
->Init(&cookie
, sizeof(magicCookie
));
70 extern "C" int apple_alac_decode_frame(unsigned char *sampleBuffer
, uint32_t bufferLength
,
71 unsigned char *dest
, int *outsize
) {
72 uint32_t numFrames
= 0;
73 BitBuffer theInputBuffer
;
74 BitBufferInit(&theInputBuffer
, sampleBuffer
, bufferLength
);
75 theDecoder
->Decode(&theInputBuffer
, dest
, Swap32BtoN(cookie
.config
.frameLength
),
76 cookie
.config
.numChannels
, &numFrames
);
81 extern "C" int apple_alac_terminate() {