ALACDecoder.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. /*
  2. * Copyright (c) 2011 Apple Inc. All rights reserved.
  3. *
  4. * @APPLE_APACHE_LICENSE_HEADER_START@
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * @APPLE_APACHE_LICENSE_HEADER_END@
  19. */
  20. /*
  21. File: ALACDecoder.cpp
  22. */
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include "ALACDecoder.h"
  26. #include "dplib.h"
  27. #include "aglib.h"
  28. #include "matrixlib.h"
  29. #include "ALACBitUtilities.h"
  30. #include "EndianPortable.h"
  31. #if (__GNUC__) > 4 || defined (__APPLE__)
  32. #pragma GCC diagnostic ignored "-Wunused-const-variable"
  33. #endif
  34. #if !defined(__APPLE__)
  35. #pragma GCC diagnostic ignored "-Wunused-but-set-variable"
  36. #endif
  37. // constants/data
  38. const uint32_t kMaxBitDepth = 32; // max allowed bit depth is 32
  39. // prototypes
  40. static void Zero16( int16_t * buffer, uint32_t numItems, uint32_t stride );
  41. static void Zero24( uint8_t * buffer, uint32_t numItems, uint32_t stride );
  42. static void Zero32( int32_t * buffer, uint32_t numItems, uint32_t stride );
  43. /*
  44. Constructor
  45. */
  46. ALACDecoder::ALACDecoder() :
  47. mMixBufferU( nil ),
  48. mMixBufferV( nil ),
  49. mPredictor( nil ),
  50. mShiftBuffer( nil )
  51. {
  52. memset( &mConfig, 0, sizeof(mConfig) );
  53. }
  54. /*
  55. Destructor
  56. */
  57. ALACDecoder::~ALACDecoder()
  58. {
  59. // delete the matrix mixing buffers
  60. if ( mMixBufferU )
  61. {
  62. free(mMixBufferU);
  63. mMixBufferU = NULL;
  64. }
  65. if ( mMixBufferV )
  66. {
  67. free(mMixBufferV);
  68. mMixBufferV = NULL;
  69. }
  70. // delete the dynamic predictor's "corrector" buffer
  71. // - note: mShiftBuffer shares memory with this buffer
  72. if ( mPredictor )
  73. {
  74. free(mPredictor);
  75. mPredictor = NULL;
  76. }
  77. }
  78. /*
  79. Init()
  80. - initialize the decoder with the given configuration
  81. */
  82. int32_t ALACDecoder::Init( void * inMagicCookie, uint32_t inMagicCookieSize )
  83. {
  84. int32_t status = ALAC_noErr;
  85. ALACSpecificConfig theConfig;
  86. uint8_t * theActualCookie = (uint8_t *)inMagicCookie;
  87. uint32_t theCookieBytesRemaining = inMagicCookieSize;
  88. // For historical reasons the decoder needs to be resilient to magic cookies vended by older encoders.
  89. // As specified in the ALACMagicCookieDescription.txt document, there may be additional data encapsulating
  90. // the ALACSpecificConfig. This would consist of format ('frma') and 'alac' atoms which precede the
  91. // ALACSpecificConfig.
  92. // See ALACMagicCookieDescription.txt for additional documentation concerning the 'magic cookie'
  93. // skip format ('frma') atom if present
  94. if (theActualCookie[4] == 'f' && theActualCookie[5] == 'r' && theActualCookie[6] == 'm' && theActualCookie[7] == 'a')
  95. {
  96. theActualCookie += 12;
  97. theCookieBytesRemaining -= 12;
  98. }
  99. // skip 'alac' atom header if present
  100. if (theActualCookie[4] == 'a' && theActualCookie[5] == 'l' && theActualCookie[6] == 'a' && theActualCookie[7] == 'c')
  101. {
  102. theActualCookie += 12;
  103. theCookieBytesRemaining -= 12;
  104. }
  105. // read the ALACSpecificConfig
  106. if (theCookieBytesRemaining >= sizeof(ALACSpecificConfig))
  107. {
  108. theConfig.frameLength = Swap32BtoN(((ALACSpecificConfig *)theActualCookie)->frameLength);
  109. theConfig.compatibleVersion = ((ALACSpecificConfig *)theActualCookie)->compatibleVersion;
  110. theConfig.bitDepth = ((ALACSpecificConfig *)theActualCookie)->bitDepth;
  111. theConfig.pb = ((ALACSpecificConfig *)theActualCookie)->pb;
  112. theConfig.mb = ((ALACSpecificConfig *)theActualCookie)->mb;
  113. theConfig.kb = ((ALACSpecificConfig *)theActualCookie)->kb;
  114. theConfig.numChannels = ((ALACSpecificConfig *)theActualCookie)->numChannels;
  115. theConfig.maxRun = Swap16BtoN(((ALACSpecificConfig *)theActualCookie)->maxRun);
  116. theConfig.maxFrameBytes = Swap32BtoN(((ALACSpecificConfig *)theActualCookie)->maxFrameBytes);
  117. theConfig.avgBitRate = Swap32BtoN(((ALACSpecificConfig *)theActualCookie)->avgBitRate);
  118. theConfig.sampleRate = Swap32BtoN(((ALACSpecificConfig *)theActualCookie)->sampleRate);
  119. mConfig = theConfig;
  120. RequireAction( mConfig.compatibleVersion <= kALACVersion, return kALAC_ParamError; );
  121. // allocate mix buffers
  122. mMixBufferU = (int32_t *) calloc( mConfig.frameLength * sizeof(int32_t), 1 );
  123. mMixBufferV = (int32_t *) calloc( mConfig.frameLength * sizeof(int32_t), 1 );
  124. // allocate dynamic predictor buffer
  125. mPredictor = (int32_t *) calloc( mConfig.frameLength * sizeof(int32_t), 1 );
  126. // "shift off" buffer shares memory with predictor buffer
  127. mShiftBuffer = (uint16_t *) mPredictor;
  128. RequireAction( (mMixBufferU != nil) && (mMixBufferV != nil) && (mPredictor != nil),
  129. status = kALAC_MemFullError; goto Exit; );
  130. }
  131. else
  132. {
  133. status = kALAC_ParamError;
  134. }
  135. // skip to Channel Layout Info
  136. // theActualCookie += sizeof(ALACSpecificConfig);
  137. // Currently, the Channel Layout Info portion of the magic cookie (as defined in the
  138. // ALACMagicCookieDescription.txt document) is unused by the decoder.
  139. Exit:
  140. return status;
  141. }
  142. /*
  143. Decode()
  144. - the decoded samples are interleaved into the output buffer in the order they arrive in
  145. the bitstream
  146. */
  147. int32_t ALACDecoder::Decode( BitBuffer * bits, uint8_t * sampleBuffer, uint32_t numSamples, uint32_t numChannels, uint32_t * outNumSamples )
  148. {
  149. BitBuffer shiftBits;
  150. uint32_t bits1, bits2;
  151. uint8_t tag;
  152. uint8_t elementInstanceTag;
  153. AGParamRec agParams;
  154. uint32_t channelIndex;
  155. int16_t coefsU[32]; // max possible size is 32 although NUMCOEPAIRS is the current limit
  156. int16_t coefsV[32];
  157. uint8_t numU, numV;
  158. uint8_t mixBits;
  159. int8_t mixRes;
  160. uint16_t unusedHeader;
  161. uint8_t escapeFlag;
  162. uint32_t chanBits;
  163. uint8_t bytesShifted;
  164. uint32_t shift;
  165. uint8_t modeU, modeV;
  166. uint32_t denShiftU, denShiftV;
  167. uint16_t pbFactorU, pbFactorV;
  168. uint16_t pb;
  169. int16_t * samples;
  170. int16_t * out16;
  171. uint8_t * out20;
  172. uint8_t * out24;
  173. int32_t * out32;
  174. uint8_t headerByte;
  175. uint8_t partialFrame;
  176. uint32_t extraBits;
  177. int32_t val;
  178. uint32_t i, j;
  179. int32_t status;
  180. RequireAction( (bits != nil) && (sampleBuffer != nil) && (outNumSamples != nil), return kALAC_ParamError; );
  181. RequireAction( numChannels > 0, return kALAC_ParamError; );
  182. mActiveElements = 0;
  183. channelIndex = 0;
  184. samples = (int16_t *) sampleBuffer;
  185. status = ALAC_noErr;
  186. *outNumSamples = numSamples;
  187. while ( status == ALAC_noErr )
  188. {
  189. // bail if we ran off the end of the buffer
  190. RequireAction( bits->cur < bits->end, status = kALAC_ParamError; goto Exit; );
  191. // copy global decode params for this element
  192. pb = mConfig.pb;
  193. // read element tag
  194. tag = BitBufferReadSmall( bits, 3 );
  195. switch ( tag )
  196. {
  197. case ID_SCE:
  198. case ID_LFE:
  199. {
  200. // mono/LFE channel
  201. elementInstanceTag = BitBufferReadSmall( bits, 4 );
  202. mActiveElements |= (1u << elementInstanceTag);
  203. // read the 12 unused header bits
  204. unusedHeader = (uint16_t) BitBufferRead( bits, 12 );
  205. RequireAction( unusedHeader == 0, status = kALAC_ParamError; goto Exit; );
  206. // read the 1-bit "partial frame" flag, 2-bit "shift-off" flag & 1-bit "escape" flag
  207. headerByte = (uint8_t) BitBufferRead( bits, 4 );
  208. partialFrame = headerByte >> 3;
  209. bytesShifted = (headerByte >> 1) & 0x3u;
  210. RequireAction( bytesShifted != 3, status = kALAC_ParamError; goto Exit; );
  211. shift = bytesShifted * 8;
  212. escapeFlag = headerByte & 0x1;
  213. chanBits = mConfig.bitDepth - (bytesShifted * 8);
  214. // check for partial frame to override requested numSamples
  215. if ( partialFrame != 0 )
  216. {
  217. numSamples = BitBufferRead( bits, 16 ) << 16;
  218. numSamples |= BitBufferRead( bits, 16 );
  219. }
  220. if ( escapeFlag == 0 )
  221. {
  222. // compressed frame, read rest of parameters
  223. mixBits = (uint8_t) BitBufferRead( bits, 8 );
  224. mixRes = (int8_t) BitBufferRead( bits, 8 );
  225. //Assert( (mixBits == 0) && (mixRes == 0) ); // no mixing for mono
  226. headerByte = (uint8_t) BitBufferRead( bits, 8 );
  227. modeU = headerByte >> 4;
  228. denShiftU = headerByte & 0xfu;
  229. headerByte = (uint8_t) BitBufferRead( bits, 8 );
  230. pbFactorU = headerByte >> 5;
  231. numU = headerByte & 0x1fu;
  232. for ( i = 0; i < numU; i++ )
  233. coefsU[i] = (int16_t) BitBufferRead( bits, 16 );
  234. // if shift active, skip the the shift buffer but remember where it starts
  235. if ( bytesShifted != 0 )
  236. {
  237. shiftBits = *bits;
  238. BitBufferAdvance( bits, (bytesShifted * 8) * numSamples );
  239. }
  240. // decompress
  241. set_ag_params( &agParams, mConfig.mb, (pb * pbFactorU) / 4, mConfig.kb, numSamples, numSamples, mConfig.maxRun );
  242. status = dyn_decomp( &agParams, bits, mPredictor, numSamples, chanBits, &bits1 );
  243. RequireNoErr( status, goto Exit; );
  244. if ( modeU == 0 )
  245. {
  246. unpc_block( mPredictor, mMixBufferU, numSamples, &coefsU[0], numU, chanBits, denShiftU );
  247. }
  248. else
  249. {
  250. // the special "numActive == 31" mode can be done in-place
  251. unpc_block( mPredictor, mPredictor, numSamples, nil, 31, chanBits, 0 );
  252. unpc_block( mPredictor, mMixBufferU, numSamples, &coefsU[0], numU, chanBits, denShiftU );
  253. }
  254. }
  255. else
  256. {
  257. //Assert( bytesShifted == 0 );
  258. // uncompressed frame, copy data into the mix buffer to use common output code
  259. shift = 32 - chanBits;
  260. if ( chanBits <= 16 )
  261. {
  262. for ( i = 0; i < numSamples; i++ )
  263. {
  264. val = (int32_t) BitBufferRead( bits, (uint8_t) chanBits );
  265. val = (val << shift) >> shift;
  266. mMixBufferU[i] = val;
  267. }
  268. }
  269. else
  270. {
  271. // BitBufferRead() can't read more than 16 bits at a time so break up the reads
  272. extraBits = chanBits - 16;
  273. for ( i = 0; i < numSamples; i++ )
  274. {
  275. val = (int32_t) BitBufferRead( bits, 16 );
  276. val = (val << 16) >> shift;
  277. mMixBufferU[i] = val | BitBufferRead( bits, (uint8_t) extraBits );
  278. }
  279. }
  280. mixBits = mixRes = 0;
  281. bits1 = chanBits * numSamples;
  282. bytesShifted = 0;
  283. }
  284. // now read the shifted values into the shift buffer
  285. if ( bytesShifted != 0 )
  286. {
  287. shift = bytesShifted * 8;
  288. //Assert( shift <= 16 );
  289. for ( i = 0; i < numSamples; i++ )
  290. mShiftBuffer[i] = (uint16_t) BitBufferRead( &shiftBits, (uint8_t) shift );
  291. }
  292. // convert 32-bit integers into output buffer
  293. switch ( mConfig.bitDepth )
  294. {
  295. case 16:
  296. out16 = &((int16_t *)sampleBuffer)[channelIndex];
  297. for ( i = 0, j = 0; i < numSamples; i++, j += numChannels )
  298. out16[j] = (int16_t) mMixBufferU[i];
  299. break;
  300. case 20:
  301. out20 = (uint8_t *)sampleBuffer + (channelIndex * 3);
  302. copyPredictorTo20( mMixBufferU, out20, numChannels, numSamples );
  303. break;
  304. case 24:
  305. out24 = (uint8_t *)sampleBuffer + (channelIndex * 3);
  306. if ( bytesShifted != 0 )
  307. copyPredictorTo24Shift( mMixBufferU, mShiftBuffer, out24, numChannels, numSamples, bytesShifted );
  308. else
  309. copyPredictorTo24( mMixBufferU, out24, numChannels, numSamples );
  310. break;
  311. case 32:
  312. out32 = &((int32_t *)sampleBuffer)[channelIndex];
  313. if ( bytesShifted != 0 )
  314. copyPredictorTo32Shift( mMixBufferU, mShiftBuffer, out32, numChannels, numSamples, bytesShifted );
  315. else
  316. copyPredictorTo32( mMixBufferU, out32, numChannels, numSamples);
  317. break;
  318. }
  319. channelIndex += 1;
  320. *outNumSamples = numSamples;
  321. break;
  322. }
  323. case ID_CPE:
  324. {
  325. // if decoding this pair would take us over the max channels limit, bail
  326. if ( (channelIndex + 2) > numChannels )
  327. goto NoMoreChannels;
  328. // stereo channel pair
  329. elementInstanceTag = BitBufferReadSmall( bits, 4 );
  330. mActiveElements |= (1u << elementInstanceTag);
  331. // read the 12 unused header bits
  332. unusedHeader = (uint16_t) BitBufferRead( bits, 12 );
  333. RequireAction( unusedHeader == 0, status = kALAC_ParamError; goto Exit; );
  334. // read the 1-bit "partial frame" flag, 2-bit "shift-off" flag & 1-bit "escape" flag
  335. headerByte = (uint8_t) BitBufferRead( bits, 4 );
  336. partialFrame = headerByte >> 3;
  337. bytesShifted = (headerByte >> 1) & 0x3u;
  338. RequireAction( bytesShifted != 3, status = kALAC_ParamError; goto Exit; );
  339. shift = bytesShifted * 8;
  340. escapeFlag = headerByte & 0x1;
  341. chanBits = mConfig.bitDepth - (bytesShifted * 8) + 1;
  342. // check for partial frame length to override requested numSamples
  343. if ( partialFrame != 0 )
  344. {
  345. numSamples = BitBufferRead( bits, 16 ) << 16;
  346. numSamples |= BitBufferRead( bits, 16 );
  347. }
  348. if ( escapeFlag == 0 )
  349. {
  350. // compressed frame, read rest of parameters
  351. mixBits = (uint8_t) BitBufferRead( bits, 8 );
  352. mixRes = (int8_t) BitBufferRead( bits, 8 );
  353. headerByte = (uint8_t) BitBufferRead( bits, 8 );
  354. modeU = headerByte >> 4;
  355. denShiftU = headerByte & 0xfu;
  356. headerByte = (uint8_t) BitBufferRead( bits, 8 );
  357. pbFactorU = headerByte >> 5;
  358. numU = headerByte & 0x1fu;
  359. for ( i = 0; i < numU; i++ )
  360. coefsU[i] = (int16_t) BitBufferRead( bits, 16 );
  361. headerByte = (uint8_t) BitBufferRead( bits, 8 );
  362. modeV = headerByte >> 4;
  363. denShiftV = headerByte & 0xfu;
  364. headerByte = (uint8_t) BitBufferRead( bits, 8 );
  365. pbFactorV = headerByte >> 5;
  366. numV = headerByte & 0x1fu;
  367. for ( i = 0; i < numV; i++ )
  368. coefsV[i] = (int16_t) BitBufferRead( bits, 16 );
  369. // if shift active, skip the interleaved shifted values but remember where they start
  370. if ( bytesShifted != 0 )
  371. {
  372. shiftBits = *bits;
  373. BitBufferAdvance( bits, (bytesShifted * 8) * 2 * numSamples );
  374. }
  375. // decompress and run predictor for "left" channel
  376. set_ag_params( &agParams, mConfig.mb, (pb * pbFactorU) / 4, mConfig.kb, numSamples, numSamples, mConfig.maxRun );
  377. status = dyn_decomp( &agParams, bits, mPredictor, numSamples, chanBits, &bits1 );
  378. RequireNoErr( status, goto Exit; );
  379. if ( modeU == 0 )
  380. {
  381. unpc_block( mPredictor, mMixBufferU, numSamples, &coefsU[0], numU, chanBits, denShiftU );
  382. }
  383. else
  384. {
  385. // the special "numActive == 31" mode can be done in-place
  386. unpc_block( mPredictor, mPredictor, numSamples, nil, 31, chanBits, 0 );
  387. unpc_block( mPredictor, mMixBufferU, numSamples, &coefsU[0], numU, chanBits, denShiftU );
  388. }
  389. // decompress and run predictor for "right" channel
  390. set_ag_params( &agParams, mConfig.mb, (pb * pbFactorV) / 4, mConfig.kb, numSamples, numSamples, mConfig.maxRun );
  391. status = dyn_decomp( &agParams, bits, mPredictor, numSamples, chanBits, &bits2 );
  392. RequireNoErr( status, goto Exit; );
  393. if ( modeV == 0 )
  394. {
  395. unpc_block( mPredictor, mMixBufferV, numSamples, &coefsV[0], numV, chanBits, denShiftV );
  396. }
  397. else
  398. {
  399. // the special "numActive == 31" mode can be done in-place
  400. unpc_block( mPredictor, mPredictor, numSamples, nil, 31, chanBits, 0 );
  401. unpc_block( mPredictor, mMixBufferV, numSamples, &coefsV[0], numV, chanBits, denShiftV );
  402. }
  403. }
  404. else
  405. {
  406. //Assert( bytesShifted == 0 );
  407. // uncompressed frame, copy data into the mix buffers to use common output code
  408. chanBits = mConfig.bitDepth;
  409. shift = 32 - chanBits;
  410. if ( chanBits <= 16 )
  411. {
  412. for ( i = 0; i < numSamples; i++ )
  413. {
  414. val = (int32_t) BitBufferRead( bits, (uint8_t) chanBits );
  415. val = (val << shift) >> shift;
  416. mMixBufferU[i] = val;
  417. val = (int32_t) BitBufferRead( bits, (uint8_t) chanBits );
  418. val = (val << shift) >> shift;
  419. mMixBufferV[i] = val;
  420. }
  421. }
  422. else
  423. {
  424. // BitBufferRead() can't read more than 16 bits at a time so break up the reads
  425. extraBits = chanBits - 16;
  426. for ( i = 0; i < numSamples; i++ )
  427. {
  428. val = (int32_t) BitBufferRead( bits, 16 );
  429. val = (val << 16) >> shift;
  430. mMixBufferU[i] = val | BitBufferRead( bits, (uint8_t)extraBits );
  431. val = (int32_t) BitBufferRead( bits, 16 );
  432. val = (val << 16) >> shift;
  433. mMixBufferV[i] = val | BitBufferRead( bits, (uint8_t)extraBits );
  434. }
  435. }
  436. bits1 = chanBits * numSamples;
  437. bits2 = chanBits * numSamples;
  438. mixBits = mixRes = 0;
  439. bytesShifted = 0;
  440. }
  441. // now read the shifted values into the shift buffer
  442. if ( bytesShifted != 0 )
  443. {
  444. shift = bytesShifted * 8;
  445. //Assert( shift <= 16 );
  446. for ( i = 0; i < (numSamples * 2); i += 2 )
  447. {
  448. mShiftBuffer[i + 0] = (uint16_t) BitBufferRead( &shiftBits, (uint8_t) shift );
  449. mShiftBuffer[i + 1] = (uint16_t) BitBufferRead( &shiftBits, (uint8_t) shift );
  450. }
  451. }
  452. // un-mix the data and convert to output format
  453. // - note that mixRes = 0 means just interleave so we use that path for uncompressed frames
  454. switch ( mConfig.bitDepth )
  455. {
  456. case 16:
  457. out16 = &((int16_t *)sampleBuffer)[channelIndex];
  458. unmix16( mMixBufferU, mMixBufferV, out16, numChannels, numSamples, mixBits, mixRes );
  459. break;
  460. case 20:
  461. out20 = (uint8_t *)sampleBuffer + (channelIndex * 3);
  462. unmix20( mMixBufferU, mMixBufferV, out20, numChannels, numSamples, mixBits, mixRes );
  463. break;
  464. case 24:
  465. out24 = (uint8_t *)sampleBuffer + (channelIndex * 3);
  466. unmix24( mMixBufferU, mMixBufferV, out24, numChannels, numSamples,
  467. mixBits, mixRes, mShiftBuffer, bytesShifted );
  468. break;
  469. case 32:
  470. out32 = &((int32_t *)sampleBuffer)[channelIndex];
  471. unmix32( mMixBufferU, mMixBufferV, out32, numChannels, numSamples,
  472. mixBits, mixRes, mShiftBuffer, bytesShifted );
  473. break;
  474. }
  475. channelIndex += 2;
  476. *outNumSamples = numSamples;
  477. break;
  478. }
  479. case ID_CCE:
  480. case ID_PCE:
  481. {
  482. // unsupported element, bail
  483. //AssertNoErr( tag );
  484. status = kALAC_ParamError;
  485. break;
  486. }
  487. case ID_DSE:
  488. {
  489. // data stream element -- parse but ignore
  490. status = this->DataStreamElement( bits );
  491. break;
  492. }
  493. case ID_FIL:
  494. {
  495. // fill element -- parse but ignore
  496. status = this->FillElement( bits );
  497. break;
  498. }
  499. case ID_END:
  500. {
  501. // frame end, all done so byte align the frame and check for overruns
  502. BitBufferByteAlign( bits, false );
  503. //Assert( bits->cur == bits->end );
  504. goto Exit;
  505. }
  506. }
  507. #if ! DEBUG
  508. // 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)
  509. // - this also protects us if the config does not match the bitstream or crap data bits follow the audio bits
  510. if ( channelIndex >= numChannels )
  511. break;
  512. #endif
  513. }
  514. NoMoreChannels:
  515. // if we get here and haven't decoded all of the requested channels, fill the remaining channels with zeros
  516. for ( ; channelIndex < numChannels; channelIndex++ )
  517. {
  518. switch ( mConfig.bitDepth )
  519. {
  520. case 16:
  521. {
  522. int16_t * fill16 = &((int16_t *)sampleBuffer)[channelIndex];
  523. Zero16( fill16, numSamples, numChannels );
  524. break;
  525. }
  526. case 24:
  527. {
  528. uint8_t * fill24 = (uint8_t *)sampleBuffer + (channelIndex * 3);
  529. Zero24( fill24, numSamples, numChannels );
  530. break;
  531. }
  532. case 32:
  533. {
  534. int32_t * fill32 = &((int32_t *)sampleBuffer)[channelIndex];
  535. Zero32( fill32, numSamples, numChannels );
  536. break;
  537. }
  538. }
  539. }
  540. Exit:
  541. return status;
  542. }
  543. #if PRAGMA_MARK
  544. #pragma mark -
  545. #endif
  546. /*
  547. FillElement()
  548. - they're just filler so we don't need 'em
  549. */
  550. int32_t ALACDecoder::FillElement( BitBuffer * bits )
  551. {
  552. int16_t count;
  553. // 4-bit count or (4-bit + 8-bit count) if 4-bit count == 15
  554. // - plus this weird -1 thing I still don't fully understand
  555. count = BitBufferReadSmall( bits, 4 );
  556. if ( count == 15 )
  557. count += (int16_t) BitBufferReadSmall( bits, 8 ) - 1;
  558. BitBufferAdvance( bits, count * 8 );
  559. RequireAction( bits->cur <= bits->end, return kALAC_ParamError; );
  560. return ALAC_noErr;
  561. }
  562. /*
  563. DataStreamElement()
  564. - we don't care about data stream elements so just skip them
  565. */
  566. int32_t ALACDecoder::DataStreamElement( BitBuffer * bits )
  567. {
  568. uint8_t element_instance_tag;
  569. int32_t data_byte_align_flag;
  570. uint16_t count;
  571. // the tag associates this data stream element with a given audio element
  572. element_instance_tag = BitBufferReadSmall( bits, 4 );
  573. data_byte_align_flag = BitBufferReadOne( bits );
  574. // 8-bit count or (8-bit + 8-bit count) if 8-bit count == 255
  575. count = BitBufferReadSmall( bits, 8 );
  576. if ( count == 255 )
  577. count += BitBufferReadSmall( bits, 8 );
  578. // the align flag means the bitstream should be byte-aligned before reading the following data bytes
  579. if ( data_byte_align_flag )
  580. BitBufferByteAlign( bits, false );
  581. // skip the data bytes
  582. BitBufferAdvance( bits, count * 8 );
  583. RequireAction( bits->cur <= bits->end, return kALAC_ParamError; );
  584. return ALAC_noErr;
  585. }
  586. /*
  587. ZeroN()
  588. - helper routines to clear out output channel buffers when decoding fewer channels than requested
  589. */
  590. static void Zero16( int16_t * buffer, uint32_t numItems, uint32_t stride )
  591. {
  592. if ( stride == 1 )
  593. {
  594. memset( buffer, 0, numItems * sizeof(int16_t) );
  595. }
  596. else
  597. {
  598. for ( uint32_t index = 0; index < (numItems * stride); index += stride )
  599. buffer[index] = 0;
  600. }
  601. }
  602. static void Zero24( uint8_t * buffer, uint32_t numItems, uint32_t stride )
  603. {
  604. if ( stride == 1 )
  605. {
  606. memset( buffer, 0, numItems * 3 );
  607. }
  608. else
  609. {
  610. for ( uint32_t index = 0; index < (numItems * stride * 3); index += (stride * 3) )
  611. {
  612. buffer[index + 0] = 0;
  613. buffer[index + 1] = 0;
  614. buffer[index + 2] = 0;
  615. }
  616. }
  617. }
  618. static void Zero32( int32_t * buffer, uint32_t numItems, uint32_t stride )
  619. {
  620. if ( stride == 1 )
  621. {
  622. memset( buffer, 0, numItems * sizeof(int32_t) );
  623. }
  624. else
  625. {
  626. for ( uint32_t index = 0; index < (numItems * stride); index += stride )
  627. buffer[index] = 0;
  628. }
  629. }