SoftAAC2.cpp revision e35ac2860e99d809a2ccca59bd4eb2f8c02d15ad
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#include <utils/Log.h> 19 20#include "SoftAAC2.h" 21 22#include <media/stagefright/foundation/ADebug.h> 23#include <media/stagefright/foundation/hexdump.h> 24#include <media/stagefright/MediaErrors.h> 25 26#define FILEREAD_MAX_LAYERS 2 27 28namespace android { 29 30template<class T> 31static void InitOMXParams(T *params) { 32 params->nSize = sizeof(T); 33 params->nVersion.s.nVersionMajor = 1; 34 params->nVersion.s.nVersionMinor = 0; 35 params->nVersion.s.nRevision = 0; 36 params->nVersion.s.nStep = 0; 37} 38 39SoftAAC2::SoftAAC2( 40 const char *name, 41 const OMX_CALLBACKTYPE *callbacks, 42 OMX_PTR appData, 43 OMX_COMPONENTTYPE **component) 44 : SimpleSoftOMXComponent(name, callbacks, appData, component), 45 mAACDecoder(NULL), 46 mStreamInfo(NULL), 47 mIsADTS(false), 48 mInputBufferCount(0), 49 mSignalledError(false), 50 mAnchorTimeUs(0), 51 mNumSamplesOutput(0), 52 mOutputPortSettingsChange(NONE) { 53 initPorts(); 54 CHECK_EQ(initDecoder(), (status_t)OK); 55} 56 57SoftAAC2::~SoftAAC2() { 58 aacDecoder_Close(mAACDecoder); 59} 60 61void SoftAAC2::initPorts() { 62 OMX_PARAM_PORTDEFINITIONTYPE def; 63 InitOMXParams(&def); 64 65 def.nPortIndex = 0; 66 def.eDir = OMX_DirInput; 67 def.nBufferCountMin = kNumInputBuffers; 68 def.nBufferCountActual = def.nBufferCountMin; 69 def.nBufferSize = 8192; 70 def.bEnabled = OMX_TRUE; 71 def.bPopulated = OMX_FALSE; 72 def.eDomain = OMX_PortDomainAudio; 73 def.bBuffersContiguous = OMX_FALSE; 74 def.nBufferAlignment = 1; 75 76 def.format.audio.cMIMEType = const_cast<char *>("audio/aac"); 77 def.format.audio.pNativeRender = NULL; 78 def.format.audio.bFlagErrorConcealment = OMX_FALSE; 79 def.format.audio.eEncoding = OMX_AUDIO_CodingAAC; 80 81 addPort(def); 82 83 def.nPortIndex = 1; 84 def.eDir = OMX_DirOutput; 85 def.nBufferCountMin = kNumOutputBuffers; 86 def.nBufferCountActual = def.nBufferCountMin; 87 def.nBufferSize = 8192 * 2; 88 def.bEnabled = OMX_TRUE; 89 def.bPopulated = OMX_FALSE; 90 def.eDomain = OMX_PortDomainAudio; 91 def.bBuffersContiguous = OMX_FALSE; 92 def.nBufferAlignment = 2; 93 94 def.format.audio.cMIMEType = const_cast<char *>("audio/raw"); 95 def.format.audio.pNativeRender = NULL; 96 def.format.audio.bFlagErrorConcealment = OMX_FALSE; 97 def.format.audio.eEncoding = OMX_AUDIO_CodingPCM; 98 99 addPort(def); 100} 101 102status_t SoftAAC2::initDecoder() { 103 status_t status = UNKNOWN_ERROR; 104 mAACDecoder = aacDecoder_Open(TT_MP4_ADIF, /* num layers */ 1); 105 if (mAACDecoder != NULL) { 106 mStreamInfo = aacDecoder_GetStreamInfo(mAACDecoder); 107 if (mStreamInfo != NULL) { 108 status = OK; 109 } 110 } 111 mIsFirst = true; 112 return status; 113} 114 115OMX_ERRORTYPE SoftAAC2::internalGetParameter( 116 OMX_INDEXTYPE index, OMX_PTR params) { 117 switch (index) { 118 case OMX_IndexParamAudioAac: 119 { 120 OMX_AUDIO_PARAM_AACPROFILETYPE *aacParams = 121 (OMX_AUDIO_PARAM_AACPROFILETYPE *)params; 122 123 if (aacParams->nPortIndex != 0) { 124 return OMX_ErrorUndefined; 125 } 126 127 aacParams->nBitRate = 0; 128 aacParams->nAudioBandWidth = 0; 129 aacParams->nAACtools = 0; 130 aacParams->nAACERtools = 0; 131 aacParams->eAACProfile = OMX_AUDIO_AACObjectMain; 132 133 aacParams->eAACStreamFormat = 134 mIsADTS 135 ? OMX_AUDIO_AACStreamFormatMP4ADTS 136 : OMX_AUDIO_AACStreamFormatMP4FF; 137 138 aacParams->eChannelMode = OMX_AUDIO_ChannelModeStereo; 139 140 if (!isConfigured()) { 141 aacParams->nChannels = 1; 142 aacParams->nSampleRate = 44100; 143 aacParams->nFrameLength = 0; 144 } else { 145 aacParams->nChannels = mStreamInfo->numChannels; 146 aacParams->nSampleRate = mStreamInfo->sampleRate; 147 aacParams->nFrameLength = mStreamInfo->frameSize; 148 } 149 150 return OMX_ErrorNone; 151 } 152 153 case OMX_IndexParamAudioPcm: 154 { 155 OMX_AUDIO_PARAM_PCMMODETYPE *pcmParams = 156 (OMX_AUDIO_PARAM_PCMMODETYPE *)params; 157 158 if (pcmParams->nPortIndex != 1) { 159 return OMX_ErrorUndefined; 160 } 161 162 pcmParams->eNumData = OMX_NumericalDataSigned; 163 pcmParams->eEndian = OMX_EndianBig; 164 pcmParams->bInterleaved = OMX_TRUE; 165 pcmParams->nBitPerSample = 16; 166 pcmParams->ePCMMode = OMX_AUDIO_PCMModeLinear; 167 pcmParams->eChannelMapping[0] = OMX_AUDIO_ChannelLF; 168 pcmParams->eChannelMapping[1] = OMX_AUDIO_ChannelRF; 169 pcmParams->eChannelMapping[2] = OMX_AUDIO_ChannelCF; 170 pcmParams->eChannelMapping[3] = OMX_AUDIO_ChannelLFE; 171 pcmParams->eChannelMapping[4] = OMX_AUDIO_ChannelLS; 172 pcmParams->eChannelMapping[5] = OMX_AUDIO_ChannelRS; 173 174 if (!isConfigured()) { 175 pcmParams->nChannels = 1; 176 pcmParams->nSamplingRate = 44100; 177 } else { 178 pcmParams->nChannels = mStreamInfo->numChannels; 179 pcmParams->nSamplingRate = mStreamInfo->sampleRate; 180 } 181 182 return OMX_ErrorNone; 183 } 184 185 default: 186 return SimpleSoftOMXComponent::internalGetParameter(index, params); 187 } 188 189} 190 191OMX_ERRORTYPE SoftAAC2::internalSetParameter( 192 OMX_INDEXTYPE index, const OMX_PTR params) { 193 switch (index) { 194 case OMX_IndexParamStandardComponentRole: 195 { 196 const OMX_PARAM_COMPONENTROLETYPE *roleParams = 197 (const OMX_PARAM_COMPONENTROLETYPE *)params; 198 199 if (strncmp((const char *)roleParams->cRole, 200 "audio_decoder.aac", 201 OMX_MAX_STRINGNAME_SIZE - 1)) { 202 return OMX_ErrorUndefined; 203 } 204 205 return OMX_ErrorNone; 206 } 207 208 case OMX_IndexParamAudioAac: 209 { 210 const OMX_AUDIO_PARAM_AACPROFILETYPE *aacParams = 211 (const OMX_AUDIO_PARAM_AACPROFILETYPE *)params; 212 213 if (aacParams->nPortIndex != 0) { 214 return OMX_ErrorUndefined; 215 } 216 217 if (aacParams->eAACStreamFormat == OMX_AUDIO_AACStreamFormatMP4FF) { 218 mIsADTS = false; 219 } else if (aacParams->eAACStreamFormat 220 == OMX_AUDIO_AACStreamFormatMP4ADTS) { 221 mIsADTS = true; 222 } else { 223 return OMX_ErrorUndefined; 224 } 225 226 return OMX_ErrorNone; 227 } 228 229 case OMX_IndexParamAudioPcm: 230 { 231 const OMX_AUDIO_PARAM_PCMMODETYPE *pcmParams = 232 (OMX_AUDIO_PARAM_PCMMODETYPE *)params; 233 234 if (pcmParams->nPortIndex != 1) { 235 return OMX_ErrorUndefined; 236 } 237 238 return OMX_ErrorNone; 239 } 240 241 default: 242 return SimpleSoftOMXComponent::internalSetParameter(index, params); 243 } 244} 245 246bool SoftAAC2::isConfigured() const { 247 return mInputBufferCount > 0; 248} 249 250void SoftAAC2::onQueueFilled(OMX_U32 portIndex) { 251 if (mSignalledError || mOutputPortSettingsChange != NONE) { 252 return; 253 } 254 255 UCHAR* inBuffer[FILEREAD_MAX_LAYERS]; 256 UINT inBufferLength[FILEREAD_MAX_LAYERS] = {0}; 257 UINT bytesValid[FILEREAD_MAX_LAYERS] = {0}; 258 259 List<BufferInfo *> &inQueue = getPortQueue(0); 260 List<BufferInfo *> &outQueue = getPortQueue(1); 261 262 if (portIndex == 0 && mInputBufferCount == 0) { 263 ++mInputBufferCount; 264 BufferInfo *info = *inQueue.begin(); 265 OMX_BUFFERHEADERTYPE *header = info->mHeader; 266 267 inBuffer[0] = header->pBuffer + header->nOffset; 268 inBufferLength[0] = header->nFilledLen; 269 270 AAC_DECODER_ERROR decoderErr = 271 aacDecoder_ConfigRaw(mAACDecoder, 272 inBuffer, 273 inBufferLength); 274 275 if (decoderErr != AAC_DEC_OK) { 276 mSignalledError = true; 277 notify(OMX_EventError, OMX_ErrorUndefined, decoderErr, NULL); 278 return; 279 } 280 inQueue.erase(inQueue.begin()); 281 info->mOwnedByUs = false; 282 notifyEmptyBufferDone(header); 283 284 ALOGI("Configuring decoder: %d Hz, %d channels", 285 mStreamInfo->sampleRate, 286 mStreamInfo->numChannels); 287 notify(OMX_EventPortSettingsChanged, 1, 0, NULL); 288 mOutputPortSettingsChange = AWAITING_DISABLED; 289 return; 290 } 291 292 while (!inQueue.empty() && !outQueue.empty()) { 293 BufferInfo *inInfo = *inQueue.begin(); 294 OMX_BUFFERHEADERTYPE *inHeader = inInfo->mHeader; 295 296 BufferInfo *outInfo = *outQueue.begin(); 297 OMX_BUFFERHEADERTYPE *outHeader = outInfo->mHeader; 298 299 if (inHeader->nFlags & OMX_BUFFERFLAG_EOS) { 300 inQueue.erase(inQueue.begin()); 301 inInfo->mOwnedByUs = false; 302 notifyEmptyBufferDone(inHeader); 303 304 // flush out the decoder's delayed data by calling DecodeFrame one more time, with 305 // the AACDEC_FLUSH flag set 306 INT_PCM *outBuffer = 307 reinterpret_cast<INT_PCM *>(outHeader->pBuffer + outHeader->nOffset); 308 AAC_DECODER_ERROR decoderErr = aacDecoder_DecodeFrame(mAACDecoder, 309 outBuffer, 310 outHeader->nAllocLen, 311 AACDEC_FLUSH); 312 if (decoderErr != AAC_DEC_OK) { 313 mSignalledError = true; 314 notify(OMX_EventError, OMX_ErrorUndefined, decoderErr, NULL); 315 return; 316 } 317 318 outHeader->nFilledLen = 319 mStreamInfo->frameSize * sizeof(int16_t) * mStreamInfo->numChannels; 320 outHeader->nFlags = OMX_BUFFERFLAG_EOS; 321 322 outQueue.erase(outQueue.begin()); 323 outInfo->mOwnedByUs = false; 324 notifyFillBufferDone(outHeader); 325 return; 326 } 327 328 if (inHeader->nOffset == 0) { 329 mAnchorTimeUs = inHeader->nTimeStamp; 330 mNumSamplesOutput = 0; 331 } 332 333 size_t adtsHeaderSize = 0; 334 if (mIsADTS) { 335 // skip 30 bits, aac_frame_length follows. 336 // ssssssss ssssiiip ppffffPc ccohCCll llllllll lll????? 337 338 const uint8_t *adtsHeader = inHeader->pBuffer + inHeader->nOffset; 339 340 bool signalError = false; 341 if (inHeader->nFilledLen < 7) { 342 ALOGE("Audio data too short to contain even the ADTS header. " 343 "Got %ld bytes.", inHeader->nFilledLen); 344 hexdump(adtsHeader, inHeader->nFilledLen); 345 signalError = true; 346 } else { 347 bool protectionAbsent = (adtsHeader[1] & 1); 348 349 unsigned aac_frame_length = 350 ((adtsHeader[3] & 3) << 11) 351 | (adtsHeader[4] << 3) 352 | (adtsHeader[5] >> 5); 353 354 if (inHeader->nFilledLen < aac_frame_length) { 355 ALOGE("Not enough audio data for the complete frame. " 356 "Got %ld bytes, frame size according to the ADTS " 357 "header is %u bytes.", 358 inHeader->nFilledLen, aac_frame_length); 359 hexdump(adtsHeader, inHeader->nFilledLen); 360 signalError = true; 361 } else { 362 adtsHeaderSize = (protectionAbsent ? 7 : 9); 363 364 inBuffer[0] = (UCHAR *)adtsHeader + adtsHeaderSize; 365 inBufferLength[0] = aac_frame_length - adtsHeaderSize; 366 367 inHeader->nOffset += adtsHeaderSize; 368 inHeader->nFilledLen -= adtsHeaderSize; 369 } 370 } 371 372 if (signalError) { 373 mSignalledError = true; 374 375 notify(OMX_EventError, 376 OMX_ErrorStreamCorrupt, 377 ERROR_MALFORMED, 378 NULL); 379 380 return; 381 } 382 } else { 383 inBuffer[0] = inHeader->pBuffer + inHeader->nOffset; 384 inBufferLength[0] = inHeader->nFilledLen; 385 } 386 387 // Fill and decode 388 INT_PCM *outBuffer = reinterpret_cast<INT_PCM *>(outHeader->pBuffer + outHeader->nOffset); 389 bytesValid[0] = inBufferLength[0]; 390 391 int prevSampleRate = mStreamInfo->sampleRate; 392 int prevNumChannels = mStreamInfo->numChannels; 393 394 AAC_DECODER_ERROR decoderErr = AAC_DEC_NOT_ENOUGH_BITS; 395 while (bytesValid[0] > 0 && decoderErr == AAC_DEC_NOT_ENOUGH_BITS) { 396 aacDecoder_Fill(mAACDecoder, 397 inBuffer, 398 inBufferLength, 399 bytesValid); 400 401 decoderErr = aacDecoder_DecodeFrame(mAACDecoder, 402 outBuffer, 403 outHeader->nAllocLen, 404 0 /* flags */); 405 406 } 407 408 /* 409 * AAC+/eAAC+ streams can be signalled in two ways: either explicitly 410 * or implicitly, according to MPEG4 spec. AAC+/eAAC+ is a dual 411 * rate system and the sampling rate in the final output is actually 412 * doubled compared with the core AAC decoder sampling rate. 413 * 414 * Explicit signalling is done by explicitly defining SBR audio object 415 * type in the bitstream. Implicit signalling is done by embedding 416 * SBR content in AAC extension payload specific to SBR, and hence 417 * requires an AAC decoder to perform pre-checks on actual audio frames. 418 * 419 * Thus, we could not say for sure whether a stream is 420 * AAC+/eAAC+ until the first data frame is decoded. 421 */ 422 if (mInputBufferCount <= 2) { 423 if (mStreamInfo->sampleRate != prevSampleRate || 424 mStreamInfo->numChannels != prevNumChannels) { 425 ALOGI("Reconfiguring decoder: %d Hz, %d channels", 426 mStreamInfo->sampleRate, 427 mStreamInfo->numChannels); 428 429 // We're going to want to revisit this input buffer, but 430 // may have already advanced the offset. Undo that if 431 // necessary. 432 inHeader->nOffset -= adtsHeaderSize; 433 inHeader->nFilledLen += adtsHeaderSize; 434 435 notify(OMX_EventPortSettingsChanged, 1, 0, NULL); 436 mOutputPortSettingsChange = AWAITING_DISABLED; 437 return; 438 } 439 } else if (!mStreamInfo->sampleRate || !mStreamInfo->numChannels) { 440 ALOGW("Invalid AAC stream"); 441 mSignalledError = true; 442 notify(OMX_EventError, OMX_ErrorUndefined, decoderErr, NULL); 443 return; 444 } 445 446 size_t numOutBytes = 447 mStreamInfo->frameSize * sizeof(int16_t) * mStreamInfo->numChannels; 448 449 if (decoderErr == AAC_DEC_OK) { 450 UINT inBufferUsedLength = inBufferLength[0] - bytesValid[0]; 451 inHeader->nFilledLen -= inBufferUsedLength; 452 inHeader->nOffset += inBufferUsedLength; 453 } else { 454 ALOGW("AAC decoder returned error %d, substituting silence", 455 decoderErr); 456 457 memset(outHeader->pBuffer + outHeader->nOffset, 0, numOutBytes); 458 459 // Discard input buffer. 460 inHeader->nFilledLen = 0; 461 462 aacDecoder_SetParam(mAACDecoder, AAC_TPDEC_CLEAR_BUFFER, 1); 463 464 // fall through 465 } 466 467 if (decoderErr == AAC_DEC_OK || mNumSamplesOutput > 0) { 468 // We'll only output data if we successfully decoded it or 469 // we've previously decoded valid data, in the latter case 470 // (decode failed) we'll output a silent frame. 471 if (mIsFirst) { 472 mIsFirst = false; 473 // the first decoded frame should be discarded to account for decoder delay 474 numOutBytes = 0; 475 } 476 477 outHeader->nFilledLen = numOutBytes; 478 outHeader->nFlags = 0; 479 480 outHeader->nTimeStamp = 481 mAnchorTimeUs 482 + (mNumSamplesOutput * 1000000ll) / mStreamInfo->sampleRate; 483 484 mNumSamplesOutput += mStreamInfo->frameSize; 485 486 outInfo->mOwnedByUs = false; 487 outQueue.erase(outQueue.begin()); 488 outInfo = NULL; 489 notifyFillBufferDone(outHeader); 490 outHeader = NULL; 491 } 492 493 if (inHeader->nFilledLen == 0) { 494 inInfo->mOwnedByUs = false; 495 inQueue.erase(inQueue.begin()); 496 inInfo = NULL; 497 notifyEmptyBufferDone(inHeader); 498 inHeader = NULL; 499 } 500 501 if (decoderErr == AAC_DEC_OK) { 502 ++mInputBufferCount; 503 } 504 } 505} 506 507void SoftAAC2::onPortFlushCompleted(OMX_U32 portIndex) { 508 if (portIndex == 0) { 509 // Make sure that the next buffer output does not still 510 // depend on fragments from the last one decoded. 511 aacDecoder_SetParam(mAACDecoder, AAC_TPDEC_CLEAR_BUFFER, 1); 512 mIsFirst = true; 513 } 514} 515 516void SoftAAC2::onPortEnableCompleted(OMX_U32 portIndex, bool enabled) { 517 if (portIndex != 1) { 518 return; 519 } 520 521 switch (mOutputPortSettingsChange) { 522 case NONE: 523 break; 524 525 case AWAITING_DISABLED: 526 { 527 CHECK(!enabled); 528 mOutputPortSettingsChange = AWAITING_ENABLED; 529 break; 530 } 531 532 default: 533 { 534 CHECK_EQ((int)mOutputPortSettingsChange, (int)AWAITING_ENABLED); 535 CHECK(enabled); 536 mOutputPortSettingsChange = NONE; 537 break; 538 } 539 } 540} 541 542} // namespace android 543 544android::SoftOMXComponent *createSoftOMXComponent( 545 const char *name, const OMX_CALLBACKTYPE *callbacks, 546 OMX_PTR appData, OMX_COMPONENTTYPE **component) { 547 return new android::SoftAAC2(name, callbacks, appData, component); 548} 549