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 /*
22 	File:		ALACEncoder.h
23 */
24 
25 #pragma once
26 
27 #include <stdint.h>
28 
29 #include "ALACAudioTypes.h"
30 
31 
32 struct BitBuffer;
33 
34 class ALACEncoder
35 {
36 	public:
37 		ALACEncoder();
38 		virtual ~ALACEncoder();
39 
40 		virtual int32_t	Encode(AudioFormatDescription theInputFormat, AudioFormatDescription theOutputFormat,
41                                    unsigned char * theReadBuffer, unsigned char * theWriteBuffer, int32_t * ioNumBytes);
42 		virtual int32_t	Finish( );
43 
SetFastMode(bool fast)44 		void				SetFastMode( bool fast ) { mFastMode = fast; };
45 
46 		// this must be called *before* InitializeEncoder()
SetFrameSize(uint32_t frameSize)47 		void				SetFrameSize( uint32_t frameSize ) { mFrameSize = frameSize; };
48 
49 		void				GetConfig( ALACSpecificConfig & config );
50         uint32_t            GetMagicCookieSize(uint32_t inNumChannels);
51         void				GetMagicCookie( void * config, uint32_t * ioSize );
52 
53         virtual int32_t	InitializeEncoder(AudioFormatDescription theOutputFormat);
54 
55     protected:
56 		virtual void		GetSourceFormat( const AudioFormatDescription * source, AudioFormatDescription * output );
57 
58 		int32_t			EncodeStereo( struct BitBuffer * bitstream, void * input, uint32_t stride, uint32_t channelIndex, uint32_t numSamples );
59 		int32_t			EncodeStereoFast( struct BitBuffer * bitstream, void * input, uint32_t stride, uint32_t channelIndex, uint32_t numSamples );
60 		int32_t			EncodeStereoEscape( struct BitBuffer * bitstream, void * input, uint32_t stride, uint32_t numSamples );
61 		int32_t			EncodeMono( struct BitBuffer * bitstream, void * input, uint32_t stride, uint32_t channelIndex, uint32_t numSamples );
62 
63 
64 		// ALAC encoder parameters
65 		int16_t					mBitDepth;
66 		bool					mFastMode;
67 
68 		// encoding state
69 		int16_t					mLastMixRes[kALACMaxChannels];
70 
71 		// encoding buffers
72 		int32_t *				mMixBufferU;
73 		int32_t *				mMixBufferV;
74 		int32_t *				mPredictorU;
75 		int32_t *				mPredictorV;
76 		uint16_t *				mShiftBufferUV;
77 
78 		uint8_t *					mWorkBuffer;
79 
80 		// per-channel coefficients buffers
81 		int16_t					mCoefsU[kALACMaxChannels][kALACMaxSearches][kALACMaxCoefs];
82 		int16_t					mCoefsV[kALACMaxChannels][kALACMaxSearches][kALACMaxCoefs];
83 
84 		// encoding statistics
85 		uint32_t					mTotalBytesGenerated;
86 		uint32_t					mAvgBitRate;
87 		uint32_t					mMaxFrameBytes;
88         uint32_t                  mFrameSize;
89         uint32_t                  mMaxOutputBytes;
90         uint32_t                  mNumChannels;
91         uint32_t                  mOutputSampleRate;
92 };
93