00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include "common.h"
00034 #include "structs.h"
00035
00036 #include <stdlib.h>
00037 #include <string.h>
00038
00039 #include "decoder.h"
00040 #include "mp4.h"
00041 #include "syntax.h"
00042 #include "error.h"
00043 #include "output.h"
00044 #include "filtbank.h"
00045 #include "drc.h"
00046 #ifdef SBR_DEC
00047 #include "sbr_dec.h"
00048 #include "sbr_syntax.h"
00049 #endif
00050 #ifdef SSR_DEC
00051 #include "ssr.h"
00052 #endif
00053
00054 #ifdef ANALYSIS
00055 uint16_t dbg_count;
00056 #endif
00057
00058
00059 static void* aac_frame_decode(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo,
00060 uint8_t *buffer, uint32_t buffer_size,
00061 void **sample_buffer, uint32_t sample_buffer_size);
00062 static void create_channel_config(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo);
00063
00064
00065 char* NEAACDECAPI NeAACDecGetErrorMessage(uint8_t errcode)
00066 {
00067 if (errcode >= NUM_ERROR_MESSAGES)
00068 return NULL;
00069 return err_msg[errcode];
00070 }
00071
00072 uint32_t NEAACDECAPI NeAACDecGetCapabilities(void)
00073 {
00074 uint32_t cap = 0;
00075
00076
00077 cap += LC_DEC_CAP;
00078
00079 #ifdef MAIN_DEC
00080 cap += MAIN_DEC_CAP;
00081 #endif
00082 #ifdef LTP_DEC
00083 cap += LTP_DEC_CAP;
00084 #endif
00085 #ifdef LD_DEC
00086 cap += LD_DEC_CAP;
00087 #endif
00088 #ifdef ERROR_RESILIENCE
00089 cap += ERROR_RESILIENCE_CAP;
00090 #endif
00091 #ifdef FIXED_POINT
00092 cap += FIXED_POINT_CAP;
00093 #endif
00094
00095 return cap;
00096 }
00097
00098 NeAACDecHandle NEAACDECAPI NeAACDecOpen(void)
00099 {
00100 uint8_t i;
00101 NeAACDecHandle hDecoder = NULL;
00102
00103 if ((hDecoder = (NeAACDecHandle)faad_malloc(sizeof(NeAACDecStruct))) == NULL)
00104 return NULL;
00105
00106 memset(hDecoder, 0, sizeof(NeAACDecStruct));
00107
00108 hDecoder->config.outputFormat = FAAD_FMT_16BIT;
00109 hDecoder->config.defObjectType = MAIN;
00110 hDecoder->config.defSampleRate = 44100;
00111 hDecoder->config.downMatrix = 0;
00112 hDecoder->adts_header_present = 0;
00113 hDecoder->adif_header_present = 0;
00114 #ifdef ERROR_RESILIENCE
00115 hDecoder->aacSectionDataResilienceFlag = 0;
00116 hDecoder->aacScalefactorDataResilienceFlag = 0;
00117 hDecoder->aacSpectralDataResilienceFlag = 0;
00118 #endif
00119 hDecoder->frameLength = 1024;
00120
00121 hDecoder->frame = 0;
00122 hDecoder->sample_buffer = NULL;
00123
00124 for (i = 0; i < MAX_CHANNELS; i++)
00125 {
00126 hDecoder->window_shape_prev[i] = 0;
00127 hDecoder->time_out[i] = NULL;
00128 hDecoder->fb_intermed[i] = NULL;
00129 #ifdef SSR_DEC
00130 hDecoder->ssr_overlap[i] = NULL;
00131 hDecoder->prev_fmd[i] = NULL;
00132 #endif
00133 #ifdef MAIN_DEC
00134 hDecoder->pred_stat[i] = NULL;
00135 #endif
00136 #ifdef LTP_DEC
00137 hDecoder->ltp_lag[i] = 0;
00138 hDecoder->lt_pred_stat[i] = NULL;
00139 #endif
00140 }
00141
00142 #ifdef SBR_DEC
00143 for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
00144 {
00145 hDecoder->sbr[i] = NULL;
00146 }
00147 #endif
00148
00149 hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
00150
00151 return hDecoder;
00152 }
00153
00154 NeAACDecConfigurationPtr NEAACDECAPI NeAACDecGetCurrentConfiguration(NeAACDecHandle hDecoder)
00155 {
00156 if (hDecoder)
00157 {
00158 NeAACDecConfigurationPtr config = &(hDecoder->config);
00159
00160 return config;
00161 }
00162
00163 return NULL;
00164 }
00165
00166 uint8_t NEAACDECAPI NeAACDecSetConfiguration(NeAACDecHandle hDecoder,
00167 NeAACDecConfigurationPtr config)
00168 {
00169 if (hDecoder && config)
00170 {
00171
00172 if (can_decode_ot(config->defObjectType) < 0)
00173 return 0;
00174 hDecoder->config.defObjectType = config->defObjectType;
00175
00176
00177 if (config->defSampleRate == 0)
00178 return 0;
00179 hDecoder->config.defSampleRate = config->defSampleRate;
00180
00181
00182 #ifdef FIXED_POINT
00183 if ((config->outputFormat < 1) || (config->outputFormat > 4))
00184 return 0;
00185 #else
00186 if ((config->outputFormat < 1) || (config->outputFormat > 5))
00187 return 0;
00188 #endif
00189 hDecoder->config.outputFormat = config->outputFormat;
00190
00191 if (config->downMatrix > 1)
00192 return 0;
00193 hDecoder->config.downMatrix = config->downMatrix;
00194
00195
00196 return 1;
00197 }
00198
00199 return 0;
00200 }
00201
00202 int32_t NEAACDECAPI NeAACDecInit(NeAACDecHandle hDecoder, uint8_t *buffer,
00203 uint32_t buffer_size,
00204 uint32_t *samplerate, uint8_t *channels)
00205 {
00206 uint32_t bits = 0;
00207 bitfile ld;
00208 adif_header adif;
00209 adts_header adts;
00210
00211 if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL))
00212 return -1;
00213
00214 hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate);
00215 hDecoder->object_type = hDecoder->config.defObjectType;
00216 *samplerate = get_sample_rate(hDecoder->sf_index);
00217 *channels = 1;
00218
00219 if (buffer != NULL)
00220 {
00221 faad_initbits(&ld, buffer, buffer_size);
00222
00223
00224 if ((buffer[0] == 'A') && (buffer[1] == 'D') &&
00225 (buffer[2] == 'I') && (buffer[3] == 'F'))
00226 {
00227 hDecoder->adif_header_present = 1;
00228
00229 get_adif_header(&adif, &ld);
00230 faad_byte_align(&ld);
00231
00232 hDecoder->sf_index = adif.pce[0].sf_index;
00233 hDecoder->object_type = adif.pce[0].object_type + 1;
00234
00235 *samplerate = get_sample_rate(hDecoder->sf_index);
00236 *channels = adif.pce[0].channels;
00237
00238 memcpy(&(hDecoder->pce), &(adif.pce[0]), sizeof(program_config));
00239 hDecoder->pce_set = 1;
00240
00241 bits = bit2byte(faad_get_processed_bits(&ld));
00242
00243
00244 } else if (faad_showbits(&ld, 12) == 0xfff) {
00245 hDecoder->adts_header_present = 1;
00246
00247 adts.old_format = hDecoder->config.useOldADTSFormat;
00248 adts_frame(&adts, &ld);
00249
00250 hDecoder->sf_index = adts.sf_index;
00251 hDecoder->object_type = adts.profile + 1;
00252
00253 *samplerate = get_sample_rate(hDecoder->sf_index);
00254 *channels = (adts.channel_configuration > 6) ?
00255 2 : adts.channel_configuration;
00256 }
00257
00258 if (ld.error)
00259 {
00260 faad_endbits(&ld);
00261 return -1;
00262 }
00263 faad_endbits(&ld);
00264 }
00265 hDecoder->channelConfiguration = *channels;
00266
00267 #if (defined(PS_DEC) || defined(DRM_PS))
00268
00269 if (*channels == 1)
00270 {
00271
00272 *channels = 2;
00273 }
00274 #endif
00275
00276 #ifdef SBR_DEC
00277
00278 if (*samplerate <= 24000 && !(hDecoder->config.dontUpSampleImplicitSBR))
00279 {
00280 *samplerate *= 2;
00281 hDecoder->forceUpSampling = 1;
00282 } else if (*samplerate > 24000 && !(hDecoder->config.dontUpSampleImplicitSBR)) {
00283 hDecoder->downSampledSBR = 1;
00284 }
00285 #endif
00286
00287
00288 #ifdef SSR_DEC
00289 if (hDecoder->object_type == SSR)
00290 hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
00291 else
00292 #endif
00293 hDecoder->fb = filter_bank_init(hDecoder->frameLength);
00294
00295 #ifdef LD_DEC
00296 if (hDecoder->object_type == LD)
00297 hDecoder->frameLength >>= 1;
00298 #endif
00299
00300 if (can_decode_ot(hDecoder->object_type) < 0)
00301 return -1;
00302
00303 return bits;
00304 }
00305
00306
00307 int8_t NEAACDECAPI NeAACDecInit2(NeAACDecHandle hDecoder, uint8_t *pBuffer,
00308 uint32_t SizeOfDecoderSpecificInfo,
00309 uint32_t *samplerate, uint8_t *channels)
00310 {
00311 int8_t rc;
00312 mp4AudioSpecificConfig mp4ASC;
00313
00314 if((hDecoder == NULL)
00315 || (pBuffer == NULL)
00316 || (SizeOfDecoderSpecificInfo < 2)
00317 || (samplerate == NULL)
00318 || (channels == NULL))
00319 {
00320 return -1;
00321 }
00322
00323 hDecoder->adif_header_present = 0;
00324 hDecoder->adts_header_present = 0;
00325
00326
00327 rc = AudioSpecificConfig2(pBuffer, SizeOfDecoderSpecificInfo, &mp4ASC,
00328 &(hDecoder->pce));
00329
00330
00331 *samplerate = mp4ASC.samplingFrequency;
00332 if (mp4ASC.channelsConfiguration)
00333 {
00334 *channels = mp4ASC.channelsConfiguration;
00335 } else {
00336 *channels = hDecoder->pce.channels;
00337 hDecoder->pce_set = 1;
00338 }
00339 #if (defined(PS_DEC) || defined(DRM_PS))
00340
00341 if (*channels == 1)
00342 {
00343
00344 *channels = 2;
00345 }
00346 #endif
00347 hDecoder->sf_index = mp4ASC.samplingFrequencyIndex;
00348 hDecoder->object_type = mp4ASC.objectTypeIndex;
00349 #ifdef ERROR_RESILIENCE
00350 hDecoder->aacSectionDataResilienceFlag = mp4ASC.aacSectionDataResilienceFlag;
00351 hDecoder->aacScalefactorDataResilienceFlag = mp4ASC.aacScalefactorDataResilienceFlag;
00352 hDecoder->aacSpectralDataResilienceFlag = mp4ASC.aacSpectralDataResilienceFlag;
00353 #endif
00354 #ifdef SBR_DEC
00355 hDecoder->sbr_present_flag = mp4ASC.sbr_present_flag;
00356 hDecoder->downSampledSBR = mp4ASC.downSampledSBR;
00357 if (hDecoder->config.dontUpSampleImplicitSBR == 0)
00358 hDecoder->forceUpSampling = mp4ASC.forceUpSampling;
00359 else
00360 hDecoder->forceUpSampling = 0;
00361
00362
00363 if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || hDecoder->forceUpSampling == 1)
00364 {
00365 hDecoder->sf_index = get_sr_index(mp4ASC.samplingFrequency / 2);
00366 }
00367 #endif
00368
00369 if (rc != 0)
00370 {
00371 return rc;
00372 }
00373 hDecoder->channelConfiguration = mp4ASC.channelsConfiguration;
00374 if (mp4ASC.frameLengthFlag)
00375 #ifdef ALLOW_SMALL_FRAMELENGTH
00376 hDecoder->frameLength = 960;
00377 #else
00378 return -1;
00379 #endif
00380
00381
00382 #ifdef SSR_DEC
00383 if (hDecoder->object_type == SSR)
00384 hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
00385 else
00386 #endif
00387 hDecoder->fb = filter_bank_init(hDecoder->frameLength);
00388
00389 #ifdef LD_DEC
00390 if (hDecoder->object_type == LD)
00391 hDecoder->frameLength >>= 1;
00392 #endif
00393
00394 return 0;
00395 }
00396
00397 #ifdef DRM
00398 int8_t NEAACDECAPI NeAACDecInitDRM(NeAACDecHandle *hDecoder, uint32_t samplerate,
00399 uint8_t channels)
00400 {
00401 if (hDecoder == NULL)
00402 return 1;
00403
00404 NeAACDecClose(*hDecoder);
00405
00406 *hDecoder = NeAACDecOpen();
00407
00408
00409 (*hDecoder)->config.defObjectType = DRM_ER_LC;
00410
00411 (*hDecoder)->config.defSampleRate = samplerate;
00412 #ifdef ERROR_RESILIENCE // This shoudl always be defined for DRM
00413 (*hDecoder)->aacSectionDataResilienceFlag = 1;
00414 (*hDecoder)->aacScalefactorDataResilienceFlag = 0;
00415 (*hDecoder)->aacSpectralDataResilienceFlag = 1;
00416 #endif
00417 (*hDecoder)->frameLength = 960;
00418 (*hDecoder)->sf_index = get_sr_index((*hDecoder)->config.defSampleRate);
00419 (*hDecoder)->object_type = (*hDecoder)->config.defObjectType;
00420
00421 if ((channels == DRMCH_STEREO) || (channels == DRMCH_SBR_STEREO))
00422 (*hDecoder)->channelConfiguration = 2;
00423 else
00424 (*hDecoder)->channelConfiguration = 1;
00425
00426 #ifdef SBR_DEC
00427 if ((channels == DRMCH_MONO) || (channels == DRMCH_STEREO))
00428 (*hDecoder)->sbr_present_flag = 0;
00429 else
00430 (*hDecoder)->sbr_present_flag = 1;
00431 #endif
00432
00433 (*hDecoder)->fb = filter_bank_init((*hDecoder)->frameLength);
00434
00435 return 0;
00436 }
00437 #endif
00438
00439 void NEAACDECAPI NeAACDecClose(NeAACDecHandle hDecoder)
00440 {
00441 uint8_t i;
00442
00443 if (hDecoder == NULL)
00444 return;
00445
00446 #ifdef PROFILE
00447 printf("AAC decoder total: %I64d cycles\n", hDecoder->cycles);
00448 printf("requant: %I64d cycles\n", hDecoder->requant_cycles);
00449 printf("spectral_data: %I64d cycles\n", hDecoder->spectral_cycles);
00450 printf("scalefactors: %I64d cycles\n", hDecoder->scalefac_cycles);
00451 printf("output: %I64d cycles\n", hDecoder->output_cycles);
00452 #endif
00453
00454 for (i = 0; i < MAX_CHANNELS; i++)
00455 {
00456 if (hDecoder->time_out[i]) faad_free(hDecoder->time_out[i]);
00457 if (hDecoder->fb_intermed[i]) faad_free(hDecoder->fb_intermed[i]);
00458 #ifdef SSR_DEC
00459 if (hDecoder->ssr_overlap[i]) faad_free(hDecoder->ssr_overlap[i]);
00460 if (hDecoder->prev_fmd[i]) faad_free(hDecoder->prev_fmd[i]);
00461 #endif
00462 #ifdef MAIN_DEC
00463 if (hDecoder->pred_stat[i]) faad_free(hDecoder->pred_stat[i]);
00464 #endif
00465 #ifdef LTP_DEC
00466 if (hDecoder->lt_pred_stat[i]) faad_free(hDecoder->lt_pred_stat[i]);
00467 #endif
00468 }
00469
00470 #ifdef SSR_DEC
00471 if (hDecoder->object_type == SSR)
00472 ssr_filter_bank_end(hDecoder->fb);
00473 else
00474 #endif
00475 filter_bank_end(hDecoder->fb);
00476
00477 drc_end(hDecoder->drc);
00478
00479 if (hDecoder->sample_buffer) faad_free(hDecoder->sample_buffer);
00480
00481 #ifdef SBR_DEC
00482 for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
00483 {
00484 if (hDecoder->sbr[i])
00485 sbrDecodeEnd(hDecoder->sbr[i]);
00486 }
00487 #endif
00488
00489 if (hDecoder) faad_free(hDecoder);
00490 }
00491
00492 void NEAACDECAPI NeAACDecPostSeekReset(NeAACDecHandle hDecoder, int32_t frame)
00493 {
00494 if (hDecoder)
00495 {
00496 hDecoder->postSeekResetFlag = 1;
00497
00498 if (frame != -1)
00499 hDecoder->frame = frame;
00500 }
00501 }
00502
00503 static void create_channel_config(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo)
00504 {
00505 hInfo->num_front_channels = 0;
00506 hInfo->num_side_channels = 0;
00507 hInfo->num_back_channels = 0;
00508 hInfo->num_lfe_channels = 0;
00509 memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
00510
00511 if (hDecoder->downMatrix)
00512 {
00513 hInfo->num_front_channels = 2;
00514 hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
00515 hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
00516 return;
00517 }
00518
00519
00520 if (hDecoder->pce_set)
00521 {
00522 uint8_t i, chpos = 0;
00523 uint8_t chdir, back_center = 0;
00524
00525 hInfo->num_front_channels = hDecoder->pce.num_front_channels;
00526 hInfo->num_side_channels = hDecoder->pce.num_side_channels;
00527 hInfo->num_back_channels = hDecoder->pce.num_back_channels;
00528 hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
00529
00530 chdir = hInfo->num_front_channels;
00531 if (chdir & 1)
00532 {
00533 hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
00534 chdir--;
00535 }
00536 for (i = 0; i < chdir; i += 2)
00537 {
00538 hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
00539 hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
00540 }
00541
00542 for (i = 0; i < hInfo->num_side_channels; i += 2)
00543 {
00544 hInfo->channel_position[chpos++] = SIDE_CHANNEL_LEFT;
00545 hInfo->channel_position[chpos++] = SIDE_CHANNEL_RIGHT;
00546 }
00547
00548 chdir = hInfo->num_back_channels;
00549 if (chdir & 1)
00550 {
00551 back_center = 1;
00552 chdir--;
00553 }
00554 for (i = 0; i < chdir; i += 2)
00555 {
00556 hInfo->channel_position[chpos++] = BACK_CHANNEL_LEFT;
00557 hInfo->channel_position[chpos++] = BACK_CHANNEL_RIGHT;
00558 }
00559 if (back_center)
00560 {
00561 hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
00562 }
00563
00564 for (i = 0; i < hInfo->num_lfe_channels; i++)
00565 {
00566 hInfo->channel_position[chpos++] = LFE_CHANNEL;
00567 }
00568
00569 } else {
00570 switch (hDecoder->channelConfiguration)
00571 {
00572 case 1:
00573 hInfo->num_front_channels = 1;
00574 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
00575 break;
00576 case 2:
00577 hInfo->num_front_channels = 2;
00578 hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
00579 hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
00580 break;
00581 case 3:
00582 hInfo->num_front_channels = 3;
00583 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
00584 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
00585 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
00586 break;
00587 case 4:
00588 hInfo->num_front_channels = 3;
00589 hInfo->num_back_channels = 1;
00590 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
00591 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
00592 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
00593 hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
00594 break;
00595 case 5:
00596 hInfo->num_front_channels = 3;
00597 hInfo->num_back_channels = 2;
00598 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
00599 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
00600 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
00601 hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
00602 hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
00603 break;
00604 case 6:
00605 hInfo->num_front_channels = 3;
00606 hInfo->num_back_channels = 2;
00607 hInfo->num_lfe_channels = 1;
00608 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
00609 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
00610 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
00611 hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
00612 hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
00613 hInfo->channel_position[5] = LFE_CHANNEL;
00614 break;
00615 case 7:
00616 hInfo->num_front_channels = 3;
00617 hInfo->num_side_channels = 2;
00618 hInfo->num_back_channels = 2;
00619 hInfo->num_lfe_channels = 1;
00620 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
00621 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
00622 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
00623 hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
00624 hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
00625 hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
00626 hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
00627 hInfo->channel_position[7] = LFE_CHANNEL;
00628 break;
00629 default:
00630 {
00631 uint8_t i;
00632 uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
00633 if (ch & 1)
00634 {
00635 uint8_t ch1 = (ch-1)/2;
00636 if (hDecoder->first_syn_ele == ID_SCE)
00637 {
00638 hInfo->num_front_channels = ch1 + 1;
00639 hInfo->num_back_channels = ch1;
00640 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
00641 for (i = 1; i <= ch1; i+=2)
00642 {
00643 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
00644 hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
00645 }
00646 for (i = ch1+1; i < ch; i+=2)
00647 {
00648 hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
00649 hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
00650 }
00651 } else {
00652 hInfo->num_front_channels = ch1;
00653 hInfo->num_back_channels = ch1 + 1;
00654 for (i = 0; i < ch1; i+=2)
00655 {
00656 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
00657 hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
00658 }
00659 for (i = ch1; i < ch-1; i+=2)
00660 {
00661 hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
00662 hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
00663 }
00664 hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
00665 }
00666 } else {
00667 uint8_t ch1 = (ch)/2;
00668 hInfo->num_front_channels = ch1;
00669 hInfo->num_back_channels = ch1;
00670 if (ch1 & 1)
00671 {
00672 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
00673 for (i = 1; i <= ch1; i+=2)
00674 {
00675 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
00676 hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
00677 }
00678 for (i = ch1+1; i < ch-1; i+=2)
00679 {
00680 hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
00681 hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
00682 }
00683 hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
00684 } else {
00685 for (i = 0; i < ch1; i+=2)
00686 {
00687 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
00688 hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
00689 }
00690 for (i = ch1; i < ch; i+=2)
00691 {
00692 hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
00693 hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
00694 }
00695 }
00696 }
00697 hInfo->num_lfe_channels = hDecoder->has_lfe;
00698 for (i = ch; i < hDecoder->fr_channels; i++)
00699 {
00700 hInfo->channel_position[i] = LFE_CHANNEL;
00701 }
00702 }
00703 break;
00704 }
00705 }
00706 }
00707
00708 void* NEAACDECAPI NeAACDecDecode(NeAACDecHandle hDecoder,
00709 NeAACDecFrameInfo *hInfo,
00710 uint8_t *buffer, uint32_t buffer_size)
00711 {
00712 return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size, NULL, 0);
00713 }
00714
00715 void* NEAACDECAPI NeAACDecDecode2(NeAACDecHandle hDecoder,
00716 NeAACDecFrameInfo *hInfo,
00717 uint8_t *buffer, uint32_t buffer_size,
00718 void **sample_buffer, uint32_t sample_buffer_size)
00719 {
00720 if ((sample_buffer == NULL) || (sample_buffer_size == 0))
00721 {
00722 hInfo->error = 27;
00723 return NULL;
00724 }
00725
00726 return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size,
00727 sample_buffer, sample_buffer_size);
00728 }
00729
00730 #ifdef DRM
00731
00732 #define ERROR_STATE_INIT 6
00733
00734 static void conceal_output(NeAACDecHandle hDecoder, uint16_t frame_len,
00735 uint8_t out_ch, void *sample_buffer)
00736 {
00737 uint16_t i;
00738 int16_t *short_sample_buffer = (int16_t*)sample_buffer;
00739 int32_t *int_sample_buffer = (int32_t*)sample_buffer;
00740 #ifndef FIXED_POINT
00741 float32_t *float_sample_buffer = (float32_t*)sample_buffer;
00742 double *double_sample_buffer = (double*)sample_buffer;
00743 #endif
00744
00745 static const int8_t mute_tab[ERROR_STATE_INIT+1] = { 0, 1, 1, 2, 2, 3, 3 };
00746
00747 if (hDecoder->error_state > 0)
00748 {
00749 switch (hDecoder->config.outputFormat)
00750 {
00751 case FAAD_FMT_16BIT:
00752 for (i = 0; i < out_ch*frame_len; i++)
00753 {
00754 short_sample_buffer[i] >>= mute_tab[hDecoder->error_state];
00755
00756 }
00757
00758 break;
00759 case FAAD_FMT_24BIT:
00760 case FAAD_FMT_32BIT:
00761 #ifdef FIXED_POINT
00762 case FAAD_FMT_FIXED:
00763 #endif
00764 for (i = 0; i < out_ch*frame_len; i++)
00765 {
00766 int_sample_buffer[i] >>= mute_tab[hDecoder->error_state];
00767
00768 }
00769 break;
00770 #ifndef FIXED_POINT
00771 case FAAD_FMT_FLOAT:
00772 for (i = 0; i < out_ch*frame_len; i++)
00773 {
00774 float_sample_buffer[i] /= (float)(1<<mute_tab[hDecoder->error_state]);
00775
00776 }
00777 break;
00778 case FAAD_FMT_DOUBLE:
00779 for (i = 0; i < out_ch*frame_len; i++)
00780 {
00781 double_sample_buffer[i] /= (float)(1<<mute_tab[hDecoder->error_state]);
00782
00783 }
00784 break;
00785 #endif
00786 }
00787
00788 hDecoder->error_state--;
00789 }
00790 }
00791 #endif
00792
00793 static void* aac_frame_decode(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo,
00794 uint8_t *buffer, uint32_t buffer_size,
00795 void **sample_buffer2, uint32_t sample_buffer_size)
00796 {
00797 uint16_t i;
00798 uint8_t channels = 0;
00799 uint8_t output_channels = 0;
00800 bitfile ld;
00801 uint32_t bitsconsumed;
00802 uint16_t frame_len;
00803 void *sample_buffer;
00804
00805 #ifdef PROFILE
00806 int64_t count = faad_get_ts();
00807 #endif
00808
00809
00810 if ((hDecoder == NULL) || (hInfo == NULL) || (buffer == NULL))
00811 {
00812 return NULL;
00813 }
00814
00815 #if 0
00816 printf("%d\n", buffer_size*8);
00817 #endif
00818
00819 frame_len = hDecoder->frameLength;
00820
00821
00822 memset(hInfo, 0, sizeof(NeAACDecFrameInfo));
00823 memset(hDecoder->internal_channel, 0, MAX_CHANNELS*sizeof(hDecoder->internal_channel[0]));
00824
00825
00826
00827
00828
00829
00830 if (buffer_size >= 128)
00831 {
00832 if (memcmp(buffer, "TAG", 3) == 0)
00833 {
00834
00835 hInfo->bytesconsumed = 128;
00836
00837 return NULL;
00838 }
00839 }
00840
00841
00842
00843 faad_initbits(&ld, buffer, buffer_size);
00844
00845 #if 0
00846 {
00847 int i;
00848 for (i = 0; i < ((buffer_size+3)>>2); i++)
00849 {
00850 uint8_t *buf;
00851 uint32_t temp = 0;
00852 buf = faad_getbitbuffer(&ld, 32);
00853
00854 temp = *((uint32_t*)buf);
00855 printf("0x%.8X\n", temp);
00856 free(buf);
00857 }
00858 faad_endbits(&ld);
00859 faad_initbits(&ld, buffer, buffer_size);
00860 }
00861 #endif
00862
00863 #ifdef DRM
00864 if (hDecoder->object_type == DRM_ER_LC)
00865 {
00866
00867 if (0)
00868 {
00869 hInfo->error = 28;
00870 goto error;
00871 }
00872
00873 faad_getbits(&ld, 8
00874 DEBUGVAR(1,1,"NeAACDecDecode(): skip CRC"));
00875 }
00876 #endif
00877
00878 if (hDecoder->adts_header_present)
00879 {
00880 adts_header adts;
00881
00882 adts.old_format = hDecoder->config.useOldADTSFormat;
00883 if ((hInfo->error = adts_frame(&adts, &ld)) > 0)
00884 goto error;
00885
00886
00887
00888
00889
00890 }
00891
00892 #ifdef ANALYSIS
00893 dbg_count = 0;
00894 #endif
00895
00896
00897 #ifdef DRM
00898 if ( (hDecoder->object_type == DRM_ER_LC))
00899 {
00900 DRM_aac_scalable_main_element(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
00901 } else {
00902 #endif
00903 raw_data_block(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
00904 #ifdef DRM
00905 }
00906 #endif
00907
00908 channels = hDecoder->fr_channels;
00909
00910 if (hInfo->error > 0)
00911 goto error;
00912
00913
00914 if (channels == 0 || channels > MAX_CHANNELS)
00915 {
00916
00917 hInfo->error = 12;
00918 goto error;
00919 }
00920
00921
00922 bitsconsumed = faad_get_processed_bits(&ld);
00923 hInfo->bytesconsumed = bit2byte(bitsconsumed);
00924 if (ld.error)
00925 {
00926 hInfo->error = 14;
00927 goto error;
00928 }
00929 faad_endbits(&ld);
00930
00931
00932 if (!hDecoder->adts_header_present && !hDecoder->adif_header_present)
00933 {
00934 if (hDecoder->channelConfiguration == 0)
00935 hDecoder->channelConfiguration = channels;
00936
00937 if (channels == 8)
00938 hDecoder->channelConfiguration = 7;
00939 if (channels == 7)
00940 hDecoder->channelConfiguration = 0;
00941 }
00942
00943 if ((channels == 5 || channels == 6) && hDecoder->config.downMatrix)
00944 {
00945 hDecoder->downMatrix = 1;
00946 output_channels = 2;
00947 } else {
00948 output_channels = channels;
00949 }
00950
00951 #if (defined(PS_DEC) || defined(DRM_PS))
00952 hDecoder->upMatrix = 0;
00953
00954 if (output_channels == 1)
00955 {
00956
00957 hDecoder->upMatrix = 1;
00958 output_channels = 2;
00959 }
00960 #endif
00961
00962
00963 create_channel_config(hDecoder, hInfo);
00964
00965
00966 hInfo->samples = frame_len*output_channels;
00967
00968 hInfo->channels = output_channels;
00969
00970 hInfo->samplerate = get_sample_rate(hDecoder->sf_index);
00971
00972 hInfo->object_type = hDecoder->object_type;
00973
00974 hInfo->sbr = NO_SBR;
00975
00976 hInfo->header_type = RAW;
00977 if (hDecoder->adif_header_present)
00978 hInfo->header_type = ADIF;
00979 if (hDecoder->adts_header_present)
00980 hInfo->header_type = ADTS;
00981 #if (defined(PS_DEC) || defined(DRM_PS))
00982 hInfo->ps = hDecoder->ps_used_global;
00983 #endif
00984
00985
00986 if (channels == 0)
00987 {
00988 hDecoder->frame++;
00989 return NULL;
00990 }
00991
00992
00993 if ((hDecoder->sample_buffer == NULL) ||
00994 (hDecoder->alloced_channels != output_channels))
00995 {
00996 static const uint8_t str[] = { sizeof(int16_t), sizeof(int32_t), sizeof(int32_t),
00997 sizeof(float32_t), sizeof(double), sizeof(int16_t), sizeof(int16_t),
00998 sizeof(int16_t), sizeof(int16_t), 0, 0, 0
00999 };
01000 uint8_t stride = str[hDecoder->config.outputFormat-1];
01001 #ifdef SBR_DEC
01002 if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || (hDecoder->forceUpSampling == 1))
01003 {
01004 stride = 2 * stride;
01005 }
01006 #endif
01007
01008 if (sample_buffer_size == 0)
01009 {
01010 if (hDecoder->sample_buffer)
01011 faad_free(hDecoder->sample_buffer);
01012 hDecoder->sample_buffer = NULL;
01013 hDecoder->sample_buffer = faad_malloc(frame_len*output_channels*stride);
01014 } else if (sample_buffer_size < frame_len*output_channels*stride) {
01015
01016 hInfo->error = 27;
01017 return NULL;
01018 }
01019 hDecoder->alloced_channels = output_channels;
01020 }
01021
01022 if (sample_buffer_size == 0)
01023 {
01024 sample_buffer = hDecoder->sample_buffer;
01025 } else {
01026 sample_buffer = *sample_buffer2;
01027 }
01028
01029 #ifdef SBR_DEC
01030 if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
01031 {
01032 uint8_t ele;
01033
01034
01035 if (!hDecoder->downSampledSBR)
01036 {
01037 frame_len *= 2;
01038 hInfo->samples *= 2;
01039 hInfo->samplerate *= 2;
01040 }
01041
01042
01043 for (ele = 0; ele < hDecoder->fr_ch_ele; ele++)
01044 {
01045 if (hDecoder->sbr[ele] == NULL)
01046 {
01047 hInfo->error = 25;
01048 goto error;
01049 }
01050 }
01051
01052
01053 if (hDecoder->sbr_present_flag == 1)
01054 {
01055 hInfo->object_type = HE_AAC;
01056 hInfo->sbr = SBR_UPSAMPLED;
01057 } else {
01058 hInfo->sbr = NO_SBR_UPSAMPLED;
01059 }
01060 if (hDecoder->downSampledSBR)
01061 {
01062 hInfo->sbr = SBR_DOWNSAMPLED;
01063 }
01064 }
01065 #endif
01066
01067
01068 sample_buffer = output_to_PCM(hDecoder, hDecoder->time_out, sample_buffer,
01069 output_channels, frame_len, hDecoder->config.outputFormat);
01070
01071
01072 #ifdef DRM
01073
01074 #endif
01075
01076
01077 hDecoder->postSeekResetFlag = 0;
01078
01079 hDecoder->frame++;
01080 #ifdef LD_DEC
01081 if (hDecoder->object_type != LD)
01082 {
01083 #endif
01084 if (hDecoder->frame <= 1)
01085 hInfo->samples = 0;
01086 #ifdef LD_DEC
01087 } else {
01088
01089 if (hDecoder->frame <= 0)
01090 hInfo->samples = 0;
01091 }
01092 #endif
01093
01094
01095 #ifdef ANALYSIS
01096 fflush(stdout);
01097 #endif
01098
01099 #ifdef PROFILE
01100 count = faad_get_ts() - count;
01101 hDecoder->cycles += count;
01102 #endif
01103
01104 return sample_buffer;
01105
01106 error:
01107
01108
01109 #ifdef DRM
01110 hDecoder->error_state = ERROR_STATE_INIT;
01111 #endif
01112
01113
01114 for (i = 0; i < MAX_CHANNELS; i++)
01115 {
01116 if (hDecoder->fb_intermed[i] != NULL)
01117 {
01118 memset(hDecoder->fb_intermed[i], 0, hDecoder->frameLength*sizeof(real_t));
01119 }
01120 }
01121 #ifdef SBR_DEC
01122 for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
01123 {
01124 if (hDecoder->sbr[i] != NULL)
01125 {
01126 sbrReset(hDecoder->sbr[i]);
01127 }
01128 }
01129 #endif
01130
01131
01132 faad_endbits(&ld);
01133
01134
01135 #ifdef ANALYSIS
01136 fflush(stdout);
01137 #endif
01138
01139 return NULL;
01140 }