Bug Summary

File:libs/libsndfile/src/ALAC/alac_encoder.c
Location:line 287, column 2
Description:Value stored to 'minBits' is never read

Annotated Source Code

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