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 "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),
57 mExtradataReady(false),
58 mIgnoreExtradata(false),
59 mFlushComplete(false),
60 mSignalledError(false),
71 mSamplingFmt(AV_SAMPLE_FMT_S16),
72 mAudioConfigChanged(false),
73 mOutputPortSettingsChange(NONE) {
74 if (!strcmp(name, "OMX.ffmpeg.mp3.decoder")) {
76 mIgnoreExtradata = true;
77 } else if (!strcmp(name, "OMX.ffmpeg.mp1.decoder")) {
79 } else if (!strcmp(name, "OMX.ffmpeg.mp2.decoder")) {
81 } else if (!strcmp(name, "OMX.ffmpeg.aac.decoder")) {
83 } else if (!strcmp(name, "OMX.ffmpeg.wma.decoder")) {
85 } else if (!strcmp(name, "OMX.ffmpeg.ra.decoder")) {
87 } else if (!strcmp(name, "OMX.ffmpeg.ac3.decoder")) {
89 } else if (!strcmp(name, "OMX.ffmpeg.ape.decoder")) {
91 } else if (!strcmp(name, "OMX.ffmpeg.dts.decoder")) {
93 } else if (!strcmp(name, "OMX.ffmpeg.flac.decoder")) {
95 } else if (!strcmp(name, "OMX.ffmpeg.vorbis.decoder")) {
101 ALOGD("SoftFFmpegAudio component: %s mMode: %d", name, mMode);
104 CHECK_EQ(initDecoder(), (status_t)OK);
107 SoftFFmpegAudio::~SoftFFmpegAudio() {
108 ALOGV("~SoftFFmpegAudio");
116 void SoftFFmpegAudio::initPorts() {
117 OMX_PARAM_PORTDEFINITIONTYPE def;
121 def.eDir = OMX_DirInput;
122 def.nBufferCountMin = kNumBuffers;
123 def.nBufferCountActual = def.nBufferCountMin;
124 if (mMode == MODE_APE) {
125 def.nBufferSize = 1000000; // ape!
126 } else if (mMode == MODE_DTS) {
127 def.nBufferSize = 1000000; // dts!
129 def.nBufferSize = 20480; // 8192 is too small
131 def.bEnabled = OMX_TRUE;
132 def.bPopulated = OMX_FALSE;
133 def.eDomain = OMX_PortDomainAudio;
134 def.bBuffersContiguous = OMX_FALSE;
135 def.nBufferAlignment = 1;
139 def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_MPEG);
140 def.format.audio.eEncoding = OMX_AUDIO_CodingMP3;
143 def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_MPEG_LAYER_I);
144 def.format.audio.eEncoding = OMX_AUDIO_CodingMP3;
147 def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_MPEG_LAYER_II);
148 def.format.audio.eEncoding = OMX_AUDIO_CodingMP3;
151 def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_AAC);
152 def.format.audio.eEncoding = OMX_AUDIO_CodingAAC;
155 def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_AC3);
157 //def.format.audio.eEncoding = OMX_AUDIO_CodingAC3;
158 def.format.audio.eEncoding = OMX_AUDIO_CodingAutoDetect; // right?? orz
161 def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_WMA);
162 def.format.audio.eEncoding = OMX_AUDIO_CodingWMA;
165 def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_RA);
166 def.format.audio.eEncoding = OMX_AUDIO_CodingRA;
169 def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_APE);
170 def.format.audio.eEncoding = OMX_AUDIO_CodingAPE;
173 def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_DTS);
174 def.format.audio.eEncoding = OMX_AUDIO_CodingDTS;
177 def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_FLAC);
178 def.format.audio.eEncoding = OMX_AUDIO_CodingFLAC;
181 def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_VORBIS);
182 def.format.audio.eEncoding = OMX_AUDIO_CodingVORBIS;
185 CHECK(!"Should not be here. Unsupported mime type and compression format");
189 def.format.audio.pNativeRender = NULL;
190 def.format.audio.bFlagErrorConcealment = OMX_FALSE;
195 def.eDir = OMX_DirOutput;
196 def.nBufferCountMin = kNumBuffers;
197 def.nBufferCountActual = def.nBufferCountMin;
198 def.nBufferSize = kOutputBufferSize;
199 def.bEnabled = OMX_TRUE;
200 def.bPopulated = OMX_FALSE;
201 def.eDomain = OMX_PortDomainAudio;
202 def.bBuffersContiguous = OMX_FALSE;
203 def.nBufferAlignment = 2;
205 def.format.audio.cMIMEType = const_cast<char *>("audio/raw");
206 def.format.audio.pNativeRender = NULL;
207 def.format.audio.bFlagErrorConcealment = OMX_FALSE;
208 def.format.audio.eEncoding = OMX_AUDIO_CodingPCM;
213 void SoftFFmpegAudio::setAVCtxToDefault(AVCodecContext *avctx, const AVCodec *codec) {
216 avctx->workaround_bugs = 1;
218 if(avctx->lowres > codec->max_lowres){
219 ALOGW("The maximum value for lowres supported by the decoder is %d",
221 avctx->lowres= codec->max_lowres;
223 avctx->idct_algo = 0;
224 avctx->skip_frame = AVDISCARD_DEFAULT;
225 avctx->skip_idct = AVDISCARD_DEFAULT;
226 avctx->skip_loop_filter = AVDISCARD_DEFAULT;
227 avctx->error_concealment = 3;
229 if(avctx->lowres) avctx->flags |= CODEC_FLAG_EMU_EDGE;
230 if (fast) avctx->flags2 |= CODEC_FLAG2_FAST;
231 if(codec->capabilities & CODEC_CAP_DR1)
232 avctx->flags |= CODEC_FLAG_EMU_EDGE;
236 void SoftFFmpegAudio::reConfigCtx() {
237 mCtx->channels = mNumChannels;
238 mCtx->sample_rate = mSamplingRate;
239 mCtx->bit_rate = mBitRate;
240 mCtx->sample_fmt = mSamplingFmt;
242 mAudioSrcFmt = mAudioTgtFmt = AV_SAMPLE_FMT_S16;
243 mAudioSrcFreq = mAudioTgtFreq = mSamplingRate;
244 mAudioSrcChannels = mAudioTgtChannels = mNumChannels;
245 mAudioSrcChannelLayout = mAudioTgtChannelLayout =
246 av_get_default_channel_layout(mNumChannels);
249 status_t SoftFFmpegAudio::initDecoder() {
252 status = initFFmpeg();
256 mFFmpegInited = true;
258 mCtx = avcodec_alloc_context3(NULL);
261 ALOGE("avcodec_alloc_context failed.");
265 mCtx->codec_type = AVMEDIA_TYPE_AUDIO;
268 mCtx->codec_id = CODEC_ID_MP3;
271 mCtx->codec_id = CODEC_ID_MP1;
274 mCtx->codec_id = CODEC_ID_MP2;
277 mCtx->codec_id = CODEC_ID_AAC;
280 mCtx->codec_id = CODEC_ID_AC3;
283 mCtx->codec_id = CODEC_ID_WMAV2; // FIXME, CODEC_ID_WMAV1 or CODEC_ID_WMAV2?
286 mCtx->codec_id = CODEC_ID_COOK; // FIXME
289 mCtx->codec_id = CODEC_ID_APE;
292 mCtx->codec_id = CODEC_ID_DTS;
295 mCtx->codec_id = CODEC_ID_FLAC;
298 mCtx->codec_id = CODEC_ID_VORBIS;
301 CHECK(!"Should not be here. Unsupported codec");
305 mCtx->codec = avcodec_find_decoder(mCtx->codec_id);
308 ALOGE("find codec failed");
312 setAVCtxToDefault(mCtx, mCtx->codec);
317 memset(mSilenceBuffer, 0, kOutputBufferSize);
322 void SoftFFmpegAudio::deInitDecoder() {
324 //avcodec_flush_buffers(mCtx); // is it necessary? crash sometimes if call it
325 if (!mCtx->extradata) {
326 av_free(mCtx->extradata);
327 mCtx->extradata = NULL;
328 mCtx->extradata_size = 0;
341 OMX_ERRORTYPE SoftFFmpegAudio::internalGetParameter(
342 OMX_INDEXTYPE index, OMX_PTR params) {
343 int32_t channels = 0;
344 int32_t sampling_rate = 0;
347 case OMX_IndexParamAudioAac:
349 OMX_AUDIO_PARAM_AACPROFILETYPE *aacParams =
350 (OMX_AUDIO_PARAM_AACPROFILETYPE *)params;
352 if (aacParams->nPortIndex != 0) {
353 return OMX_ErrorUndefined;
356 aacParams->nBitRate = 0;
357 aacParams->nAudioBandWidth = 0;
358 aacParams->nAACtools = 0;
359 aacParams->nAACERtools = 0;
360 aacParams->eAACProfile = OMX_AUDIO_AACObjectMain;
361 aacParams->eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4FF;
362 aacParams->eChannelMode = OMX_AUDIO_ChannelModeStereo;
364 aacParams->nChannels = mNumChannels;
365 aacParams->nSampleRate = mSamplingRate;
367 return OMX_ErrorNone;
369 case OMX_IndexParamAudioWma:
371 OMX_AUDIO_PARAM_WMATYPE *wmaParams =
372 (OMX_AUDIO_PARAM_WMATYPE *)params;
374 if (wmaParams->nPortIndex != 0) {
375 return OMX_ErrorUndefined;
378 wmaParams->nChannels = 0;
379 wmaParams->nSamplingRate = 0;
380 wmaParams->nBitRate = 0;
381 wmaParams->eFormat = OMX_AUDIO_WMAFormatUnused;
383 return OMX_ErrorNone;
385 case OMX_IndexParamAudioRa:
387 OMX_AUDIO_PARAM_RATYPE *raParams =
388 (OMX_AUDIO_PARAM_RATYPE *)params;
390 if (raParams->nPortIndex != 0) {
391 return OMX_ErrorUndefined;
394 raParams->nChannels = 0;
395 raParams->nSamplingRate = 0;
396 raParams->eFormat = OMX_AUDIO_RAFormatUnused;
398 return OMX_ErrorNone;
400 case OMX_IndexParamAudioApe:
402 OMX_AUDIO_PARAM_APETYPE *apeParams =
403 (OMX_AUDIO_PARAM_APETYPE *)params;
405 if (apeParams->nPortIndex != 0) {
406 return OMX_ErrorUndefined;
409 apeParams->nChannels = 0;
410 apeParams->nSamplingRate = 0;
412 return OMX_ErrorNone;
414 case OMX_IndexParamAudioDts:
416 OMX_AUDIO_PARAM_DTSTYPE *dtsParams =
417 (OMX_AUDIO_PARAM_DTSTYPE *)params;
419 if (dtsParams->nPortIndex != 0) {
420 return OMX_ErrorUndefined;
423 dtsParams->nChannels = 0;
424 dtsParams->nSamplingRate = 0;
426 return OMX_ErrorNone;
428 case OMX_IndexParamAudioFlac:
430 OMX_AUDIO_PARAM_FLACTYPE *flacParams =
431 (OMX_AUDIO_PARAM_FLACTYPE *)params;
433 if (flacParams->nPortIndex != 0) {
434 return OMX_ErrorUndefined;
437 flacParams->nChannels = 0;
438 flacParams->nSampleRate = 0;
440 return OMX_ErrorNone;
442 case OMX_IndexParamAudioPcm:
444 OMX_AUDIO_PARAM_PCMMODETYPE *pcmParams =
445 (OMX_AUDIO_PARAM_PCMMODETYPE *)params;
447 if (pcmParams->nPortIndex > 1) {
448 return OMX_ErrorUndefined;
451 pcmParams->eNumData = OMX_NumericalDataSigned;
452 pcmParams->eEndian = OMX_EndianBig;
453 pcmParams->bInterleaved = OMX_TRUE;
454 pcmParams->nBitPerSample = 16;
455 pcmParams->ePCMMode = OMX_AUDIO_PCMModeLinear;
456 pcmParams->eChannelMapping[0] = OMX_AUDIO_ChannelLF;
457 pcmParams->eChannelMapping[1] = OMX_AUDIO_ChannelRF;
459 // update src and target(except aac), only once!
460 adjustAudioParameter();
462 pcmParams->nChannels = channels;
463 pcmParams->nSamplingRate = sampling_rate;
465 return OMX_ErrorNone;
467 case OMX_IndexParamAudioVorbis:
469 OMX_AUDIO_PARAM_VORBISTYPE *vorbisParams =
470 (OMX_AUDIO_PARAM_VORBISTYPE *)params;
472 if (vorbisParams->nPortIndex != 0) {
473 return OMX_ErrorUndefined;
476 vorbisParams->nBitRate = 0;
477 vorbisParams->nMinBitRate = 0;
478 vorbisParams->nMaxBitRate = 0;
479 vorbisParams->nAudioBandWidth = 0;
480 vorbisParams->nQuality = 3;
481 vorbisParams->bManaged = OMX_FALSE;
482 vorbisParams->bDownmix = OMX_FALSE;
484 vorbisParams->nChannels = 1;
485 vorbisParams->nSampleRate = 44100;
487 if (!isConfigured()) {
488 vorbisParams->nChannels = 1;
489 vorbisParams->nSampleRate = 44100;
491 vorbisParams->nChannels = mVi->channels;
492 vorbisParams->nSampleRate = mVi->rate;
493 vorbisParams->nBitRate = mVi->bitrate_nominal;
494 vorbisParams->nMinBitRate = mVi->bitrate_lower;
495 vorbisParams->nMaxBitRate = mVi->bitrate_upper;
498 return OMX_ErrorNone;
502 return SimpleSoftOMXComponent::internalGetParameter(index, params);
506 OMX_ERRORTYPE SoftFFmpegAudio::isRoleSupported(
507 const OMX_PARAM_COMPONENTROLETYPE *roleParams) {
508 bool supported = true;
512 if (strncmp((const char *)roleParams->cRole,
513 "audio_decoder.mp3", OMX_MAX_STRINGNAME_SIZE - 1))
517 if (strncmp((const char *)roleParams->cRole,
518 "audio_decoder.mp1", OMX_MAX_STRINGNAME_SIZE - 1))
522 if (strncmp((const char *)roleParams->cRole,
523 "audio_decoder.mp2", OMX_MAX_STRINGNAME_SIZE - 1))
527 if (strncmp((const char *)roleParams->cRole,
528 "audio_decoder.aac", OMX_MAX_STRINGNAME_SIZE - 1))
532 if (strncmp((const char *)roleParams->cRole,
533 "audio_decoder.ac3", OMX_MAX_STRINGNAME_SIZE - 1))
537 if (strncmp((const char *)roleParams->cRole,
538 "audio_decoder.wma", OMX_MAX_STRINGNAME_SIZE - 1))
542 if (strncmp((const char *)roleParams->cRole,
543 "audio_decoder.ra", OMX_MAX_STRINGNAME_SIZE - 1))
547 if (strncmp((const char *)roleParams->cRole,
548 "audio_decoder.ape", OMX_MAX_STRINGNAME_SIZE - 1))
552 if (strncmp((const char *)roleParams->cRole,
553 "audio_decoder.dts", OMX_MAX_STRINGNAME_SIZE - 1))
557 if (strncmp((const char *)roleParams->cRole,
558 "audio_decoder.flac", OMX_MAX_STRINGNAME_SIZE - 1))
562 if (strncmp((const char *)roleParams->cRole,
563 "audio_decoder.vorbis", OMX_MAX_STRINGNAME_SIZE - 1))
567 CHECK(!"Should not be here. Unsupported role.");
572 ALOGE("unsupported role: %s", (const char *)roleParams->cRole);
573 return OMX_ErrorUndefined;
576 return OMX_ErrorNone;
579 void SoftFFmpegAudio::adjustAudioParameter() {
580 int32_t channels = 0;
581 int32_t sampling_rate = 0;
583 // channels support 1 or 2 only
584 channels = mNumChannels >= 2 ? 2 : 1;
586 // 4000 <= sampling rate <= 48000
587 sampling_rate = mSamplingRate;
588 if (mSamplingRate < 4000) {
589 sampling_rate = 4000;
590 } else if (mSamplingRate > 48000) {
591 sampling_rate = 48000;
594 // update src and target(only wma), only once!
595 mAudioSrcChannels = mAudioTgtChannels = channels;
596 mAudioSrcFreq = mAudioTgtFreq = sampling_rate;
597 mAudioSrcFmt = mAudioTgtFmt = AV_SAMPLE_FMT_S16;
598 mAudioSrcChannelLayout = mAudioTgtChannelLayout =
599 av_get_default_channel_layout(channels);
602 OMX_ERRORTYPE SoftFFmpegAudio::internalSetParameter(
603 OMX_INDEXTYPE index, const OMX_PTR params) {
605 case OMX_IndexParamStandardComponentRole:
607 const OMX_PARAM_COMPONENTROLETYPE *roleParams =
608 (const OMX_PARAM_COMPONENTROLETYPE *)params;
609 return isRoleSupported(roleParams);
611 case OMX_IndexParamAudioPcm:
613 const OMX_AUDIO_PARAM_PCMMODETYPE *pcmParams =
614 (const OMX_AUDIO_PARAM_PCMMODETYPE *)params;
616 if (pcmParams->nPortIndex != 1) {
617 return OMX_ErrorUndefined;
620 mNumChannels = pcmParams->nChannels;
621 mSamplingRate = pcmParams->nSamplingRate;
623 return OMX_ErrorNone;
625 case OMX_IndexParamAudioAac:
627 const OMX_AUDIO_PARAM_AACPROFILETYPE *aacParams =
628 (const OMX_AUDIO_PARAM_AACPROFILETYPE *)params;
630 if (aacParams->nPortIndex != 0) {
631 return OMX_ErrorUndefined;
634 mNumChannels = aacParams->nChannels;
635 mSamplingRate = aacParams->nSampleRate;
637 // update src and target(only aac), only once!
638 adjustAudioParameter();
640 ALOGV("got OMX_IndexParamAudioAac, mNumChannels: %d, mSamplingRate: %d",
641 mNumChannels, mSamplingRate);
643 return OMX_ErrorNone;
645 case OMX_IndexParamAudioWma:
647 OMX_AUDIO_PARAM_WMATYPE *wmaParams =
648 (OMX_AUDIO_PARAM_WMATYPE *)params;
650 if (wmaParams->nPortIndex != 0) {
651 return OMX_ErrorUndefined;
654 if (wmaParams->eFormat == OMX_AUDIO_WMAFormat7) {
655 mCtx->codec_id = CODEC_ID_WMAV2;
656 } else if (wmaParams->eFormat == OMX_AUDIO_WMAFormat8) {
657 mCtx->codec_id = CODEC_ID_WMAPRO;
658 } else if (wmaParams->eFormat == OMX_AUDIO_WMAFormat9) {
659 mCtx->codec_id = CODEC_ID_WMALOSSLESS;
661 ALOGE("unsupported wma codec: 0x%x", wmaParams->eFormat);
662 return OMX_ErrorUndefined;
665 mNumChannels = wmaParams->nChannels;
666 mSamplingRate = wmaParams->nSamplingRate;
667 mBitRate = wmaParams->nBitRate;
668 mBlockAlign = wmaParams->nBlockAlign;
670 // wmadec needs bitrate, block_align
671 mCtx->bit_rate = mBitRate;
672 mCtx->block_align = mBlockAlign;
674 // update src and target(only wma), only once!
675 adjustAudioParameter();
677 ALOGV("got OMX_IndexParamAudioWma, mNumChannels: %d, "
678 "mSamplingRate: %d, mBitRate: %d, mBlockAlign: %d",
679 mNumChannels, mSamplingRate, mBitRate, mBlockAlign);
681 return OMX_ErrorNone;
683 case OMX_IndexParamAudioRa:
685 OMX_AUDIO_PARAM_RATYPE *raParams =
686 (OMX_AUDIO_PARAM_RATYPE *)params;
688 if (raParams->nPortIndex != 0) {
689 return OMX_ErrorUndefined;
692 mCtx->codec_id = CODEC_ID_COOK;
694 mNumChannels = raParams->nChannels;
695 mSamplingRate = raParams->nSamplingRate;
696 // FIXME, HACK!!!, I use the nNumRegions parameter pass blockAlign!!!
697 // the cook audio codec need blockAlign!
698 mBlockAlign = raParams->nNumRegions;
700 // cook decoder need block_align
701 mCtx->block_align = mBlockAlign;
703 // update src and target(only wma), only once!
704 adjustAudioParameter();
706 ALOGV("got OMX_IndexParamAudioRa, mNumChannels: %d, "
707 "mSamplingRate: %d, mBlockAlign: %d",
708 mNumChannels, mSamplingRate, mBlockAlign);
710 return OMX_ErrorNone;
712 case OMX_IndexParamAudioApe:
714 OMX_AUDIO_PARAM_APETYPE *apeParams =
715 (OMX_AUDIO_PARAM_APETYPE *)params;
717 if (apeParams->nPortIndex != 0) {
718 return OMX_ErrorUndefined;
721 mNumChannels = apeParams->nChannels;
722 mSamplingRate = apeParams->nSamplingRate;
724 // ape decoder need bits_per_coded_sample
725 mCtx->bits_per_coded_sample = apeParams->nBitsPerSample;
727 // update src and target, only once!
728 adjustAudioParameter();
730 ALOGV("got OMX_IndexParamAudioApe, mNumChannels: %d, "
731 "mSamplingRate: %d, nBitsPerSample: %lu",
732 mNumChannels, mSamplingRate, apeParams->nBitsPerSample);
734 return OMX_ErrorNone;
736 case OMX_IndexParamAudioDts:
738 OMX_AUDIO_PARAM_DTSTYPE *dtsParams =
739 (OMX_AUDIO_PARAM_DTSTYPE *)params;
741 if (dtsParams->nPortIndex != 0) {
742 return OMX_ErrorUndefined;
745 mNumChannels = dtsParams->nChannels;
746 mSamplingRate = dtsParams->nSamplingRate;
748 // update src and target, only once!
749 adjustAudioParameter();
751 ALOGV("got OMX_IndexParamAudioDts, mNumChannels: %d, mSamplingRate: %d",
752 mNumChannels, mSamplingRate);
754 return OMX_ErrorNone;
756 case OMX_IndexParamAudioFlac:
758 OMX_AUDIO_PARAM_FLACTYPE *flacParams =
759 (OMX_AUDIO_PARAM_FLACTYPE *)params;
761 if (flacParams->nPortIndex != 0) {
762 return OMX_ErrorUndefined;
765 mNumChannels = flacParams->nChannels;
766 mSamplingRate = flacParams->nSampleRate;
768 // update src and target, only once!
769 adjustAudioParameter();
771 ALOGV("got OMX_IndexParamAudioFlac, mNumChannels: %d, mSamplingRate: %d",
772 mNumChannels, mSamplingRate);
774 return OMX_ErrorNone;
776 case OMX_IndexParamAudioVorbis:
778 const OMX_AUDIO_PARAM_VORBISTYPE *vorbisParams =
779 (const OMX_AUDIO_PARAM_VORBISTYPE *)params;
781 if (vorbisParams->nPortIndex != 0) {
782 return OMX_ErrorUndefined;
785 ALOGD("got OMX_IndexParamAudioVorbis, "
786 "mNumChannels=%lu, mSamplingRate=%lu, nBitRate=%lu, "
787 "nMinBitRate=%lu, nMaxBitRate=%lu",
788 vorbisParams->nChannels, vorbisParams->nSampleRate,
789 vorbisParams->nBitRate, vorbisParams->nMinBitRate,
790 vorbisParams->nMaxBitRate);
792 return OMX_ErrorNone;
795 ALOGI("internalSetParameter, index: 0x%x", index);
796 return SimpleSoftOMXComponent::internalSetParameter(index, params);
800 void SoftFFmpegAudio::onQueueFilled(OMX_U32 portIndex) {
804 int64_t decChannelLayout;
805 int32_t inputBufferUsedLength = 0;
806 BufferInfo *inInfo = NULL;
807 OMX_BUFFERHEADERTYPE *inHeader = NULL;
809 if (mSignalledError || mOutputPortSettingsChange != NONE) {
813 List<BufferInfo *> &inQueue = getPortQueue(0);
814 List<BufferInfo *> &outQueue = getPortQueue(1);
816 while ((!inQueue.empty() //there are input buffers to be emptied
817 || mAudioBufferSize > 0 //there are left decoded frames
818 || mReceivedEOS) //need it to fill eos outbuffer
819 && !outQueue.empty()) { //there are out buffers to be filled
820 if (!inQueue.empty()) {
821 inInfo = *inQueue.begin();
822 inHeader = inInfo->mHeader;
828 BufferInfo *outInfo = *outQueue.begin();
829 OMX_BUFFERHEADERTYPE *outHeader = outInfo->mHeader;
831 if (inHeader && inHeader->nFlags & OMX_BUFFERFLAG_EOS) {
832 CHECK(mInputBufferSize == 0);
833 ALOGD("ffmpeg audio decoder empty eos inbuf");
834 inQueue.erase(inQueue.begin());
835 inInfo->mOwnedByUs = false;
836 notifyEmptyBufferDone(inHeader);
838 continue; //empty all input buffers in inQueue
841 if (mReceivedEOS && (mFlushComplete || !(mCtx->codec->capabilities & CODEC_CAP_DELAY))) {
842 ALOGD("ffmpeg audio decoder fill eos outbuf");
843 outHeader->nFilledLen = 0;
844 outHeader->nFlags = OMX_BUFFERFLAG_EOS;
846 outQueue.erase(outQueue.begin());
847 outInfo->mOwnedByUs = false;
848 notifyFillBufferDone(outHeader);
852 if (inHeader && inHeader->nFlags & OMX_BUFFERFLAG_CODECCONFIG) {
853 ALOGI("got extradata, ignore: %d, size: %lu", mIgnoreExtradata, inHeader->nFilledLen);
854 hexdump(inHeader->pBuffer + inHeader->nOffset, inHeader->nFilledLen);
855 if (!mExtradataReady && !mIgnoreExtradata) {
856 int orig_extradata_size = mCtx->extradata_size;
857 mCtx->extradata_size += inHeader->nFilledLen;
858 mCtx->extradata = (uint8_t *)av_realloc(mCtx->extradata,
859 mCtx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
860 if (!mCtx->extradata) {
861 ALOGE("ffmpeg audio decoder failed to alloc extradata memory.");
862 notify(OMX_EventError, OMX_ErrorInsufficientResources, 0, NULL);
863 mSignalledError = true;
867 memcpy(mCtx->extradata + orig_extradata_size,
868 inHeader->pBuffer + inHeader->nOffset, inHeader->nFilledLen);
869 memset(mCtx->extradata + mCtx->extradata_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
871 inInfo->mOwnedByUs = false;
872 inQueue.erase(inQueue.begin());
874 notifyEmptyBufferDone(inHeader);
879 if (mIgnoreExtradata) {
880 ALOGI("got extradata, size: %lu, but ignore it", inHeader->nFilledLen);
881 inInfo->mOwnedByUs = false;
882 inQueue.erase(inQueue.begin());
884 notifyEmptyBufferDone(inHeader);
892 if (!mExtradataReady && !mIgnoreExtradata) {
893 ALOGI("extradata is ready, size: %d", mCtx->extradata_size);
894 hexdump(mCtx->extradata, mCtx->extradata_size);
895 mExtradataReady = true;
898 // find decoder again as codec_id may have changed
899 mCtx->codec = avcodec_find_decoder(mCtx->codec_id);
901 ALOGE("ffmpeg audio decoder failed to find codec");
902 notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL);
903 mSignalledError = true;
907 setAVCtxToDefault(mCtx, mCtx->codec);
909 //we should update mCtx here!
912 ALOGI("open ffmpeg decoder now");
913 err = avcodec_open2(mCtx, mCtx->codec, NULL);
915 ALOGE("ffmpeg audio decoder failed to initialize. (%d)", err);
916 notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL);
917 mSignalledError = true;
921 ALOGI("open ffmpeg audio decoder, mCtx sample_rate: %d, "
922 "channels: %d, channel_layout: %llu, sample_fmt: %s",
923 mCtx->sample_rate, mCtx->channels, mCtx->channel_layout,
924 av_get_sample_fmt_name(mCtx->sample_fmt));
927 //update the audio clock with the pts
928 if (inHeader && inHeader->nOffset == 0
929 && mInputBufferSize == 0 // the last input buffer have be emptied
930 && mAudioBufferSize == 0) { // the last decoded frame have be filled
931 mAnchorTimeUs = inHeader->nTimeStamp;
932 mInputBufferSize = inHeader->nFilledLen;
935 if ((inHeader && mAudioBufferSize == 0) //hope to get more decoded frame while have not read eos pkt
936 || (!inHeader && !mFlushComplete)) { //hope to get left decoded frame while have read eos pkt
938 memset(&pkt, 0, sizeof(pkt));
939 av_init_packet(&pkt);
942 CHECK(mAudioBufferSize == 0);
943 pkt.data = (uint8_t *)inHeader->pBuffer + inHeader->nOffset;
944 pkt.size = inHeader->nFilledLen;
945 pkt.pts = inHeader->nTimeStamp; // ingore it, we will compute it
947 CHECK(mFlushComplete == false);
950 pkt.pts = AV_NOPTS_VALUE;
953 ALOGV("pkt size: %d, pts: %lld", pkt.size, pkt.pts);
956 if (!(mFrame = avcodec_alloc_frame())) {
957 ALOGE("ffmpeg audio decoder failed to alloc memory.");
958 notify(OMX_EventError, OMX_ErrorInsufficientResources, 0, NULL);
959 mSignalledError = true;
963 avcodec_get_frame_defaults(mFrame);
967 inputBufferUsedLength = 0;
968 len = avcodec_decode_audio4(mCtx, mFrame, &gotFrm, &pkt);
970 ALOGE("ffmpeg audio decoder failed to decode frame. "
971 "consume pkt len: %d", len);
973 /* if !mAudioConfigChanged, Don't fill the out buffer */
974 if (!mAudioConfigChanged) {
975 inInfo->mOwnedByUs = false;
976 inQueue.erase(inQueue.begin());
978 notifyEmptyBufferDone(inHeader);
981 mInputBufferSize = 0; // need?
985 //inputBufferUsedLength = inHeader->nFilledLen;
986 /* if error, we skip the frame and play silence instead */
987 mPAudioBuffer = mSilenceBuffer;
988 mAudioBufferSize = kOutputBufferSize;
992 ALOGV("ffmpeg audio decoder, consume pkt len: %d", len);
996 CHECK(mFlushComplete == false);
999 inputBufferUsedLength = inHeader->nFilledLen;
1001 inputBufferUsedLength = len;
1003 CHECK_GE(inHeader->nFilledLen, inputBufferUsedLength);
1004 inHeader->nOffset += inputBufferUsedLength;
1005 inHeader->nFilledLen -= inputBufferUsedLength;
1006 mInputBufferSize -= inputBufferUsedLength;
1007 if (inHeader->nFilledLen == 0) {
1008 inInfo->mOwnedByUs = false;
1009 inQueue.erase(inQueue.begin());
1011 notifyEmptyBufferDone(inHeader);
1014 //reset mInputBufferSize
1015 mInputBufferSize = 0;
1020 ALOGI("ffmpeg audio decoder failed to get more frame.");
1021 /* stop sending empty packets if the decoder is finished */
1022 if (!pkt.data && mCtx->codec->capabilities & CODEC_CAP_DELAY)
1023 mFlushComplete = true;
1028 * FIXME, check mAudioConfigChanged when the first time you call the audio4!
1029 * mCtx->sample_rate and mCtx->channels may be changed by audio decoder later, why???
1031 if (!mAudioConfigChanged) {
1032 //if (mCtx->channels != mNumChannels || mCtx->sample_rate != mSamplingRate
1033 // || mCtx->sample_fmt != mSamplingFmt) {
1034 if (mCtx->channels != mNumChannels || mCtx->sample_rate != mSamplingRate) {
1035 ALOGI("audio OMX_EventPortSettingsChanged, mCtx->channels: %d, "
1036 "mNumChannels: %d, mCtx->sample_rate: %d, mSamplingRate: %d, "
1037 "mCtx->sample_fmt: %s, mSamplingFmt: %s",
1038 mCtx->channels, mNumChannels, mCtx->sample_rate, mSamplingRate,
1039 av_get_sample_fmt_name(mCtx->sample_fmt),
1040 av_get_sample_fmt_name(mSamplingFmt));
1041 mNumChannels = mCtx->channels;
1042 mSamplingRate = mCtx->sample_rate;
1043 mSamplingFmt = mCtx->sample_fmt;
1044 mAudioConfigChanged = true;
1045 notify(OMX_EventPortSettingsChanged, 1, 0, NULL);
1046 mOutputPortSettingsChange = AWAITING_DISABLED;
1049 // match with the default, set mAudioConfigChanged true anyway!
1050 mAudioConfigChanged = true;
1051 mSamplingFmt = mCtx->sample_fmt;
1055 dataSize = av_samples_get_buffer_size(NULL, mNumChannels,
1056 mFrame->nb_samples, mSamplingFmt, 1);
1059 ALOGV("audio decoder, nb_samples: %d, get buffer size: %d",
1060 mFrame->nb_samples, dataSize);
1061 ALOGV("audio decoder: mCtx channel_layout: %llu, "
1062 "channels: %d, channels_from_layout: %d",
1063 mCtx->channel_layout, mCtx->channels,
1064 av_get_channel_layout_nb_channels(mCtx->channel_layout));
1067 decChannelLayout = av_get_default_channel_layout(mNumChannels);
1068 if (mSamplingFmt != mAudioSrcFmt
1069 || decChannelLayout != mAudioSrcChannelLayout
1070 || mSamplingRate != mAudioSrcFreq) {
1073 mSwrCtx = swr_alloc_set_opts(NULL,
1074 mAudioTgtChannelLayout, mAudioTgtFmt, mAudioTgtFreq,
1075 decChannelLayout, mSamplingFmt, mSamplingRate,
1077 if (!mSwrCtx || swr_init(mSwrCtx) < 0) {
1078 ALOGE("Cannot create sample rate converter for conversion "
1079 "of %d Hz %s %d channels to %d Hz %s %d channels!",
1081 av_get_sample_fmt_name(mSamplingFmt),
1084 av_get_sample_fmt_name(mAudioTgtFmt),
1086 notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL);
1087 mSignalledError = true;
1091 ALOGI("Create sample rate converter for conversion "
1092 "of %d Hz %s %d channels(%lld channel_layout) "
1093 "to %d Hz %s %d channels(%lld channel_layout)!",
1095 av_get_sample_fmt_name(mSamplingFmt),
1097 mAudioTgtChannelLayout,
1099 av_get_sample_fmt_name(mAudioTgtFmt),
1103 mAudioSrcChannelLayout = decChannelLayout;
1104 mAudioSrcChannels = mNumChannels;
1105 mAudioSrcFreq = mSamplingRate;
1106 mAudioSrcFmt = mSamplingFmt;
1110 //const uint8_t *in[] = { mFrame->data[0] };
1111 const uint8_t **in = (const uint8_t **)mFrame->extended_data;
1112 uint8_t *out[] = {mAudioBuf2};
1113 int out_count = sizeof(mAudioBuf2) / mAudioTgtChannels /
1114 av_get_bytes_per_sample(mAudioTgtFmt);
1116 ALOGV("swr_convert 1, out_count: %d, mFrame->nb_samples: %d, "
1117 "src frm: %s, tgt frm: %s",
1118 out_count, mFrame->nb_samples,
1119 av_get_sample_fmt_name(mCtx->sample_fmt),
1120 av_get_sample_fmt_name(mAudioTgtFmt));
1122 int len2 = swr_convert(mSwrCtx, out, out_count, in, mFrame->nb_samples);
1124 ALOGE("audio_resample() failed");
1127 if (len2 == out_count) {
1128 ALOGE("warning: audio buffer is probably too small");
1131 mPAudioBuffer = mAudioBuf2;
1132 mAudioBufferSize = len2 * mAudioTgtChannels *
1133 av_get_bytes_per_sample(mAudioTgtFmt);
1135 mPAudioBuffer = mFrame->data[0];
1136 mAudioBufferSize = dataSize;
1140 ALOGV("ffmpeg audio decoder get frame. consume pkt len: %d, "
1141 "nb_samples(before resample): %d, mAudioBufferSize: %d",
1142 len, mFrame->nb_samples, mAudioBufferSize);
1144 } //if ((inHeader && mAudioBufferSize == 0) || (!inHeader && !mFlushComplete))
1146 size_t copyToOutputBufferLen = mAudioBufferSize;
1147 if (mAudioBufferSize > kOutputBufferSize)
1148 copyToOutputBufferLen = kOutputBufferSize;
1150 outHeader->nOffset = 0;
1151 outHeader->nFilledLen = copyToOutputBufferLen;
1152 outHeader->nTimeStamp = mAnchorTimeUs +
1153 (mNumFramesOutput * 1000000ll) / mSamplingRate;
1154 memcpy(outHeader->pBuffer, mPAudioBuffer, copyToOutputBufferLen);
1155 outHeader->nFlags = 0;
1158 ALOGV("ffmpeg audio decoder, fill out buffer, "
1159 "pts: %lld, mNumFramesOutput: %lld",
1160 outHeader->nTimeStamp, mNumFramesOutput);
1163 mPAudioBuffer += copyToOutputBufferLen;
1164 mAudioBufferSize -= copyToOutputBufferLen;
1165 mNumFramesOutput += copyToOutputBufferLen /
1166 (av_get_bytes_per_sample(mAudioTgtFmt) * mNumChannels);
1168 // reset mNumFramesOutput
1169 if (mInputBufferSize == 0 && mAudioBufferSize == 0) {
1171 ALOGV("~~~~ reset mNumFramesOutput to 0");
1173 mNumFramesOutput = 0;
1176 outInfo->mOwnedByUs = false;
1177 outQueue.erase(outQueue.begin());
1179 notifyFillBufferDone(outHeader);
1184 void SoftFFmpegAudio::onPortFlushCompleted(OMX_U32 portIndex) {
1185 if (portIndex == 0 && mCtx) {
1186 // Make sure that the next buffer output does not still
1187 // depend on fragments from the last one decoded.
1188 avcodec_flush_buffers(mCtx);
1190 mReceivedEOS = false;
1191 mFlushComplete = false;
1193 mInputBufferSize = 0;
1194 mNumFramesOutput = 0;
1195 mAudioBufferSize = 0;
1196 mPAudioBuffer = NULL;
1197 //don't free mFrame!
1200 void SoftFFmpegAudio::onPortEnableCompleted(OMX_U32 portIndex, bool enabled) {
1201 if (portIndex != 1) {
1205 switch (mOutputPortSettingsChange) {
1209 case AWAITING_DISABLED:
1212 mOutputPortSettingsChange = AWAITING_ENABLED;
1218 CHECK_EQ((int)mOutputPortSettingsChange, (int)AWAITING_ENABLED);
1220 mOutputPortSettingsChange = NONE;
1226 } // namespace android
1228 android::SoftOMXComponent *createSoftOMXComponent(
1229 const char *name, const OMX_CALLBACKTYPE *callbacks,
1230 OMX_PTR appData, OMX_COMPONENTTYPE **component) {
1231 return new android::SoftFFmpegAudio(name, callbacks, appData, component);