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.
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 <OMX_FFExt.h>
29 #include "utils/ffmpeg_utils.h"
40 static void InitOMXParams(T *params) {
41 params->nSize = sizeof(T);
42 params->nVersion.s.nVersionMajor = 1;
43 params->nVersion.s.nVersionMinor = 0;
44 params->nVersion.s.nRevision = 0;
45 params->nVersion.s.nStep = 0;
48 void SoftFFmpegAudio::setMode(const char *name) {
49 if (!strcmp(name, "OMX.ffmpeg.mp3.decoder")) {
51 mIgnoreExtradata = true;
52 } else if (!strcmp(name, "OMX.ffmpeg.mp1.decoder")) {
54 } else if (!strcmp(name, "OMX.ffmpeg.mp2.decoder")) {
56 } else if (!strcmp(name, "OMX.ffmpeg.aac.decoder")) {
58 } else if (!strcmp(name, "OMX.ffmpeg.wma.decoder")) {
60 } else if (!strcmp(name, "OMX.ffmpeg.ra.decoder")) {
62 } else if (!strcmp(name, "OMX.ffmpeg.ac3.decoder")) {
64 } else if (!strcmp(name, "OMX.ffmpeg.ape.decoder")) {
66 } else if (!strcmp(name, "OMX.ffmpeg.dts.decoder")) {
68 } else if (!strcmp(name, "OMX.ffmpeg.flac.decoder")) {
70 } else if (!strcmp(name, "OMX.ffmpeg.vorbis.decoder")) {
72 } else if (!strcmp(name, "OMX.ffmpeg.aheuristic.decoder")) {
73 mMode = MODE_HEURISTIC;
79 SoftFFmpegAudio::SoftFFmpegAudio(
81 const OMX_CALLBACKTYPE *callbacks,
83 OMX_COMPONENTTYPE **component)
84 : SimpleSoftOMXComponent(name, callbacks, appData, component),
89 mCodecAlreadyOpened(false),
90 mExtradataReady(false),
91 mIgnoreExtradata(false),
92 mFlushComplete(false),
93 mSignalledError(false),
99 mResampledDataSize(0),
100 mAudioConfigChanged(false),
101 mOutputPortSettingsChange(NONE) {
105 ALOGD("SoftFFmpegAudio component: %s mMode: %d", name, mMode);
108 CHECK_EQ(initDecoder(), (status_t)OK);
111 SoftFFmpegAudio::~SoftFFmpegAudio() {
112 ALOGV("~SoftFFmpegAudio");
120 void SoftFFmpegAudio::initPorts() {
121 OMX_PARAM_PORTDEFINITIONTYPE def;
125 def.eDir = OMX_DirInput;
126 def.nBufferCountMin = kNumBuffers;
127 def.nBufferCountActual = def.nBufferCountMin;
128 if (mMode == MODE_APE) {
129 def.nBufferSize = 1000000; // ape!
130 } else if (mMode == MODE_DTS) {
131 def.nBufferSize = 1000000; // dts!
133 def.nBufferSize = 20480; // 8192 is too small
135 def.bEnabled = OMX_TRUE;
136 def.bPopulated = OMX_FALSE;
137 def.eDomain = OMX_PortDomainAudio;
138 def.bBuffersContiguous = OMX_FALSE;
139 def.nBufferAlignment = 1;
143 def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_MPEG);
144 def.format.audio.eEncoding = OMX_AUDIO_CodingMP3;
147 def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_MPEG_LAYER_I);
148 def.format.audio.eEncoding = OMX_AUDIO_CodingMP3;
151 def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_MPEG_LAYER_II);
152 def.format.audio.eEncoding = OMX_AUDIO_CodingMP3;
155 def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_AAC);
156 def.format.audio.eEncoding = OMX_AUDIO_CodingAAC;
159 def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_AC3);
161 //def.format.audio.eEncoding = OMX_AUDIO_CodingAC3;
162 def.format.audio.eEncoding = OMX_AUDIO_CodingAutoDetect; // right?? orz
165 def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_WMA);
166 def.format.audio.eEncoding = OMX_AUDIO_CodingWMA;
169 def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_RA);
170 def.format.audio.eEncoding = OMX_AUDIO_CodingRA;
173 def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_APE);
174 def.format.audio.eEncoding = OMX_AUDIO_CodingAPE;
177 def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_DTS);
178 def.format.audio.eEncoding = OMX_AUDIO_CodingDTS;
181 def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_FLAC);
182 def.format.audio.eEncoding = OMX_AUDIO_CodingFLAC;
185 def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_VORBIS);
186 def.format.audio.eEncoding = OMX_AUDIO_CodingVORBIS;
189 def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_FFMPEG);
190 def.format.audio.eEncoding = OMX_AUDIO_CodingAutoDetect;
193 CHECK(!"Should not be here. Unsupported mime type and compression format");
197 def.format.audio.pNativeRender = NULL;
198 def.format.audio.bFlagErrorConcealment = OMX_FALSE;
203 def.eDir = OMX_DirOutput;
204 def.nBufferCountMin = kNumBuffers;
205 def.nBufferCountActual = def.nBufferCountMin;
206 def.nBufferSize = kOutputBufferSize;
207 def.bEnabled = OMX_TRUE;
208 def.bPopulated = OMX_FALSE;
209 def.eDomain = OMX_PortDomainAudio;
210 def.bBuffersContiguous = OMX_FALSE;
211 def.nBufferAlignment = 2;
213 def.format.audio.cMIMEType = const_cast<char *>("audio/raw");
214 def.format.audio.pNativeRender = NULL;
215 def.format.audio.bFlagErrorConcealment = OMX_FALSE;
216 def.format.audio.eEncoding = OMX_AUDIO_CodingPCM;
221 void SoftFFmpegAudio::setAVCtxToDefault(AVCodecContext *avctx, const AVCodec *codec) {
224 avctx->workaround_bugs = 1;
226 if(avctx->lowres > codec->max_lowres){
227 ALOGW("The maximum value for lowres supported by the decoder is %d",
229 avctx->lowres= codec->max_lowres;
231 avctx->idct_algo = 0;
232 avctx->skip_frame = AVDISCARD_DEFAULT;
233 avctx->skip_idct = AVDISCARD_DEFAULT;
234 avctx->skip_loop_filter = AVDISCARD_DEFAULT;
235 avctx->error_concealment = 3;
237 if(avctx->lowres) avctx->flags |= CODEC_FLAG_EMU_EDGE;
238 if (fast) avctx->flags2 |= CODEC_FLAG2_FAST;
239 if(codec->capabilities & CODEC_CAP_DR1)
240 avctx->flags |= CODEC_FLAG_EMU_EDGE;
244 void SoftFFmpegAudio::configDefaultCtx() {
246 mCtx->sample_rate = 44100;
248 mCtx->sample_fmt = AV_SAMPLE_FMT_S16;
250 mAudioSrcChannels = mAudioTgtChannels = 2;
251 mAudioSrcFreq = mAudioTgtFreq = 44100;
252 mAudioSrcFmt = mAudioTgtFmt = AV_SAMPLE_FMT_S16;
253 mAudioSrcChannelLayout = mAudioTgtChannelLayout =
254 av_get_default_channel_layout(2);
257 status_t SoftFFmpegAudio::initDecoder() {
260 status = initFFmpeg();
264 mFFmpegInited = true;
266 mCtx = avcodec_alloc_context3(NULL);
269 ALOGE("avcodec_alloc_context failed.");
273 mCtx->codec_type = AVMEDIA_TYPE_AUDIO;
276 mCtx->codec_id = CODEC_ID_MP3;
279 mCtx->codec_id = CODEC_ID_MP1;
282 mCtx->codec_id = CODEC_ID_MP2;
285 mCtx->codec_id = CODEC_ID_AAC;
288 mCtx->codec_id = CODEC_ID_AC3;
291 mCtx->codec_id = CODEC_ID_WMAV2; // FIXME, CODEC_ID_WMAV1 or CODEC_ID_WMAV2?
294 mCtx->codec_id = CODEC_ID_COOK;
297 mCtx->codec_id = CODEC_ID_APE;
300 mCtx->codec_id = CODEC_ID_DTS;
303 mCtx->codec_id = CODEC_ID_FLAC;
306 mCtx->codec_id = CODEC_ID_VORBIS;
309 mCtx->codec_id = CODEC_ID_NONE;
312 CHECK(!"Should not be here. Unsupported codec");
319 mCtx->extradata = NULL;
320 mCtx->extradata_size = 0;
322 memset(mSilenceBuffer, 0, kOutputBufferSize);
327 void SoftFFmpegAudio::deInitDecoder() {
329 //avcodec_flush_buffers(mCtx); // is it necessary? crash sometimes if call it
330 if (!mCtx->extradata) {
331 av_free(mCtx->extradata);
332 mCtx->extradata = NULL;
333 mCtx->extradata_size = 0;
346 OMX_ERRORTYPE SoftFFmpegAudio::internalGetParameter(
347 OMX_INDEXTYPE index, OMX_PTR params) {
349 case OMX_IndexParamAudioAac:
351 OMX_AUDIO_PARAM_AACPROFILETYPE *profile =
352 (OMX_AUDIO_PARAM_AACPROFILETYPE *)params;
354 if (profile->nPortIndex != 0) {
355 return OMX_ErrorUndefined;
358 profile->nBitRate = 0;
359 profile->nAudioBandWidth = 0;
360 profile->nAACtools = 0;
361 profile->nAACERtools = 0;
362 profile->eAACProfile = OMX_AUDIO_AACObjectMain;
363 profile->eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4FF;
364 profile->eChannelMode = OMX_AUDIO_ChannelModeStereo;
366 profile->nChannels = 0;
367 profile->nSampleRate = 0;
369 return OMX_ErrorNone;
371 case OMX_IndexParamAudioWma:
373 OMX_AUDIO_PARAM_WMATYPE *profile =
374 (OMX_AUDIO_PARAM_WMATYPE *)params;
376 if (profile->nPortIndex != 0) {
377 return OMX_ErrorUndefined;
380 profile->nChannels = 0;
381 profile->nSamplingRate = 0;
382 profile->nBitRate = 0;
383 profile->eFormat = OMX_AUDIO_WMAFormatUnused;
385 return OMX_ErrorNone;
387 case OMX_IndexParamAudioRa:
389 OMX_AUDIO_PARAM_RATYPE *profile =
390 (OMX_AUDIO_PARAM_RATYPE *)params;
392 if (profile->nPortIndex != 0) {
393 return OMX_ErrorUndefined;
396 profile->nChannels = 0;
397 profile->nSamplingRate = 0;
398 profile->eFormat = OMX_AUDIO_RAFormatUnused;
400 return OMX_ErrorNone;
402 case OMX_IndexParamAudioApe:
404 OMX_AUDIO_PARAM_APETYPE *profile =
405 (OMX_AUDIO_PARAM_APETYPE *)params;
407 if (profile->nPortIndex != 0) {
408 return OMX_ErrorUndefined;
411 profile->nChannels = 0;
412 profile->nSamplingRate = 0;
414 return OMX_ErrorNone;
416 case OMX_IndexParamAudioDts:
418 OMX_AUDIO_PARAM_DTSTYPE *profile =
419 (OMX_AUDIO_PARAM_DTSTYPE *)params;
421 if (profile->nPortIndex != 0) {
422 return OMX_ErrorUndefined;
425 profile->nChannels = 0;
426 profile->nSamplingRate = 0;
428 return OMX_ErrorNone;
430 case OMX_IndexParamAudioFlac:
432 OMX_AUDIO_PARAM_FLACTYPE *profile =
433 (OMX_AUDIO_PARAM_FLACTYPE *)params;
435 if (profile->nPortIndex != 0) {
436 return OMX_ErrorUndefined;
439 profile->nChannels = 0;
440 profile->nSampleRate = 0;
442 return OMX_ErrorNone;
444 case OMX_IndexParamAudioPcm:
446 OMX_AUDIO_PARAM_PCMMODETYPE *profile =
447 (OMX_AUDIO_PARAM_PCMMODETYPE *)params;
449 if (profile->nPortIndex > 1) {
450 return OMX_ErrorUndefined;
453 profile->eNumData = OMX_NumericalDataSigned;
454 profile->eEndian = OMX_EndianBig;
455 profile->bInterleaved = OMX_TRUE;
456 profile->nBitPerSample = 16;
457 profile->ePCMMode = OMX_AUDIO_PCMModeLinear;
458 profile->eChannelMapping[0] = OMX_AUDIO_ChannelLF;
459 profile->eChannelMapping[1] = OMX_AUDIO_ChannelRF;
461 profile->nChannels = 2;
462 profile->nSamplingRate = 44100;
464 return OMX_ErrorNone;
466 case OMX_IndexParamAudioVorbis:
468 OMX_AUDIO_PARAM_VORBISTYPE *profile =
469 (OMX_AUDIO_PARAM_VORBISTYPE *)params;
471 if (profile->nPortIndex != 0) {
472 return OMX_ErrorUndefined;
475 profile->nBitRate = 0;
476 profile->nMinBitRate = 0;
477 profile->nMaxBitRate = 0;
478 profile->nAudioBandWidth = 0;
479 profile->nQuality = 3;
480 profile->bManaged = OMX_FALSE;
481 profile->bDownmix = OMX_FALSE;
483 profile->nChannels = 1;
484 profile->nSampleRate = 44100;
486 if (!isConfigured()) {
487 profile->nChannels = 1;
488 profile->nSampleRate = 44100;
490 profile->nChannels = mVi->channels;
491 profile->nSampleRate = mVi->rate;
492 profile->nBitRate = mVi->bitrate_nominal;
493 profile->nMinBitRate = mVi->bitrate_lower;
494 profile->nMaxBitRate = mVi->bitrate_upper;
497 return OMX_ErrorNone;
501 if ((OMX_FF_INDEXTYPE)index == OMX_IndexParamAudioFFmpeg)
503 OMX_AUDIO_PARAM_FFMPEGTYPE *profile =
504 (OMX_AUDIO_PARAM_FFMPEGTYPE *)params;
506 if (profile->nPortIndex != 0) {
507 return OMX_ErrorUndefined;
510 profile->eCodecId = 0;
511 profile->nChannels = 0;
512 profile->nBitRate = 0;
513 profile->nBitsPerSample = 0;
514 profile->nSampleRate = 0;
515 profile->nBlockAlign = 0;
516 profile->eSampleFormat = 0;
518 return OMX_ErrorNone;
521 return SimpleSoftOMXComponent::internalGetParameter(index, params);
525 OMX_ERRORTYPE SoftFFmpegAudio::isRoleSupported(
526 const OMX_PARAM_COMPONENTROLETYPE *roleParams) {
527 bool supported = true;
531 if (strncmp((const char *)roleParams->cRole,
532 "audio_decoder.mp3", OMX_MAX_STRINGNAME_SIZE - 1))
536 if (strncmp((const char *)roleParams->cRole,
537 "audio_decoder.mp1", OMX_MAX_STRINGNAME_SIZE - 1))
541 if (strncmp((const char *)roleParams->cRole,
542 "audio_decoder.mp2", OMX_MAX_STRINGNAME_SIZE - 1))
546 if (strncmp((const char *)roleParams->cRole,
547 "audio_decoder.aac", OMX_MAX_STRINGNAME_SIZE - 1))
551 if (strncmp((const char *)roleParams->cRole,
552 "audio_decoder.ac3", OMX_MAX_STRINGNAME_SIZE - 1))
556 if (strncmp((const char *)roleParams->cRole,
557 "audio_decoder.wma", OMX_MAX_STRINGNAME_SIZE - 1))
561 if (strncmp((const char *)roleParams->cRole,
562 "audio_decoder.ra", OMX_MAX_STRINGNAME_SIZE - 1))
566 if (strncmp((const char *)roleParams->cRole,
567 "audio_decoder.ape", OMX_MAX_STRINGNAME_SIZE - 1))
571 if (strncmp((const char *)roleParams->cRole,
572 "audio_decoder.dts", OMX_MAX_STRINGNAME_SIZE - 1))
576 if (strncmp((const char *)roleParams->cRole,
577 "audio_decoder.flac", OMX_MAX_STRINGNAME_SIZE - 1))
581 if (strncmp((const char *)roleParams->cRole,
582 "audio_decoder.vorbis", OMX_MAX_STRINGNAME_SIZE - 1))
586 if (strncmp((const char *)roleParams->cRole,
587 "audio_decoder.heuristic", OMX_MAX_STRINGNAME_SIZE - 1))
591 CHECK(!"Should not be here. Unsupported role.");
596 ALOGE("unsupported role: %s", (const char *)roleParams->cRole);
597 return OMX_ErrorUndefined;
600 return OMX_ErrorNone;
603 void SoftFFmpegAudio::adjustAudioParameter() {
604 int32_t channels = 0;
605 int32_t sampling_rate = 0;
607 sampling_rate = mCtx->sample_rate;
609 //channels support 1 or 2 only
610 channels = mCtx->channels >= 2 ? 2 : 1;
612 //4000 <= sampling rate <= 48000
613 if (sampling_rate < 4000) {
614 sampling_rate = 4000;
615 } else if (sampling_rate > 48000) {
616 sampling_rate = 48000;
619 mAudioSrcChannels = mAudioTgtChannels = channels;
620 mAudioSrcFreq = mAudioTgtFreq = sampling_rate;
621 mAudioSrcFmt = mAudioTgtFmt = AV_SAMPLE_FMT_S16;
622 mAudioSrcChannelLayout = mAudioTgtChannelLayout =
623 av_get_default_channel_layout(channels);
626 OMX_ERRORTYPE SoftFFmpegAudio::internalSetParameter(
627 OMX_INDEXTYPE index, const OMX_PTR params) {
629 case OMX_IndexParamStandardComponentRole:
631 const OMX_PARAM_COMPONENTROLETYPE *roleParams =
632 (const OMX_PARAM_COMPONENTROLETYPE *)params;
633 return isRoleSupported(roleParams);
635 case OMX_IndexParamAudioPcm:
637 const OMX_AUDIO_PARAM_PCMMODETYPE *profile =
638 (const OMX_AUDIO_PARAM_PCMMODETYPE *)params;
640 if (profile->nPortIndex != 1) {
641 return OMX_ErrorUndefined;
644 mCtx->channels = profile->nChannels;
645 mCtx->sample_rate = profile->nSamplingRate;
646 mCtx->bits_per_coded_sample = profile->nBitPerSample;
648 ALOGV("got OMX_IndexParamAudioPcm, nChannels:%lu, "
649 "nSampleRate:%lu, nBitsPerSample:%lu",
650 profile->nChannels, profile->nSamplingRate,
651 profile->nBitPerSample);
653 return OMX_ErrorNone;
655 case OMX_IndexParamAudioAac:
657 const OMX_AUDIO_PARAM_AACPROFILETYPE *profile =
658 (const OMX_AUDIO_PARAM_AACPROFILETYPE *)params;
660 if (profile->nPortIndex != 0) {
661 return OMX_ErrorUndefined;
664 mCtx->channels = profile->nChannels;
665 mCtx->sample_rate = profile->nSampleRate;
667 adjustAudioParameter();
669 ALOGV("got OMX_IndexParamAudioAac, nChannels:%lu, nSamplingRate:%lu",
670 profile->nChannels, profile->nSampleRate);
672 return OMX_ErrorNone;
674 case OMX_IndexParamAudioWma:
676 OMX_AUDIO_PARAM_WMATYPE *profile =
677 (OMX_AUDIO_PARAM_WMATYPE *)params;
679 if (profile->nPortIndex != 0) {
680 return OMX_ErrorUndefined;
683 if (profile->eFormat == OMX_AUDIO_WMAFormat7) {
684 mCtx->codec_id = CODEC_ID_WMAV2;
685 } else if (profile->eFormat == OMX_AUDIO_WMAFormat8) {
686 mCtx->codec_id = CODEC_ID_WMAPRO;
687 } else if (profile->eFormat == OMX_AUDIO_WMAFormat9) {
688 mCtx->codec_id = CODEC_ID_WMALOSSLESS;
690 ALOGE("unsupported wma codec: 0x%x", profile->eFormat);
691 return OMX_ErrorUndefined;
694 mCtx->channels = profile->nChannels;
695 mCtx->sample_rate = profile->nSamplingRate;
697 // wmadec needs bitrate, block_align
698 mCtx->bit_rate = profile->nBitRate;
699 mCtx->block_align = profile->nBlockAlign;
701 adjustAudioParameter();
703 ALOGV("got OMX_IndexParamAudioWma, nChannels:%u, "
704 "nSampleRate:%lu, nBitRate:%lu, nBlockAlign:%u",
705 profile->nChannels, profile->nSamplingRate,
706 profile->nBitRate, profile->nBlockAlign);
708 return OMX_ErrorNone;
710 case OMX_IndexParamAudioRa:
712 OMX_AUDIO_PARAM_RATYPE *profile =
713 (OMX_AUDIO_PARAM_RATYPE *)params;
715 if (profile->nPortIndex != 0) {
716 return OMX_ErrorUndefined;
719 mCtx->channels = profile->nChannels;
720 mCtx->sample_rate = profile->nSamplingRate;
722 // FIXME, HACK!!!, I use the nNumRegions parameter pass blockAlign!!!
723 // the cook audio codec need blockAlign!
724 mCtx->block_align = profile->nNumRegions;
726 adjustAudioParameter();
728 ALOGV("got OMX_IndexParamAudioRa, nChannels:%lu, "
729 "nSampleRate:%lu, nBlockAlign:%d",
730 profile->nChannels, profile->nSamplingRate, mCtx->block_align);
732 return OMX_ErrorNone;
734 case OMX_IndexParamAudioApe:
736 OMX_AUDIO_PARAM_APETYPE *profile =
737 (OMX_AUDIO_PARAM_APETYPE *)params;
739 if (profile->nPortIndex != 0) {
740 return OMX_ErrorUndefined;
743 mCtx->channels = profile->nChannels;
744 mCtx->sample_rate = profile->nSamplingRate;
746 // ape decoder need bits_per_coded_sample
747 mCtx->bits_per_coded_sample = profile->nBitsPerSample;
749 adjustAudioParameter();
751 ALOGV("got OMX_IndexParamAudioApe, nChannels:%lu, "
752 "nSampleRate:%lu, nBitsPerSample:%lu",
753 profile->nChannels, profile->nSamplingRate,
754 profile->nBitsPerSample);
756 return OMX_ErrorNone;
758 case OMX_IndexParamAudioDts:
760 OMX_AUDIO_PARAM_DTSTYPE *profile =
761 (OMX_AUDIO_PARAM_DTSTYPE *)params;
763 if (profile->nPortIndex != 0) {
764 return OMX_ErrorUndefined;
767 mCtx->channels = profile->nChannels;
768 mCtx->sample_rate = profile->nSamplingRate;
770 adjustAudioParameter();
772 ALOGV("got OMX_IndexParamAudioDts, nChannels:%lu, nSamplingRate:%lu",
773 profile->nChannels, profile->nSamplingRate);
775 return OMX_ErrorNone;
777 case OMX_IndexParamAudioFlac:
779 OMX_AUDIO_PARAM_FLACTYPE *profile =
780 (OMX_AUDIO_PARAM_FLACTYPE *)params;
782 if (profile->nPortIndex != 0) {
783 return OMX_ErrorUndefined;
786 mCtx->channels = profile->nChannels;
787 mCtx->sample_rate = profile->nSampleRate;
789 adjustAudioParameter();
791 ALOGV("got OMX_IndexParamAudioFlac, nChannels:%lu, nSamplingRate:%lu",
792 profile->nChannels, profile->nSampleRate);
794 return OMX_ErrorNone;
796 case OMX_IndexParamAudioVorbis:
798 const OMX_AUDIO_PARAM_VORBISTYPE *profile =
799 (const OMX_AUDIO_PARAM_VORBISTYPE *)params;
801 if (profile->nPortIndex != 0) {
802 return OMX_ErrorUndefined;
805 mCtx->channels = profile->nChannels;
806 mCtx->sample_rate = profile->nSampleRate;
808 ALOGD("got OMX_IndexParamAudioVorbis, "
809 "nChannels=%lu, nSampleRate=%lu, nBitRate=%lu, "
810 "nMinBitRate=%lu, nMaxBitRate=%lu",
811 profile->nChannels, profile->nSampleRate,
812 profile->nBitRate, profile->nMinBitRate,
813 profile->nMaxBitRate);
815 return OMX_ErrorNone;
818 if ((OMX_FF_INDEXTYPE)index == OMX_IndexParamAudioFFmpeg)
820 OMX_AUDIO_PARAM_FFMPEGTYPE *profile =
821 (OMX_AUDIO_PARAM_FFMPEGTYPE *)params;
823 if (profile->nPortIndex != 0) {
824 return OMX_ErrorUndefined;
827 mCtx->codec_id = (enum AVCodecID)profile->eCodecId;
828 mCtx->channels = profile->nChannels;
829 mCtx->bit_rate = profile->nBitRate;
830 mCtx->bits_per_coded_sample = profile->nBitsPerSample;
831 mCtx->sample_rate = profile->nSampleRate;
832 mCtx->block_align = profile->nBlockAlign;
833 mCtx->sample_fmt = (AVSampleFormat)profile->eSampleFormat;
835 adjustAudioParameter();
837 ALOGD("got OMX_IndexParamAudioFFmpeg, "
838 "eCodecId:%ld(%s), nChannels:%lu, nBitRate:%lu, "
839 "nBitsPerSample:%lu, nSampleRate:%lu, "
840 "nBlockAlign:%lu, eSampleFormat:%lu(%s)",
841 profile->eCodecId, avcodec_get_name(mCtx->codec_id),
842 profile->nChannels, profile->nBitRate,
843 profile->nBitsPerSample, profile->nSampleRate,
844 profile->nBlockAlign, profile->eSampleFormat,
845 av_get_sample_fmt_name(mCtx->sample_fmt));
847 return OMX_ErrorNone;
850 ALOGI("internalSetParameter, index: 0x%x", index);
851 return SimpleSoftOMXComponent::internalSetParameter(index, params);
855 void SoftFFmpegAudio::onQueueFilled(OMX_U32 portIndex) {
859 int64_t decChannelLayout = 0;
860 int32_t inputBufferUsedLength = 0;
861 BufferInfo *inInfo = NULL;
862 OMX_BUFFERHEADERTYPE *inHeader = NULL;
864 if (mSignalledError || mOutputPortSettingsChange != NONE) {
868 List<BufferInfo *> &inQueue = getPortQueue(0);
869 List<BufferInfo *> &outQueue = getPortQueue(1);
871 while ((!inQueue.empty() //there are input buffers to be emptied
872 || mResampledDataSize > 0 //there are left decoded frame
873 || mReceivedEOS) //need to fill eos outbuffer
874 && !outQueue.empty()) { //there are out buffers to be filled
875 if (!inQueue.empty()) {
876 inInfo = *inQueue.begin();
877 inHeader = inInfo->mHeader;
883 BufferInfo *outInfo = *outQueue.begin();
884 OMX_BUFFERHEADERTYPE *outHeader = outInfo->mHeader;
887 if (inHeader && inHeader->nFlags & OMX_BUFFERFLAG_EOS) {
888 CHECK(mInputBufferSize == 0);
889 ALOGD("ffmpeg audio decoder empty eos inbuf");
890 inQueue.erase(inQueue.begin());
891 inInfo->mOwnedByUs = false;
892 notifyEmptyBufferDone(inHeader);
894 continue; //empty eos input buffer
898 if (mReceivedEOS && (mFlushComplete || !(mCtx->codec->capabilities & CODEC_CAP_DELAY))) {
899 ALOGD("ffmpeg audio decoder fill eos outbuf");
900 outHeader->nFilledLen = 0;
901 outHeader->nFlags = OMX_BUFFERFLAG_EOS;
903 outQueue.erase(outQueue.begin());
904 outInfo->mOwnedByUs = false;
905 notifyFillBufferDone(outHeader);
910 if (inHeader && inHeader->nFlags & OMX_BUFFERFLAG_CODECCONFIG) {
911 ALOGI("got extradata, ignore: %d, size: %lu", mIgnoreExtradata, inHeader->nFilledLen);
912 hexdump(inHeader->pBuffer + inHeader->nOffset, inHeader->nFilledLen);
914 if (mIgnoreExtradata) {
915 ALOGI("got extradata, size: %lu, but ignore it", inHeader->nFilledLen);
916 inInfo->mOwnedByUs = false;
917 inQueue.erase(inQueue.begin());
919 notifyEmptyBufferDone(inHeader);
925 if (!mExtradataReady) {
926 int orig_extradata_size = mCtx->extradata_size;
927 mCtx->extradata_size += inHeader->nFilledLen;
928 mCtx->extradata = (uint8_t *)av_realloc(mCtx->extradata,
929 mCtx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
930 if (!mCtx->extradata) {
931 ALOGE("ffmpeg audio decoder failed to alloc extradata memory.");
932 notify(OMX_EventError, OMX_ErrorInsufficientResources, 0, NULL);
933 mSignalledError = true;
937 memcpy(mCtx->extradata + orig_extradata_size,
938 inHeader->pBuffer + inHeader->nOffset, inHeader->nFilledLen);
939 memset(mCtx->extradata + mCtx->extradata_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
941 inInfo->mOwnedByUs = false;
942 inQueue.erase(inQueue.begin());
944 notifyEmptyBufferDone(inHeader);
952 if (!mCodecAlreadyOpened) {
953 if (!mExtradataReady && !mIgnoreExtradata) {
954 ALOGI("extradata is ready, size: %d", mCtx->extradata_size);
955 hexdump(mCtx->extradata, mCtx->extradata_size);
956 mExtradataReady = true;
960 mCtx->codec = avcodec_find_decoder(mCtx->codec_id);
962 ALOGE("ffmpeg audio decoder failed to find codec");
963 notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL);
964 mSignalledError = true;
968 setAVCtxToDefault(mCtx, mCtx->codec);
970 ALOGD("begin to open ffmpeg audio decoder, mCtx sample_rate: %d, "
971 "channels: %d, , sample_fmt: %s",
972 mCtx->sample_rate, mCtx->channels,
973 av_get_sample_fmt_name(mCtx->sample_fmt));
975 err = avcodec_open2(mCtx, mCtx->codec, NULL);
977 ALOGE("ffmpeg audio decoder failed to initialize. (%d)", err);
978 notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL);
979 mSignalledError = true;
983 mCodecAlreadyOpened = true;
985 ALOGD("open ffmpeg audio decoder success, mCtx sample_rate: %d, "
986 "channels: %d, sample_fmt: %s",
987 mCtx->sample_rate, mCtx->channels,
988 av_get_sample_fmt_name(mCtx->sample_fmt));
991 //update the audio clock with the pts
992 if (inHeader && inHeader->nOffset == 0
993 && mInputBufferSize == 0 // the last input buffer have be emptied
994 && mResampledDataSize == 0) { // the last decoded frame have be filled
995 if (inHeader->nTimeStamp != SF_NOPTS_VALUE) {
996 mAudioClock = inHeader->nTimeStamp; //if no pts in input buffer
998 //reset to AV_NOPTS_VALUE
999 inHeader->nTimeStamp = AV_NOPTS_VALUE;
1001 mInputBufferSize = inHeader->nFilledLen;
1004 if ((inHeader && mResampledDataSize == 0) //hope to get more decoded frame while have not read eos pkt
1005 || (mReceivedEOS && !mFlushComplete)) { //hope to get left decoded frame while have read eos pkt
1007 memset(&pkt, 0, sizeof(pkt));
1008 av_init_packet(&pkt);
1011 CHECK(mResampledDataSize == 0);
1012 pkt.data = (uint8_t *)inHeader->pBuffer + inHeader->nOffset;
1013 pkt.size = inHeader->nFilledLen;
1014 pkt.pts = inHeader->nTimeStamp; // ingore it, we will compute it
1016 CHECK(mReceivedEOS == true);
1017 CHECK(mFlushComplete == false);
1020 pkt.pts = AV_NOPTS_VALUE;
1023 if (pkt.pts != AV_NOPTS_VALUE)
1025 ALOGV("pkt size:%d, pts:%lld", pkt.size, pkt.pts);
1027 ALOGV("pkt size:%d, pts:N/A", pkt.size);
1031 if (!(mFrame = avcodec_alloc_frame())) {
1032 ALOGE("ffmpeg audio decoder failed to alloc memory.");
1033 notify(OMX_EventError, OMX_ErrorInsufficientResources, 0, NULL);
1034 mSignalledError = true;
1038 avcodec_get_frame_defaults(mFrame);
1042 inputBufferUsedLength = 0;
1044 //decode audio packet
1045 len = avcodec_decode_audio4(mCtx, mFrame, &gotFrm, &pkt);
1047 // A negative error code is returned if an error occurred during decoding
1049 ALOGE("ffmpeg audio decoder failed to decode frame. "
1050 "consume pkt len: %d", len);
1052 /* if !mAudioConfigChanged, Don't fill the out buffer */
1053 if (!mAudioConfigChanged) {
1054 inInfo->mOwnedByUs = false;
1055 inQueue.erase(inQueue.begin());
1057 notifyEmptyBufferDone(inHeader);
1060 mInputBufferSize = 0; // need?
1064 //inputBufferUsedLength = inHeader->nFilledLen;
1065 /* if error, we skip the frame and play silence instead */
1066 mResampledData = mSilenceBuffer;
1067 mResampledDataSize = kOutputBufferSize; //FIXME, need to calculate the size
1071 ALOGV("ffmpeg audio decoder, consume pkt len: %d", len);
1075 CHECK(mFlushComplete == false);
1078 inputBufferUsedLength = mInputBufferSize;
1080 inputBufferUsedLength = len;
1083 CHECK_GE(inHeader->nFilledLen, inputBufferUsedLength);
1084 inHeader->nOffset += inputBufferUsedLength;
1085 inHeader->nFilledLen -= inputBufferUsedLength;
1086 mInputBufferSize -= inputBufferUsedLength;
1087 if (inHeader->nFilledLen == 0) {
1088 inInfo->mOwnedByUs = false;
1089 inQueue.erase(inQueue.begin());
1091 notifyEmptyBufferDone(inHeader);
1094 //reset mInputBufferSize
1095 mInputBufferSize = 0;
1100 ALOGI("ffmpeg audio decoder failed to get more frame.");
1101 /* stop sending empty packets if the decoder is finished */
1102 if (!pkt.data && mCtx->codec->capabilities & CODEC_CAP_DELAY)
1103 mFlushComplete = true;
1109 * FIXME, check mAudioConfigChanged when the first time you call the audio4!
1110 * mCtx->sample_rate and mCtx->channels may be changed by audio decoder later, why???
1112 if (!mAudioConfigChanged) {
1113 //if (mCtx->channels != mNumChannels || mCtx->sample_rate != mSamplingRate
1114 // || mCtx->sample_fmt != mSamplingFmt) {
1115 if (mCtx->channels != mNumChannels || mCtx->sample_rate != mSamplingRate) {
1116 ALOGI("audio OMX_EventPortSettingsChanged, mCtx->channels: %d, "
1117 "mNumChannels: %d, mCtx->sample_rate: %d, mSamplingRate: %d, "
1118 "mCtx->sample_fmt: %s, mSamplingFmt: %s",
1119 mCtx->channels, mNumChannels, mCtx->sample_rate, mSamplingRate,
1120 av_get_sample_fmt_name(mCtx->sample_fmt),
1121 av_get_sample_fmt_name(mSamplingFmt));
1122 mNumChannels = mCtx->channels;
1123 mSamplingRate = mCtx->sample_rate;
1124 mSamplingFmt = mCtx->sample_fmt;
1125 mAudioConfigChanged = true;
1126 notify(OMX_EventPortSettingsChanged, 1, 0, NULL);
1127 mOutputPortSettingsChange = AWAITING_DISABLED;
1130 // match with the default, set mAudioConfigChanged true anyway!
1131 mAudioConfigChanged = true;
1132 mSamplingFmt = mCtx->sample_fmt;
1137 dataSize = av_samples_get_buffer_size(NULL, mCtx->channels,
1138 mFrame->nb_samples, mCtx->sample_fmt, 1);
1141 ALOGV("ffmpeg audio decoder, nb_samples:%d, get buffer size:%d",
1142 mFrame->nb_samples, dataSize);
1146 (mCtx->channel_layout && mCtx->channels == av_get_channel_layout_nb_channels(mCtx->channel_layout)) ?
1147 mCtx->channel_layout : av_get_default_channel_layout(mCtx->channels);
1149 if (mCtx->sample_fmt != mAudioSrcFmt
1150 || decChannelLayout != mAudioSrcChannelLayout
1151 || mCtx->sample_rate != mAudioSrcFreq) {
1154 mSwrCtx = swr_alloc_set_opts(NULL,
1155 mAudioTgtChannelLayout, mAudioTgtFmt, mAudioTgtFreq,
1156 decChannelLayout, mCtx->sample_fmt, mCtx->sample_rate,
1158 if (!mSwrCtx || swr_init(mSwrCtx) < 0) {
1159 ALOGE("Cannot create sample rate converter for conversion "
1160 "of %d Hz %s %d channels to %d Hz %s %d channels!",
1162 av_get_sample_fmt_name(mCtx->sample_fmt),
1165 av_get_sample_fmt_name(mAudioTgtFmt),
1167 notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL);
1168 mSignalledError = true;
1172 char src_layout_name[256] = {0};
1173 char tgt_layout_name[256] = {0};
1174 av_get_channel_layout_string(src_layout_name, sizeof(src_layout_name),
1175 mCtx->channels, decChannelLayout);
1176 av_get_channel_layout_string(tgt_layout_name, sizeof(tgt_layout_name),
1177 mAudioTgtChannels, mAudioTgtChannelLayout);
1178 ALOGI("Create sample rate converter for conversion "
1179 "of %d Hz %s %d channels(%s) "
1180 "to %d Hz %s %d channels(%s)!",
1182 av_get_sample_fmt_name(mCtx->sample_fmt),
1186 av_get_sample_fmt_name(mAudioTgtFmt),
1190 mAudioSrcChannelLayout = decChannelLayout;
1191 mAudioSrcChannels = mCtx->channels;
1192 mAudioSrcFreq = mCtx->sample_rate;
1193 mAudioSrcFmt = mCtx->sample_fmt;
1197 const uint8_t **in = (const uint8_t **)mFrame->extended_data;
1198 uint8_t *out[] = {mAudioBuffer2};
1199 int out_count = sizeof(mAudioBuffer2) / mAudioTgtChannels /
1200 av_get_bytes_per_sample(mAudioTgtFmt);
1202 ALOGV("swr_convert 1, out_count: %d, mFrame->nb_samples: %d, "
1203 "src frm: %s, tgt frm: %s",
1204 out_count, mFrame->nb_samples,
1205 av_get_sample_fmt_name(mCtx->sample_fmt),
1206 av_get_sample_fmt_name(mAudioTgtFmt));
1208 int len2 = swr_convert(mSwrCtx, out, out_count, in, mFrame->nb_samples);
1210 ALOGE("audio_resample() failed");
1213 if (len2 == out_count) {
1214 ALOGE("warning: audio buffer is probably too small");
1217 mResampledData = mAudioBuffer2;
1218 mResampledDataSize = len2 * mAudioTgtChannels *
1219 av_get_bytes_per_sample(mAudioTgtFmt);
1221 mResampledData = mFrame->data[0];
1222 mResampledDataSize = dataSize;
1226 ALOGV("ffmpeg audio decoder get frame. consume pkt len: %d, "
1227 "nb_samples(before resample): %d, mResampledDataSize: %d",
1228 len, mFrame->nb_samples, mResampledDataSize);
1230 } //if ((inHeader && mResampledDataSize == 0) || (mReceivedEOS && !mFlushComplete))
1232 size_t copyToOutputBufferLen = mResampledDataSize;
1233 if (mResampledDataSize > kOutputBufferSize)
1234 copyToOutputBufferLen = kOutputBufferSize;
1236 outHeader->nOffset = 0;
1237 outHeader->nFilledLen = copyToOutputBufferLen;
1238 outHeader->nTimeStamp = mAudioClock;
1239 memcpy(outHeader->pBuffer, mResampledData, copyToOutputBufferLen);
1240 outHeader->nFlags = 0;
1243 ALOGV("ffmpeg audio decoder, fill out buffer, "
1244 "pts: %lld, mNumFramesOutput: %lld",
1245 outHeader->nTimeStamp, mNumFramesOutput);
1248 mResampledData += copyToOutputBufferLen;
1249 mResampledDataSize -= copyToOutputBufferLen;
1250 mNumFramesOutput += copyToOutputBufferLen /
1251 (av_get_bytes_per_sample(mAudioTgtFmt) * mAudioTgtChannels);
1254 //mAudioClock += (mNumFramesOutput * 1000000ll) / mAudioTgtFreq;
1256 mAudioClock += copyToOutputBufferLen /
1257 (mAudioTgtChannels * mAudioTgtFreq * av_get_bytes_per_sample(mAudioTgtFmt));
1258 //(mCtx->channels * mCtx->sample_rate * av_get_bytes_per_sample(mCtx->sample_fmt));
1260 // reset mNumFramesOutput
1261 if (mInputBufferSize == 0 && mResampledDataSize == 0) {
1263 ALOGV("~~~~ reset mNumFramesOutput to 0");
1265 mNumFramesOutput = 0;
1268 outInfo->mOwnedByUs = false;
1269 outQueue.erase(outQueue.begin());
1271 notifyFillBufferDone(outHeader);
1276 void SoftFFmpegAudio::onPortFlushCompleted(OMX_U32 portIndex) {
1277 ALOGV("ffmpeg audio decoder flush port(%lu)", portIndex);
1278 if (portIndex == 0 && mCtx) {
1279 // Make sure that the next buffer output does not still
1280 // depend on fragments from the last one decoded.
1281 avcodec_flush_buffers(mCtx);
1283 mReceivedEOS = false;
1284 mFlushComplete = false;
1286 mInputBufferSize = 0;
1287 mNumFramesOutput = 0;
1288 mResampledDataSize = 0;
1289 mResampledData = NULL;
1290 //don't free mFrame!
1293 void SoftFFmpegAudio::onPortEnableCompleted(OMX_U32 portIndex, bool enabled) {
1294 if (portIndex != 1) {
1298 switch (mOutputPortSettingsChange) {
1302 case AWAITING_DISABLED:
1305 mOutputPortSettingsChange = AWAITING_ENABLED;
1311 CHECK_EQ((int)mOutputPortSettingsChange, (int)AWAITING_ENABLED);
1313 mOutputPortSettingsChange = NONE;
1319 } // namespace android
1321 android::SoftOMXComponent *createSoftOMXComponent(
1322 const char *name, const OMX_CALLBACKTYPE *callbacks,
1323 OMX_PTR appData, OMX_COMPONENTTYPE **component) {
1324 return new android::SoftFFmpegAudio(name, callbacks, appData, component);