ALACEncoder.cpp 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432
  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: ALACEncoder.cpp
  22. */
  23. // build stuff
  24. #define VERBOSE_DEBUG 0
  25. // headers
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include "ALACEncoder.h"
  30. #include "aglib.h"
  31. #include "dplib.h"
  32. #include "matrixlib.h"
  33. #include "ALACBitUtilities.h"
  34. #include "ALACAudioTypes.h"
  35. #include "EndianPortable.h"
  36. #if (__GNUC__) > 4 || defined (__APPLE__)
  37. #pragma GCC diagnostic ignored "-Wunused-const-variable"
  38. #endif
  39. #if !defined(__APPLE__)
  40. #pragma GCC diagnostic ignored "-Wunused-but-set-variable"
  41. #endif
  42. // Note: in C you can't typecast to a 2-dimensional array pointer but that's what we need when
  43. // picking which coefs to use so we declare this typedef b/c we *can* typecast to this type
  44. typedef int16_t (*SearchCoefs)[kALACMaxCoefs];
  45. // defines/constants
  46. const uint32_t kALACEncoderMagic = 'dpge';
  47. const uint32_t kMaxSampleSize = 32; // max allowed bit width is 32
  48. const uint32_t kDefaultMixBits = 2;
  49. const uint32_t kDefaultMixRes = 0;
  50. const uint32_t kMaxRes = 4;
  51. const uint32_t kDefaultNumUV = 8;
  52. const uint32_t kMinUV = 4;
  53. const uint32_t kMaxUV = 8;
  54. // static functions
  55. #if VERBOSE_DEBUG
  56. static void AddFiller( BitBuffer * bits, int32_t numBytes );
  57. #endif
  58. /*
  59. Map Format: 3-bit field per channel which is the same as the "element tag" that should be placed
  60. at the beginning of the frame for that channel. Indicates whether SCE, CPE, or LFE.
  61. Each particular field is accessed via the current channel index. Note that the channel
  62. index increments by two for channel pairs.
  63. For example:
  64. C L R 3-channel input = (ID_CPE << 3) | (ID_SCE)
  65. index 0 value = (map & (0x7ul << (0 * 3))) >> (0 * 3)
  66. index 1 value = (map & (0x7ul << (1 * 3))) >> (1 * 3)
  67. C L R Ls Rs LFE 5.1-channel input = (ID_LFE << 15) | (ID_CPE << 9) | (ID_CPE << 3) | (ID_SCE)
  68. index 0 value = (map & (0x7ul << (0 * 3))) >> (0 * 3)
  69. index 1 value = (map & (0x7ul << (1 * 3))) >> (1 * 3)
  70. index 3 value = (map & (0x7ul << (3 * 3))) >> (3 * 3)
  71. index 5 value = (map & (0x7ul << (5 * 3))) >> (5 * 3)
  72. index 7 value = (map & (0x7ul << (7 * 3))) >> (7 * 3)
  73. */
  74. static const uint32_t sChannelMaps[kALACMaxChannels] =
  75. {
  76. ID_SCE,
  77. ID_CPE,
  78. (ID_CPE << 3) | (ID_SCE),
  79. (ID_SCE << 9) | (ID_CPE << 3) | (ID_SCE),
  80. (ID_CPE << 9) | (ID_CPE << 3) | (ID_SCE),
  81. (ID_SCE << 15) | (ID_CPE << 9) | (ID_CPE << 3) | (ID_SCE),
  82. (ID_SCE << 18) | (ID_SCE << 15) | (ID_CPE << 9) | (ID_CPE << 3) | (ID_SCE),
  83. (ID_SCE << 21) | (ID_CPE << 15) | (ID_CPE << 9) | (ID_CPE << 3) | (ID_SCE)
  84. };
  85. static const uint32_t sSupportediPodSampleRates[] =
  86. {
  87. 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000
  88. };
  89. /*
  90. Constructor
  91. */
  92. ALACEncoder::ALACEncoder() :
  93. mBitDepth( 0 ),
  94. mFastMode( 0 ),
  95. mMixBufferU( nil ),
  96. mMixBufferV( nil ),
  97. mPredictorU( nil ),
  98. mPredictorV( nil ),
  99. mShiftBufferUV( nil ),
  100. mWorkBuffer( nil ),
  101. mTotalBytesGenerated( 0 ),
  102. mAvgBitRate( 0 ),
  103. mMaxFrameBytes( 0 )
  104. {
  105. // overrides
  106. mFrameSize = kALACDefaultFrameSize;
  107. }
  108. /*
  109. Destructor
  110. */
  111. ALACEncoder::~ALACEncoder()
  112. {
  113. // delete the matrix mixing buffers
  114. if ( mMixBufferU )
  115. {
  116. free(mMixBufferU);
  117. mMixBufferU = NULL;
  118. }
  119. if ( mMixBufferV )
  120. {
  121. free(mMixBufferV);
  122. mMixBufferV = NULL;
  123. }
  124. // delete the dynamic predictor's "corrector" buffers
  125. if ( mPredictorU )
  126. {
  127. free(mPredictorU);
  128. mPredictorU = NULL;
  129. }
  130. if ( mPredictorV )
  131. {
  132. free(mPredictorV);
  133. mPredictorV = NULL;
  134. }
  135. // delete the unused byte shift buffer
  136. if ( mShiftBufferUV )
  137. {
  138. free(mShiftBufferUV);
  139. mShiftBufferUV = NULL;
  140. }
  141. // delete the work buffer
  142. if ( mWorkBuffer )
  143. {
  144. free(mWorkBuffer);
  145. mWorkBuffer = NULL;
  146. }
  147. }
  148. #if PRAGMA_MARK
  149. #pragma mark -
  150. #endif
  151. /*
  152. HEADER SPECIFICATION
  153. For every segment we adopt the following header:
  154. 1 byte reserved (always 0)
  155. 1 byte flags (see below)
  156. [4 byte frame length] (optional, see below)
  157. ---Next, the per-segment ALAC parameters---
  158. 1 byte mixBits (middle-side parameter)
  159. 1 byte mixRes (middle-side parameter, interpreted as signed char)
  160. 1 byte shiftU (4 bits modeU, 4 bits denShiftU)
  161. 1 byte filterU (3 bits pbFactorU, 5 bits numU)
  162. (numU) shorts (signed DP coefficients for V channel)
  163. ---Next, 2nd-channel ALAC parameters in case of stereo mode---
  164. 1 byte shiftV (4 bits modeV, 4 bits denShiftV)
  165. 1 byte filterV (3 bits pbFactorV, 5 bits numV)
  166. (numV) shorts (signed DP coefficients for V channel)
  167. ---After this come the shift-off bytes for (>= 24)-bit data (n-byte shift) if indicated---
  168. ---Then comes the AG-compressor bitstream---
  169. FLAGS
  170. -----
  171. The presence of certain flag bits changes the header format such that the parameters might
  172. not even be sent. The currently defined flags format is:
  173. 0000psse
  174. where 0 = reserved, must be 0
  175. p = 1-bit field "partial frame" flag indicating 32-bit frame length follows this byte
  176. ss = 2-bit field indicating "number of shift-off bytes ignored by compression"
  177. e = 1-bit field indicating "escape"
  178. The "partial frame" flag means that the following segment is not equal to the frame length specified
  179. in the out-of-band decoder configuration. This allows the decoder to deal with end-of-file partial
  180. segments without incurring the 32-bit overhead for each segment.
  181. The "shift-off" field indicates the number of bytes at the bottom of the word that were passed through
  182. uncompressed. The reason for this is that the entropy inherent in the LS bytes of >= 24-bit words
  183. quite often means that the frame would have to be "escaped" b/c the compressed size would be >= the
  184. uncompressed size. However, by shifting the input values down and running the remaining bits through
  185. the normal compression algorithm, a net win can be achieved. If this field is non-zero, it means that
  186. the shifted-off bytes follow after the parameter section of the header and before the compressed
  187. bitstream. Note that doing this also allows us to use matrixing on 32-bit inputs after one or more
  188. bytes are shifted off the bottom which helps the eventual compression ratio. For stereo channels,
  189. the shifted off bytes are interleaved.
  190. The "escape" flag means that this segment was not compressed b/c the compressed size would be
  191. >= uncompressed size. In that case, the audio data was passed through uncompressed after the header.
  192. The other header parameter bytes will not be sent.
  193. PARAMETERS
  194. ----------
  195. If the segment is not a partial or escape segment, the total header size (in bytes) is given exactly by:
  196. 4 + (2 + 2 * numU) (mono mode)
  197. 4 + (2 + 2 * numV) + (2 + 2 * numV) (stereo mode)
  198. where the ALAC filter-lengths numU, numV are bounded by a
  199. constant (in the current source, numU, numV <= NUMCOEPAIRS), and
  200. this forces an absolute upper bound on header size.
  201. Each segment-decode process loads up these bytes from the front of the
  202. local stream, in the above order, then follows with the entropy-encoded
  203. bits for the given segment.
  204. To generalize middle-side, there are various mixing modes including middle-side, each lossless,
  205. as embodied in the mix() and unmix() functions. These functions exploit a generalized middle-side
  206. transformation:
  207. u := [(rL + (m-r)R)/m];
  208. v := L - R;
  209. where [ ] denotes integer floor. The (lossless) inverse is
  210. L = u + v - [rV/m];
  211. R = L - v;
  212. In the segment header, m and r are encoded in mixBits and mixRes.
  213. Classical "middle-side" is obtained with m = 2, r = 1, but now
  214. we have more generalized mixes.
  215. NOTES
  216. -----
  217. The relevance of the ALAC coefficients is explained in detail
  218. in patent documents.
  219. */
  220. /*
  221. EncodeStereo()
  222. - encode a channel pair
  223. */
  224. int32_t ALACEncoder::EncodeStereo( BitBuffer * bitstream, void * inputBuffer, uint32_t stride, uint32_t channelIndex, uint32_t numSamples )
  225. {
  226. BitBuffer workBits;
  227. BitBuffer startBits = *bitstream; // squirrel away copy of current state in case we need to go back and do an escape packet
  228. AGParamRec agParams;
  229. uint32_t bits1, bits2;
  230. uint32_t dilate;
  231. int32_t mixBits, mixRes, maxRes;
  232. uint32_t minBits, minBits1, minBits2;
  233. uint32_t numU, numV;
  234. uint32_t mode;
  235. uint32_t pbFactor;
  236. uint32_t chanBits;
  237. uint32_t denShift;
  238. uint8_t bytesShifted;
  239. SearchCoefs coefsU;
  240. SearchCoefs coefsV;
  241. uint32_t index;
  242. uint8_t partialFrame;
  243. uint32_t escapeBits;
  244. bool doEscape;
  245. int32_t status = ALAC_noErr;
  246. // make sure we handle this bit-depth before we get going
  247. RequireAction( (mBitDepth == 16) || (mBitDepth == 20) || (mBitDepth == 24) || (mBitDepth == 32), return kALAC_ParamError; );
  248. // reload coefs pointers for this channel pair
  249. // - note that, while you might think they should be re-initialized per block, retaining state across blocks
  250. // actually results in better overall compression
  251. // - strangely, re-using the same coefs for the different passes of the "mixRes" search loop instead of using
  252. // different coefs for the different passes of "mixRes" results in even better compression
  253. coefsU = (SearchCoefs) mCoefsU[channelIndex];
  254. coefsV = (SearchCoefs) mCoefsV[channelIndex];
  255. // matrix encoding adds an extra bit but 32-bit inputs cannot be matrixed b/c 33 is too many
  256. // so enable 16-bit "shift off" and encode in 17-bit mode
  257. // - in addition, 24-bit mode really improves with one byte shifted off
  258. if ( mBitDepth == 32 )
  259. bytesShifted = 2;
  260. else if ( mBitDepth >= 24 )
  261. bytesShifted = 1;
  262. else
  263. bytesShifted = 0;
  264. chanBits = mBitDepth - (bytesShifted * 8) + 1;
  265. // flag whether or not this is a partial frame
  266. partialFrame = (numSamples == mFrameSize) ? 0 : 1;
  267. // brute-force encode optimization loop
  268. // - run over variations of the encoding params to find the best choice
  269. mixBits = kDefaultMixBits;
  270. maxRes = kMaxRes;
  271. numU = numV = kDefaultNumUV;
  272. denShift = DENSHIFT_DEFAULT;
  273. mode = 0;
  274. pbFactor = 4;
  275. dilate = 8;
  276. minBits = minBits1 = minBits2 = 1ul << 31;
  277. int32_t bestRes = mLastMixRes[channelIndex];
  278. for ( mixRes = 0; mixRes <= maxRes; mixRes++ )
  279. {
  280. // mix the stereo inputs
  281. switch ( mBitDepth )
  282. {
  283. case 16:
  284. mix16( (int16_t *) inputBuffer, stride, mMixBufferU, mMixBufferV, numSamples/dilate, mixBits, mixRes );
  285. break;
  286. case 20:
  287. mix20( (uint8_t *) inputBuffer, stride, mMixBufferU, mMixBufferV, numSamples/dilate, mixBits, mixRes );
  288. break;
  289. case 24:
  290. // includes extraction of shifted-off bytes
  291. mix24( (uint8_t *) inputBuffer, stride, mMixBufferU, mMixBufferV, numSamples/dilate,
  292. mixBits, mixRes, mShiftBufferUV, bytesShifted );
  293. break;
  294. case 32:
  295. // includes extraction of shifted-off bytes
  296. mix32( (int32_t *) inputBuffer, stride, mMixBufferU, mMixBufferV, numSamples/dilate,
  297. mixBits, mixRes, mShiftBufferUV, bytesShifted );
  298. break;
  299. }
  300. BitBufferInit( &workBits, mWorkBuffer, mMaxOutputBytes );
  301. // run the dynamic predictors
  302. pc_block( mMixBufferU, mPredictorU, numSamples/dilate, coefsU[numU - 1], numU, chanBits, DENSHIFT_DEFAULT );
  303. pc_block( mMixBufferV, mPredictorV, numSamples/dilate, coefsV[numV - 1], numV, chanBits, DENSHIFT_DEFAULT );
  304. // run the lossless compressor on each channel
  305. set_ag_params( &agParams, MB0, (pbFactor * PB0) / 4, KB0, numSamples/dilate, numSamples/dilate, MAX_RUN_DEFAULT );
  306. status = dyn_comp( &agParams, mPredictorU, &workBits, numSamples/dilate, chanBits, &bits1 );
  307. RequireNoErr( status, goto Exit; );
  308. set_ag_params( &agParams, MB0, (pbFactor * PB0) / 4, KB0, numSamples/dilate, numSamples/dilate, MAX_RUN_DEFAULT );
  309. status = dyn_comp( &agParams, mPredictorV, &workBits, numSamples/dilate, chanBits, &bits2 );
  310. RequireNoErr( status, goto Exit; );
  311. // look for best match
  312. if ( (bits1 + bits2) < minBits1 )
  313. {
  314. minBits1 = bits1 + bits2;
  315. bestRes = mixRes;
  316. }
  317. }
  318. mLastMixRes[channelIndex] = (int16_t)bestRes;
  319. // mix the stereo inputs with the current best mixRes
  320. mixRes = mLastMixRes[channelIndex];
  321. switch ( mBitDepth )
  322. {
  323. case 16:
  324. mix16( (int16_t *) inputBuffer, stride, mMixBufferU, mMixBufferV, numSamples, mixBits, mixRes );
  325. break;
  326. case 20:
  327. mix20( (uint8_t *) inputBuffer, stride, mMixBufferU, mMixBufferV, numSamples, mixBits, mixRes );
  328. break;
  329. case 24:
  330. // also extracts the shifted off bytes into the shift buffers
  331. mix24( (uint8_t *) inputBuffer, stride, mMixBufferU, mMixBufferV, numSamples,
  332. mixBits, mixRes, mShiftBufferUV, bytesShifted );
  333. break;
  334. case 32:
  335. // also extracts the shifted off bytes into the shift buffers
  336. mix32( (int32_t *) inputBuffer, stride, mMixBufferU, mMixBufferV, numSamples,
  337. mixBits, mixRes, mShiftBufferUV, bytesShifted );
  338. break;
  339. }
  340. // now it's time for the predictor coefficient search loop
  341. numU = numV = kMinUV;
  342. minBits1 = minBits2 = 1ul << 31;
  343. for ( uint32_t numUV = kMinUV; numUV <= kMaxUV; numUV += 4 )
  344. {
  345. BitBufferInit( &workBits, mWorkBuffer, mMaxOutputBytes );
  346. dilate = 32;
  347. // run the predictor over the same data multiple times to help it converge
  348. for ( uint32_t converge = 0; converge < 8; converge++ )
  349. {
  350. pc_block( mMixBufferU, mPredictorU, numSamples/dilate, coefsU[numUV-1], numUV, chanBits, DENSHIFT_DEFAULT );
  351. pc_block( mMixBufferV, mPredictorV, numSamples/dilate, coefsV[numUV-1], numUV, chanBits, DENSHIFT_DEFAULT );
  352. }
  353. dilate = 8;
  354. set_ag_params( &agParams, MB0, (pbFactor * PB0)/4, KB0, numSamples/dilate, numSamples/dilate, MAX_RUN_DEFAULT );
  355. status = dyn_comp( &agParams, mPredictorU, &workBits, numSamples/dilate, chanBits, &bits1 );
  356. if ( (bits1 * dilate + 16 * numUV) < minBits1 )
  357. {
  358. minBits1 = bits1 * dilate + 16 * numUV;
  359. numU = numUV;
  360. }
  361. set_ag_params( &agParams, MB0, (pbFactor * PB0)/4, KB0, numSamples/dilate, numSamples/dilate, MAX_RUN_DEFAULT );
  362. status = dyn_comp( &agParams, mPredictorV, &workBits, numSamples/dilate, chanBits, &bits2 );
  363. if ( (bits2 * dilate + 16 * numUV) < minBits2 )
  364. {
  365. minBits2 = bits2 * dilate + 16 * numUV;
  366. numV = numUV;
  367. }
  368. }
  369. // test for escape hatch if best calculated compressed size turns out to be more than the input size
  370. minBits = minBits1 + minBits2 + (8 /* mixRes/maxRes/etc. */ * 8) + ((partialFrame == true) ? 32 : 0);
  371. if ( bytesShifted != 0 )
  372. minBits += (numSamples * (bytesShifted * 8) * 2);
  373. escapeBits = (numSamples * mBitDepth * 2) + ((partialFrame == true) ? 32 : 0) + (2 * 8); /* 2 common header bytes */
  374. doEscape = (minBits >= escapeBits) ? true : false;
  375. if ( doEscape == false )
  376. {
  377. // write bitstream header and coefs
  378. BitBufferWrite( bitstream, 0, 12 );
  379. BitBufferWrite( bitstream, (partialFrame << 3) | (bytesShifted << 1), 4 );
  380. if ( partialFrame )
  381. BitBufferWrite( bitstream, numSamples, 32 );
  382. BitBufferWrite( bitstream, mixBits, 8 );
  383. BitBufferWrite( bitstream, mixRes, 8 );
  384. //Assert( (mode < 16) && (DENSHIFT_DEFAULT < 16) );
  385. //Assert( (pbFactor < 8) && (numU < 32) );
  386. //Assert( (pbFactor < 8) && (numV < 32) );
  387. BitBufferWrite( bitstream, (mode << 4) | DENSHIFT_DEFAULT, 8 );
  388. BitBufferWrite( bitstream, (pbFactor << 5) | numU, 8 );
  389. for ( index = 0; index < numU; index++ )
  390. BitBufferWrite( bitstream, coefsU[numU - 1][index], 16 );
  391. BitBufferWrite( bitstream, (mode << 4) | DENSHIFT_DEFAULT, 8 );
  392. BitBufferWrite( bitstream, (pbFactor << 5) | numV, 8 );
  393. for ( index = 0; index < numV; index++ )
  394. BitBufferWrite( bitstream, coefsV[numV - 1][index], 16 );
  395. // if shift active, write the interleaved shift buffers
  396. if ( bytesShifted != 0 )
  397. {
  398. uint32_t bitShift = bytesShifted * 8;
  399. //Assert( bitShift <= 16 );
  400. for ( index = 0; index < (numSamples * 2); index += 2 )
  401. {
  402. uint32_t shiftedVal;
  403. shiftedVal = ((uint32_t)mShiftBufferUV[index + 0] << bitShift) | (uint32_t)mShiftBufferUV[index + 1];
  404. BitBufferWrite( bitstream, shiftedVal, bitShift * 2 );
  405. }
  406. }
  407. // run the dynamic predictor and lossless compression for the "left" channel
  408. // - note: to avoid allocating more buffers, we're mixing and matching between the available buffers instead
  409. // of only using "U" buffers for the U-channel and "V" buffers for the V-channel
  410. if ( mode == 0 )
  411. {
  412. pc_block( mMixBufferU, mPredictorU, numSamples, coefsU[numU - 1], numU, chanBits, DENSHIFT_DEFAULT );
  413. }
  414. else
  415. {
  416. pc_block( mMixBufferU, mPredictorV, numSamples, coefsU[numU - 1], numU, chanBits, DENSHIFT_DEFAULT );
  417. pc_block( mPredictorV, mPredictorU, numSamples, nil, 31, chanBits, 0 );
  418. }
  419. set_ag_params( &agParams, MB0, (pbFactor * PB0) / 4, KB0, numSamples, numSamples, MAX_RUN_DEFAULT );
  420. status = dyn_comp( &agParams, mPredictorU, bitstream, numSamples, chanBits, &bits1 );
  421. RequireNoErr( status, goto Exit; );
  422. // run the dynamic predictor and lossless compression for the "right" channel
  423. if ( mode == 0 )
  424. {
  425. pc_block( mMixBufferV, mPredictorV, numSamples, coefsV[numV - 1], numV, chanBits, DENSHIFT_DEFAULT );
  426. }
  427. else
  428. {
  429. pc_block( mMixBufferV, mPredictorU, numSamples, coefsV[numV - 1], numV, chanBits, DENSHIFT_DEFAULT );
  430. pc_block( mPredictorU, mPredictorV, numSamples, nil, 31, chanBits, 0 );
  431. }
  432. set_ag_params( &agParams, MB0, (pbFactor * PB0) / 4, KB0, numSamples, numSamples, MAX_RUN_DEFAULT );
  433. status = dyn_comp( &agParams, mPredictorV, bitstream, numSamples, chanBits, &bits2 );
  434. RequireNoErr( status, goto Exit; );
  435. /* if we happened to create a compressed packet that was actually bigger than an escape packet would be,
  436. chuck it and do an escape packet
  437. */
  438. minBits = BitBufferGetPosition( bitstream ) - BitBufferGetPosition( &startBits );
  439. if ( minBits >= escapeBits )
  440. {
  441. *bitstream = startBits; // reset bitstream state
  442. doEscape = true;
  443. printf( "compressed frame too big: %u vs. %u \n", minBits, escapeBits );
  444. }
  445. }
  446. if ( doEscape == true )
  447. {
  448. /* escape */
  449. status = this->EncodeStereoEscape( bitstream, inputBuffer, stride, numSamples );
  450. #if VERBOSE_DEBUG
  451. DebugMsg( "escape!: %lu vs %lu", minBits, escapeBits );
  452. #endif
  453. }
  454. Exit:
  455. return status;
  456. }
  457. /*
  458. EncodeStereoFast()
  459. - encode a channel pair without the search loop for maximum possible speed
  460. */
  461. int32_t ALACEncoder::EncodeStereoFast( BitBuffer * bitstream, void * inputBuffer, uint32_t stride, uint32_t channelIndex, uint32_t numSamples )
  462. {
  463. BitBuffer startBits = *bitstream; // squirrel away current bit position in case we decide to use escape hatch
  464. AGParamRec agParams;
  465. uint32_t bits1, bits2;
  466. int32_t mixBits, mixRes;
  467. uint32_t minBits, minBits1, minBits2;
  468. uint32_t numU, numV;
  469. uint32_t mode;
  470. uint32_t pbFactor;
  471. uint32_t chanBits;
  472. uint32_t denShift;
  473. uint8_t bytesShifted;
  474. SearchCoefs coefsU;
  475. SearchCoefs coefsV;
  476. uint32_t index;
  477. uint8_t partialFrame;
  478. uint32_t escapeBits;
  479. bool doEscape;
  480. int32_t status;
  481. // make sure we handle this bit-depth before we get going
  482. RequireAction( (mBitDepth == 16) || (mBitDepth == 20) || (mBitDepth == 24) || (mBitDepth == 32), return kALAC_ParamError; );
  483. // reload coefs pointers for this channel pair
  484. // - note that, while you might think they should be re-initialized per block, retaining state across blocks
  485. // actually results in better overall compression
  486. // - strangely, re-using the same coefs for the different passes of the "mixRes" search loop instead of using
  487. // different coefs for the different passes of "mixRes" results in even better compression
  488. coefsU = (SearchCoefs) mCoefsU[channelIndex];
  489. coefsV = (SearchCoefs) mCoefsV[channelIndex];
  490. // matrix encoding adds an extra bit but 32-bit inputs cannot be matrixed b/c 33 is too many
  491. // so enable 16-bit "shift off" and encode in 17-bit mode
  492. // - in addition, 24-bit mode really improves with one byte shifted off
  493. if ( mBitDepth == 32 )
  494. bytesShifted = 2;
  495. else if ( mBitDepth >= 24 )
  496. bytesShifted = 1;
  497. else
  498. bytesShifted = 0;
  499. chanBits = mBitDepth - (bytesShifted * 8) + 1;
  500. // flag whether or not this is a partial frame
  501. partialFrame = (numSamples == mFrameSize) ? 0 : 1;
  502. // set up default encoding parameters for "fast" mode
  503. mixBits = kDefaultMixBits;
  504. mixRes = kDefaultMixRes;
  505. numU = numV = kDefaultNumUV;
  506. denShift = DENSHIFT_DEFAULT;
  507. mode = 0;
  508. pbFactor = 4;
  509. minBits = minBits1 = minBits2 = 1ul << 31;
  510. // mix the stereo inputs with default mixBits/mixRes
  511. switch ( mBitDepth )
  512. {
  513. case 16:
  514. mix16( (int16_t *) inputBuffer, stride, mMixBufferU, mMixBufferV, numSamples, mixBits, mixRes );
  515. break;
  516. case 20:
  517. mix20( (uint8_t *) inputBuffer, stride, mMixBufferU, mMixBufferV, numSamples, mixBits, mixRes );
  518. break;
  519. case 24:
  520. // also extracts the shifted off bytes into the shift buffers
  521. mix24( (uint8_t *) inputBuffer, stride, mMixBufferU, mMixBufferV, numSamples,
  522. mixBits, mixRes, mShiftBufferUV, bytesShifted );
  523. break;
  524. case 32:
  525. // also extracts the shifted off bytes into the shift buffers
  526. mix32( (int32_t *) inputBuffer, stride, mMixBufferU, mMixBufferV, numSamples,
  527. mixBits, mixRes, mShiftBufferUV, bytesShifted );
  528. break;
  529. }
  530. /* speculatively write the bitstream assuming the compressed version will be smaller */
  531. // write bitstream header and coefs
  532. BitBufferWrite( bitstream, 0, 12 );
  533. BitBufferWrite( bitstream, (partialFrame << 3) | (bytesShifted << 1), 4 );
  534. if ( partialFrame )
  535. BitBufferWrite( bitstream, numSamples, 32 );
  536. BitBufferWrite( bitstream, mixBits, 8 );
  537. BitBufferWrite( bitstream, mixRes, 8 );
  538. //Assert( (mode < 16) && (DENSHIFT_DEFAULT < 16) );
  539. //Assert( (pbFactor < 8) && (numU < 32) );
  540. //Assert( (pbFactor < 8) && (numV < 32) );
  541. BitBufferWrite( bitstream, (mode << 4) | DENSHIFT_DEFAULT, 8 );
  542. BitBufferWrite( bitstream, (pbFactor << 5) | numU, 8 );
  543. for ( index = 0; index < numU; index++ )
  544. BitBufferWrite( bitstream, coefsU[numU - 1][index], 16 );
  545. BitBufferWrite( bitstream, (mode << 4) | DENSHIFT_DEFAULT, 8 );
  546. BitBufferWrite( bitstream, (pbFactor << 5) | numV, 8 );
  547. for ( index = 0; index < numV; index++ )
  548. BitBufferWrite( bitstream, coefsV[numV - 1][index], 16 );
  549. // if shift active, write the interleaved shift buffers
  550. if ( bytesShifted != 0 )
  551. {
  552. uint32_t bitShift = bytesShifted * 8;
  553. //Assert( bitShift <= 16 );
  554. for ( index = 0; index < (numSamples * 2); index += 2 )
  555. {
  556. uint32_t shiftedVal;
  557. shiftedVal = ((uint32_t)mShiftBufferUV[index + 0] << bitShift) | (uint32_t)mShiftBufferUV[index + 1];
  558. BitBufferWrite( bitstream, shiftedVal, bitShift * 2 );
  559. }
  560. }
  561. // run the dynamic predictor and lossless compression for the "left" channel
  562. // - note: we always use mode 0 in the "fast" path so we don't need the code for mode != 0
  563. pc_block( mMixBufferU, mPredictorU, numSamples, coefsU[numU - 1], numU, chanBits, DENSHIFT_DEFAULT );
  564. set_ag_params( &agParams, MB0, (pbFactor * PB0) / 4, KB0, numSamples, numSamples, MAX_RUN_DEFAULT );
  565. status = dyn_comp( &agParams, mPredictorU, bitstream, numSamples, chanBits, &bits1 );
  566. RequireNoErr( status, goto Exit; );
  567. // run the dynamic predictor and lossless compression for the "right" channel
  568. pc_block( mMixBufferV, mPredictorV, numSamples, coefsV[numV - 1], numV, chanBits, DENSHIFT_DEFAULT );
  569. set_ag_params( &agParams, MB0, (pbFactor * PB0) / 4, KB0, numSamples, numSamples, MAX_RUN_DEFAULT );
  570. status = dyn_comp( &agParams, mPredictorV, bitstream, numSamples, chanBits, &bits2 );
  571. RequireNoErr( status, goto Exit; );
  572. // do bit requirement calculations
  573. minBits1 = bits1 + (numU * sizeof(int16_t) * 8);
  574. minBits2 = bits2 + (numV * sizeof(int16_t) * 8);
  575. // test for escape hatch if best calculated compressed size turns out to be more than the input size
  576. minBits = minBits1 + minBits2 + (8 /* mixRes/maxRes/etc. */ * 8) + ((partialFrame == true) ? 32 : 0);
  577. if ( bytesShifted != 0 )
  578. minBits += (numSamples * (bytesShifted * 8) * 2);
  579. escapeBits = (numSamples * mBitDepth * 2) + ((partialFrame == true) ? 32 : 0) + (2 * 8); /* 2 common header bytes */
  580. doEscape = (minBits >= escapeBits) ? true : false;
  581. if ( doEscape == false )
  582. {
  583. /* if we happened to create a compressed packet that was actually bigger than an escape packet would be,
  584. chuck it and do an escape packet
  585. */
  586. minBits = BitBufferGetPosition( bitstream ) - BitBufferGetPosition( &startBits );
  587. if ( minBits >= escapeBits )
  588. {
  589. doEscape = true;
  590. printf( "compressed frame too big: %u vs. %u\n", minBits, escapeBits );
  591. }
  592. }
  593. if ( doEscape == true )
  594. {
  595. /* escape */
  596. // reset bitstream position since we speculatively wrote the compressed version
  597. *bitstream = startBits;
  598. // write escape frame
  599. status = this->EncodeStereoEscape( bitstream, inputBuffer, stride, numSamples );
  600. #if VERBOSE_DEBUG
  601. DebugMsg( "escape!: %u vs %u", minBits, (numSamples * mBitDepth * 2) );
  602. #endif
  603. }
  604. Exit:
  605. return status;
  606. }
  607. /*
  608. EncodeStereoEscape()
  609. - encode stereo escape frame
  610. */
  611. int32_t ALACEncoder::EncodeStereoEscape( BitBuffer * bitstream, void * inputBuffer, uint32_t stride, uint32_t numSamples )
  612. {
  613. int16_t * input16;
  614. int32_t * input32;
  615. uint8_t partialFrame;
  616. uint32_t index;
  617. // flag whether or not this is a partial frame
  618. partialFrame = (numSamples == mFrameSize) ? 0 : 1;
  619. // write bitstream header
  620. BitBufferWrite( bitstream, 0, 12 );
  621. BitBufferWrite( bitstream, (partialFrame << 3) | 1, 4 ); // LSB = 1 means "frame not compressed"
  622. if ( partialFrame )
  623. BitBufferWrite( bitstream, numSamples, 32 );
  624. // just copy the input data to the output buffer
  625. switch ( mBitDepth )
  626. {
  627. case 16:
  628. input16 = (int16_t *) inputBuffer;
  629. for ( index = 0; index < (numSamples * stride); index += stride )
  630. {
  631. BitBufferWrite( bitstream, input16[index + 0], 16 );
  632. BitBufferWrite( bitstream, input16[index + 1], 16 );
  633. }
  634. break;
  635. case 20:
  636. // mix20() with mixres param = 0 means de-interleave so use it to simplify things
  637. mix20( (uint8_t *) inputBuffer, stride, mMixBufferU, mMixBufferV, numSamples, 0, 0 );
  638. for ( index = 0; index < numSamples; index++ )
  639. {
  640. BitBufferWrite( bitstream, mMixBufferU[index], 20 );
  641. BitBufferWrite( bitstream, mMixBufferV[index], 20 );
  642. }
  643. break;
  644. case 24:
  645. // mix24() with mixres param = 0 means de-interleave so use it to simplify things
  646. mix24( (uint8_t *) inputBuffer, stride, mMixBufferU, mMixBufferV, numSamples, 0, 0, mShiftBufferUV, 0 );
  647. for ( index = 0; index < numSamples; index++ )
  648. {
  649. BitBufferWrite( bitstream, mMixBufferU[index], 24 );
  650. BitBufferWrite( bitstream, mMixBufferV[index], 24 );
  651. }
  652. break;
  653. case 32:
  654. input32 = (int32_t *) inputBuffer;
  655. for ( index = 0; index < (numSamples * stride); index += stride )
  656. {
  657. BitBufferWrite( bitstream, input32[index + 0], 32 );
  658. BitBufferWrite( bitstream, input32[index + 1], 32 );
  659. }
  660. break;
  661. }
  662. return ALAC_noErr;
  663. }
  664. /*
  665. EncodeMono()
  666. - encode a mono input buffer
  667. */
  668. int32_t ALACEncoder::EncodeMono( BitBuffer * bitstream, void * inputBuffer, uint32_t stride, uint32_t channelIndex, uint32_t numSamples )
  669. {
  670. BitBuffer startBits = *bitstream; // squirrel away copy of current state in case we need to go back and do an escape packet
  671. AGParamRec agParams;
  672. uint32_t bits1;
  673. uint32_t numU;
  674. SearchCoefs coefsU;
  675. uint32_t dilate;
  676. uint32_t minBits, bestU;
  677. uint32_t minU, maxU;
  678. uint32_t index, index2;
  679. uint8_t bytesShifted;
  680. uint32_t shift;
  681. uint32_t mask;
  682. uint32_t chanBits;
  683. uint8_t pbFactor;
  684. uint8_t partialFrame;
  685. int16_t * input16;
  686. int32_t * input32;
  687. uint32_t escapeBits;
  688. bool doEscape;
  689. int32_t status;
  690. // make sure we handle this bit-depth before we get going
  691. RequireAction( (mBitDepth == 16) || (mBitDepth == 20) || (mBitDepth == 24) || (mBitDepth == 32), return kALAC_ParamError; );
  692. status = ALAC_noErr;
  693. // reload coefs array from previous frame
  694. coefsU = (SearchCoefs) mCoefsU[channelIndex];
  695. // pick bit depth for actual encoding
  696. // - we lop off the lower byte(s) for 24-/32-bit encodings
  697. if ( mBitDepth == 32 )
  698. bytesShifted = 2;
  699. else if ( mBitDepth >= 24 )
  700. bytesShifted = 1;
  701. else
  702. bytesShifted = 0;
  703. shift = bytesShifted * 8;
  704. mask = (1ul << shift) - 1;
  705. chanBits = mBitDepth - (bytesShifted * 8);
  706. // flag whether or not this is a partial frame
  707. partialFrame = (numSamples == mFrameSize) ? 0 : 1;
  708. // convert N-bit data to 32-bit for predictor
  709. switch ( mBitDepth )
  710. {
  711. case 16:
  712. {
  713. // convert 16-bit data to 32-bit for predictor
  714. input16 = (int16_t *) inputBuffer;
  715. for ( index = 0, index2 = 0; index < numSamples; index++, index2 += stride )
  716. mMixBufferU[index] = (int32_t) input16[index2];
  717. break;
  718. }
  719. case 20:
  720. // convert 20-bit data to 32-bit for predictor
  721. copy20ToPredictor( (uint8_t *) inputBuffer, stride, mMixBufferU, numSamples );
  722. break;
  723. case 24:
  724. // convert 24-bit data to 32-bit for the predictor and extract the shifted off byte(s)
  725. copy24ToPredictor( (uint8_t *) inputBuffer, stride, mMixBufferU, numSamples );
  726. for ( index = 0; index < numSamples; index++ )
  727. {
  728. mShiftBufferUV[index] = (uint16_t)(mMixBufferU[index] & mask);
  729. mMixBufferU[index] >>= shift;
  730. }
  731. break;
  732. case 32:
  733. {
  734. // just copy the 32-bit input data for the predictor and extract the shifted off byte(s)
  735. input32 = (int32_t *) inputBuffer;
  736. for ( index = 0, index2 = 0; index < numSamples; index++, index2 += stride )
  737. {
  738. int32_t val = input32[index2];
  739. mShiftBufferUV[index] = (uint16_t)(val & mask);
  740. mMixBufferU[index] = val >> shift;
  741. }
  742. break;
  743. }
  744. }
  745. // brute-force encode optimization loop (implied "encode depth" of 0 if comparing to cmd line tool)
  746. // - run over variations of the encoding params to find the best choice
  747. minU = 4;
  748. maxU = 8;
  749. minBits = 1ul << 31;
  750. pbFactor = 4;
  751. minBits = 1ul << 31;
  752. bestU = minU;
  753. for ( numU = minU; numU <= maxU; numU += 4 )
  754. {
  755. BitBuffer workBits;
  756. uint32_t numBits;
  757. BitBufferInit( &workBits, mWorkBuffer, mMaxOutputBytes );
  758. dilate = 32;
  759. for ( uint32_t converge = 0; converge < 7; converge++ )
  760. pc_block( mMixBufferU, mPredictorU, numSamples/dilate, coefsU[numU-1], numU, chanBits, DENSHIFT_DEFAULT );
  761. dilate = 8;
  762. pc_block( mMixBufferU, mPredictorU, numSamples/dilate, coefsU[numU-1], numU, chanBits, DENSHIFT_DEFAULT );
  763. set_ag_params( &agParams, MB0, (pbFactor * PB0) / 4, KB0, numSamples/dilate, numSamples/dilate, MAX_RUN_DEFAULT );
  764. status = dyn_comp( &agParams, mPredictorU, &workBits, numSamples/dilate, chanBits, &bits1 );
  765. RequireNoErr( status, goto Exit; );
  766. numBits = (dilate * bits1) + (16 * numU);
  767. if ( numBits < minBits )
  768. {
  769. bestU = numU;
  770. minBits = numBits;
  771. }
  772. }
  773. // test for escape hatch if best calculated compressed size turns out to be more than the input size
  774. // - first, add bits for the header bytes mixRes/maxRes/shiftU/filterU
  775. minBits += (4 /* mixRes/maxRes/etc. */ * 8) + ((partialFrame == true) ? 32 : 0);
  776. if ( bytesShifted != 0 )
  777. minBits += (numSamples * (bytesShifted * 8));
  778. escapeBits = (numSamples * mBitDepth) + ((partialFrame == true) ? 32 : 0) + (2 * 8); /* 2 common header bytes */
  779. doEscape = (minBits >= escapeBits) ? true : false;
  780. if ( doEscape == false )
  781. {
  782. // write bitstream header
  783. BitBufferWrite( bitstream, 0, 12 );
  784. BitBufferWrite( bitstream, (partialFrame << 3) | (bytesShifted << 1), 4 );
  785. if ( partialFrame )
  786. BitBufferWrite( bitstream, numSamples, 32 );
  787. BitBufferWrite( bitstream, 0, 16 ); // mixBits = mixRes = 0
  788. // write the params and predictor coefs
  789. numU = bestU;
  790. BitBufferWrite( bitstream, (0 << 4) | DENSHIFT_DEFAULT, 8 ); // modeU = 0
  791. BitBufferWrite( bitstream, (pbFactor << 5) | numU, 8 );
  792. for ( index = 0; index < numU; index++ )
  793. BitBufferWrite( bitstream, coefsU[numU-1][index], 16 );
  794. // if shift active, write the interleaved shift buffers
  795. if ( bytesShifted != 0 )
  796. {
  797. for ( index = 0; index < numSamples; index++ )
  798. BitBufferWrite( bitstream, mShiftBufferUV[index], shift );
  799. }
  800. // run the dynamic predictor with the best result
  801. pc_block( mMixBufferU, mPredictorU, numSamples, coefsU[numU-1], numU, chanBits, DENSHIFT_DEFAULT );
  802. // do lossless compression
  803. set_standard_ag_params( &agParams, numSamples, numSamples );
  804. status = dyn_comp( &agParams, mPredictorU, bitstream, numSamples, chanBits, &bits1 );
  805. //AssertNoErr( status );
  806. /* if we happened to create a compressed packet that was actually bigger than an escape packet would be,
  807. chuck it and do an escape packet
  808. */
  809. minBits = BitBufferGetPosition( bitstream ) - BitBufferGetPosition( &startBits );
  810. if ( minBits >= escapeBits )
  811. {
  812. *bitstream = startBits; // reset bitstream state
  813. doEscape = true;
  814. printf( "compressed frame too big: %u vs. %u\n", minBits, escapeBits );
  815. }
  816. }
  817. if ( doEscape == true )
  818. {
  819. // write bitstream header and coefs
  820. BitBufferWrite( bitstream, 0, 12 );
  821. BitBufferWrite( bitstream, (partialFrame << 3) | 1, 4 ); // LSB = 1 means "frame not compressed"
  822. if ( partialFrame )
  823. BitBufferWrite( bitstream, numSamples, 32 );
  824. // just copy the input data to the output buffer
  825. switch ( mBitDepth )
  826. {
  827. case 16:
  828. input16 = (int16_t *) inputBuffer;
  829. for ( index = 0; index < (numSamples * stride); index += stride )
  830. BitBufferWrite( bitstream, input16[index], 16 );
  831. break;
  832. case 20:
  833. // convert 20-bit data to 32-bit for simplicity
  834. copy20ToPredictor( (uint8_t *) inputBuffer, stride, mMixBufferU, numSamples );
  835. for ( index = 0; index < numSamples; index++ )
  836. BitBufferWrite( bitstream, mMixBufferU[index], 20 );
  837. break;
  838. case 24:
  839. // convert 24-bit data to 32-bit for simplicity
  840. copy24ToPredictor( (uint8_t *) inputBuffer, stride, mMixBufferU, numSamples );
  841. for ( index = 0; index < numSamples; index++ )
  842. BitBufferWrite( bitstream, mMixBufferU[index], 24 );
  843. break;
  844. case 32:
  845. input32 = (int32_t *) inputBuffer;
  846. for ( index = 0; index < (numSamples * stride); index += stride )
  847. BitBufferWrite( bitstream, input32[index], 32 );
  848. break;
  849. }
  850. #if VERBOSE_DEBUG
  851. DebugMsg( "escape!: %lu vs %lu", minBits, (numSamples * mBitDepth) );
  852. #endif
  853. }
  854. Exit:
  855. return status;
  856. }
  857. #if PRAGMA_MARK
  858. #pragma mark -
  859. #endif
  860. /*
  861. Encode()
  862. - encode the next block of samples
  863. */
  864. int32_t ALACEncoder::Encode(AudioFormatDescription theInputFormat, AudioFormatDescription theOutputFormat,
  865. unsigned char * theReadBuffer, unsigned char * theWriteBuffer, int32_t * ioNumBytes)
  866. {
  867. uint32_t numFrames;
  868. uint32_t outputSize;
  869. BitBuffer bitstream;
  870. int32_t status;
  871. numFrames = *ioNumBytes/theInputFormat.mBytesPerPacket;
  872. // create a bit buffer structure pointing to our output buffer
  873. BitBufferInit( &bitstream, theWriteBuffer, mMaxOutputBytes );
  874. if ( theInputFormat.mChannelsPerFrame == 2 )
  875. {
  876. // add 3-bit frame start tag ID_CPE = channel pair & 4-bit element instance tag = 0
  877. BitBufferWrite( &bitstream, ID_CPE, 3 );
  878. BitBufferWrite( &bitstream, 0, 4 );
  879. // encode stereo input buffer
  880. if ( mFastMode == false )
  881. status = this->EncodeStereo( &bitstream, theReadBuffer, 2, 0, numFrames );
  882. else
  883. status = this->EncodeStereoFast( &bitstream, theReadBuffer, 2, 0, numFrames );
  884. RequireNoErr( status, goto Exit; );
  885. }
  886. else if ( theInputFormat.mChannelsPerFrame == 1 )
  887. {
  888. // add 3-bit frame start tag ID_SCE = mono channel & 4-bit element instance tag = 0
  889. BitBufferWrite( &bitstream, ID_SCE, 3 );
  890. BitBufferWrite( &bitstream, 0, 4 );
  891. // encode mono input buffer
  892. status = this->EncodeMono( &bitstream, theReadBuffer, 1, 0, numFrames );
  893. RequireNoErr( status, goto Exit; );
  894. }
  895. else
  896. {
  897. char * inputBuffer;
  898. uint32_t tag;
  899. uint32_t channelIndex;
  900. uint32_t inputIncrement;
  901. uint8_t stereoElementTag;
  902. uint8_t monoElementTag;
  903. uint8_t lfeElementTag;
  904. inputBuffer = (char *) theReadBuffer;
  905. inputIncrement = ((mBitDepth + 7) / 8);
  906. stereoElementTag = 0;
  907. monoElementTag = 0;
  908. lfeElementTag = 0;
  909. for ( channelIndex = 0; channelIndex < theInputFormat.mChannelsPerFrame; )
  910. {
  911. tag = (sChannelMaps[theInputFormat.mChannelsPerFrame - 1] & (0x7ul << (channelIndex * 3))) >> (channelIndex * 3);
  912. BitBufferWrite( &bitstream, tag, 3 );
  913. switch ( tag )
  914. {
  915. case ID_SCE:
  916. // mono
  917. BitBufferWrite( &bitstream, monoElementTag, 4 );
  918. status = this->EncodeMono( &bitstream, inputBuffer, theInputFormat.mChannelsPerFrame, channelIndex, numFrames );
  919. inputBuffer += inputIncrement;
  920. channelIndex++;
  921. monoElementTag++;
  922. break;
  923. case ID_CPE:
  924. // stereo
  925. BitBufferWrite( &bitstream, stereoElementTag, 4 );
  926. status = this->EncodeStereo( &bitstream, inputBuffer, theInputFormat.mChannelsPerFrame, channelIndex, numFrames );
  927. inputBuffer += (inputIncrement * 2);
  928. channelIndex += 2;
  929. stereoElementTag++;
  930. break;
  931. case ID_LFE:
  932. // LFE channel (subwoofer)
  933. BitBufferWrite( &bitstream, lfeElementTag, 4 );
  934. status = this->EncodeMono( &bitstream, inputBuffer, theInputFormat.mChannelsPerFrame, channelIndex, numFrames );
  935. inputBuffer += inputIncrement;
  936. channelIndex++;
  937. lfeElementTag++;
  938. break;
  939. default:
  940. printf( "That ain't right! (%u)\n", tag );
  941. status = kALAC_ParamError;
  942. goto Exit;
  943. }
  944. RequireNoErr( status, goto Exit; );
  945. }
  946. }
  947. #if VERBOSE_DEBUG
  948. {
  949. // if there is room left in the output buffer, add some random fill data to test decoder
  950. int32_t bitsLeft;
  951. int32_t bytesLeft;
  952. bitsLeft = BitBufferGetPosition( &bitstream ) - 3; // - 3 for ID_END tag
  953. bytesLeft = bitstream.byteSize - ((bitsLeft + 7) / 8);
  954. if ( (bytesLeft > 20) && ((bytesLeft & 0x4u) != 0) )
  955. AddFiller( &bitstream, bytesLeft );
  956. }
  957. #endif
  958. // add 3-bit frame end tag: ID_END
  959. BitBufferWrite( &bitstream, ID_END, 3 );
  960. // byte-align the output data
  961. BitBufferByteAlign( &bitstream, true );
  962. outputSize = BitBufferGetPosition( &bitstream ) / 8;
  963. //Assert( outputSize <= mMaxOutputBytes );
  964. // all good, let iTunes know what happened and remember the total number of input sample frames
  965. *ioNumBytes = outputSize;
  966. //mEncodedFrames += encodeMsg->numInputSamples;
  967. // gather encoding stats
  968. mTotalBytesGenerated += outputSize;
  969. mMaxFrameBytes = MAX( mMaxFrameBytes, outputSize );
  970. status = ALAC_noErr;
  971. Exit:
  972. return status;
  973. }
  974. /*
  975. Finish()
  976. - drain out any leftover samples
  977. */
  978. int32_t ALACEncoder::Finish()
  979. {
  980. /* // finalize bit rate statistics
  981. if ( mSampleSize.numEntries != 0 )
  982. mAvgBitRate = (uint32_t)( (((float)mTotalBytesGenerated * 8.0f) / (float)mSampleSize.numEntries) * ((float)mSampleRate / (float)mFrameSize) );
  983. else
  984. mAvgBitRate = 0;
  985. */
  986. return ALAC_noErr;
  987. }
  988. #if PRAGMA_MARK
  989. #pragma mark -
  990. #endif
  991. /*
  992. GetConfig()
  993. */
  994. void ALACEncoder::GetConfig( ALACSpecificConfig & config )
  995. {
  996. config.frameLength = Swap32NtoB(mFrameSize);
  997. config.compatibleVersion = (uint8_t) kALACCompatibleVersion;
  998. config.bitDepth = (uint8_t) mBitDepth;
  999. config.pb = (uint8_t) PB0;
  1000. config.kb = (uint8_t) KB0;
  1001. config.mb = (uint8_t) MB0;
  1002. config.numChannels = (uint8_t) mNumChannels;
  1003. config.maxRun = Swap16NtoB((uint16_t) MAX_RUN_DEFAULT);
  1004. config.maxFrameBytes = Swap32NtoB(mMaxFrameBytes);
  1005. config.avgBitRate = Swap32NtoB(mAvgBitRate);
  1006. config.sampleRate = Swap32NtoB(mOutputSampleRate);
  1007. }
  1008. uint32_t ALACEncoder::GetMagicCookieSize(uint32_t inNumChannels)
  1009. {
  1010. if (inNumChannels > 2)
  1011. {
  1012. return sizeof(ALACSpecificConfig) + kChannelAtomSize + sizeof(ALACAudioChannelLayout);
  1013. }
  1014. else
  1015. {
  1016. return sizeof(ALACSpecificConfig);
  1017. }
  1018. }
  1019. void ALACEncoder::GetMagicCookie(void * outCookie, uint32_t * ioSize)
  1020. {
  1021. ALACSpecificConfig theConfig = {0};
  1022. ALACAudioChannelLayout theChannelLayout = {0};
  1023. uint8_t theChannelAtom[kChannelAtomSize] = {0, 0, 0, 0, 'c', 'h', 'a', 'n', 0, 0, 0, 0};
  1024. uint32_t theCookieSize = sizeof(ALACSpecificConfig);
  1025. uint8_t * theCookiePointer = (uint8_t *)outCookie;
  1026. GetConfig(theConfig);
  1027. if (theConfig.numChannels > 2)
  1028. {
  1029. theChannelLayout.mChannelLayoutTag = ALACChannelLayoutTags[theConfig.numChannels - 1];
  1030. theCookieSize += (sizeof(ALACAudioChannelLayout) + kChannelAtomSize);
  1031. }
  1032. if (*ioSize >= theCookieSize)
  1033. {
  1034. memcpy(theCookiePointer, &theConfig, sizeof(ALACSpecificConfig));
  1035. theChannelAtom[3] = (sizeof(ALACAudioChannelLayout) + kChannelAtomSize);
  1036. if (theConfig.numChannels > 2)
  1037. {
  1038. theCookiePointer += sizeof(ALACSpecificConfig);
  1039. memcpy(theCookiePointer, theChannelAtom, kChannelAtomSize);
  1040. theCookiePointer += kChannelAtomSize;
  1041. memcpy(theCookiePointer, &theChannelLayout, sizeof(ALACAudioChannelLayout));
  1042. }
  1043. *ioSize = theCookieSize;
  1044. }
  1045. else
  1046. {
  1047. *ioSize = 0; // no incomplete cookies
  1048. }
  1049. }
  1050. /*
  1051. InitializeEncoder()
  1052. - initialize the encoder component with the current config
  1053. */
  1054. int32_t ALACEncoder::InitializeEncoder(AudioFormatDescription theOutputFormat)
  1055. {
  1056. int32_t status;
  1057. mOutputSampleRate = theOutputFormat.mSampleRate;
  1058. mNumChannels = theOutputFormat.mChannelsPerFrame;
  1059. switch(theOutputFormat.mFormatFlags)
  1060. {
  1061. case 1:
  1062. mBitDepth = 16;
  1063. break;
  1064. case 2:
  1065. mBitDepth = 20;
  1066. break;
  1067. case 3:
  1068. mBitDepth = 24;
  1069. break;
  1070. case 4:
  1071. mBitDepth = 32;
  1072. break;
  1073. default:
  1074. break;
  1075. }
  1076. // set up default encoding parameters and state
  1077. // - note: mFrameSize is set in the constructor or via SetFrameSize() which must be called before this routine
  1078. for ( uint32_t index = 0; index < kALACMaxChannels; index++ )
  1079. mLastMixRes[index] = kDefaultMixRes;
  1080. // the maximum output frame size can be no bigger than (samplesPerBlock * numChannels * ((10 + sampleSize)/8) + 1)
  1081. // but note that this can be bigger than the input size!
  1082. // - since we don't yet know what our input format will be, use our max allowed sample size in the calculation
  1083. mMaxOutputBytes = mFrameSize * mNumChannels * ((10 + kMaxSampleSize) / 8) + 1;
  1084. // allocate mix buffers
  1085. mMixBufferU = (int32_t *) calloc( mFrameSize * sizeof(int32_t), 1 );
  1086. mMixBufferV = (int32_t *) calloc( mFrameSize * sizeof(int32_t), 1 );
  1087. // allocate dynamic predictor buffers
  1088. mPredictorU = (int32_t *) calloc( mFrameSize * sizeof(int32_t), 1 );
  1089. mPredictorV = (int32_t *) calloc( mFrameSize * sizeof(int32_t), 1 );
  1090. // allocate combined shift buffer
  1091. mShiftBufferUV = (uint16_t *) calloc( mFrameSize * 2 * sizeof(uint16_t),1 );
  1092. // allocate work buffer for search loop
  1093. mWorkBuffer = (uint8_t *) calloc( mMaxOutputBytes, 1 );
  1094. RequireAction( (mMixBufferU != nil) && (mMixBufferV != nil) &&
  1095. (mPredictorU != nil) && (mPredictorV != nil) &&
  1096. (mShiftBufferUV != nil) && (mWorkBuffer != nil ),
  1097. status = kALAC_MemFullError; goto Exit; );
  1098. status = ALAC_noErr;
  1099. // initialize coefs arrays once b/c retaining state across blocks actually improves the encode ratio
  1100. for ( int32_t channel = 0; channel < (int32_t)mNumChannels; channel++ )
  1101. {
  1102. for ( int32_t search = 0; search < kALACMaxSearches; search++ )
  1103. {
  1104. init_coefs( mCoefsU[channel][search], DENSHIFT_DEFAULT, kALACMaxCoefs );
  1105. init_coefs( mCoefsV[channel][search], DENSHIFT_DEFAULT, kALACMaxCoefs );
  1106. }
  1107. }
  1108. Exit:
  1109. return status;
  1110. }
  1111. /*
  1112. GetSourceFormat()
  1113. - given the input format, return one of our supported formats
  1114. */
  1115. void ALACEncoder::GetSourceFormat( const AudioFormatDescription * source, AudioFormatDescription * output )
  1116. {
  1117. // default is 16-bit native endian
  1118. // - note: for float input we assume that's coming from one of our decoders (mp3, aac) so it only makes sense
  1119. // to encode to 16-bit since the source was lossy in the first place
  1120. // - note: if not a supported bit depth, find the closest supported bit depth to the input one
  1121. if ( (source->mFormatID != kALACFormatLinearPCM) || ((source->mFormatFlags & kALACFormatFlagIsFloat) != 0) ||
  1122. ( source->mBitsPerChannel <= 16 ) )
  1123. mBitDepth = 16;
  1124. else if ( source->mBitsPerChannel <= 20 )
  1125. mBitDepth = 20;
  1126. else if ( source->mBitsPerChannel <= 24 )
  1127. mBitDepth = 24;
  1128. else
  1129. mBitDepth = 32;
  1130. // we support 16/20/24/32-bit integer data at any sample rate and our target number of channels
  1131. // and sample rate were specified when we were configured
  1132. /*
  1133. MakeUncompressedAudioFormat( mNumChannels, (float) mOutputSampleRate, mBitDepth, kAudioFormatFlagsNativeIntegerPacked, output );
  1134. */
  1135. }
  1136. #if VERBOSE_DEBUG
  1137. #if PRAGMA_MARK
  1138. #pragma mark -
  1139. #endif
  1140. /*
  1141. AddFiller()
  1142. - add fill and data stream elements to the bitstream to test the decoder
  1143. */
  1144. static void AddFiller( BitBuffer * bits, int32_t numBytes )
  1145. {
  1146. uint8_t tag;
  1147. uint32_t index;
  1148. // out of lameness, subtract 6 bytes to deal with header + alignment as required for fill/data elements
  1149. numBytes -= 6;
  1150. if ( numBytes <= 0 )
  1151. return;
  1152. // randomly pick Fill or Data Stream Element based on numBytes requested
  1153. tag = (numBytes & 0x8) ? ID_FIL : ID_DSE;
  1154. BitBufferWrite( bits, tag, 3 );
  1155. if ( tag == ID_FIL )
  1156. {
  1157. // can't write more than 269 bytes in a fill element
  1158. numBytes = (numBytes > 269) ? 269 : numBytes;
  1159. // fill element = 4-bit size unless >= 15 then 4-bit size + 8-bit extension size
  1160. if ( numBytes >= 15 )
  1161. {
  1162. uint16_t extensionSize;
  1163. BitBufferWrite( bits, 15, 4 );
  1164. // 8-bit extension count field is "extra + 1" which is weird but I didn't define the syntax
  1165. // - otherwise, there's no way to represent 15
  1166. // - for example, to really mean 15 bytes you must encode extensionSize = 1
  1167. // - why it's not like data stream elements I have no idea
  1168. extensionSize = (numBytes - 15) + 1;
  1169. Assert( extensionSize <= 255 );
  1170. BitBufferWrite( bits, extensionSize, 8 );
  1171. }
  1172. else
  1173. BitBufferWrite( bits, numBytes, 4 );
  1174. BitBufferWrite( bits, 0x10, 8 ); // extension_type = FILL_DATA = b0001 or'ed with fill_nibble = b0000
  1175. for ( index = 0; index < (numBytes - 1); index++ )
  1176. BitBufferWrite( bits, 0xa5, 8 ); // fill_byte = b10100101 = 0xa5
  1177. }
  1178. else
  1179. {
  1180. // can't write more than 510 bytes in a data stream element
  1181. numBytes = (numBytes > 510) ? 510 : numBytes;
  1182. BitBufferWrite( bits, 0, 4 ); // element instance tag
  1183. BitBufferWrite( bits, 1, 1 ); // byte-align flag = true
  1184. // data stream element = 8-bit size unless >= 255 then 8-bit size + 8-bit size
  1185. if ( numBytes >= 255 )
  1186. {
  1187. BitBufferWrite( bits, 255, 8 );
  1188. BitBufferWrite( bits, numBytes - 255, 8 );
  1189. }
  1190. else
  1191. BitBufferWrite( bits, numBytes, 8 );
  1192. BitBufferByteAlign( bits, true ); // byte-align with zeros
  1193. for ( index = 0; index < numBytes; index++ )
  1194. BitBufferWrite( bits, 0x5a, 8 );
  1195. }
  1196. }
  1197. #endif /* VERBOSE_DEBUG */