Line 0
Link Here
|
|
|
1 |
--- src/aacPlusEncoder.cpp.orig 2010-05-10 07:18:48.000000000 +0900 |
2 |
+++ src/aacPlusEncoder.cpp 2010-10-13 07:42:25.000000000 +0900 |
3 |
@@ -5,8 +5,8 @@ |
4 |
Tyrell DarkIce |
5 |
|
6 |
File : aacPlusEncoder.cpp |
7 |
- Version : $Revision: 474 $ |
8 |
- Author : $Author: rafael@riseup.net $ |
9 |
+ Version : $Revision$ |
10 |
+ Author : $Author$ |
11 |
Location : $HeadURL$ |
12 |
|
13 |
Copyright notice: |
14 |
@@ -51,7 +51,7 @@ |
15 |
/*------------------------------------------------------------------------------ |
16 |
* File identity |
17 |
*----------------------------------------------------------------------------*/ |
18 |
-static const char fileid[] = "$Id: aacPlusEncoder.cpp 474 2010-05-10 01:18:15Z rafael@riseup.net $"; |
19 |
+static const char fileid[] = "$Id$"; |
20 |
|
21 |
|
22 |
/* =============================================== local function prototypes */ |
23 |
@@ -76,82 +76,27 @@ |
24 |
"aacplus lib opening underlying sink error"); |
25 |
} |
26 |
|
27 |
- reportEvent(1, "Using aacplus codec version", "720 3gpp"); |
28 |
+ reportEvent(1, "Using aacplus codec"); |
29 |
|
30 |
- bitrate = getOutBitrate() * 1000; |
31 |
- bandwidth = 0; |
32 |
- useParametricStereo = 0; |
33 |
- numAncDataBytes=0; |
34 |
- coreWriteOffset = 0; |
35 |
- envReadOffset = 0; |
36 |
- writeOffset = INPUT_DELAY*MAX_CHANNELS; |
37 |
- writtenSamples = 0; |
38 |
- aacEnc = NULL; |
39 |
- hEnvEnc=NULL; |
40 |
- |
41 |
- /* set up basic parameters for aacPlus codec */ |
42 |
- AacInitDefaultConfig(&config); |
43 |
- nChannelsAAC = nChannelsSBR = getOutChannel(); |
44 |
- |
45 |
- if ( (getInChannel() == 2) && (bitrate >= 16000) && (bitrate < 44001) ) { |
46 |
- useParametricStereo = 1; |
47 |
- nChannelsAAC = 1; |
48 |
- nChannelsSBR = 2; |
49 |
- |
50 |
- reportEvent(10, "use Parametric Stereo"); |
51 |
- |
52 |
- envReadOffset = (MAX_DS_FILTER_DELAY + INPUT_DELAY)*MAX_CHANNELS; |
53 |
- coreWriteOffset = CORE_INPUT_OFFSET_PS; |
54 |
- writeOffset = envReadOffset; |
55 |
- } else { |
56 |
- /* set up 2:1 downsampling */ |
57 |
- InitIIR21_Resampler(&(IIR21_reSampler[0])); |
58 |
- InitIIR21_Resampler(&(IIR21_reSampler[1])); |
59 |
- |
60 |
- if(IIR21_reSampler[0].delay > MAX_DS_FILTER_DELAY) |
61 |
- throw Exception(__FILE__, __LINE__, "IIR21 resampler delay is bigger then MAX_DS_FILTER_DELAY"); |
62 |
- writeOffset += IIR21_reSampler[0].delay*MAX_CHANNELS; |
63 |
+ encoderHandle = aacplusEncOpen(getOutSampleRate(), |
64 |
+ getInChannel(), |
65 |
+ &inputSamples, |
66 |
+ &maxOutputBytes); |
67 |
+ |
68 |
+ aacplusEncConfiguration * aacplusConfig; |
69 |
+ |
70 |
+ aacplusConfig = aacplusEncGetCurrentConfiguration(encoderHandle); |
71 |
+ |
72 |
+ aacplusConfig->bitRate = getOutBitrate() * 1000; |
73 |
+ aacplusConfig->bandWidth = lowpass; |
74 |
+ aacplusConfig->outputFormat = 1; |
75 |
+ aacplusConfig->inputFormat = AACPLUS_INPUT_16BIT; |
76 |
+ aacplusConfig->nChannelsOut = getOutChannel(); |
77 |
+ |
78 |
+ if (!aacplusEncSetConfiguration(encoderHandle, aacplusConfig)) { |
79 |
+ throw Exception(__FILE__, __LINE__, |
80 |
+ "error configuring libaacplus library"); |
81 |
} |
82 |
- |
83 |
- sampleRateAAC = getOutSampleRate(); |
84 |
- config.bitRate = bitrate; |
85 |
- config.nChannelsIn=getInChannel(); |
86 |
- config.nChannelsOut=nChannelsAAC; |
87 |
- config.bandWidth=bandwidth; |
88 |
- |
89 |
- /* set up SBR configuration */ |
90 |
- if(!IsSbrSettingAvail(bitrate, nChannelsAAC, sampleRateAAC, &sampleRateAAC)) |
91 |
- throw Exception(__FILE__, __LINE__, "No valid SBR configuration found"); |
92 |
- |
93 |
- InitializeSbrDefaults (&sbrConfig); |
94 |
- sbrConfig.usePs = useParametricStereo; |
95 |
- |
96 |
- AdjustSbrSettings( &sbrConfig, |
97 |
- bitrate, |
98 |
- nChannelsAAC, |
99 |
- sampleRateAAC, |
100 |
- AACENC_TRANS_FAC, |
101 |
- 24000); |
102 |
- |
103 |
- EnvOpen( &hEnvEnc, |
104 |
- inBuf + coreWriteOffset, |
105 |
- &sbrConfig, |
106 |
- &config.bandWidth); |
107 |
- |
108 |
- /* set up AAC encoder, now that samling rate is known */ |
109 |
- config.sampleRate = sampleRateAAC; |
110 |
- if (AacEncOpen(&aacEnc, config) != 0){ |
111 |
- AacEncClose(aacEnc); |
112 |
- throw Exception(__FILE__, __LINE__, "Initialisation of AAC failed !"); |
113 |
- } |
114 |
- |
115 |
- init_plans(); |
116 |
- |
117 |
- /* create the ADTS header */ |
118 |
- adts_hdr(outBuf, &config); |
119 |
- |
120 |
- inSamples = AACENC_BLOCKSIZE * getInChannel() * 2; |
121 |
- |
122 |
|
123 |
// initialize the resampling coverter if needed |
124 |
if ( converter ) { |
125 |
@@ -159,8 +104,8 @@ |
126 |
converterData.input_frames = 4096/((getInBitsPerSample() / 8) * getInChannel()); |
127 |
converterData.data_in = new float[converterData.input_frames*getInChannel()]; |
128 |
converterData.output_frames = (int) (converterData.input_frames * resampleRatio + 1); |
129 |
- if ((int) inSamples > getInChannel() * converterData.output_frames) { |
130 |
- resampledOffset = new float[2 * inSamples]; |
131 |
+ if ((int) inputSamples > getInChannel() * converterData.output_frames) { |
132 |
+ resampledOffset = new float[2 * inputSamples]; |
133 |
} else { |
134 |
resampledOffset = new float[2 * getInChannel() * converterData.input_frames]; |
135 |
} |
136 |
@@ -178,13 +123,9 @@ |
137 |
} |
138 |
|
139 |
aacplusOpen = true; |
140 |
- reportEvent(10, "bitrate=", bitrate); |
141 |
- reportEvent(10, "nChannelsIn", getInChannel()); |
142 |
- reportEvent(10, "nChannelsOut", getOutChannel()); |
143 |
- reportEvent(10, "nChannelsSBR", nChannelsSBR); |
144 |
- reportEvent(10, "nChannelsAAC", nChannelsAAC); |
145 |
- reportEvent(10, "sampleRateAAC", sampleRateAAC); |
146 |
- reportEvent(10, "inSamples", inSamples); |
147 |
+ reportEvent(10, "nChannelsAAC", aacplusConfig->nChannelsOut); |
148 |
+ reportEvent(10, "sampleRateAAC", aacplusConfig->sampleRate); |
149 |
+ reportEvent(10, "inSamples", inputSamples); |
150 |
return true; |
151 |
} |
152 |
|
153 |
@@ -199,21 +140,23 @@ |
154 |
if ( !isOpen() || len == 0) { |
155 |
return 0; |
156 |
} |
157 |
- |
158 |
+ |
159 |
unsigned int channels = getInChannel(); |
160 |
unsigned int bitsPerSample = getInBitsPerSample(); |
161 |
unsigned int sampleSize = (bitsPerSample / 8) * channels; |
162 |
+ unsigned char * b = (unsigned char*) buf; |
163 |
unsigned int processed = len - (len % sampleSize); |
164 |
unsigned int nSamples = processed / sampleSize; |
165 |
- unsigned int samples = (unsigned int) nSamples * channels; |
166 |
- int processedSamples = 0; |
167 |
- |
168 |
- |
169 |
+ unsigned char * aacplusBuf = new unsigned char[maxOutputBytes]; |
170 |
+ int samples = (int) nSamples * channels; |
171 |
+ int processedSamples = 0; |
172 |
+ |
173 |
+ |
174 |
|
175 |
if ( converter ) { |
176 |
unsigned int converted; |
177 |
#ifdef HAVE_SRC_LIB |
178 |
- src_short_to_float_array ((short *) buf, converterData.data_in, samples); |
179 |
+ src_short_to_float_array ((short *) b, converterData.data_in, samples); |
180 |
converterData.input_frames = nSamples; |
181 |
converterData.data_out = resampledOffset + (resampledOffsetSize * channels); |
182 |
int srcError = src_process (converter, &converterData); |
183 |
@@ -224,7 +167,6 @@ |
184 |
int inCount = nSamples; |
185 |
short int * shortBuffer = new short int[samples]; |
186 |
int outCount = (int) (inCount * resampleRatio); |
187 |
- unsigned char * b = (unsigned char*) buf; |
188 |
Util::conv( bitsPerSample, b, processed, shortBuffer, isInBigEndian()); |
189 |
converted = converter->resample( inCount, |
190 |
outCount+1, |
191 |
@@ -235,18 +177,27 @@ |
192 |
resampledOffsetSize += converted; |
193 |
|
194 |
// encode samples (if enough) |
195 |
- while(resampledOffsetSize - processedSamples >= inSamples/channels) { |
196 |
+ while(resampledOffsetSize - processedSamples >= inputSamples/channels) { |
197 |
+ int outputBytes; |
198 |
#ifdef HAVE_SRC_LIB |
199 |
- short *shortData = new short[inSamples]; |
200 |
+ short *shortData = new short[inputSamples]; |
201 |
src_float_to_short_array(resampledOffset + (processedSamples * channels), |
202 |
- shortData, inSamples) ; |
203 |
- |
204 |
- encodeAacSamples (shortData, inSamples, channels); |
205 |
+ shortData, inputSamples) ; |
206 |
+ outputBytes = aacplusEncEncode(encoderHandle, |
207 |
+ (int32_t*) shortData, |
208 |
+ inputSamples, |
209 |
+ aacplusBuf, |
210 |
+ maxOutputBytes); |
211 |
delete [] shortData; |
212 |
#else |
213 |
- encodeAacSamples (&resampledOffset[processedSamples*channels], inSamples, channels); |
214 |
+ outputBytes = aacplusEncEncode(encoderHandle, |
215 |
+ (int32_t*) &resampledOffset[processedSamples*channels], |
216 |
+ inputSamples, |
217 |
+ aacplusBuf, |
218 |
+ maxOutputBytes); |
219 |
#endif |
220 |
- processedSamples+=inSamples/channels; |
221 |
+ getSink()->write(aacplusBuf, outputBytes); |
222 |
+ processedSamples+=inputSamples/channels; |
223 |
} |
224 |
|
225 |
if (processedSamples && (int) resampledOffsetSize >= processedSamples) { |
226 |
@@ -262,70 +213,27 @@ |
227 |
#endif |
228 |
} |
229 |
} else { |
230 |
- encodeAacSamples ((short *) buf, samples, channels); |
231 |
- } |
232 |
+ while (processedSamples < samples) { |
233 |
+ int outputBytes; |
234 |
+ int inSamples = samples - processedSamples < (int) inputSamples |
235 |
+ ? samples - processedSamples |
236 |
+ : inputSamples; |
237 |
+ |
238 |
+ outputBytes = aacplusEncEncode(encoderHandle, |
239 |
+ (int32_t*) (b + processedSamples/sampleSize), |
240 |
+ inSamples, |
241 |
+ aacplusBuf, |
242 |
+ maxOutputBytes); |
243 |
+ getSink()->write(aacplusBuf, outputBytes); |
244 |
|
245 |
- return samples; |
246 |
-} |
247 |
- |
248 |
-void |
249 |
-aacPlusEncoder :: encodeAacSamples (short *TimeDataPcm, unsigned int samples, int channels) |
250 |
- throw ( Exception ) |
251 |
-{ |
252 |
- unsigned int i; |
253 |
- int ch, outSamples, numOutBytes; |
254 |
- |
255 |
- for (i=0; i<samples; i++) |
256 |
- inBuf[(2/channels)*i+writeOffset+writtenSamples] = (float) TimeDataPcm[i]; |
257 |
- |
258 |
- writtenSamples+=samples; |
259 |
- |
260 |
- if (writtenSamples < inSamples) |
261 |
- return; |
262 |
- |
263 |
- /* encode one SBR frame */ |
264 |
- EnvEncodeFrame( hEnvEnc, |
265 |
- inBuf + envReadOffset, |
266 |
- inBuf + coreWriteOffset, |
267 |
- MAX_CHANNELS, |
268 |
- &numAncDataBytes, |
269 |
- ancDataBytes); |
270 |
- |
271 |
- /* 2:1 downsampling for AAC core */ |
272 |
- if (!useParametricStereo) { |
273 |
- for( ch=0; ch<nChannelsAAC; ch++ ) |
274 |
- IIR21_Downsample( &(IIR21_reSampler[ch]), |
275 |
- inBuf + writeOffset+ch, |
276 |
- writtenSamples/channels, |
277 |
- MAX_CHANNELS, |
278 |
- inBuf+ch, |
279 |
- &outSamples, |
280 |
- MAX_CHANNELS); |
281 |
- } |
282 |
- |
283 |
- /* encode one AAC frame */ |
284 |
- AacEncEncode( aacEnc, |
285 |
- inBuf, |
286 |
- useParametricStereo ? 1 : MAX_CHANNELS, /* stride (step) */ |
287 |
- ancDataBytes, |
288 |
- &numAncDataBytes, |
289 |
- (unsigned *) (outBuf+ADTS_HEADER_SIZE), |
290 |
- &numOutBytes); |
291 |
- if (useParametricStereo) { |
292 |
- memcpy( inBuf,inBuf+AACENC_BLOCKSIZE,CORE_INPUT_OFFSET_PS*sizeof(float)); |
293 |
- } else { |
294 |
- memmove( inBuf,inBuf+AACENC_BLOCKSIZE*2*MAX_CHANNELS,writeOffset*sizeof(float)); |
295 |
- } |
296 |
- |
297 |
- /* Write one frame of encoded audio */ |
298 |
- if (numOutBytes) { |
299 |
- adts_hdr_up(outBuf, numOutBytes); |
300 |
- sink->write(outBuf, numOutBytes+ADTS_HEADER_SIZE); |
301 |
+ processedSamples += inSamples; |
302 |
+ } |
303 |
} |
304 |
- |
305 |
- writtenSamples=0; |
306 |
|
307 |
- return; |
308 |
+ delete[] aacplusBuf; |
309 |
+ |
310 |
+// return processedSamples; |
311 |
+ return samples; |
312 |
} |
313 |
|
314 |
/*------------------------------------------------------------------------------ |
315 |
@@ -352,12 +260,7 @@ |
316 |
if ( isOpen() ) { |
317 |
flush(); |
318 |
|
319 |
- destroy_plans(); |
320 |
- AacEncClose(aacEnc); |
321 |
- if (hEnvEnc) { |
322 |
- EnvClose(hEnvEnc); |
323 |
- } |
324 |
- |
325 |
+ aacplusEncClose(encoderHandle); |
326 |
aacplusOpen = false; |
327 |
|
328 |
sink->close(); |