-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathaacplusenc.c
555 lines (430 loc) · 13.3 KB
/
aacplusenc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "FloatFR.h"
#include "au_channel.h"
#include "aacenc.h"
#include "iir32resample.h"
#include "resampler.h"
#include "sbr_main.h"
#include "aac_ram.h"
#include "aac_rom.h"
#include "adts.h"
#include "cfftn.h"
#include "config.h"
/* dynamic buffer of SBR that can be reused for resampling */
extern float sbr_envRBuffer[];
#define CORE_DELAY (1600)
#define INPUT_DELAY ((CORE_DELAY)*2 +6*64-2048+1) /* ((1600 (core codec)*2 (multi rate) + 6*64 (sbr dec delay) - 2048 (sbr enc delay) + magic */
#define MAX_DS_FILTER_DELAY 16 /* the additional max resampler filter delay (source fs) */
#define CORE_INPUT_OFFSET_PS (0) /* (96-64) makes AAC still some 64 core samples too early wrt SBR ... maybe -32 would be even more correct, but 1024-32 would need additional SBR bitstream delay by one frame */
#define bEncodeMono (inputInfo.nChannels == 1)
/*
input buffer (1ch)
|------------ 1537 -------------|-----|---------- 2048 -------------|
(core2sbr delay ) ds (read, core and ds area)
*/
static float
inputBuffer[(AACENC_BLOCKSIZE * 2 + MAX_DS_FILTER_DELAY +
INPUT_DELAY) * MAX_CHANNELS];
char outputBuffer[ADTS_HEADER_SIZE + (6144 / 8) * MAX_CHANNELS];
static IIR21_RESAMPLER IIR21_reSampler[MAX_CHANNELS];
int main(int argc, char *argv[])
{
AACENC_CONFIG config;
WavInfo inputInfo;
FILE *inputFile;
FILE *hADTSFile;
int error;
int bitrate;
int nChannelsAAC, nChannelsSBR;
unsigned int sampleRateAAC;
int frmCnt;
int bandwidth = 0;
unsigned int numAncDataBytes = 0;
unsigned char ancDataBytes[MAX_PAYLOAD_SIZE];
unsigned int ancDataLength = 0;
/*!< required only for interfacing with audio output library, thus not counted into RAM requirements */
short TimeDataPcm[AACENC_BLOCKSIZE * 2 * MAX_CHANNELS];
int numSamplesRead;
int bDoIIR2Downsample = 0;
int bDingleRate = 0;
int useParametricStereo = 0;
int coreWriteOffset = 0;
int coreReadOffset = 0;
int envWriteOffset = 0;
int envReadOffset = 0;
int writeOffset = INPUT_DELAY * MAX_CHANNELS;
int percent = -1;
struct AAC_ENCODER *aacEnc = 0;
int bDoUpsample = 0;
int upsampleReadOffset = 0;
int inSamples;
int bDoIIR32Resample = 0;
int nSamplesPerChannel;
const int nRuns = 4;
float *resamplerScratch = sbr_envRBuffer;
HANDLE_SBR_ENCODER hEnvEnc = NULL;
/* initialize the 3GPP instrumenting tool */
fprintf(stderr,
"\n"
"*************************************************************\n"
"* Enhanced aacPlus Encoder\n"
"* Build %s, %s\n"
"* Matteo Croce <[email protected]>\n"
"*************************************************************\n\n",
__DATE__, __TIME__);
/*
* parse command line arguments
*/
if (argc != 4) {
fprintf(stderr,
"\nUsage: %s <source.wav> <destination.aac> <bitrate>\n",
argv[0]);
fprintf(stderr,
"\nUse - as filename for stdin and/or stdout.\n");
fprintf(stderr, "\nExample: %s song.wav song.aac 32\n",
argv[0]);
return 0;
}
bitrate = atoi(argv[3]) * 1000;;
fflush(stderr);
/*
open audio input file
*/
inputFile = AuChannelOpen(argv[1], &inputInfo);
if (!inputFile) {
fprintf(stderr, "could not open %s\n", argv[1]);
exit(10);
}
/*
set up basic parameters for aacPlus codec
*/
AacInitDefaultConfig(&config);
nChannelsAAC = nChannelsSBR = inputInfo.nChannels;
if ((inputInfo.nChannels == 2) && (bitrate >= 16000)
&& (bitrate < 44001))
useParametricStereo = 1;
if (useParametricStereo) {
nChannelsAAC = 1;
nChannelsSBR = 2;
}
if (((inputInfo.sampleRate == 48000) && (nChannelsAAC == 2)
&& (bitrate < 24000))
|| ((inputInfo.sampleRate == 48000) && (nChannelsAAC == 1)
&& (bitrate < 12000))
)
bDoIIR32Resample = 1;
if (inputInfo.sampleRate == 16000) {
bDoUpsample = 1;
inputInfo.sampleRate = 32000;
bDingleRate = 1;
}
sampleRateAAC = inputInfo.sampleRate;
if (bDoIIR32Resample)
sampleRateAAC = 32000;
config.bitRate = bitrate;
config.nChannelsIn = inputInfo.nChannels;
config.nChannelsOut = nChannelsAAC;
config.bandWidth = bandwidth;
/*
set up SBR configuration
*/
if (!IsSbrSettingAvail
(bitrate, nChannelsAAC, sampleRateAAC, &sampleRateAAC)) {
fprintf(stderr,
"No valid SBR configuration found for:\n\t\t\tbitrate=%d\n\t\t\tchannels=%d\n\t\t\tsamplerate=%d\n",
bitrate, nChannelsAAC, sampleRateAAC);
exit(10);
}
{
sbrConfiguration sbrConfig;
envReadOffset = 0;
coreWriteOffset = 0;
if (useParametricStereo) {
envReadOffset =
(MAX_DS_FILTER_DELAY + INPUT_DELAY) * MAX_CHANNELS;
coreWriteOffset = CORE_INPUT_OFFSET_PS;
writeOffset = envReadOffset;
}
InitializeSbrDefaults(&sbrConfig);
sbrConfig.usePs = useParametricStereo;
AdjustSbrSettings(&sbrConfig,
bitrate,
nChannelsAAC,
sampleRateAAC, AACENC_TRANS_FAC, 24000);
EnvOpen(&hEnvEnc,
inputBuffer + coreWriteOffset,
&sbrConfig, &config.bandWidth);
/* set IIR 2:1 downsampling */
bDoIIR2Downsample = (bDoUpsample) ? 0 : 1;
if (useParametricStereo)
bDoIIR2Downsample = 0;
}
/*
set up mode-dependant 1:2 upsampling
*/
if (bDoUpsample) {
if (inputInfo.nChannels > 1) {
fprintf(stderr,
"\n Stereo @ 16kHz input sample rate is not supported\n");
return -1;
}
InitIIR21_Resampler(IIR21_reSampler);
InitIIR21_Resampler(IIR21_reSampler + 1);
assert(IIR21_reSampler[0].delay <= MAX_DS_FILTER_DELAY);
if (useParametricStereo) {
writeOffset += AACENC_BLOCKSIZE * MAX_CHANNELS;
upsampleReadOffset = writeOffset;
envWriteOffset = envReadOffset;
} else {
writeOffset += AACENC_BLOCKSIZE * MAX_CHANNELS;
coreReadOffset = writeOffset;
upsampleReadOffset = writeOffset - (((INPUT_DELAY - IIR21_reSampler[0].delay) >> 1) * MAX_CHANNELS);
envWriteOffset = ((INPUT_DELAY - IIR21_reSampler[0].delay) & 0x1) * MAX_CHANNELS;
envReadOffset = 0;
}
} else if (bDoIIR2Downsample) {
/*
set up 2:1 downsampling
*/
InitIIR21_Resampler(IIR21_reSampler);
InitIIR21_Resampler(IIR21_reSampler + 1);
assert(IIR21_reSampler[0].delay <= MAX_DS_FILTER_DELAY);
writeOffset += IIR21_reSampler[0].delay * MAX_CHANNELS;
}
if (bDoIIR32Resample)
IIR32Init();
/*
set up AAC encoder, now that samling rate is known
*/
config.sampleRate = sampleRateAAC;
error = AacEncOpen(&aacEnc, config);
if (error) {
fprintf(stderr, "\n Initialisation of AAC failed !\n");
AacEncClose(aacEnc);
AuChannelClose(inputFile);
return 1;
}
if (strcmp(argv[2], "-") == 0)
hADTSFile = stdout;
else
hADTSFile = fopen(argv[2], "wb");
if (!hADTSFile) {
fprintf(stderr, "\nFailed to create ADTS file\n");
exit(10);
}
/*
Be verbose
*/
fprintf(stderr, "input file %s: \nsr = %d, nc = %d\n\n", argv[1],
inputInfo.sampleRate, inputInfo.nChannels);
fprintf(stderr, "output file %s: \nbr = %d sr-OUT = %d nc-OUT = %d\n\n",
argv[2], bitrate, sampleRateAAC * 2, nChannelsSBR);
fflush(stderr);
init_plans();
frmCnt = 0;
memset(TimeDataPcm, 0, sizeof(TimeDataPcm));
/*
set up input samples block size feed
*/
if (bDoIIR32Resample) {
inSamples = IIR32GetResamplerFeed(AACENC_BLOCKSIZE * inputInfo.nChannels * 2) / nRuns;
assert(inSamples <= AACENC_BLOCKSIZE * 2);
} else {
inSamples = AACENC_BLOCKSIZE * inputInfo.nChannels * 2;
if (bDoUpsample)
inSamples = inSamples >> 1;
}
/* create the ADTS header */
adts_hdr(outputBuffer, &config);
/*
The frame loop
*/
while (1) {
int i, ch, outSamples, numOutBytes, newpercent = -1;
/*
File input read, resample and downmix
*/
if (bDoIIR32Resample) {
/* resampling from 48 kHz to 32 kHz prior to encoding */
int stopLoop = 0;
int nSamplesProcessed = 0;
int r;
const int nDSOutBlockSize = AACENC_BLOCKSIZE * 2 / nRuns;
int stride = inputInfo.nChannels;
if (inputInfo.nChannels == 1)
stride = 2;
/* counting previous operations */
for (r = 0; r < nRuns; r++) {
if (AuChannelReadShort(inputFile, TimeDataPcm, inSamples, &numSamplesRead)) {
stopLoop = 1;
break;
}
/* copy from short to float input buffer */
/* resamplerScratch[]
TimeDataPcm[]
*/
for (i = 0; i < numSamplesRead; i++)
resamplerScratch[i] = (float)TimeDataPcm[i];
switch (inputInfo.nChannels) {
case 1:
nSamplesPerChannel = numSamplesRead;
break;
case 2:
nSamplesPerChannel = numSamplesRead >> 1;
break;
default:
nSamplesPerChannel = numSamplesRead / inputInfo.nChannels;
}
/* supposed to return exact number samples to encode one block of audio */
nSamplesProcessed +=
IIR32Resample(resamplerScratch,
&inputBuffer[writeOffset + r * nDSOutBlockSize * stride],
nSamplesPerChannel,
nDSOutBlockSize,
inputInfo.nChannels);
if (inputInfo.nChannels == 1 && stride == 2)
/* reordering necessary since the encoder takes interleaved data */
for (i = nDSOutBlockSize - 1; i >= 0; i--)
inputBuffer[writeOffset + r * nDSOutBlockSize * 2 + 2 * i] =
inputBuffer[writeOffset + r * nDSOutBlockSize * 2 + i];
}
if (stopLoop)
break;
numSamplesRead = nSamplesProcessed;
} else {
/* no resampling prior to encoding required */
/* read from file */
if (AuChannelReadShort(inputFile, TimeDataPcm,
inSamples, &numSamplesRead))
break;
/* copy from short to float input buffer */
/* inputBuffer[i+writeOffset]
TimeDataPcm[i]
*/
if (inputInfo.nChannels == nChannelsSBR)
for (i = 0; i < numSamplesRead; i++)
inputBuffer[i + writeOffset] = (float)TimeDataPcm[i];
/* copy from short to float input buffer, reordering necessary since the encoder takes interleaved data */
if (inputInfo.nChannels == 1)
/* inputBuffer[writeOffset+MAX_CHANNELS*i]
inputBuffer[writeOffset+i]
*/
for (i = 0; i < numSamplesRead; i++)
inputBuffer[writeOffset + 2 * i] = (float)TimeDataPcm[i];
/* copy from short to float input buffer, downmix stereo input signal to mono, reordering necessary since the encoder takes interleaved data */
if ((inputInfo.nChannels == 2) && bEncodeMono)
/* inputBuffer[writeOffset+2*i]
inputBuffer[writeOffset+2*i+1]
*/
for (i = 0; i < numSamplesRead / 2; i++)
inputBuffer[writeOffset + 2 * i] = ((float)TimeDataPcm[2 * i] + (float)TimeDataPcm[2 * i + 1]) * 0.5f;
} /*end if (bDoIIR32Resample) */
if (bDoUpsample)
for (ch = 0; ch < inputInfo.nChannels; ch++)
IIR21_Upsample(&(IIR21_reSampler[ch]),
inputBuffer +
upsampleReadOffset + ch,
numSamplesRead /
inputInfo.nChannels,
MAX_CHANNELS,
inputBuffer + envWriteOffset +
ch, &outSamples, MAX_CHANNELS);
/*
encode one SBR frame
*/
EnvEncodeFrame(hEnvEnc,
inputBuffer + envReadOffset,
inputBuffer + coreWriteOffset,
MAX_CHANNELS, &numAncDataBytes, ancDataBytes);
/*
2:1 downsampling for AAC core
*/
if (bDoIIR2Downsample)
for (ch = 0; ch < nChannelsAAC; ch++)
IIR21_Downsample(&(IIR21_reSampler[ch]),
inputBuffer + writeOffset + ch,
numSamplesRead /
inputInfo.nChannels,
MAX_CHANNELS, inputBuffer + ch,
&outSamples, MAX_CHANNELS);
/* SBR side info data is passed as ancillary data */
if (numAncDataBytes == 0)
numAncDataBytes = ancDataLength;
/*
encode one AAC frame
*/
if (hEnvEnc && useParametricStereo) {
AacEncEncode(aacEnc, inputBuffer, 1, /* stride */
ancDataBytes,
&numAncDataBytes,
(unsigned *)(outputBuffer + ADTS_HEADER_SIZE),
&numOutBytes);
if (hEnvEnc)
memcpy(inputBuffer,
inputBuffer + AACENC_BLOCKSIZE,
CORE_INPUT_OFFSET_PS * sizeof(float));
} else {
AacEncEncode(aacEnc,
inputBuffer + coreReadOffset,
MAX_CHANNELS,
ancDataBytes,
&numAncDataBytes,
(unsigned *)(outputBuffer + ADTS_HEADER_SIZE),
&numOutBytes);
if (hEnvEnc) {
if (bDoUpsample) {
memmove(&inputBuffer[envReadOffset],
&inputBuffer[envReadOffset + AACENC_BLOCKSIZE * MAX_CHANNELS * 2],
(envWriteOffset - envReadOffset) *
sizeof(float));
memmove(&inputBuffer
[upsampleReadOffset],
&inputBuffer[upsampleReadOffset + AACENC_BLOCKSIZE * MAX_CHANNELS],
(writeOffset - upsampleReadOffset) *
sizeof(float));
} else
memmove(inputBuffer,
inputBuffer +
AACENC_BLOCKSIZE * 2 *
MAX_CHANNELS,
writeOffset * sizeof(float));
}
}
/*
Write one frame of encoded audio to file
*/
if (numOutBytes) {
adts_hdr_up(outputBuffer, numOutBytes);
fwrite(outputBuffer, 1, numOutBytes + ADTS_HEADER_SIZE, hADTSFile);
}
frmCnt++;
/* 3GPP instrumenting tool: measure worst case work load at end of each decoding loop */
if (inputInfo.nSamples > 0) {
newpercent = frmCnt * AACENC_BLOCKSIZE * inputInfo.nChannels / (inputInfo.nSamples / (4 * 100));
}
if (newpercent != percent) {
percent = newpercent;
fprintf(stderr, "[%d%%]\r", newpercent);
fflush(stderr);
}
}
fprintf(stderr, "\n");
fflush(stderr);
/*
Close encoder
*/
if (numSamplesRead > 0) {
AacEncClose(aacEnc);
}
AuChannelClose(inputFile);
destroy_plans();
if (hEnvEnc)
EnvClose(hEnvEnc);
/* 3GPP instrumenting tool: print output data */
fprintf(stderr, "\nencoding finished\n");
fflush(stderr);
return 0;
}