SoftAAC2.cpp revision a5a103c579d5ecde233e04c00c90f4d15b216f29
1/* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#define LOG_TAG "SoftAAC2" 18//#define LOG_NDEBUG 0 19#include <utils/Log.h> 20 21#include "SoftAAC2.h" 22 23#include <cutils/properties.h> 24#include <media/stagefright/foundation/ADebug.h> 25#include <media/stagefright/foundation/hexdump.h> 26#include <media/stagefright/MediaErrors.h> 27 28#include <math.h> 29 30#define FILEREAD_MAX_LAYERS 2 31 32#define DRC_DEFAULT_MOBILE_REF_LEVEL 64 /* 64*-0.25dB = -16 dB below full scale for mobile conf */ 33#define DRC_DEFAULT_MOBILE_DRC_CUT 127 /* maximum compression of dynamic range for mobile conf */ 34#define DRC_DEFAULT_MOBILE_DRC_BOOST 127 /* maximum compression of dynamic range for mobile conf */ 35#define DRC_DEFAULT_MOBILE_DRC_HEAVY 1 /* switch for heavy compression for mobile conf */ 36#define DRC_DEFAULT_MOBILE_ENC_LEVEL -1 /* encoder target level; -1 => the value is unknown, otherwise dB step value (e.g. 64 for -16 dB) */ 37#define MAX_CHANNEL_COUNT 8 /* maximum number of audio channels that can be decoded */ 38// names of properties that can be used to override the default DRC settings 39#define PROP_DRC_OVERRIDE_REF_LEVEL "aac_drc_reference_level" 40#define PROP_DRC_OVERRIDE_CUT "aac_drc_cut" 41#define PROP_DRC_OVERRIDE_BOOST "aac_drc_boost" 42#define PROP_DRC_OVERRIDE_HEAVY "aac_drc_heavy" 43#define PROP_DRC_OVERRIDE_ENC_LEVEL "aac_drc_enc_target_level" 44 45namespace android { 46 47template<class T> 48static void InitOMXParams(T *params) { 49 params->nSize = sizeof(T); 50 params->nVersion.s.nVersionMajor = 1; 51 params->nVersion.s.nVersionMinor = 0; 52 params->nVersion.s.nRevision = 0; 53 params->nVersion.s.nStep = 0; 54} 55 56SoftAAC2::SoftAAC2( 57 const char *name, 58 const OMX_CALLBACKTYPE *callbacks, 59 OMX_PTR appData, 60 OMX_COMPONENTTYPE **component) 61 : SimpleSoftOMXComponent(name, callbacks, appData, component), 62 mAACDecoder(NULL), 63 mStreamInfo(NULL), 64 mIsADTS(false), 65 mInputBufferCount(0), 66 mOutputBufferCount(0), 67 mSignalledError(false), 68 mLastInHeader(NULL), 69 mCurrentInputTime(0), 70 mOutputPortSettingsChange(NONE) { 71 initPorts(); 72 CHECK_EQ(initDecoder(), (status_t)OK); 73} 74 75SoftAAC2::~SoftAAC2() { 76 aacDecoder_Close(mAACDecoder); 77 delete mOutputDelayRingBuffer; 78} 79 80void SoftAAC2::initPorts() { 81 OMX_PARAM_PORTDEFINITIONTYPE def; 82 InitOMXParams(&def); 83 84 def.nPortIndex = 0; 85 def.eDir = OMX_DirInput; 86 def.nBufferCountMin = kNumInputBuffers; 87 def.nBufferCountActual = def.nBufferCountMin; 88 def.nBufferSize = 8192; 89 def.bEnabled = OMX_TRUE; 90 def.bPopulated = OMX_FALSE; 91 def.eDomain = OMX_PortDomainAudio; 92 def.bBuffersContiguous = OMX_FALSE; 93 def.nBufferAlignment = 1; 94 95 def.format.audio.cMIMEType = const_cast<char *>("audio/aac"); 96 def.format.audio.pNativeRender = NULL; 97 def.format.audio.bFlagErrorConcealment = OMX_FALSE; 98 def.format.audio.eEncoding = OMX_AUDIO_CodingAAC; 99 100 addPort(def); 101 102 def.nPortIndex = 1; 103 def.eDir = OMX_DirOutput; 104 def.nBufferCountMin = kNumOutputBuffers; 105 def.nBufferCountActual = def.nBufferCountMin; 106 def.nBufferSize = 4096 * MAX_CHANNEL_COUNT; 107 def.bEnabled = OMX_TRUE; 108 def.bPopulated = OMX_FALSE; 109 def.eDomain = OMX_PortDomainAudio; 110 def.bBuffersContiguous = OMX_FALSE; 111 def.nBufferAlignment = 2; 112 113 def.format.audio.cMIMEType = const_cast<char *>("audio/raw"); 114 def.format.audio.pNativeRender = NULL; 115 def.format.audio.bFlagErrorConcealment = OMX_FALSE; 116 def.format.audio.eEncoding = OMX_AUDIO_CodingPCM; 117 118 addPort(def); 119} 120 121status_t SoftAAC2::initDecoder() { 122 status_t status = UNKNOWN_ERROR; 123 mAACDecoder = aacDecoder_Open(TT_MP4_ADIF, /* num layers */ 1); 124 if (mAACDecoder != NULL) { 125 mStreamInfo = aacDecoder_GetStreamInfo(mAACDecoder); 126 if (mStreamInfo != NULL) { 127 status = OK; 128 } 129 } 130 131 mEndOfInput = false; 132 mEndOfOutput = false; 133 mOutputDelayCompensated = 0; 134 mOutputDelayRingBufferSize = 2048 * MAX_CHANNEL_COUNT * kNumDelayBlocksMax; 135 mOutputDelayRingBuffer = new short[mOutputDelayRingBufferSize]; 136 mOutputDelayRingBufferWritePos = 0; 137 mOutputDelayRingBufferReadPos = 0; 138 139 if (mAACDecoder == NULL) { 140 ALOGE("AAC decoder is null. TODO: Can not call aacDecoder_SetParam in the following code"); 141 } 142 143 //aacDecoder_SetParam(mAACDecoder, AAC_PCM_LIMITER_ENABLE, 0); 144 145 //init DRC wrapper 146 mDrcWrap.setDecoderHandle(mAACDecoder); 147 mDrcWrap.submitStreamData(mStreamInfo); 148 149 // for streams that contain metadata, use the mobile profile DRC settings unless overridden by platform properties 150 // TODO: change the DRC settings depending on audio output device type (HDMI, loadspeaker, headphone) 151 char value[PROPERTY_VALUE_MAX]; 152 // DRC_PRES_MODE_WRAP_DESIRED_TARGET 153 if (property_get(PROP_DRC_OVERRIDE_REF_LEVEL, value, NULL)) { 154 unsigned refLevel = atoi(value); 155 ALOGV("AAC decoder using desired DRC target reference level of %d instead of %d", refLevel, 156 DRC_DEFAULT_MOBILE_REF_LEVEL); 157 mDrcWrap.setParam(DRC_PRES_MODE_WRAP_DESIRED_TARGET, refLevel); 158 } else { 159 mDrcWrap.setParam(DRC_PRES_MODE_WRAP_DESIRED_TARGET, DRC_DEFAULT_MOBILE_REF_LEVEL); 160 } 161 // DRC_PRES_MODE_WRAP_DESIRED_ATT_FACTOR 162 if (property_get(PROP_DRC_OVERRIDE_CUT, value, NULL)) { 163 unsigned cut = atoi(value); 164 ALOGV("AAC decoder using desired DRC attenuation factor of %d instead of %d", cut, 165 DRC_DEFAULT_MOBILE_DRC_CUT); 166 mDrcWrap.setParam(DRC_PRES_MODE_WRAP_DESIRED_ATT_FACTOR, cut); 167 } else { 168 mDrcWrap.setParam(DRC_PRES_MODE_WRAP_DESIRED_ATT_FACTOR, DRC_DEFAULT_MOBILE_DRC_CUT); 169 } 170 // DRC_PRES_MODE_WRAP_DESIRED_BOOST_FACTOR 171 if (property_get(PROP_DRC_OVERRIDE_BOOST, value, NULL)) { 172 unsigned boost = atoi(value); 173 ALOGV("AAC decoder using desired DRC boost factor of %d instead of %d", boost, 174 DRC_DEFAULT_MOBILE_DRC_BOOST); 175 mDrcWrap.setParam(DRC_PRES_MODE_WRAP_DESIRED_BOOST_FACTOR, boost); 176 } else { 177 mDrcWrap.setParam(DRC_PRES_MODE_WRAP_DESIRED_BOOST_FACTOR, DRC_DEFAULT_MOBILE_DRC_BOOST); 178 } 179 // DRC_PRES_MODE_WRAP_DESIRED_HEAVY 180 if (property_get(PROP_DRC_OVERRIDE_HEAVY, value, NULL)) { 181 unsigned heavy = atoi(value); 182 ALOGV("AAC decoder using desried DRC heavy compression switch of %d instead of %d", heavy, 183 DRC_DEFAULT_MOBILE_DRC_HEAVY); 184 mDrcWrap.setParam(DRC_PRES_MODE_WRAP_DESIRED_HEAVY, heavy); 185 } else { 186 mDrcWrap.setParam(DRC_PRES_MODE_WRAP_DESIRED_HEAVY, DRC_DEFAULT_MOBILE_DRC_HEAVY); 187 } 188 // DRC_PRES_MODE_WRAP_ENCODER_TARGET 189 if (property_get(PROP_DRC_OVERRIDE_ENC_LEVEL, value, NULL)) { 190 unsigned encoderRefLevel = atoi(value); 191 ALOGV("AAC decoder using encoder-side DRC reference level of %d instead of %d", 192 encoderRefLevel, DRC_DEFAULT_MOBILE_ENC_LEVEL); 193 mDrcWrap.setParam(DRC_PRES_MODE_WRAP_ENCODER_TARGET, encoderRefLevel); 194 } else { 195 mDrcWrap.setParam(DRC_PRES_MODE_WRAP_ENCODER_TARGET, DRC_DEFAULT_MOBILE_ENC_LEVEL); 196 } 197 198 return status; 199} 200 201OMX_ERRORTYPE SoftAAC2::internalGetParameter( 202 OMX_INDEXTYPE index, OMX_PTR params) { 203 switch (index) { 204 case OMX_IndexParamAudioAac: 205 { 206 OMX_AUDIO_PARAM_AACPROFILETYPE *aacParams = 207 (OMX_AUDIO_PARAM_AACPROFILETYPE *)params; 208 209 if (aacParams->nPortIndex != 0) { 210 return OMX_ErrorUndefined; 211 } 212 213 aacParams->nBitRate = 0; 214 aacParams->nAudioBandWidth = 0; 215 aacParams->nAACtools = 0; 216 aacParams->nAACERtools = 0; 217 aacParams->eAACProfile = OMX_AUDIO_AACObjectMain; 218 219 aacParams->eAACStreamFormat = 220 mIsADTS 221 ? OMX_AUDIO_AACStreamFormatMP4ADTS 222 : OMX_AUDIO_AACStreamFormatMP4FF; 223 224 aacParams->eChannelMode = OMX_AUDIO_ChannelModeStereo; 225 226 if (!isConfigured()) { 227 aacParams->nChannels = 1; 228 aacParams->nSampleRate = 44100; 229 aacParams->nFrameLength = 0; 230 } else { 231 aacParams->nChannels = mStreamInfo->numChannels; 232 aacParams->nSampleRate = mStreamInfo->sampleRate; 233 aacParams->nFrameLength = mStreamInfo->frameSize; 234 } 235 236 return OMX_ErrorNone; 237 } 238 239 case OMX_IndexParamAudioPcm: 240 { 241 OMX_AUDIO_PARAM_PCMMODETYPE *pcmParams = 242 (OMX_AUDIO_PARAM_PCMMODETYPE *)params; 243 244 if (pcmParams->nPortIndex != 1) { 245 return OMX_ErrorUndefined; 246 } 247 248 pcmParams->eNumData = OMX_NumericalDataSigned; 249 pcmParams->eEndian = OMX_EndianBig; 250 pcmParams->bInterleaved = OMX_TRUE; 251 pcmParams->nBitPerSample = 16; 252 pcmParams->ePCMMode = OMX_AUDIO_PCMModeLinear; 253 pcmParams->eChannelMapping[0] = OMX_AUDIO_ChannelLF; 254 pcmParams->eChannelMapping[1] = OMX_AUDIO_ChannelRF; 255 pcmParams->eChannelMapping[2] = OMX_AUDIO_ChannelCF; 256 pcmParams->eChannelMapping[3] = OMX_AUDIO_ChannelLFE; 257 pcmParams->eChannelMapping[4] = OMX_AUDIO_ChannelLS; 258 pcmParams->eChannelMapping[5] = OMX_AUDIO_ChannelRS; 259 260 if (!isConfigured()) { 261 pcmParams->nChannels = 1; 262 pcmParams->nSamplingRate = 44100; 263 } else { 264 pcmParams->nChannels = mStreamInfo->numChannels; 265 pcmParams->nSamplingRate = mStreamInfo->sampleRate; 266 } 267 268 return OMX_ErrorNone; 269 } 270 271 default: 272 return SimpleSoftOMXComponent::internalGetParameter(index, params); 273 } 274} 275 276OMX_ERRORTYPE SoftAAC2::internalSetParameter( 277 OMX_INDEXTYPE index, const OMX_PTR params) { 278 switch (index) { 279 case OMX_IndexParamStandardComponentRole: 280 { 281 const OMX_PARAM_COMPONENTROLETYPE *roleParams = 282 (const OMX_PARAM_COMPONENTROLETYPE *)params; 283 284 if (strncmp((const char *)roleParams->cRole, 285 "audio_decoder.aac", 286 OMX_MAX_STRINGNAME_SIZE - 1)) { 287 return OMX_ErrorUndefined; 288 } 289 290 return OMX_ErrorNone; 291 } 292 293 case OMX_IndexParamAudioAac: 294 { 295 const OMX_AUDIO_PARAM_AACPROFILETYPE *aacParams = 296 (const OMX_AUDIO_PARAM_AACPROFILETYPE *)params; 297 298 if (aacParams->nPortIndex != 0) { 299 return OMX_ErrorUndefined; 300 } 301 302 if (aacParams->eAACStreamFormat == OMX_AUDIO_AACStreamFormatMP4FF) { 303 mIsADTS = false; 304 } else if (aacParams->eAACStreamFormat 305 == OMX_AUDIO_AACStreamFormatMP4ADTS) { 306 mIsADTS = true; 307 } else { 308 return OMX_ErrorUndefined; 309 } 310 311 return OMX_ErrorNone; 312 } 313 314 case OMX_IndexParamAudioPcm: 315 { 316 const OMX_AUDIO_PARAM_PCMMODETYPE *pcmParams = 317 (OMX_AUDIO_PARAM_PCMMODETYPE *)params; 318 319 if (pcmParams->nPortIndex != 1) { 320 return OMX_ErrorUndefined; 321 } 322 323 return OMX_ErrorNone; 324 } 325 326 default: 327 return SimpleSoftOMXComponent::internalSetParameter(index, params); 328 } 329} 330 331bool SoftAAC2::isConfigured() const { 332 return mInputBufferCount > 0; 333} 334 335void SoftAAC2::configureDownmix() const { 336 char value[PROPERTY_VALUE_MAX]; 337 if (!(property_get("media.aac_51_output_enabled", value, NULL) 338 && (!strcmp(value, "1") || !strcasecmp(value, "true")))) { 339 ALOGI("limiting to stereo output"); 340 aacDecoder_SetParam(mAACDecoder, AAC_PCM_MAX_OUTPUT_CHANNELS, 2); 341 // By default, the decoder creates a 5.1 channel downmix signal 342 // for seven and eight channel input streams. To enable 6.1 and 7.1 channel output 343 // use aacDecoder_SetParam(mAACDecoder, AAC_PCM_MAX_OUTPUT_CHANNELS, -1) 344 } 345} 346 347bool SoftAAC2::outputDelayRingBufferPutSamples(INT_PCM *samples, int32_t numSamples) { 348 if (mOutputDelayRingBufferWritePos + numSamples <= mOutputDelayRingBufferSize 349 && (mOutputDelayRingBufferReadPos <= mOutputDelayRingBufferWritePos 350 || mOutputDelayRingBufferReadPos > mOutputDelayRingBufferWritePos + numSamples)) { 351 // faster memcopy loop without checks, if the preconditions allow this 352 for (int32_t i = 0; i < numSamples; i++) { 353 mOutputDelayRingBuffer[mOutputDelayRingBufferWritePos++] = samples[i]; 354 } 355 356 if (mOutputDelayRingBufferWritePos >= mOutputDelayRingBufferSize) { 357 mOutputDelayRingBufferWritePos -= mOutputDelayRingBufferSize; 358 } 359 if (mOutputDelayRingBufferWritePos == mOutputDelayRingBufferReadPos) { 360 ALOGE("RING BUFFER OVERFLOW"); 361 return false; 362 } 363 } else { 364 ALOGV("slow SoftAAC2::outputDelayRingBufferPutSamples()"); 365 366 for (int32_t i = 0; i < numSamples; i++) { 367 mOutputDelayRingBuffer[mOutputDelayRingBufferWritePos] = samples[i]; 368 mOutputDelayRingBufferWritePos++; 369 if (mOutputDelayRingBufferWritePos >= mOutputDelayRingBufferSize) { 370 mOutputDelayRingBufferWritePos -= mOutputDelayRingBufferSize; 371 } 372 if (mOutputDelayRingBufferWritePos == mOutputDelayRingBufferReadPos) { 373 ALOGE("RING BUFFER OVERFLOW"); 374 return false; 375 } 376 } 377 } 378 return true; 379} 380 381int32_t SoftAAC2::outputDelayRingBufferGetSamples(INT_PCM *samples, int32_t numSamples) { 382 if (mOutputDelayRingBufferReadPos + numSamples <= mOutputDelayRingBufferSize 383 && (mOutputDelayRingBufferWritePos < mOutputDelayRingBufferReadPos 384 || mOutputDelayRingBufferWritePos >= mOutputDelayRingBufferReadPos + numSamples)) { 385 // faster memcopy loop without checks, if the preconditions allow this 386 if (samples != 0) { 387 for (int32_t i = 0; i < numSamples; i++) { 388 samples[i] = mOutputDelayRingBuffer[mOutputDelayRingBufferReadPos++]; 389 } 390 } else { 391 mOutputDelayRingBufferReadPos += numSamples; 392 } 393 if (mOutputDelayRingBufferReadPos >= mOutputDelayRingBufferSize) { 394 mOutputDelayRingBufferReadPos -= mOutputDelayRingBufferSize; 395 } 396 } else { 397 ALOGV("slow SoftAAC2::outputDelayRingBufferGetSamples()"); 398 399 for (int32_t i = 0; i < numSamples; i++) { 400 if (mOutputDelayRingBufferWritePos == mOutputDelayRingBufferReadPos) { 401 ALOGE("RING BUFFER UNDERRUN"); 402 return -1; 403 } 404 if (samples != 0) { 405 samples[i] = mOutputDelayRingBuffer[mOutputDelayRingBufferReadPos]; 406 } 407 mOutputDelayRingBufferReadPos++; 408 if (mOutputDelayRingBufferReadPos >= mOutputDelayRingBufferSize) { 409 mOutputDelayRingBufferReadPos -= mOutputDelayRingBufferSize; 410 } 411 } 412 } 413 return numSamples; 414} 415 416int32_t SoftAAC2::outputDelayRingBufferSamplesAvailable() { 417 int32_t available = mOutputDelayRingBufferWritePos - mOutputDelayRingBufferReadPos; 418 if (available < 0) { 419 available += mOutputDelayRingBufferSize; 420 } 421 if (available < 0) { 422 ALOGE("FATAL RING BUFFER ERROR"); 423 return 0; 424 } 425 return available; 426} 427 428int32_t SoftAAC2::outputDelayRingBufferSamplesLeft() { 429 return mOutputDelayRingBufferSize - outputDelayRingBufferSamplesAvailable(); 430} 431 432void SoftAAC2::onQueueFilled(OMX_U32 portIndex) { 433 if (mSignalledError || mOutputPortSettingsChange != NONE) { 434 return; 435 } 436 437 UCHAR* inBuffer[FILEREAD_MAX_LAYERS]; 438 UINT inBufferLength[FILEREAD_MAX_LAYERS] = {0}; 439 UINT bytesValid[FILEREAD_MAX_LAYERS] = {0}; 440 441 List<BufferInfo *> &inQueue = getPortQueue(0); 442 List<BufferInfo *> &outQueue = getPortQueue(1); 443 444 if (portIndex == 0 && mInputBufferCount == 0) { 445 BufferInfo *inInfo = *inQueue.begin(); 446 OMX_BUFFERHEADERTYPE *inHeader = inInfo->mHeader; 447 448 inBuffer[0] = inHeader->pBuffer + inHeader->nOffset; 449 inBufferLength[0] = inHeader->nFilledLen; 450 451 AAC_DECODER_ERROR decoderErr = 452 aacDecoder_ConfigRaw(mAACDecoder, 453 inBuffer, 454 inBufferLength); 455 456 if (decoderErr != AAC_DEC_OK) { 457 ALOGW("aacDecoder_ConfigRaw decoderErr = 0x%4.4x", decoderErr); 458 mSignalledError = true; 459 notify(OMX_EventError, OMX_ErrorUndefined, decoderErr, NULL); 460 return; 461 } 462 463 mInputBufferCount++; 464 mOutputBufferCount++; // fake increase of outputBufferCount to keep the counters aligned 465 466 inInfo->mOwnedByUs = false; 467 inQueue.erase(inQueue.begin()); 468 inInfo = NULL; 469 notifyEmptyBufferDone(inHeader); 470 inHeader = NULL; 471 472 configureDownmix(); 473 // Only send out port settings changed event if both sample rate 474 // and numChannels are valid. 475 if (mStreamInfo->sampleRate && mStreamInfo->numChannels) { 476 ALOGI("Initially configuring decoder: %d Hz, %d channels", 477 mStreamInfo->sampleRate, 478 mStreamInfo->numChannels); 479 480 notify(OMX_EventPortSettingsChanged, 1, 0, NULL); 481 mOutputPortSettingsChange = AWAITING_DISABLED; 482 } 483 484 return; 485 } 486 487 while ((!inQueue.empty() || mEndOfInput) && !outQueue.empty()) { 488 if (!inQueue.empty()) { 489 INT_PCM tmpOutBuffer[2048 * MAX_CHANNEL_COUNT]; 490 BufferInfo *inInfo = *inQueue.begin(); 491 OMX_BUFFERHEADERTYPE *inHeader = inInfo->mHeader; 492 493 if (inHeader->nFlags & OMX_BUFFERFLAG_EOS) { 494 mEndOfInput = true; 495 } else { 496 mEndOfInput = false; 497 } 498 499 if (inHeader->nFilledLen == 0) { 500 inInfo->mOwnedByUs = false; 501 inQueue.erase(inQueue.begin()); 502 mLastInHeader = NULL; 503 inInfo = NULL; 504 notifyEmptyBufferDone(inHeader); 505 inHeader = NULL; 506 } else { 507 if (mIsADTS) { 508 size_t adtsHeaderSize = 0; 509 // skip 30 bits, aac_frame_length follows. 510 // ssssssss ssssiiip ppffffPc ccohCCll llllllll lll????? 511 512 const uint8_t *adtsHeader = inHeader->pBuffer + inHeader->nOffset; 513 514 bool signalError = false; 515 if (inHeader->nFilledLen < 7) { 516 ALOGE("Audio data too short to contain even the ADTS header. " 517 "Got %d bytes.", inHeader->nFilledLen); 518 hexdump(adtsHeader, inHeader->nFilledLen); 519 signalError = true; 520 } else { 521 bool protectionAbsent = (adtsHeader[1] & 1); 522 523 unsigned aac_frame_length = 524 ((adtsHeader[3] & 3) << 11) 525 | (adtsHeader[4] << 3) 526 | (adtsHeader[5] >> 5); 527 528 if (inHeader->nFilledLen < aac_frame_length) { 529 ALOGE("Not enough audio data for the complete frame. " 530 "Got %d bytes, frame size according to the ADTS " 531 "header is %u bytes.", 532 inHeader->nFilledLen, aac_frame_length); 533 hexdump(adtsHeader, inHeader->nFilledLen); 534 signalError = true; 535 } else { 536 adtsHeaderSize = (protectionAbsent ? 7 : 9); 537 538 inBuffer[0] = (UCHAR *)adtsHeader + adtsHeaderSize; 539 inBufferLength[0] = aac_frame_length - adtsHeaderSize; 540 541 inHeader->nOffset += adtsHeaderSize; 542 inHeader->nFilledLen -= adtsHeaderSize; 543 } 544 } 545 546 if (signalError) { 547 mSignalledError = true; 548 549 notify(OMX_EventError, 550 OMX_ErrorStreamCorrupt, 551 ERROR_MALFORMED, 552 NULL); 553 554 return; 555 } 556 } else { 557 inBuffer[0] = inHeader->pBuffer + inHeader->nOffset; 558 inBufferLength[0] = inHeader->nFilledLen; 559 } 560 561 // Fill and decode 562 bytesValid[0] = inBufferLength[0]; 563 564 INT prevSampleRate = mStreamInfo->sampleRate; 565 INT prevNumChannels = mStreamInfo->numChannels; 566 567 if (inHeader != mLastInHeader) { 568 mLastInHeader = inHeader; 569 mCurrentInputTime = inHeader->nTimeStamp; 570 } else { 571 if (mStreamInfo->sampleRate) { 572 mCurrentInputTime += mStreamInfo->aacSamplesPerFrame * 573 1000000ll / mStreamInfo->sampleRate; 574 } else { 575 ALOGW("no sample rate yet"); 576 } 577 } 578 mAnchorTimes.add(mCurrentInputTime); 579 aacDecoder_Fill(mAACDecoder, 580 inBuffer, 581 inBufferLength, 582 bytesValid); 583 584 // run DRC check 585 mDrcWrap.submitStreamData(mStreamInfo); 586 mDrcWrap.update(); 587 588 AAC_DECODER_ERROR decoderErr = 589 aacDecoder_DecodeFrame(mAACDecoder, 590 tmpOutBuffer, 591 2048 * MAX_CHANNEL_COUNT, 592 0 /* flags */); 593 594 if (decoderErr != AAC_DEC_OK) { 595 ALOGW("aacDecoder_DecodeFrame decoderErr = 0x%4.4x", decoderErr); 596 } 597 598 if (decoderErr == AAC_DEC_NOT_ENOUGH_BITS) { 599 ALOGE("AAC_DEC_NOT_ENOUGH_BITS should never happen"); 600 mSignalledError = true; 601 notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL); 602 return; 603 } 604 605 if (bytesValid[0] != 0) { 606 ALOGE("bytesValid[0] != 0 should never happen"); 607 mSignalledError = true; 608 notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL); 609 return; 610 } 611 612 size_t numOutBytes = 613 mStreamInfo->frameSize * sizeof(int16_t) * mStreamInfo->numChannels; 614 615 if (decoderErr == AAC_DEC_OK) { 616 if (!outputDelayRingBufferPutSamples(tmpOutBuffer, 617 mStreamInfo->frameSize * mStreamInfo->numChannels)) { 618 mSignalledError = true; 619 notify(OMX_EventError, OMX_ErrorUndefined, decoderErr, NULL); 620 return; 621 } 622 UINT inBufferUsedLength = inBufferLength[0] - bytesValid[0]; 623 inHeader->nFilledLen -= inBufferUsedLength; 624 inHeader->nOffset += inBufferUsedLength; 625 } else { 626 ALOGW("AAC decoder returned error 0x%4.4x, substituting silence", decoderErr); 627 628 memset(tmpOutBuffer, 0, numOutBytes); // TODO: check for overflow 629 630 if (!outputDelayRingBufferPutSamples(tmpOutBuffer, 631 mStreamInfo->frameSize * mStreamInfo->numChannels)) { 632 mSignalledError = true; 633 notify(OMX_EventError, OMX_ErrorUndefined, decoderErr, NULL); 634 return; 635 } 636 637 // Discard input buffer. 638 inHeader->nFilledLen = 0; 639 640 aacDecoder_SetParam(mAACDecoder, AAC_TPDEC_CLEAR_BUFFER, 1); 641 642 // fall through 643 } 644 645 /* 646 * AAC+/eAAC+ streams can be signalled in two ways: either explicitly 647 * or implicitly, according to MPEG4 spec. AAC+/eAAC+ is a dual 648 * rate system and the sampling rate in the final output is actually 649 * doubled compared with the core AAC decoder sampling rate. 650 * 651 * Explicit signalling is done by explicitly defining SBR audio object 652 * type in the bitstream. Implicit signalling is done by embedding 653 * SBR content in AAC extension payload specific to SBR, and hence 654 * requires an AAC decoder to perform pre-checks on actual audio frames. 655 * 656 * Thus, we could not say for sure whether a stream is 657 * AAC+/eAAC+ until the first data frame is decoded. 658 */ 659 if (mOutputBufferCount > 1) { 660 if (mStreamInfo->sampleRate != prevSampleRate || 661 mStreamInfo->numChannels != prevNumChannels) { 662 ALOGE("can not reconfigure AAC output"); 663 mSignalledError = true; 664 notify(OMX_EventError, OMX_ErrorUndefined, decoderErr, NULL); 665 return; 666 } 667 } 668 if (mInputBufferCount <= 2) { // TODO: <= 1 669 if (mStreamInfo->sampleRate != prevSampleRate || 670 mStreamInfo->numChannels != prevNumChannels) { 671 ALOGI("Reconfiguring decoder: %d->%d Hz, %d->%d channels", 672 prevSampleRate, mStreamInfo->sampleRate, 673 prevNumChannels, mStreamInfo->numChannels); 674 675 notify(OMX_EventPortSettingsChanged, 1, 0, NULL); 676 mOutputPortSettingsChange = AWAITING_DISABLED; 677 678 if (inHeader->nFilledLen == 0) { 679 inInfo->mOwnedByUs = false; 680 mInputBufferCount++; 681 inQueue.erase(inQueue.begin()); 682 mLastInHeader = NULL; 683 inInfo = NULL; 684 notifyEmptyBufferDone(inHeader); 685 inHeader = NULL; 686 } 687 return; 688 } 689 } else if (!mStreamInfo->sampleRate || !mStreamInfo->numChannels) { 690 ALOGW("Invalid AAC stream"); 691 mSignalledError = true; 692 notify(OMX_EventError, OMX_ErrorUndefined, decoderErr, NULL); 693 return; 694 } 695 if (inHeader->nFilledLen == 0) { 696 inInfo->mOwnedByUs = false; 697 mInputBufferCount++; 698 inQueue.erase(inQueue.begin()); 699 mLastInHeader = NULL; 700 inInfo = NULL; 701 notifyEmptyBufferDone(inHeader); 702 inHeader = NULL; 703 } else { 704 ALOGV("inHeader->nFilledLen = %d", inHeader->nFilledLen); 705 } 706 } 707 } 708 709 int32_t outputDelay = mStreamInfo->outputDelay * mStreamInfo->numChannels; 710 711 if (!mEndOfInput && mOutputDelayCompensated < outputDelay) { 712 // discard outputDelay at the beginning 713 int32_t toCompensate = outputDelay - mOutputDelayCompensated; 714 int32_t discard = outputDelayRingBufferSamplesAvailable(); 715 if (discard > toCompensate) { 716 discard = toCompensate; 717 } 718 int32_t discarded = outputDelayRingBufferGetSamples(0, discard); 719 mOutputDelayCompensated += discarded; 720 continue; 721 } 722 723 if (mEndOfInput) { 724 while (mOutputDelayCompensated > 0) { 725 // a buffer big enough for MAX_CHANNEL_COUNT channels of decoded HE-AAC 726 INT_PCM tmpOutBuffer[2048 * MAX_CHANNEL_COUNT]; 727 728 // run DRC check 729 mDrcWrap.submitStreamData(mStreamInfo); 730 mDrcWrap.update(); 731 732 AAC_DECODER_ERROR decoderErr = 733 aacDecoder_DecodeFrame(mAACDecoder, 734 tmpOutBuffer, 735 2048 * MAX_CHANNEL_COUNT, 736 AACDEC_FLUSH); 737 if (decoderErr != AAC_DEC_OK) { 738 ALOGW("aacDecoder_DecodeFrame decoderErr = 0x%4.4x", decoderErr); 739 } 740 741 int32_t tmpOutBufferSamples = mStreamInfo->frameSize * mStreamInfo->numChannels; 742 if (tmpOutBufferSamples > mOutputDelayCompensated) { 743 tmpOutBufferSamples = mOutputDelayCompensated; 744 } 745 outputDelayRingBufferPutSamples(tmpOutBuffer, tmpOutBufferSamples); 746 mOutputDelayCompensated -= tmpOutBufferSamples; 747 } 748 } 749 750 while (!outQueue.empty() 751 && outputDelayRingBufferSamplesAvailable() 752 >= mStreamInfo->frameSize * mStreamInfo->numChannels) { 753 BufferInfo *outInfo = *outQueue.begin(); 754 OMX_BUFFERHEADERTYPE *outHeader = outInfo->mHeader; 755 756 if (outHeader->nOffset != 0) { 757 ALOGE("outHeader->nOffset != 0 is not handled"); 758 mSignalledError = true; 759 notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL); 760 return; 761 } 762 763 INT_PCM *outBuffer = 764 reinterpret_cast<INT_PCM *>(outHeader->pBuffer + outHeader->nOffset); 765 if (outHeader->nOffset 766 + mStreamInfo->frameSize * mStreamInfo->numChannels * sizeof(int16_t) 767 > outHeader->nAllocLen) { 768 ALOGE("buffer overflow"); 769 mSignalledError = true; 770 notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL); 771 return; 772 773 } 774 int32_t ns = outputDelayRingBufferGetSamples(outBuffer, 775 mStreamInfo->frameSize * mStreamInfo->numChannels); // TODO: check for overflow 776 if (ns != mStreamInfo->frameSize * mStreamInfo->numChannels) { 777 ALOGE("not a complete frame of samples available"); 778 mSignalledError = true; 779 notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL); 780 return; 781 } 782 783 outHeader->nFilledLen = mStreamInfo->frameSize * mStreamInfo->numChannels 784 * sizeof(int16_t); 785 if (mEndOfInput && !outQueue.empty() && outputDelayRingBufferSamplesAvailable() == 0) { 786 outHeader->nFlags = OMX_BUFFERFLAG_EOS; 787 mEndOfOutput = true; 788 } else { 789 outHeader->nFlags = 0; 790 } 791 792 outHeader->nTimeStamp = mAnchorTimes.isEmpty() ? 0 : mAnchorTimes.itemAt(0); 793 mAnchorTimes.removeAt(0); 794 795 mOutputBufferCount++; 796 outInfo->mOwnedByUs = false; 797 outQueue.erase(outQueue.begin()); 798 outInfo = NULL; 799 notifyFillBufferDone(outHeader); 800 outHeader = NULL; 801 } 802 803 if (mEndOfInput) { 804 if (outputDelayRingBufferSamplesAvailable() > 0 805 && outputDelayRingBufferSamplesAvailable() 806 < mStreamInfo->frameSize * mStreamInfo->numChannels) { 807 ALOGE("not a complete frame of samples available"); 808 mSignalledError = true; 809 notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL); 810 return; 811 } 812 813 if (mEndOfInput && !outQueue.empty() && outputDelayRingBufferSamplesAvailable() == 0) { 814 if (!mEndOfOutput) { 815 // send empty block signaling EOS 816 mEndOfOutput = true; 817 BufferInfo *outInfo = *outQueue.begin(); 818 OMX_BUFFERHEADERTYPE *outHeader = outInfo->mHeader; 819 820 if (outHeader->nOffset != 0) { 821 ALOGE("outHeader->nOffset != 0 is not handled"); 822 mSignalledError = true; 823 notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL); 824 return; 825 } 826 827 INT_PCM *outBuffer = reinterpret_cast<INT_PCM *>(outHeader->pBuffer 828 + outHeader->nOffset); 829 int32_t ns = 0; 830 outHeader->nFilledLen = 0; 831 outHeader->nFlags = OMX_BUFFERFLAG_EOS; 832 833 outHeader->nTimeStamp = mAnchorTimes.itemAt(0); 834 mAnchorTimes.removeAt(0); 835 836 mOutputBufferCount++; 837 outInfo->mOwnedByUs = false; 838 outQueue.erase(outQueue.begin()); 839 outInfo = NULL; 840 notifyFillBufferDone(outHeader); 841 outHeader = NULL; 842 } 843 break; // if outQueue not empty but no more output 844 } 845 } 846 } 847} 848 849void SoftAAC2::onPortFlushCompleted(OMX_U32 portIndex) { 850 if (portIndex == 0) { 851 // Make sure that the next buffer output does not still 852 // depend on fragments from the last one decoded. 853 // drain all existing data 854 drainDecoder(); 855 mAnchorTimes.clear(); 856 mLastInHeader = NULL; 857 } else { 858 while (outputDelayRingBufferSamplesAvailable() > 0) { 859 int32_t ns = outputDelayRingBufferGetSamples(0, 860 mStreamInfo->frameSize * mStreamInfo->numChannels); 861 if (ns != mStreamInfo->frameSize * mStreamInfo->numChannels) { 862 ALOGE("not a complete frame of samples available"); 863 } 864 mOutputBufferCount++; 865 } 866 mOutputDelayRingBufferReadPos = mOutputDelayRingBufferWritePos; 867 } 868} 869 870void SoftAAC2::drainDecoder() { 871 int32_t outputDelay = mStreamInfo->outputDelay * mStreamInfo->numChannels; 872 873 // flush decoder until outputDelay is compensated 874 while (mOutputDelayCompensated > 0) { 875 // a buffer big enough for MAX_CHANNEL_COUNT channels of decoded HE-AAC 876 INT_PCM tmpOutBuffer[2048 * MAX_CHANNEL_COUNT]; 877 878 // run DRC check 879 mDrcWrap.submitStreamData(mStreamInfo); 880 mDrcWrap.update(); 881 882 AAC_DECODER_ERROR decoderErr = 883 aacDecoder_DecodeFrame(mAACDecoder, 884 tmpOutBuffer, 885 2048 * MAX_CHANNEL_COUNT, 886 AACDEC_FLUSH); 887 if (decoderErr != AAC_DEC_OK) { 888 ALOGW("aacDecoder_DecodeFrame decoderErr = 0x%4.4x", decoderErr); 889 } 890 891 int32_t tmpOutBufferSamples = mStreamInfo->frameSize * mStreamInfo->numChannels; 892 if (tmpOutBufferSamples > mOutputDelayCompensated) { 893 tmpOutBufferSamples = mOutputDelayCompensated; 894 } 895 outputDelayRingBufferPutSamples(tmpOutBuffer, tmpOutBufferSamples); 896 897 mOutputDelayCompensated -= tmpOutBufferSamples; 898 } 899} 900 901void SoftAAC2::onReset() { 902 drainDecoder(); 903 // reset the "configured" state 904 mInputBufferCount = 0; 905 mOutputBufferCount = 0; 906 mOutputDelayCompensated = 0; 907 mOutputDelayRingBufferWritePos = 0; 908 mOutputDelayRingBufferReadPos = 0; 909 mEndOfInput = false; 910 mEndOfOutput = false; 911 mAnchorTimes.clear(); 912 mLastInHeader = NULL; 913 914 // To make the codec behave the same before and after a reset, we need to invalidate the 915 // streaminfo struct. This does that: 916 mStreamInfo->sampleRate = 0; // TODO: mStreamInfo is read only 917 918 mSignalledError = false; 919 mOutputPortSettingsChange = NONE; 920} 921 922void SoftAAC2::onPortEnableCompleted(OMX_U32 portIndex, bool enabled) { 923 if (portIndex != 1) { 924 return; 925 } 926 927 switch (mOutputPortSettingsChange) { 928 case NONE: 929 break; 930 931 case AWAITING_DISABLED: 932 { 933 CHECK(!enabled); 934 mOutputPortSettingsChange = AWAITING_ENABLED; 935 break; 936 } 937 938 default: 939 { 940 CHECK_EQ((int)mOutputPortSettingsChange, (int)AWAITING_ENABLED); 941 CHECK(enabled); 942 mOutputPortSettingsChange = NONE; 943 break; 944 } 945 } 946} 947 948} // namespace android 949 950android::SoftOMXComponent *createSoftOMXComponent( 951 const char *name, const OMX_CALLBACKTYPE *callbacks, 952 OMX_PTR appData, OMX_COMPONENTTYPE **component) { 953 return new android::SoftAAC2(name, callbacks, appData, component); 954} 955