-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaacdec.c
558 lines (481 loc) · 17.2 KB
/
aacdec.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
556
557
/* ***** BEGIN LICENSE BLOCK *****
* Source last modified: $Id: aacdec.c,v 1.1 2005/02/26 01:47:31 jrecker Exp $
*
* Portions Copyright (c) 1995-2005 RealNetworks, Inc. All Rights Reserved.
*
* The contents of this file, and the files included with this file,
* are subject to the current version of the RealNetworks Public
* Source License (the "RPSL") available at
* http://www.helixcommunity.org/content/rpsl unless you have licensed
* the file under the current version of the RealNetworks Community
* Source License (the "RCSL") available at
* http://www.helixcommunity.org/content/rcsl, in which case the RCSL
* will apply. You may also obtain the license terms directly from
* RealNetworks. You may not use this file except in compliance with
* the RPSL or, if you have a valid RCSL with RealNetworks applicable
* to this file, the RCSL. Please see the applicable RPSL or RCSL for
* the rights, obligations and limitations governing use of the
* contents of the file.
*
* This file is part of the Helix DNA Technology. RealNetworks is the
* developer of the Original Code and owns the copyrights in the
* portions it created.
*
* This file, and the files included with this file, is distributed
* and made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY
* KIND, EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS
* ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET
* ENJOYMENT OR NON-INFRINGEMENT.
*
* Technology Compatibility Kit Test Suite(s) Location:
* http://www.helixcommunity.org/content/tck
*
* Contributor(s):
*
* ***** END LICENSE BLOCK ***** */
/**************************************************************************************
* Fixed-point HE-AAC decoder
* Jon Recker ([email protected]), Ken Cooke ([email protected])
* February 2005
*
* aacdec.c - platform-independent top level decoder API
**************************************************************************************/
#include "aaccommon.h"
//#include "profile.h"
#define PROFILE_START(x)
#define PROFILE_END()
/**************************************************************************************
* Function: AACInitDecoder
*
* Description: allocate memory for platform-specific data
* clear all the user-accessible fields
* initialize SBR decoder if enabled
*
* Inputs: none
*
* Outputs: none
*
* Return: handle to AAC decoder instance, 0 if malloc fails
**************************************************************************************/
HAACDecoder AACInitDecoder(void)
{
AACDecInfo *aacDecInfo;
aacDecInfo = AllocateBuffers();
if (!aacDecInfo)
return 0;
#ifdef AAC_ENABLE_SBR
if (InitSBR(aacDecInfo)) {
AACFreeDecoder(aacDecInfo);
return 0;
}
#endif
return (HAACDecoder)aacDecInfo;
}
HAACDecoder AACInitDecoderPre(void *ptr, int sz)
{
AACDecInfo *aacDecInfo;
aacDecInfo = AllocateBuffersPre(&ptr, &sz);
if (!aacDecInfo)
return 0;
#ifdef AAC_ENABLE_SBR
if (InitSBRPre(aacDecInfo, &ptr, &sz)) {
return 0;
}
#endif
return (HAACDecoder)aacDecInfo;
}
/**************************************************************************************
* Function: AACFreeDecoder
*
* Description: free platform-specific data allocated by AACInitDecoder
* free SBR decoder if enabled
*
* Inputs: valid AAC decoder instance pointer (HAACDecoder)
*
* Outputs: none
*
* Return: none
**************************************************************************************/
void AACFreeDecoder(HAACDecoder hAACDecoder)
{
AACDecInfo *aacDecInfo = (AACDecInfo *)hAACDecoder;
if (!aacDecInfo)
return;
#ifdef AAC_ENABLE_SBR
FreeSBR(aacDecInfo);
#endif
FreeBuffers(aacDecInfo);
}
/**************************************************************************************
* Function: AACFindSyncWord
*
* Description: locate the next byte-alinged sync word in the raw AAC stream
*
* Inputs: buffer to search for sync word
* max number of bytes to search in buffer
*
* Outputs: none
*
* Return: offset to first sync word (bytes from start of buf)
* -1 if sync not found after searching nBytes
**************************************************************************************/
int AACFindSyncWord(unsigned char *buf, int nBytes)
{
int i;
/* find byte-aligned syncword (12 bits = 0xFFF) */
for (i = 0; i < nBytes - 1; i++) {
if ( (buf[i+0] & SYNCWORDH) == SYNCWORDH && (buf[i+1] & SYNCWORDL) == SYNCWORDL )
return i;
}
return -1;
}
/**************************************************************************************
* Function: AACGetLastFrameInfo
*
* Description: get info about last AAC frame decoded (number of samples decoded,
* sample rate, bit rate, etc.)
*
* Inputs: valid AAC decoder instance pointer (HAACDecoder)
* pointer to AACFrameInfo struct
*
* Outputs: filled-in AACFrameInfo struct
*
* Return: none
*
* Notes: call this right after calling AACDecode()
**************************************************************************************/
void AACGetLastFrameInfo(HAACDecoder hAACDecoder, AACFrameInfo *aacFrameInfo)
{
AACDecInfo *aacDecInfo = (AACDecInfo *)hAACDecoder;
if (!aacDecInfo) {
aacFrameInfo->bitRate = 0;
aacFrameInfo->nChans = 0;
aacFrameInfo->sampRateCore = 0;
aacFrameInfo->sampRateOut = 0;
aacFrameInfo->bitsPerSample = 0;
aacFrameInfo->outputSamps = 0;
aacFrameInfo->profile = 0;
aacFrameInfo->tnsUsed = 0;
aacFrameInfo->pnsUsed = 0;
} else {
aacFrameInfo->bitRate = aacDecInfo->bitRate;
aacFrameInfo->nChans = aacDecInfo->nChans;
aacFrameInfo->sampRateCore = aacDecInfo->sampRate;
aacFrameInfo->sampRateOut = aacDecInfo->sampRate * (aacDecInfo->sbrEnabled ? 2 : 1);
aacFrameInfo->bitsPerSample = 16;
aacFrameInfo->outputSamps = aacDecInfo->nChans * AAC_MAX_NSAMPS * (aacDecInfo->sbrEnabled ? 2 : 1);
aacFrameInfo->profile = aacDecInfo->profile;
aacFrameInfo->tnsUsed = aacDecInfo->tnsUsed;
aacFrameInfo->pnsUsed = aacDecInfo->pnsUsed;
}
}
/**************************************************************************************
* Function: AACSetRawBlockParams
*
* Description: set internal state variables for decoding a stream of raw data blocks
*
* Inputs: valid AAC decoder instance pointer (HAACDecoder)
* flag indicating source of parameters
* AACFrameInfo struct, with the members nChans, sampRate, and profile
* optionally filled-in
*
* Outputs: updated codec state
*
* Return: 0 if successful, error code (< 0) if error
*
* Notes: if copyLast == 1, then the codec sets up its internal state (for
* decoding raw blocks) based on previously-decoded ADTS header info
* if copyLast == 0, then the codec uses the values passed in
* aacFrameInfo to configure its internal state (useful when the
* source is MP4 format, for example)
**************************************************************************************/
int AACSetRawBlockParams(HAACDecoder hAACDecoder, int copyLast, AACFrameInfo *aacFrameInfo)
{
AACDecInfo *aacDecInfo = (AACDecInfo *)hAACDecoder;
if (!aacDecInfo)
return ERR_AAC_NULL_POINTER;
aacDecInfo->format = AAC_FF_RAW;
if (copyLast)
return SetRawBlockParams(aacDecInfo, 1, 0, 0, 0);
else
return SetRawBlockParams(aacDecInfo, 0, aacFrameInfo->nChans, aacFrameInfo->sampRateCore, aacFrameInfo->profile);
}
/**************************************************************************************
* Function: AACSetFormat
*
* Description: set internal state variables for decoding a stream of raw data blocks
*
* Inputs: valid AAC decoder instance pointer (HAACDecoder)
* AAC transport format
*
* Outputs: updated codec state
*
* Return: 0 if successful, error code (< 0) if error
*
**************************************************************************************/
int AACSetFormat(HAACDecoder hAACDecoder, int format)
{
AACDecInfo *aacDecInfo = (AACDecInfo *)hAACDecoder;
if (!aacDecInfo)
return ERR_AAC_NULL_POINTER;
aacDecInfo->format = format;
return ERR_AAC_NONE;
}
/**************************************************************************************
* Function: AACFlushCodec
*
* Description: flush internal codec state (after seeking, for example)
*
* Inputs: valid AAC decoder instance pointer (HAACDecoder)
*
* Outputs: updated state variables in aacDecInfo
*
* Return: 0 if successful, error code (< 0) if error
**************************************************************************************/
int AACFlushCodec(HAACDecoder hAACDecoder)
{
int ch;
AACDecInfo *aacDecInfo = (AACDecInfo *)hAACDecoder;
if (!aacDecInfo)
return ERR_AAC_NULL_POINTER;
/* reset common state variables which change per-frame
* don't touch state variables which are (usually) constant for entire clip
* (nChans, sampRate, profile, format, sbrEnabled)
*/
aacDecInfo->prevBlockID = AAC_ID_INVALID;
aacDecInfo->currBlockID = AAC_ID_INVALID;
aacDecInfo->currInstTag = -1;
for (ch = 0; ch < MAX_NCHANS_ELEM; ch++)
aacDecInfo->sbDeinterleaveReqd[ch] = 0;
aacDecInfo->adtsBlocksLeft = 0;
aacDecInfo->tnsUsed = 0;
aacDecInfo->pnsUsed = 0;
/* reset internal codec state (flush overlap buffers, etc.) */
FlushCodec(aacDecInfo);
#ifdef AAC_ENABLE_SBR
FlushCodecSBR(aacDecInfo);
#endif
return ERR_AAC_NONE;
}
/**************************************************************************************
* Function: AACGetFrameLength
*
* Description: find and get AAC frame length
*
* Inputs: valid AAC decoder instance pointer (HAACDecoder)
* double pointer to buffer of AAC data
* data bytes in inbuf
* pointer to return frame length
*
* Outputs: updated inbuf pointer
* updated bytesLeft
* updated bytesFrames
*
* Return: 0 if successful, error code (< 0) if error
*
**************************************************************************************/
int AACGetFrameLength(HAACDecoder hAACDecoder, unsigned char **inbuf, int bytes, int *bytesFrames)
{
AACDecInfo *aacDecInfo = (AACDecInfo *)hAACDecoder;
int err = ERR_AAC_MPEG4_UNSUPPORTED;
int offset, bitOffset, bitsAvail;
unsigned char *inptr;
if (!aacDecInfo)
return ERR_AAC_NULL_POINTER;
if (aacDecInfo->format != AAC_FF_ADTS
&& aacDecInfo->format != AAC_FF_LATM_MCP1)
return ERR_AAC_MPEG4_UNSUPPORTED;
*bytesFrames = 0;
inptr = *inbuf;
bitOffset = 0;
bitsAvail = (bytes) << 3;
if (aacDecInfo->format == AAC_FF_ADTS) {
offset = AACFindSyncWord(inptr, bytes);
if (offset < 0) {
*bytesFrames = ADTS_HEADER_BYTES;
return ERR_AAC_NO_SYNCWORD;
}
inptr += offset;
bitsAvail -= (offset << 3);
*inbuf = inptr;
err = GetADTSFrameLength(inptr, bitsAvail, bytesFrames);
} else if (aacDecInfo->format == AAC_FF_LATM_MCP1)
err = UnpackLATMHeader(aacDecInfo, &inptr, &bitOffset, &bitsAvail, bytesFrames);
return err;
}
/**************************************************************************************
* Function: AACDecode
*
* Description: decode AAC frame
*
* Inputs: valid AAC decoder instance pointer (HAACDecoder)
* double pointer to buffer of AAC data
* pointer to number of valid bytes remaining in inbuf
* pointer to outbuf, big enough to hold one frame of decoded PCM samples
* (outbuf must be double-sized if SBR enabled)
*
* Outputs: PCM data in outbuf, interleaved LRLRLR... if stereo
* number of output samples = 1024 per channel (2048 if SBR enabled)
* updated inbuf pointer
* updated bytesLeft
*
* Return: 0 if successful, error code (< 0) if error
*
* Notes: inbuf pointer and bytesLeft are not updated until whole frame is
* successfully decoded, so if ERR_AAC_INDATA_UNDERFLOW is returned
* just call AACDecode again with more data in inbuf
**************************************************************************************/
int AACDecode(HAACDecoder hAACDecoder, unsigned char **inbuf, int *bytesLeft, short *outbuf)
{
int err, offset, bitOffset, bitsAvail;
int ch, baseChan, elementChans;
unsigned char *inptr;
AACDecInfo *aacDecInfo = (AACDecInfo *)hAACDecoder;
#ifdef AAC_ENABLE_SBR
int baseChanSBR, elementChansSBR;
#endif
if (!aacDecInfo)
return ERR_AAC_NULL_POINTER;
/* make local copies (see "Notes" above) */
inptr = *inbuf;
bitOffset = 0;
bitsAvail = (*bytesLeft) << 3;
/* first time through figure out what the file format is */
if (aacDecInfo->format == AAC_FF_Unknown) {
if (bitsAvail < 32)
return ERR_AAC_INDATA_UNDERFLOW;
if (IS_ADIF(inptr)) {
/* unpack ADIF header */
aacDecInfo->format = AAC_FF_ADIF;
err = UnpackADIFHeader(aacDecInfo, &inptr, &bitOffset, &bitsAvail);
if (err)
return err;
} else {
/* assume ADTS by default */
aacDecInfo->format = AAC_FF_ADTS;
}
}
/* if ADTS, search for start of next frame */
if (aacDecInfo->format == AAC_FF_ADTS) {
/* can have 1-4 raw data blocks per ADTS frame (header only present for first one) */
if (aacDecInfo->adtsBlocksLeft == 0) {
offset = AACFindSyncWord(inptr, bitsAvail >> 3);
if (offset < 0)
return ERR_AAC_INDATA_UNDERFLOW;
inptr += offset;
bitsAvail -= (offset << 3);
err = UnpackADTSHeader(aacDecInfo, &inptr, &bitOffset, &bitsAvail, NULL);
if (err)
return err;
if (aacDecInfo->nChans == -1) {
/* figure out implicit channel mapping if necessary */
err = GetADTSChannelMapping(aacDecInfo, inptr, bitOffset, bitsAvail);
if (err)
return err;
}
}
aacDecInfo->adtsBlocksLeft--;
} else if (aacDecInfo->format == AAC_FF_RAW) {
err = PrepareRawBlock(aacDecInfo);
if (err)
return err;
} else if (aacDecInfo->format == AAC_FF_LATM_MCP1) {
err = UnpackLATMHeader(aacDecInfo, &inptr, &bitOffset, &bitsAvail, NULL);
if (err)
return err;
}
/* check for valid number of channels */
if (aacDecInfo->nChans > AAC_MAX_NCHANS || aacDecInfo->nChans <= 0)
return ERR_AAC_NCHANS_TOO_HIGH;
/* will be set later if active in this frame */
aacDecInfo->tnsUsed = 0;
aacDecInfo->pnsUsed = 0;
bitOffset = 0;
baseChan = 0;
#ifdef AAC_ENABLE_SBR
baseChanSBR = 0;
#endif
do {
/* parse next syntactic element */
err = DecodeNextElement(aacDecInfo, &inptr, &bitOffset, &bitsAvail);
if (err)
return err;
elementChans = elementNumChans[aacDecInfo->currBlockID];
if (baseChan + elementChans > AAC_MAX_NCHANS)
return ERR_AAC_NCHANS_TOO_HIGH;
/* noiseless decoder and dequantizer */
for (ch = 0; ch < elementChans; ch++) {
PROFILE_START("noiseless decoder");
err = DecodeNoiselessData(aacDecInfo, &inptr, &bitOffset, &bitsAvail, ch);
PROFILE_END();
if (err)
return err;
PROFILE_START("dequant");
if (Dequantize(aacDecInfo, ch))
return ERR_AAC_DEQUANT;
PROFILE_END();
}
PROFILE_START("mid-side and intensity stereo");
/* mid-side and intensity stereo */
if (aacDecInfo->currBlockID == AAC_ID_CPE) {
if (StereoProcess(aacDecInfo))
return ERR_AAC_STEREO_PROCESS;
}
PROFILE_END();
/* PNS, TNS, inverse transform */
for (ch = 0; ch < elementChans; ch++) {
PROFILE_START("PNS");
if (PNS(aacDecInfo, ch))
return ERR_AAC_PNS;
PROFILE_END();
if (aacDecInfo->sbDeinterleaveReqd[ch]) {
/* deinterleave short blocks, if required */
if (DeinterleaveShortBlocks(aacDecInfo, ch))
return ERR_AAC_SHORT_BLOCK_DEINT;
aacDecInfo->sbDeinterleaveReqd[ch] = 0;
}
PROFILE_START("TNS");
if (TNSFilter(aacDecInfo, ch))
return ERR_AAC_TNS;
PROFILE_END();
PROFILE_START("IMDCT");
if (IMDCT(aacDecInfo, ch, baseChan + ch, outbuf))
return ERR_AAC_IMDCT;
PROFILE_END();
}
#ifdef AAC_ENABLE_SBR
if (aacDecInfo->sbrEnabled && (aacDecInfo->currBlockID == AAC_ID_FIL || aacDecInfo->currBlockID == AAC_ID_LFE)) {
if (aacDecInfo->currBlockID == AAC_ID_LFE)
elementChansSBR = elementNumChans[AAC_ID_LFE];
else if (aacDecInfo->currBlockID == AAC_ID_FIL && (aacDecInfo->prevBlockID == AAC_ID_SCE || aacDecInfo->prevBlockID == AAC_ID_CPE))
elementChansSBR = elementNumChans[aacDecInfo->prevBlockID];
else
elementChansSBR = 0;
if (baseChanSBR + elementChansSBR > AAC_MAX_NCHANS)
return ERR_AAC_SBR_NCHANS_TOO_HIGH;
/* parse SBR extension data if present (contained in a fill element) */
if (DecodeSBRBitstream(aacDecInfo, baseChanSBR))
return ERR_AAC_SBR_BITSTREAM;
/* apply SBR */
if (DecodeSBRData(aacDecInfo, baseChanSBR, outbuf))
return ERR_AAC_SBR_DATA;
baseChanSBR += elementChansSBR;
}
#endif
baseChan += elementChans;
} while (aacDecInfo->currBlockID != AAC_ID_END);
/* byte align after each raw_data_block */
if (bitOffset) {
inptr++;
bitsAvail -= (8-bitOffset);
bitOffset = 0;
if (bitsAvail < 0)
return ERR_AAC_INDATA_UNDERFLOW;
}
/* update pointers */
aacDecInfo->frameCount++;
*bytesLeft -= (inptr - *inbuf);
*inbuf = inptr;
return ERR_AAC_NONE;
}