]>
Commit | Line | Data |
---|---|---|
1f75fe4e MB |
1 | /* |
2 | * This file is part of Shairport Sync. | |
3 | * Copyright (c) Mike Brady 2019 | |
4 | * All rights reserved. | |
5 | * | |
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: | |
13 | * | |
14 | * The above copyright notice and this permission notice shall be | |
15 | * included in all copies or substantial portions of the Software. | |
16 | * | |
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. | |
25 | */ | |
26 | ||
945483d9 MB |
27 | #include <string.h> |
28 | ||
29 | // these are headers for the ALAC decoder, utilities and endian utilities | |
945483d9 | 30 | #include <alac/ALACBitUtilities.h> |
064bd293 | 31 | #include <alac/ALACDecoder.h> |
945483d9 MB |
32 | #include <alac/EndianPortable.h> |
33 | ||
945483d9 | 34 | #include "apple_alac.h" |
064bd293 | 35 | #include "config.h" |
945483d9 MB |
36 | |
37 | typedef struct magicCookie { | |
38 | ALACSpecificConfig config; | |
39 | ALACAudioChannelLayout channelLayoutInfo; // seems to be unused | |
40 | } magicCookie; | |
41 | ||
42 | magicCookie cookie; | |
064bd293 | 43 | ALACDecoder *theDecoder; |
945483d9 | 44 | |
0479994c | 45 | extern "C" int apple_alac_init(int32_t fmtp[12]) { |
945483d9 | 46 | |
064bd293 MB |
47 | memset(&cookie, 0, sizeof(magicCookie)); |
48 | ||
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 | |
51 | ||
0479994c | 52 | cookie.config.frameLength = Swap32NtoB(352); |
064bd293 MB |
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 | |
0479994c | 60 | cookie.config.maxFrameBytes = Swap32NtoB(fmtp[9]); // uint32_t should be 0; |
064bd293 MB |
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; | |
63 | ||
945483d9 | 64 | theDecoder = new ALACDecoder; |
064bd293 | 65 | theDecoder->Init(&cookie, sizeof(magicCookie)); |
945483d9 MB |
66 | |
67 | return 0; | |
68 | } | |
69 | ||
064bd293 MB |
70 | extern "C" int apple_alac_decode_frame(unsigned char *sampleBuffer, uint32_t bufferLength, |
71 | unsigned char *dest, int *outsize) { | |
945483d9 MB |
72 | uint32_t numFrames = 0; |
73 | BitBuffer theInputBuffer; | |
74 | BitBufferInit(&theInputBuffer, sampleBuffer, bufferLength); | |
064bd293 MB |
75 | theDecoder->Decode(&theInputBuffer, dest, Swap32BtoN(cookie.config.frameLength), |
76 | cookie.config.numChannels, &numFrames); | |
945483d9 MB |
77 | *outsize = numFrames; |
78 | return 0; | |
79 | } | |
80 | ||
63707285 MB |
81 | extern "C" int apple_alac_terminate() { |
82 | delete (theDecoder); | |
83 | return 0; | |
84 | } |