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