2 * Copyright 2012 Michael Chen <omxcodec@gmail.com>
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "SoftFFmpegAudio"
19 #include <utils/Log.h>
21 #include "SoftFFmpegAudio.h"
23 #include <media/stagefright/foundation/ADebug.h>
24 #include <media/stagefright/foundation/hexdump.h>
25 #include <media/stagefright/MediaDefs.h>
27 #include "ffmpeg_utils/ffmpeg_utils.h"
38 static void InitOMXParams(T *params) {
39 params->nSize = sizeof(T);
40 params->nVersion.s.nVersionMajor = 1;
41 params->nVersion.s.nVersionMinor = 0;
42 params->nVersion.s.nRevision = 0;
43 params->nVersion.s.nStep = 0;
46 SoftFFmpegAudio::SoftFFmpegAudio(
48 const OMX_CALLBACKTYPE *callbacks,
50 OMX_COMPONENTTYPE **component)
51 : SimpleSoftOMXComponent(name, callbacks, appData, component),
56 mExtradataReady(false),
57 mIgnoreExtradata(false),
58 mFlushComplete(false),
59 mSignalledError(false),
68 mAudioConfigChanged(false),
69 mOutputPortSettingsChange(NONE) {
70 if (!strcmp(name, "OMX.ffmpeg.mp3.decoder")) {
72 mIgnoreExtradata = true;
73 } else if (!strcmp(name, "OMX.ffmpeg.mp1.decoder")) {
75 } else if (!strcmp(name, "OMX.ffmpeg.mp2.decoder")) {
77 } else if (!strcmp(name, "OMX.ffmpeg.aac.decoder")) {
80 CHECK(!strcmp(name, "OMX.ffmpeg.ac3.decoder"));
84 LOGV("SoftFFmpegAudio component: %s", name);
87 CHECK_EQ(initDecoder(), (status_t)OK);
90 SoftFFmpegAudio::~SoftFFmpegAudio() {
92 LOGV("~SoftFFmpegAudio");
97 void SoftFFmpegAudio::initPorts() {
98 OMX_PARAM_PORTDEFINITIONTYPE def;
102 def.eDir = OMX_DirInput;
103 def.nBufferCountMin = kNumBuffers;
104 def.nBufferCountActual = def.nBufferCountMin;
105 def.nBufferSize = 8192;
106 def.bEnabled = OMX_TRUE;
107 def.bPopulated = OMX_FALSE;
108 def.eDomain = OMX_PortDomainAudio;
109 def.bBuffersContiguous = OMX_FALSE;
110 def.nBufferAlignment = 1;
114 def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_MPEG);
115 def.format.audio.eEncoding = OMX_AUDIO_CodingMP3;
118 def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_MPEG_LAYER_I);
119 def.format.audio.eEncoding = OMX_AUDIO_CodingMP3;
122 def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_MPEG_LAYER_II);
123 def.format.audio.eEncoding = OMX_AUDIO_CodingMP3;
126 def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_AAC);
127 def.format.audio.eEncoding = OMX_AUDIO_CodingAAC;
130 def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_AC3);
132 //def.format.audio.eEncoding = OMX_AUDIO_CodingAC3;
135 CHECK(!"Should not be here. Unsupported mime type and compression format");
139 def.format.audio.pNativeRender = NULL;
140 def.format.audio.bFlagErrorConcealment = OMX_FALSE;
145 def.eDir = OMX_DirOutput;
146 def.nBufferCountMin = kNumBuffers;
147 def.nBufferCountActual = def.nBufferCountMin;
148 def.nBufferSize = kOutputBufferSize;
149 def.bEnabled = OMX_TRUE;
150 def.bPopulated = OMX_FALSE;
151 def.eDomain = OMX_PortDomainAudio;
152 def.bBuffersContiguous = OMX_FALSE;
153 def.nBufferAlignment = 2;
155 def.format.audio.cMIMEType = const_cast<char *>("audio/raw");
156 def.format.audio.pNativeRender = NULL;
157 def.format.audio.bFlagErrorConcealment = OMX_FALSE;
158 def.format.audio.eEncoding = OMX_AUDIO_CodingPCM;
163 static int lockmgr(void **mtx, enum AVLockOp op) {
166 *mtx = (void *)SDL_CreateMutex();
171 return !!SDL_LockMutex((SDL_mutex *)*mtx);
172 case AV_LOCK_RELEASE:
173 return !!SDL_UnlockMutex((SDL_mutex *)*mtx);
174 case AV_LOCK_DESTROY:
175 SDL_DestroyMutex((SDL_mutex *)*mtx);
181 status_t SoftFFmpegAudio::initFFmpeg() {
182 //nam_av_log_set_flags(AV_LOG_SKIP_REPEATED);
183 av_log_set_level(AV_LOG_DEBUG);
184 av_log_set_callback(nam_av_log_callback);
186 /* register all codecs, demux and protocols */
187 avcodec_register_all();
189 avdevice_register_all();
192 avformat_network_init();
196 if (av_lockmgr_register(lockmgr)) {
197 LOGE("could not initialize lock manager!");
204 void SoftFFmpegAudio::deInitFFmpeg() {
205 av_lockmgr_register(NULL);
207 avformat_network_deinit();
210 void SoftFFmpegAudio::setAVCtxToDefault(AVCodecContext *avctx, const AVCodec *codec) {
213 avctx->workaround_bugs = 1;
215 if(avctx->lowres > codec->max_lowres){
216 LOGW("The maximum value for lowres supported by the decoder is %d",
218 avctx->lowres= codec->max_lowres;
220 avctx->idct_algo = 0;
221 avctx->skip_frame = AVDISCARD_DEFAULT;
222 avctx->skip_idct = AVDISCARD_DEFAULT;
223 avctx->skip_loop_filter = AVDISCARD_DEFAULT;
224 avctx->error_concealment = 3;
226 if(avctx->lowres) avctx->flags |= CODEC_FLAG_EMU_EDGE;
227 if (fast) avctx->flags2 |= CODEC_FLAG2_FAST;
228 if(codec->capabilities & CODEC_CAP_DR1)
229 avctx->flags |= CODEC_FLAG_EMU_EDGE;
232 status_t SoftFFmpegAudio::initDecoder() {
235 status = initFFmpeg();
239 mCtx = avcodec_alloc_context3(NULL);
242 LOGE("avcodec_alloc_context failed.");
243 return OMX_ErrorInsufficientResources;
246 mCtx->codec_type = AVMEDIA_TYPE_AUDIO;
249 mCtx->codec_id = CODEC_ID_MP3;
252 mCtx->codec_id = CODEC_ID_MP1;
255 mCtx->codec_id = CODEC_ID_MP2;
258 mCtx->codec_id = CODEC_ID_AAC;
261 mCtx->codec_id = CODEC_ID_AC3;
264 CHECK(!"Should not be here. Unsupported codec");
268 mCtx->codec = avcodec_find_decoder(mCtx->codec_id);
271 LOGE("find codec failed");
272 return OMX_ErrorNotImplemented;
275 setAVCtxToDefault(mCtx, mCtx->codec);
278 // FIXME, defer to open? ref: OMXCodec.cpp:setAudioOutputFormat
279 err = avcodec_open2(mCtx, mCtx->codec, NULL);
281 LOGE("ffmpeg audio decoder failed to initialize. (%d)", err);
282 return OMX_ErrorUndefined;
286 mCtx->sample_fmt = AV_SAMPLE_FMT_S16;
288 mAudioSrcFmt = mAudioTgtFmt = AV_SAMPLE_FMT_S16;
289 mAudioSrcFreq = mAudioTgtFreq = mSamplingRate;
290 mAudioSrcChannels = mAudioTgtChannels = mNumChannels;
291 mAudioSrcChannelLayout = mAudioTgtChannelLayout =
292 av_get_default_channel_layout(mNumChannels);
294 memset(mSilenceBuffer, 0, kOutputBufferSize);
296 return OMX_ErrorNone;
299 void SoftFFmpegAudio::deInitDecoder() {
301 avcodec_flush_buffers(mCtx);
302 if (!mCtx->extradata) {
303 av_free(mCtx->extradata);
304 mCtx->extradata = NULL;
305 mCtx->extradata_size = 0;
319 OMX_ERRORTYPE SoftFFmpegAudio::internalGetParameter(
320 OMX_INDEXTYPE index, OMX_PTR params) {
322 case OMX_IndexParamAudioAac:
324 OMX_AUDIO_PARAM_AACPROFILETYPE *aacParams =
325 (OMX_AUDIO_PARAM_AACPROFILETYPE *)params;
327 if (aacParams->nPortIndex != 0) {
328 return OMX_ErrorUndefined;
331 aacParams->nBitRate = 0;
332 aacParams->nAudioBandWidth = 0;
333 aacParams->nAACtools = 0;
334 aacParams->nAACERtools = 0;
335 aacParams->eAACProfile = OMX_AUDIO_AACObjectMain;
336 aacParams->eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4FF;
337 aacParams->eChannelMode = OMX_AUDIO_ChannelModeStereo;
339 aacParams->nChannels = mNumChannels;
340 aacParams->nSampleRate = mSamplingRate;
342 return OMX_ErrorNone;
344 case OMX_IndexParamAudioPcm:
346 OMX_AUDIO_PARAM_PCMMODETYPE *pcmParams =
347 (OMX_AUDIO_PARAM_PCMMODETYPE *)params;
349 if (pcmParams->nPortIndex > 1) {
350 return OMX_ErrorUndefined;
353 pcmParams->eNumData = OMX_NumericalDataSigned;
354 pcmParams->eEndian = OMX_EndianBig;
355 pcmParams->bInterleaved = OMX_TRUE;
356 pcmParams->nBitPerSample = 16;
357 pcmParams->ePCMMode = OMX_AUDIO_PCMModeLinear;
358 pcmParams->eChannelMapping[0] = OMX_AUDIO_ChannelLF;
359 pcmParams->eChannelMapping[1] = OMX_AUDIO_ChannelRF;
361 pcmParams->nChannels = mNumChannels;
362 pcmParams->nSamplingRate = mSamplingRate;
364 return OMX_ErrorNone;
368 return SimpleSoftOMXComponent::internalGetParameter(index, params);
372 OMX_ERRORTYPE SoftFFmpegAudio::internalSetParameter(
373 OMX_INDEXTYPE index, const OMX_PTR params) {
375 case OMX_IndexParamStandardComponentRole:
377 const OMX_PARAM_COMPONENTROLETYPE *roleParams =
378 (const OMX_PARAM_COMPONENTROLETYPE *)params;
380 bool supported = true;
383 if (strncmp((const char *)roleParams->cRole,
384 "audio_decoder.mp3", OMX_MAX_STRINGNAME_SIZE - 1))
388 if (strncmp((const char *)roleParams->cRole,
389 "audio_decoder.mp1", OMX_MAX_STRINGNAME_SIZE - 1))
393 if (strncmp((const char *)roleParams->cRole,
394 "audio_decoder.mp2", OMX_MAX_STRINGNAME_SIZE - 1))
398 if (strncmp((const char *)roleParams->cRole,
399 "audio_decoder.aac", OMX_MAX_STRINGNAME_SIZE - 1))
403 if (strncmp((const char *)roleParams->cRole,
404 "audio_decoder.ac3", OMX_MAX_STRINGNAME_SIZE - 1))
408 CHECK(!"Should not be here. Unsupported role.");
412 LOGE("unsupported role: %s", (const char *)roleParams->cRole);
413 return OMX_ErrorUndefined;
416 return OMX_ErrorNone;
418 case OMX_IndexParamAudioAac:
420 const OMX_AUDIO_PARAM_AACPROFILETYPE *aacParams =
421 (const OMX_AUDIO_PARAM_AACPROFILETYPE *)params;
423 if (aacParams->nPortIndex != 0) {
424 return OMX_ErrorUndefined;
427 mNumChannels = aacParams->nChannels;
428 mSamplingRate = aacParams->nSampleRate;
430 LOGV("got OMX_IndexParamAudioAac, mNumChannels: %d, mSamplingRate: %d",
431 mNumChannels, mSamplingRate);
433 return OMX_ErrorNone;
436 LOGI("internalSetParameter, index: 0x%x", index);
437 return SimpleSoftOMXComponent::internalSetParameter(index, params);
441 void SoftFFmpegAudio::onQueueFilled(OMX_U32 portIndex) {
444 size_t resampledDataSize = 0;
445 int64_t decChannelLayout;
446 int32_t inputBufferUsedLength = 0;
447 BufferInfo *inInfo = NULL;
448 OMX_BUFFERHEADERTYPE *inHeader = NULL;
450 if (mSignalledError || mOutputPortSettingsChange != NONE) {
454 List<BufferInfo *> &inQueue = getPortQueue(0);
455 List<BufferInfo *> &outQueue = getPortQueue(1);
457 while ((!inQueue.empty() || mInputBufferSize > 0 ||
458 mAudioBufferSize > 0 || mFlushComplete) && !outQueue.empty()) {
459 if (!inQueue.empty() || mInputBufferSize > 0) {
460 inInfo = *inQueue.begin();
461 inHeader = inInfo->mHeader;
467 BufferInfo *outInfo = *outQueue.begin();
468 OMX_BUFFERHEADERTYPE *outHeader = outInfo->mHeader;
470 if (!mAudioConfigChanged && mMode == MODE_AAC &&
471 (mCtx->channels != mNumChannels || mCtx->sample_rate != mSamplingRate)) {
472 LOGI("audio OMX_EventPortSettingsChanged, mCtx->channels: %d, mNumChannels: %d, mCtx->sample_rate: %d, mSamplingRate: %d",
473 mCtx->channels, mNumChannels, mCtx->sample_rate, mSamplingRate);
474 mCtx->channels = mNumChannels;
475 mCtx->sample_rate = mSamplingRate;
477 mAudioConfigChanged = true;
478 notify(OMX_EventPortSettingsChanged, 1, 0, NULL);
479 mOutputPortSettingsChange = AWAITING_DISABLED;
484 if (inHeader && inHeader->nFlags & OMX_BUFFERFLAG_EOS) {
485 inQueue.erase(inQueue.begin());
486 inInfo->mOwnedByUs = false;
487 notifyEmptyBufferDone(inHeader);
491 if (mReceivedEOS && (mFlushComplete || !(mCtx->codec->capabilities & CODEC_CAP_DELAY))) {
492 outHeader->nFilledLen = 0;
493 outHeader->nFlags = OMX_BUFFERFLAG_EOS;
495 outQueue.erase(outQueue.begin());
496 outInfo->mOwnedByUs = false;
497 notifyFillBufferDone(outHeader);
501 if (inHeader && inHeader->nFlags & OMX_BUFFERFLAG_CODECCONFIG) {
502 LOGI("got extradata, ignore: %d, size: %lu", mIgnoreExtradata, inHeader->nFilledLen);
503 hexdump(inHeader->pBuffer + inHeader->nOffset, inHeader->nFilledLen);
504 if (!mExtradataReady && !mIgnoreExtradata) {
505 int orig_extradata_size = mCtx->extradata_size;
506 mCtx->extradata_size += inHeader->nFilledLen;
507 mCtx->extradata = (uint8_t *)realloc(mCtx->extradata,
508 mCtx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
509 if (!mCtx->extradata) {
510 LOGE("ffmpeg audio decoder failed to alloc extradata memory.");
511 notify(OMX_EventError, OMX_ErrorInsufficientResources, 0, NULL);
512 mSignalledError = true;
516 memcpy(mCtx->extradata + orig_extradata_size,
517 inHeader->pBuffer + inHeader->nOffset, inHeader->nFilledLen);
518 memset(mCtx->extradata + mCtx->extradata_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
520 inInfo->mOwnedByUs = false;
521 inQueue.erase(inQueue.begin());
523 notifyEmptyBufferDone(inHeader);
528 if (mIgnoreExtradata) {
529 LOGI("got extradata, size: %lu, but ignore it", inHeader->nFilledLen);
530 inInfo->mOwnedByUs = false;
531 inQueue.erase(inQueue.begin());
533 notifyEmptyBufferDone(inHeader);
541 if (!mExtradataReady && !mIgnoreExtradata) {
542 LOGI("extradata is ready");
543 hexdump(mCtx->extradata, mCtx->extradata_size);
544 mExtradataReady = true;
546 LOGI("open ffmpeg decoder now");
548 err = avcodec_open2(mCtx, mCtx->codec, NULL);
550 LOGE("ffmpeg audio decoder failed to initialize. (%d)", err);
551 notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL);
552 mSignalledError = true;
558 /* update the audio clock with the pts */
559 if (inHeader && inHeader->nOffset == 0) {
560 mAnchorTimeUs = inHeader->nTimeStamp;
561 mNumFramesOutput = 0;
562 mInputBufferSize = inHeader->nFilledLen;
565 if (inHeader && mAudioBufferSize == 0 && !mFlushComplete) {
567 av_init_packet(&pkt);
568 if (!mFlushComplete) {
569 pkt.data = (uint8_t *)inHeader->pBuffer + inHeader->nOffset;
570 pkt.size = inHeader->nFilledLen;
571 pkt.pts = inHeader->nTimeStamp; // ingore it, we will compute it
575 pkt.pts = AV_NOPTS_VALUE;
578 LOGV("pkt size: %d, pts: %lld", pkt.size, pkt.pts);
581 if (!(mFrame = avcodec_alloc_frame())) {
582 LOGE("ffmpeg audio decoder failed to alloc memory.");
583 notify(OMX_EventError, OMX_ErrorInsufficientResources, 0, NULL);
584 mSignalledError = true;
588 avcodec_get_frame_defaults(mFrame);
592 inputBufferUsedLength = 0;
593 len = avcodec_decode_audio4(mCtx, mFrame, &gotFrm, &pkt);
595 LOGE("ffmpeg audio decoder failed to decode frame. (0x%x)", len);
596 inputBufferUsedLength = inHeader->nFilledLen;
597 /* if error, we skip the frame and play silence instead */
598 mPAudioBuffer = mSilenceBuffer;
599 mAudioBufferSize = kOutputBufferSize;
600 } else if (!gotFrm) {
601 LOGI("ffmpeg audio decoder failed to get frame.");
602 /* stop sending empty packets if the decoder is finished */
603 if (!pkt.data && mCtx->codec->capabilities & CODEC_CAP_DELAY)
604 mFlushComplete = true;
608 * FIXME, check mAudioConfigChanged when the first time you call the audio4!
609 * mCtx->sample_rate and mCtx->channels may be changed by audio decoder later, why???
611 if (!mAudioConfigChanged) {
612 if (mCtx->channels != mNumChannels || mCtx->sample_rate != mSamplingRate) {
613 LOGI("audio OMX_EventPortSettingsChanged, mCtx->channels: %d, mNumChannels: %d, mCtx->sample_rate: %d, mSamplingRate: %d",
614 mCtx->channels, mNumChannels, mCtx->sample_rate, mSamplingRate);
615 mNumChannels = mCtx->channels;
616 mSamplingRate = mCtx->sample_rate;
617 mAudioConfigChanged = true;
618 notify(OMX_EventPortSettingsChanged, 1, 0, NULL);
619 mOutputPortSettingsChange = AWAITING_DISABLED;
622 // match with the default, set mAudioConfigChanged true anyway!
623 mAudioConfigChanged = true;
627 inputBufferUsedLength = len;
628 mPAudioBuffer = mFrame->data[0];
629 mAudioBufferSize = av_samples_get_buffer_size(NULL, mCtx->channels,
631 mCtx->sample_fmt, 1);
632 LOGV("ffmpeg audio decoder get frame. (%d), mAudioBufferSize: %d", len, mAudioBufferSize);
636 size_t copyToOutputBufferLen = mAudioBufferSize;
637 if (mAudioBufferSize > kOutputBufferSize)
638 copyToOutputBufferLen = kOutputBufferSize;
640 outHeader->nOffset = 0;
641 outHeader->nFilledLen = copyToOutputBufferLen;
642 outHeader->nTimeStamp = mAnchorTimeUs
643 + (mNumFramesOutput * 1000000ll) / mSamplingRate;
644 memcpy(outHeader->pBuffer, mPAudioBuffer, copyToOutputBufferLen);
645 outHeader->nFlags = 0;
647 mPAudioBuffer += copyToOutputBufferLen;
648 mAudioBufferSize -= copyToOutputBufferLen;
649 mNumFramesOutput += copyToOutputBufferLen / av_get_bytes_per_sample(mCtx->sample_fmt) / mNumChannels;
652 CHECK_GE(inHeader->nFilledLen, inputBufferUsedLength);
653 inHeader->nOffset += inputBufferUsedLength;
654 inHeader->nFilledLen -= inputBufferUsedLength;
655 mInputBufferSize -= inputBufferUsedLength;
656 if (inHeader->nFilledLen == 0) {
657 inInfo->mOwnedByUs = false;
658 inQueue.erase(inQueue.begin());
660 notifyEmptyBufferDone(inHeader);
663 mInputBufferSize = 0;
667 outInfo->mOwnedByUs = false;
668 outQueue.erase(outQueue.begin());
670 notifyFillBufferDone(outHeader);
675 void SoftFFmpegAudio::onPortFlushCompleted(OMX_U32 portIndex) {
676 if (portIndex == 0 && mCtx) {
677 // Make sure that the next buffer output does not still
678 // depend on fragments from the last one decoded.
679 avcodec_flush_buffers(mCtx);
684 void SoftFFmpegAudio::onPortEnableCompleted(OMX_U32 portIndex, bool enabled) {
685 if (portIndex != 1) {
689 switch (mOutputPortSettingsChange) {
693 case AWAITING_DISABLED:
696 mOutputPortSettingsChange = AWAITING_ENABLED;
702 CHECK_EQ((int)mOutputPortSettingsChange, (int)AWAITING_ENABLED);
704 mOutputPortSettingsChange = NONE;
710 } // namespace android
712 android::SoftOMXComponent *createSoftOMXComponent(
713 const char *name, const OMX_CALLBACKTYPE *callbacks,
714 OMX_PTR appData, OMX_COMPONENTTYPE **component) {
715 return new android::SoftFFmpegAudio(name, callbacks, appData, component);