2 * Copyright 2012 Michael Chen <omxcodec@gmail.com>
3 * Copyright 2015 The CyanogenMod Project
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 //#define LOG_NDEBUG 0
19 #define LOG_TAG "SoftFFmpegAudio"
20 #include <utils/Log.h>
21 #include <cutils/properties.h>
23 #include "SoftFFmpegAudio.h"
24 #include "FFmpegComponents.h"
26 #include <media/stagefright/foundation/ADebug.h>
27 #include <media/stagefright/foundation/hexdump.h>
28 #include <media/stagefright/ACodec.h>
29 #include <media/stagefright/MediaDefs.h>
31 #include <OMX_AudioExt.h>
32 #include <OMX_IndexExt.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 int64_t *SoftFFmpegAudio::sAudioClock;
50 SoftFFmpegAudio::SoftFFmpegAudio(
52 const char *componentRole,
53 OMX_AUDIO_CODINGTYPE codingType,
54 enum AVCodecID codecID,
55 const OMX_CALLBACKTYPE *callbacks,
57 OMX_COMPONENTTYPE **component)
58 : SimpleSoftOMXComponent(name, callbacks, appData, component),
60 mCodingType(codingType),
61 mFFmpegAlreadyInited(false),
62 mCodecAlreadyOpened(false),
63 mExtradataReady(false),
64 mIgnoreExtradata(false),
68 mEOSStatus(INPUT_DATA_AVAILABLE),
69 mSignalledError(false),
72 mResampledDataSize(0),
73 mOutputPortSettingsChange(NONE),
74 mReconfiguring(false) {
78 ALOGD("SoftFFmpegAudio component: %s mCodingType: %d",
82 CHECK_EQ(initDecoder(codecID), (status_t)OK);
85 SoftFFmpegAudio::~SoftFFmpegAudio() {
86 ALOGV("~SoftFFmpegAudio");
88 if (mFFmpegAlreadyInited) {
93 void SoftFFmpegAudio::initPorts() {
94 OMX_PARAM_PORTDEFINITIONTYPE def;
98 def.eDir = OMX_DirInput;
99 def.nBufferCountMin = kNumInputBuffers;
100 def.nBufferCountActual = def.nBufferCountMin;
102 if (mCodingType == (OMX_AUDIO_CODINGTYPE)OMX_AUDIO_CodingAPE) {
103 def.nBufferSize = 1000000; // ape!
104 } else if (mCodingType == (OMX_AUDIO_CODINGTYPE)OMX_AUDIO_CodingDTS) {
105 def.nBufferSize = 1000000; // dts!
107 // max aggregated buffer size from nuplayer
108 def.nBufferSize = 32 * 1024;
111 def.bEnabled = OMX_TRUE;
112 def.bPopulated = OMX_FALSE;
113 def.eDomain = OMX_PortDomainAudio;
114 def.bBuffersContiguous = OMX_FALSE;
115 def.nBufferAlignment = 1;
117 //def.format.audio.cMIMEType = const_cast<char *>("audio/raw");
118 def.format.audio.pNativeRender = NULL;
119 def.format.audio.bFlagErrorConcealment = OMX_FALSE;
120 def.format.audio.eEncoding = mCodingType;
125 def.eDir = OMX_DirOutput;
126 def.nBufferCountMin = kNumOutputBuffers;
127 def.nBufferCountActual = def.nBufferCountMin;
128 def.nBufferSize = kOutputBufferSize;
129 def.bEnabled = OMX_TRUE;
130 def.bPopulated = OMX_FALSE;
131 def.eDomain = OMX_PortDomainAudio;
132 def.bBuffersContiguous = OMX_FALSE;
133 def.nBufferAlignment = 2;
135 def.format.audio.cMIMEType = const_cast<char *>("audio/raw");
136 def.format.audio.pNativeRender = NULL;
137 def.format.audio.bFlagErrorConcealment = OMX_FALSE;
138 def.format.audio.eEncoding = OMX_AUDIO_CodingPCM;
143 void SoftFFmpegAudio::setDefaultCtx(AVCodecContext *avctx, const AVCodec *codec) {
146 avctx->workaround_bugs = 1;
147 avctx->idct_algo = 0;
148 avctx->skip_frame = AVDISCARD_DEFAULT;
149 avctx->skip_idct = AVDISCARD_DEFAULT;
150 avctx->skip_loop_filter = AVDISCARD_DEFAULT;
151 avctx->error_concealment = 3;
153 avctx->flags |= AV_CODEC_FLAG_BITEXACT;
155 if (fast) avctx->flags2 |= AV_CODEC_FLAG2_FAST;
156 #ifdef CODEC_FLAG_EMU_EDGE
157 if (codec->capabilities & AV_CODEC_CAP_DR1)
158 avctx->flags |= CODEC_FLAG_EMU_EDGE;
162 bool SoftFFmpegAudio::isConfigured() {
163 return mCtx->channels > 0;
166 void SoftFFmpegAudio::resetCtx() {
168 mCtx->sample_rate = 44100;
170 mCtx->sample_fmt = AV_SAMPLE_FMT_NONE;
172 mAudioSrcChannels = mAudioTgtChannels = 2;
173 mAudioSrcFreq = mAudioTgtFreq = 44100;
174 mAudioSrcFmt = mAudioTgtFmt = AV_SAMPLE_FMT_NONE;
175 mAudioSrcChannelLayout = mAudioTgtChannelLayout =
176 av_get_default_channel_layout(mAudioSrcChannels);
179 void SoftFFmpegAudio::initVorbisHdr() {
181 for (i = 0; i < 3; i++) {
182 mVorbisHeaderStart[i] = NULL;
183 mVorbisHeaderLen[i] = 0;
187 void SoftFFmpegAudio::deinitVorbisHdr() {
189 for (i = 0; i < 3; i++) {
190 if (mVorbisHeaderLen[i] > 0) {
191 av_free(mVorbisHeaderStart[i]);
192 mVorbisHeaderStart[i] = NULL;
193 mVorbisHeaderLen[i] = 0;
198 status_t SoftFFmpegAudio::initDecoder(enum AVCodecID codecID) {
201 status = initFFmpeg();
205 mFFmpegAlreadyInited = true;
207 mCtx = avcodec_alloc_context3(NULL);
209 ALOGE("avcodec_alloc_context failed.");
213 mCtx->codec_type = AVMEDIA_TYPE_AUDIO;
214 mCtx->codec_id = codecID;
219 mCtx->extradata = NULL;
220 mCtx->extradata_size = 0;
224 memset(mSilenceBuffer, 0, kOutputBufferSize);
229 void SoftFFmpegAudio::deInitDecoder() {
231 if (!mCtx->extradata) {
232 av_free(mCtx->extradata);
233 mCtx->extradata = NULL;
234 mCtx->extradata_size = 0;
239 if (mCodecAlreadyOpened) {
241 mCodecAlreadyOpened = false;
247 av_frame_free(&mFrame);
250 #ifdef LIBAV_CONFIG_H
259 OMX_ERRORTYPE SoftFFmpegAudio::internalGetParameter(
260 OMX_INDEXTYPE index, OMX_PTR params) {
261 ALOGV("internalGetParameter index:0x%x", index);
262 switch ((int)index) {
263 case OMX_IndexParamAudioPcm:
265 OMX_AUDIO_PARAM_PCMMODETYPE *profile =
266 (OMX_AUDIO_PARAM_PCMMODETYPE *)params;
268 if (profile->nPortIndex > kOutputPortIndex) {
269 return OMX_ErrorUndefined;
272 profile->eNumData = OMX_NumericalDataSigned;
273 profile->eEndian = OMX_EndianBig;
274 profile->bInterleaved = OMX_TRUE;
275 profile->ePCMMode = OMX_AUDIO_PCMModeLinear;
276 profile->nBitPerSample = 32;
277 profile->eNumData = OMX_NumericalDataFloat;
279 if (isConfigured()) {
280 switch (av_get_packed_sample_fmt(mAudioTgtFmt)) {
281 case AV_SAMPLE_FMT_U8:
282 profile->nBitPerSample = 8;
283 profile->eNumData = OMX_NumericalDataUnsigned;
285 case AV_SAMPLE_FMT_S16:
286 profile->nBitPerSample = 16;
287 profile->eNumData = OMX_NumericalDataSigned;
289 case AV_SAMPLE_FMT_S32:
290 profile->nBitPerSample = 32;
291 profile->eNumData = OMX_NumericalDataSigned;
294 profile->nBitPerSample = 32;
295 profile->eNumData = OMX_NumericalDataFloat;
300 if (ACodec::getOMXChannelMapping(mAudioTgtChannels, profile->eChannelMapping) != OK) {
301 return OMX_ErrorNone;
304 profile->nChannels = mAudioTgtChannels;
305 profile->nSamplingRate = mAudioTgtFreq;
307 //mCtx has been updated(adjustAudioParams)!
308 ALOGV("get pcm params, nChannels:%u, nSamplingRate:%u, nBitPerSample:%u",
309 profile->nChannels, profile->nSamplingRate, profile->nBitPerSample);
311 return OMX_ErrorNone;
314 case OMX_IndexParamAudioAac:
316 OMX_AUDIO_PARAM_AACPROFILETYPE *profile =
317 (OMX_AUDIO_PARAM_AACPROFILETYPE *)params;
319 if (profile->nPortIndex != kInputPortIndex) {
320 return OMX_ErrorUndefined;
323 profile->nBitRate = 0;
324 profile->nAudioBandWidth = 0;
325 profile->nAACtools = 0;
326 profile->nAACERtools = 0;
327 profile->eAACProfile = OMX_AUDIO_AACObjectMain;
328 profile->eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4FF;
329 profile->eChannelMode = OMX_AUDIO_ChannelModeStereo;
331 profile->nChannels = mCtx->channels;
332 profile->nSampleRate = mCtx->sample_rate;
334 return OMX_ErrorNone;
337 case OMX_IndexParamAudioMp3:
339 OMX_AUDIO_PARAM_MP3TYPE *profile =
340 (OMX_AUDIO_PARAM_MP3TYPE *)params;
342 if (profile->nPortIndex != kInputPortIndex) {
343 return OMX_ErrorUndefined;
346 profile->nBitRate = 0;
347 profile->nAudioBandWidth = 0;
348 profile->eChannelMode = OMX_AUDIO_ChannelModeStereo;
349 profile->eFormat = OMX_AUDIO_MP3StreamFormatMP1Layer3;
351 profile->nChannels = mCtx->channels;
352 profile->nSampleRate = mCtx->sample_rate;
354 return OMX_ErrorNone;
356 case OMX_IndexParamAudioVorbis:
358 OMX_AUDIO_PARAM_VORBISTYPE *profile =
359 (OMX_AUDIO_PARAM_VORBISTYPE *)params;
361 if (profile->nPortIndex != kInputPortIndex) {
362 return OMX_ErrorUndefined;
365 profile->nBitRate = 0;
366 profile->nMinBitRate = 0;
367 profile->nMaxBitRate = 0;
368 profile->nAudioBandWidth = 0;
369 profile->nQuality = 3;
370 profile->bManaged = OMX_FALSE;
371 profile->bDownmix = OMX_FALSE;
373 profile->nChannels = mCtx->channels;
374 profile->nSampleRate = mCtx->sample_rate;
376 return OMX_ErrorNone;
379 case OMX_IndexParamAudioWma:
381 OMX_AUDIO_PARAM_WMATYPE *profile =
382 (OMX_AUDIO_PARAM_WMATYPE *)params;
384 if (profile->nPortIndex != kInputPortIndex) {
385 return OMX_ErrorUndefined;
388 profile->eFormat = OMX_AUDIO_WMAFormatUnused;
390 profile->nChannels = mCtx->channels;
391 profile->nSamplingRate = mCtx->sample_rate;
393 profile->nBlockAlign = mCtx->block_align;
394 profile->nBitRate = mCtx->bit_rate;
396 return OMX_ErrorNone;
399 case OMX_IndexParamAudioRa:
401 OMX_AUDIO_PARAM_RATYPE *profile =
402 (OMX_AUDIO_PARAM_RATYPE *)params;
404 if (profile->nPortIndex != kInputPortIndex) {
405 return OMX_ErrorUndefined;
408 profile->eFormat = OMX_AUDIO_RAFormatUnused;
410 profile->nChannels = mCtx->channels;
411 profile->nSamplingRate = mCtx->sample_rate;
413 profile->nNumRegions = mCtx->block_align;
415 return OMX_ErrorNone;
418 case OMX_IndexParamAudioFlac:
420 OMX_AUDIO_PARAM_FLACTYPE *profile =
421 (OMX_AUDIO_PARAM_FLACTYPE *)params;
423 if (profile->nPortIndex != kInputPortIndex) {
424 return OMX_ErrorUndefined;
427 profile->nChannels = mCtx->channels;
428 profile->nSampleRate = mCtx->sample_rate;
429 profile->nCompressionLevel = mCtx->bits_per_raw_sample;
430 return OMX_ErrorNone;
433 case OMX_IndexParamAudioMp2:
435 OMX_AUDIO_PARAM_MP2TYPE *profile =
436 (OMX_AUDIO_PARAM_MP2TYPE *)params;
438 if (profile->nPortIndex != kInputPortIndex) {
439 return OMX_ErrorUndefined;
442 profile->nChannels = mCtx->channels;
443 profile->nSampleRate = mCtx->sample_rate;
445 return OMX_ErrorNone;
448 case OMX_IndexParamAudioAndroidAc3:
450 OMX_AUDIO_PARAM_ANDROID_AC3TYPE *profile =
451 (OMX_AUDIO_PARAM_ANDROID_AC3TYPE *)params;
453 if (profile->nPortIndex != kInputPortIndex) {
454 return OMX_ErrorUndefined;
457 profile->nChannels = mCtx->channels;
458 profile->nSampleRate = mCtx->sample_rate;
460 return OMX_ErrorNone;
464 case OMX_IndexParamAudioAc3:
466 OMX_AUDIO_PARAM_AC3TYPE *profile =
467 (OMX_AUDIO_PARAM_AC3TYPE *)params;
469 if (profile->nPortIndex != kInputPortIndex) {
470 return OMX_ErrorUndefined;
473 profile->nChannels = mCtx->channels;
474 profile->nSamplingRate = mCtx->sample_rate;
476 return OMX_ErrorNone;
479 case OMX_IndexParamAudioAlac:
481 OMX_AUDIO_PARAM_ALACTYPE *profile =
482 (OMX_AUDIO_PARAM_ALACTYPE *)params;
484 if (profile->nPortIndex != kInputPortIndex) {
485 return OMX_ErrorUndefined;
488 profile->nChannels = mCtx->channels;
489 profile->nSamplingRate = mCtx->sample_rate;
491 profile->nBitsPerSample = mCtx->bits_per_coded_sample;
493 return OMX_ErrorNone;
496 case OMX_IndexParamAudioApe:
498 OMX_AUDIO_PARAM_APETYPE *profile =
499 (OMX_AUDIO_PARAM_APETYPE *)params;
501 if (profile->nPortIndex != kInputPortIndex) {
502 return OMX_ErrorUndefined;
505 profile->nChannels = mCtx->channels;
506 profile->nSamplingRate = mCtx->sample_rate;
508 profile->nBitsPerSample = mCtx->bits_per_coded_sample;
510 return OMX_ErrorNone;
513 case OMX_IndexParamAudioDts:
515 OMX_AUDIO_PARAM_DTSTYPE *profile =
516 (OMX_AUDIO_PARAM_DTSTYPE *)params;
518 if (profile->nPortIndex != kInputPortIndex) {
519 return OMX_ErrorUndefined;
522 profile->nChannels = mCtx->channels;
523 profile->nSamplingRate = mCtx->sample_rate;
525 return OMX_ErrorNone;
528 case OMX_IndexParamAudioFFmpeg:
530 OMX_AUDIO_PARAM_FFMPEGTYPE *profile =
531 (OMX_AUDIO_PARAM_FFMPEGTYPE *)params;
533 if (profile->nPortIndex != kInputPortIndex) {
534 return OMX_ErrorUndefined;
537 profile->eCodecId = mCtx->codec_id;
538 profile->nBitRate = mCtx->bit_rate;
539 profile->nBlockAlign = mCtx->block_align;
541 profile->nBitsPerSample = mCtx->bits_per_raw_sample;
542 profile->eSampleFormat = mCtx->sample_fmt;
544 profile->nChannels = mCtx->channels;
545 profile->nSampleRate = mCtx->sample_rate;
547 return OMX_ErrorNone;
552 return SimpleSoftOMXComponent::internalGetParameter(index, params);
556 OMX_ERRORTYPE SoftFFmpegAudio::isRoleSupported(
557 const OMX_PARAM_COMPONENTROLETYPE *roleParams) {
558 for (size_t i = 0; i < kNumAudioComponents; i++) {
559 if (mCodingType == kAudioComponents[i].mAudioCodingType &&
560 strncmp((const char *)roleParams->cRole,
561 kAudioComponents[i].mRole, OMX_MAX_STRINGNAME_SIZE - 1) == 0) {
562 return OMX_ErrorNone;
565 ALOGE("unsupported role: %s", (const char *)roleParams->cRole);
566 return OMX_ErrorUndefined;
569 void SoftFFmpegAudio::adjustAudioParams() {
571 mReconfiguring = isConfigured();
573 // let android audio mixer to downmix if there is no multichannel output
574 // and use number of channels from the source file, useful for HDMI/offload output
575 mAudioTgtChannels = mCtx->channels;
577 mAudioTgtFreq = FFMIN(192000, FFMAX(8000, mCtx->sample_rate));
579 mAudioTgtChannels = mCtx->channels;
580 mAudioTgtFreq = mCtx->sample_rate;
582 mAudioTgtChannelLayout = av_get_default_channel_layout(mAudioTgtChannels);
584 ALOGV("adjustAudioParams: [channels=%d freq=%d fmt=%s]",
585 mCtx->channels, mCtx->sample_rate, av_get_sample_fmt_name(mAudioTgtFmt));
588 OMX_ERRORTYPE SoftFFmpegAudio::internalSetParameter(
589 OMX_INDEXTYPE index, const OMX_PTR params) {
590 //ALOGV("internalSetParameter index:0x%x", index);
591 switch ((int)index) {
592 case OMX_IndexParamStandardComponentRole:
594 const OMX_PARAM_COMPONENTROLETYPE *roleParams =
595 (const OMX_PARAM_COMPONENTROLETYPE *)params;
596 return isRoleSupported(roleParams);
599 case OMX_IndexParamAudioPcm:
601 const OMX_AUDIO_PARAM_PCMMODETYPE *profile =
602 (const OMX_AUDIO_PARAM_PCMMODETYPE *)params;
604 if (profile->nPortIndex != kOutputPortIndex) {
605 return OMX_ErrorUndefined;
608 switch (profile->nBitPerSample) {
610 mAudioTgtFmt = AV_SAMPLE_FMT_U8;
613 mAudioTgtFmt = AV_SAMPLE_FMT_S16;
616 mAudioTgtFmt = AV_SAMPLE_FMT_S32;
619 if (profile->eNumData == OMX_NumericalDataFloat) {
620 mAudioTgtFmt = AV_SAMPLE_FMT_FLT;
623 mAudioTgtFmt = AV_SAMPLE_FMT_S32;
626 ALOGE("Unknown PCM encoding, assuming floating point");
627 mAudioTgtFmt = AV_SAMPLE_FMT_FLT;
630 mAudioTgtFreq = profile->nSamplingRate;
631 mAudioTgtChannels = profile->nChannels;
633 ALOGV("set OMX_IndexParamAudioPcm, nChannels:%u, "
634 "nSampleRate:%u, nBitPerSample:%u",
635 profile->nChannels, profile->nSamplingRate,
636 profile->nBitPerSample);
638 return OMX_ErrorNone;
641 case OMX_IndexParamAudioAac:
643 const OMX_AUDIO_PARAM_AACPROFILETYPE *profile =
644 (const OMX_AUDIO_PARAM_AACPROFILETYPE *)params;
646 if (profile->nPortIndex != kInputPortIndex) {
647 return OMX_ErrorUndefined;
650 mCtx->channels = profile->nChannels;
651 mCtx->sample_rate = profile->nSampleRate;
655 ALOGV("set OMX_IndexParamAudioAac, nChannels:%u, nSampleRate:%u",
656 profile->nChannels, profile->nSampleRate);
658 return OMX_ErrorNone;
661 case OMX_IndexParamAudioMp3:
663 const OMX_AUDIO_PARAM_MP3TYPE *profile =
664 (const OMX_AUDIO_PARAM_MP3TYPE *)params;
666 if (profile->nPortIndex != kInputPortIndex) {
667 return OMX_ErrorUndefined;
670 mCtx->channels = profile->nChannels;
671 mCtx->sample_rate = profile->nSampleRate;
675 ALOGV("set OMX_IndexParamAudioMp3, nChannels:%u, nSampleRate:%u",
676 profile->nChannels, profile->nSampleRate);
678 return OMX_ErrorNone;
681 case OMX_IndexParamAudioVorbis:
683 const OMX_AUDIO_PARAM_VORBISTYPE *profile =
684 (const OMX_AUDIO_PARAM_VORBISTYPE *)params;
686 if (profile->nPortIndex != kInputPortIndex) {
687 return OMX_ErrorUndefined;
690 mCtx->channels = profile->nChannels;
691 mCtx->sample_rate = profile->nSampleRate;
695 ALOGD("set OMX_IndexParamAudioVorbis, "
696 "nChannels=%u, nSampleRate=%u, nBitRate=%u, "
697 "nMinBitRate=%u, nMaxBitRate=%u",
698 profile->nChannels, profile->nSampleRate,
699 profile->nBitRate, profile->nMinBitRate,
700 profile->nMaxBitRate);
702 return OMX_ErrorNone;
705 case OMX_IndexParamAudioWma:
707 OMX_AUDIO_PARAM_WMATYPE *profile =
708 (OMX_AUDIO_PARAM_WMATYPE *)params;
710 if (profile->nPortIndex != kInputPortIndex) {
711 return OMX_ErrorUndefined;
714 if (profile->eFormat == OMX_AUDIO_WMAFormat7) {
715 mCtx->codec_id = AV_CODEC_ID_WMAV2;
716 } else if (profile->eFormat == OMX_AUDIO_WMAFormat8) {
717 mCtx->codec_id = AV_CODEC_ID_WMAPRO;
718 } else if (profile->eFormat == OMX_AUDIO_WMAFormat9) {
719 mCtx->codec_id = AV_CODEC_ID_WMALOSSLESS;
721 ALOGE("unsupported wma codec: 0x%x", profile->eFormat);
722 return OMX_ErrorUndefined;
725 mCtx->channels = profile->nChannels;
726 mCtx->sample_rate = profile->nSamplingRate;
728 // wmadec needs bitrate, block_align
729 mCtx->bit_rate = profile->nBitRate;
730 mCtx->block_align = profile->nBlockAlign;
734 ALOGV("set OMX_IndexParamAudioWma, nChannels:%u, "
735 "nSampleRate:%u, nBitRate:%u, nBlockAlign:%u",
736 profile->nChannels, profile->nSamplingRate,
737 profile->nBitRate, profile->nBlockAlign);
739 return OMX_ErrorNone;
742 case OMX_IndexParamAudioRa:
744 OMX_AUDIO_PARAM_RATYPE *profile =
745 (OMX_AUDIO_PARAM_RATYPE *)params;
747 if (profile->nPortIndex != kInputPortIndex) {
748 return OMX_ErrorUndefined;
751 mCtx->channels = profile->nChannels;
752 mCtx->sample_rate = profile->nSamplingRate;
754 // FIXME, HACK!!!, I use the nNumRegions parameter pass blockAlign!!!
755 // the cook audio codec need blockAlign!
756 mCtx->block_align = profile->nNumRegions;
760 ALOGV("set OMX_IndexParamAudioRa, nChannels:%u, "
761 "nSampleRate:%u, nBlockAlign:%d",
762 profile->nChannels, profile->nSamplingRate, mCtx->block_align);
764 return OMX_ErrorNone;
767 case OMX_IndexParamAudioFlac:
769 OMX_AUDIO_PARAM_FLACTYPE *profile =
770 (OMX_AUDIO_PARAM_FLACTYPE *)params;
772 if (profile->nPortIndex != kInputPortIndex) {
773 return OMX_ErrorUndefined;
776 mCtx->channels = profile->nChannels;
777 mCtx->sample_rate = profile->nSampleRate;
781 ALOGV("set OMX_IndexParamAudioFlac, nChannels:%u, nSampleRate:%u ",
782 profile->nChannels, profile->nSampleRate);
784 return OMX_ErrorNone;
787 case OMX_IndexParamAudioMp2:
789 OMX_AUDIO_PARAM_MP2TYPE *profile =
790 (OMX_AUDIO_PARAM_MP2TYPE *)params;
792 if (profile->nPortIndex != kInputPortIndex) {
793 return OMX_ErrorUndefined;
796 mCtx->channels = profile->nChannels;
797 mCtx->sample_rate = profile->nSampleRate;
801 ALOGV("set OMX_IndexParamAudioMp2, nChannels:%u, nSampleRate:%u",
802 profile->nChannels, profile->nSampleRate);
804 return OMX_ErrorNone;
807 case OMX_IndexParamAudioAc3:
809 OMX_AUDIO_PARAM_AC3TYPE *profile =
810 (OMX_AUDIO_PARAM_AC3TYPE *)params;
812 if (profile->nPortIndex != kInputPortIndex) {
813 return OMX_ErrorUndefined;
816 mCtx->channels = profile->nChannels;
817 mCtx->sample_rate = profile->nSamplingRate;
821 ALOGV("set OMX_IndexParamAudioAc3, nChannels:%u, nSampleRate:%u",
822 profile->nChannels, profile->nSamplingRate);
824 return OMX_ErrorNone;
827 case OMX_IndexParamAudioAndroidAc3:
829 OMX_AUDIO_PARAM_ANDROID_AC3TYPE *profile =
830 (OMX_AUDIO_PARAM_ANDROID_AC3TYPE *)params;
832 if (profile->nPortIndex != kInputPortIndex) {
833 return OMX_ErrorUndefined;
836 mCtx->channels = profile->nChannels;
837 mCtx->sample_rate = profile->nSampleRate;
841 ALOGV("set OMX_IndexParamAudioAndroidAc3, nChannels:%u, nSampleRate:%u",
842 profile->nChannels, profile->nSampleRate);
844 return OMX_ErrorNone;
847 case OMX_IndexParamAudioAlac:
849 OMX_AUDIO_PARAM_ALACTYPE *profile =
850 (OMX_AUDIO_PARAM_ALACTYPE *)params;
852 if (profile->nPortIndex != kInputPortIndex) {
853 return OMX_ErrorUndefined;
856 mCtx->channels = profile->nChannels;
857 mCtx->sample_rate = profile->nSamplingRate;
858 mCtx->bits_per_coded_sample = profile->nBitsPerSample;
862 ALOGV("set OMX_IndexParamAudioAlac, nChannels:%u, "
863 "nSampleRate:%u, nBitsPerSample:%u",
864 profile->nChannels, profile->nSamplingRate,
865 profile->nBitsPerSample);
867 return OMX_ErrorNone;
870 case OMX_IndexParamAudioApe:
872 OMX_AUDIO_PARAM_APETYPE *profile =
873 (OMX_AUDIO_PARAM_APETYPE *)params;
875 if (profile->nPortIndex != kInputPortIndex) {
876 return OMX_ErrorUndefined;
879 mCtx->channels = profile->nChannels;
880 mCtx->sample_rate = profile->nSamplingRate;
881 mCtx->bits_per_coded_sample = profile->nBitsPerSample;
885 ALOGV("set OMX_IndexParamAudioApe, nChannels:%u, "
886 "nSampleRate:%u, nBitsPerSample:%u",
887 profile->nChannels, profile->nSamplingRate,
888 profile->nBitsPerSample);
890 return OMX_ErrorNone;
893 case OMX_IndexParamAudioDts:
895 OMX_AUDIO_PARAM_DTSTYPE *profile =
896 (OMX_AUDIO_PARAM_DTSTYPE *)params;
898 if (profile->nPortIndex != kInputPortIndex) {
899 return OMX_ErrorUndefined;
902 mCtx->channels = profile->nChannels;
903 mCtx->sample_rate = profile->nSamplingRate;
907 ALOGV("set OMX_IndexParamAudioDts, nChannels:%u, nSampleRate:%u",
908 profile->nChannels, profile->nSamplingRate);
910 return OMX_ErrorNone;
913 case OMX_IndexParamAudioFFmpeg:
915 OMX_AUDIO_PARAM_FFMPEGTYPE *profile =
916 (OMX_AUDIO_PARAM_FFMPEGTYPE *)params;
918 if (profile->nPortIndex != kInputPortIndex) {
919 return OMX_ErrorUndefined;
922 mCtx->codec_id = (enum AVCodecID)profile->eCodecId;
923 mCtx->channels = profile->nChannels;
924 mCtx->bit_rate = profile->nBitRate;
925 mCtx->sample_rate = profile->nSampleRate;
926 mCtx->block_align = profile->nBlockAlign;
927 mCtx->bits_per_coded_sample = profile->nBitsPerSample;
928 mCtx->sample_fmt = (AVSampleFormat)profile->eSampleFormat;
932 ALOGD("set OMX_IndexParamAudioFFmpeg, "
933 "eCodecId:%d(%s), nChannels:%u, nBitRate:%u, "
934 "nBitsPerSample:%u, nSampleRate:%u, "
935 "nBlockAlign:%u, eSampleFormat:%u(%s)",
936 profile->eCodecId, avcodec_get_name(mCtx->codec_id),
937 profile->nChannels, profile->nBitRate,
938 profile->nBitsPerSample, profile->nSampleRate,
939 profile->nBlockAlign, profile->eSampleFormat,
940 av_get_sample_fmt_name(mCtx->sample_fmt));
941 return OMX_ErrorNone;
946 return SimpleSoftOMXComponent::internalSetParameter(index, params);
950 int32_t SoftFFmpegAudio::handleVorbisExtradata(OMX_BUFFERHEADERTYPE *inHeader)
952 uint8_t *p = inHeader->pBuffer + inHeader->nOffset;
953 int len = inHeader->nFilledLen;
958 } else if (p[0] == 3) {
960 } else if (p[0] == 5) {
963 ALOGE("error vorbis codec config");
964 return ERR_INVALID_PARAM;
967 mVorbisHeaderStart[index] = (uint8_t *)av_mallocz(len);
968 if (!mVorbisHeaderStart[index]) {
969 ALOGE("oom for vorbis extradata");
972 memcpy(mVorbisHeaderStart[index], p, len);
973 mVorbisHeaderLen[index] = inHeader->nFilledLen;
978 int32_t SoftFFmpegAudio::handleExtradata() {
979 List<BufferInfo *> &inQueue = getPortQueue(kInputPortIndex);
980 BufferInfo *inInfo = *inQueue.begin();
981 OMX_BUFFERHEADERTYPE *inHeader = inInfo->mHeader;
983 ALOGI("got extradata, ignore: %d, size: %u",
984 mIgnoreExtradata, inHeader->nFilledLen);
985 hexdump(inHeader->pBuffer + inHeader->nOffset, inHeader->nFilledLen);
987 if (mIgnoreExtradata) {
988 ALOGI("got extradata, size: %u, but ignore it", inHeader->nFilledLen);
990 if (!mExtradataReady) {
991 uint32_t ret = ERR_OK;
992 if (mCtx->codec_id == AV_CODEC_ID_VORBIS) {
993 if ((ret = handleVorbisExtradata(inHeader)) != ERR_OK) {
994 ALOGE("ffmpeg audio decoder failed to alloc vorbis extradata memory.");
998 int orig_extradata_size = mCtx->extradata_size;
999 mCtx->extradata_size += inHeader->nFilledLen;
1000 mCtx->extradata = (uint8_t *)av_realloc(mCtx->extradata,
1001 mCtx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
1002 if (!mCtx->extradata) {
1003 ALOGE("ffmpeg audio decoder failed to alloc extradata memory.");
1007 memcpy(mCtx->extradata + orig_extradata_size,
1008 inHeader->pBuffer + inHeader->nOffset, inHeader->nFilledLen);
1009 memset(mCtx->extradata + mCtx->extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1014 inInfo->mOwnedByUs = false;
1015 inQueue.erase(inQueue.begin());
1017 notifyEmptyBufferDone(inHeader);
1023 int32_t SoftFFmpegAudio::openDecoder() {
1024 if (mCodecAlreadyOpened) {
1028 if (!mExtradataReady && !mIgnoreExtradata) {
1029 if (mCtx->codec_id == AV_CODEC_ID_VORBIS) {
1030 if (!setup_vorbis_extradata(&mCtx->extradata,
1031 &mCtx->extradata_size,
1032 (const uint8_t **)mVorbisHeaderStart,
1033 mVorbisHeaderLen)) {
1038 ALOGI("extradata is ready, size: %d", mCtx->extradata_size);
1039 hexdump(mCtx->extradata, mCtx->extradata_size);
1040 mExtradataReady = true;
1044 mCtx->codec = avcodec_find_decoder(mCtx->codec_id);
1046 ALOGE("ffmpeg audio decoder failed to find codec");
1047 return ERR_CODEC_NOT_FOUND;
1050 setDefaultCtx(mCtx, mCtx->codec);
1052 ALOGD("begin to open ffmpeg audio decoder(%s), mCtx sample_rate: %d, channels: %d",
1053 avcodec_get_name(mCtx->codec_id),
1054 mCtx->sample_rate, mCtx->channels);
1056 int err = avcodec_open2(mCtx, mCtx->codec, NULL);
1058 ALOGE("ffmpeg audio decoder failed to initialize.(%s)", av_err2str(err));
1059 return ERR_DECODER_OPEN_FAILED;
1061 mCodecAlreadyOpened = true;
1063 ALOGD("open ffmpeg audio decoder(%s) success, mCtx sample_rate: %d, "
1064 "channels: %d, sample_fmt: %s, bits_per_coded_sample: %d, bits_per_raw_sample: %d",
1065 avcodec_get_name(mCtx->codec_id),
1066 mCtx->sample_rate, mCtx->channels,
1067 av_get_sample_fmt_name(mCtx->sample_fmt),
1068 mCtx->bits_per_coded_sample, mCtx->bits_per_raw_sample);
1070 mFrame = av_frame_alloc();
1072 ALOGE("oom for video frame");
1076 mAudioSrcFmt = mCtx->sample_fmt;
1077 mAudioSrcChannels = mCtx->channels;
1078 mAudioSrcFreq = mCtx->sample_rate;
1079 mAudioSrcChannelLayout = av_get_default_channel_layout(mCtx->channels);
1084 void SoftFFmpegAudio::updateTimeStamp(OMX_BUFFERHEADERTYPE *inHeader) {
1085 CHECK_EQ(mInputBufferSize, 0);
1087 //XXX reset to AV_NOPTS_VALUE if the pts is invalid
1088 if (inHeader->nTimeStamp == SF_NOPTS_VALUE) {
1089 inHeader->nTimeStamp = AV_NOPTS_VALUE;
1092 //update the audio clock if the pts is valid
1093 if (inHeader->nTimeStamp != AV_NOPTS_VALUE) {
1094 setAudioClock(inHeader->nTimeStamp);
1098 void SoftFFmpegAudio::initPacket(AVPacket *pkt,
1099 OMX_BUFFERHEADERTYPE *inHeader) {
1100 memset(pkt, 0, sizeof(AVPacket));
1101 av_init_packet(pkt);
1104 pkt->data = (uint8_t *)inHeader->pBuffer + inHeader->nOffset;
1105 pkt->size = inHeader->nFilledLen;
1106 pkt->pts = inHeader->nTimeStamp; //ingore it, we will compute it
1110 pkt->pts = AV_NOPTS_VALUE;
1114 if (pkt->pts != AV_NOPTS_VALUE)
1116 ALOGV("pkt size:%d, pts:%lld", pkt->size, pkt->pts);
1118 ALOGV("pkt size:%d, pts:N/A", pkt->size);
1123 int32_t SoftFFmpegAudio::decodeAudio() {
1126 int32_t ret = ERR_OK;
1127 int32_t inputBufferUsedLength = 0;
1128 bool is_flush = (mEOSStatus == OUTPUT_FRAMES_FLUSHED);
1129 List<BufferInfo *> &inQueue = getPortQueue(kInputPortIndex);
1130 BufferInfo *inInfo = NULL;
1131 OMX_BUFFERHEADERTYPE *inHeader = NULL;
1133 CHECK_EQ(mResampledDataSize, 0);
1135 if (!is_flush && !inQueue.empty()) {
1136 inInfo = *inQueue.begin();
1137 if (inInfo != NULL) {
1138 inHeader = inInfo->mHeader;
1140 if (mInputBufferSize == 0) {
1141 updateTimeStamp(inHeader);
1142 mInputBufferSize = inHeader->nFilledLen;
1147 if (mEOSStatus == INPUT_EOS_SEEN && (!inHeader || inHeader->nFilledLen == 0)
1148 && !(mCtx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
1153 initPacket(&pkt, inHeader);
1155 len = avcodec_decode_audio4(mCtx, mFrame, &gotFrm, &pkt);
1156 av_packet_unref(&pkt);
1158 //a negative error code is returned if an error occurred during decoding
1160 ALOGW("ffmpeg audio decoder err, we skip the frame and play silence instead");
1161 mResampledData = mSilenceBuffer;
1162 mResampledDataSize = kOutputBufferSize;
1166 ALOGV("ffmpeg audio decoder, consume pkt len: %d", len);
1170 ALOGI("ffmpeg audio decoder failed to get frame.");
1172 //stop sending empty packets if the decoder is finished
1173 if (is_flush && mCtx->codec->capabilities & AV_CODEC_CAP_DELAY) {
1174 ALOGI("ffmpeg audio decoder failed to get more frames when flush.");
1180 ret = resampleAudio();
1186 //if error, we skip the frame
1187 inputBufferUsedLength = mInputBufferSize;
1189 inputBufferUsedLength = len;
1191 mInputBufferSize -= inputBufferUsedLength;
1193 if (inHeader != NULL) {
1194 CHECK_GE(inHeader->nFilledLen, inputBufferUsedLength);
1195 inHeader->nOffset += inputBufferUsedLength;
1196 inHeader->nFilledLen -= inputBufferUsedLength;
1198 if (inHeader->nFilledLen == 0) {
1199 CHECK_EQ(mInputBufferSize, 0);
1200 inQueue.erase(inQueue.begin());
1201 inInfo->mOwnedByUs = false;
1202 notifyEmptyBufferDone(inHeader);
1210 #ifdef LIBAV_CONFIG_H
1211 #define av_frame_get_channels(f) av_get_channel_layout_nb_channels(f->channel_layout)
1214 int32_t SoftFFmpegAudio::resampleAudio() {
1216 int64_t channelLayout = 0;
1217 size_t dataSize = 0;
1219 dataSize = av_samples_get_buffer_size(NULL, av_frame_get_channels(mFrame),
1220 mFrame->nb_samples, (enum AVSampleFormat)mFrame->format, 1);
1223 ALOGV("ffmpeg audio decoder, nb_samples:%d, get buffer size:%d",
1224 mFrame->nb_samples, dataSize);
1227 channels = av_get_channel_layout_nb_channels(mFrame->channel_layout);
1229 (mFrame->channel_layout && av_frame_get_channels(mFrame) == channels) ?
1230 mFrame->channel_layout : av_get_default_channel_layout(av_frame_get_channels(mFrame));
1232 // Create if we're reconfiguring, if the format changed mid-stream, or
1233 // if the output format is actually different
1234 if ((mReconfiguring && mSwrCtx) || (!mSwrCtx
1235 && (mFrame->format != mAudioSrcFmt
1236 || channelLayout != mAudioSrcChannelLayout
1237 || (unsigned int)mFrame->sample_rate != mAudioSrcFreq
1238 || mAudioSrcFmt != mAudioTgtFmt
1239 || mAudioSrcChannelLayout != mAudioTgtChannelLayout
1240 || mAudioSrcFreq != mAudioTgtFreq))) {
1241 #ifdef LIBAV_CONFIG_H
1247 mSwrCtx = swr_alloc_set_opts(NULL,
1248 mAudioTgtChannelLayout, mAudioTgtFmt, mAudioTgtFreq,
1249 channelLayout, (enum AVSampleFormat)mFrame->format, mFrame->sample_rate,
1251 if (!mSwrCtx || swr_init(mSwrCtx) < 0) {
1253 ALOGE("Cannot create sample rate converter for conversion "
1254 "of %d Hz %s %d channels to %d Hz %s %d channels!",
1255 mFrame->sample_rate,
1256 av_get_sample_fmt_name((enum AVSampleFormat)mFrame->format),
1257 av_frame_get_channels(mFrame),
1259 av_get_sample_fmt_name(mAudioTgtFmt),
1261 return ERR_SWR_INIT_FAILED;
1264 char src_layout_name[1024] = {0};
1265 char tgt_layout_name[1024] = {0};
1266 av_get_channel_layout_string(src_layout_name, sizeof(src_layout_name),
1267 mCtx->channels, channelLayout);
1268 av_get_channel_layout_string(tgt_layout_name, sizeof(tgt_layout_name),
1269 mAudioTgtChannels, mAudioTgtChannelLayout);
1270 ALOGI("Create sample rate converter for conversion "
1271 "of %d Hz %s %d channels(%s) "
1272 "to %d Hz %s %d channels(%s)!",
1273 mFrame->sample_rate,
1274 av_get_sample_fmt_name((enum AVSampleFormat)mFrame->format),
1275 av_frame_get_channels(mFrame),
1278 av_get_sample_fmt_name(mAudioTgtFmt),
1282 mAudioSrcChannelLayout = channelLayout;
1283 mAudioSrcChannels = av_frame_get_channels(mFrame);
1284 mAudioSrcFreq = mFrame->sample_rate;
1285 mAudioSrcFmt = (enum AVSampleFormat)mFrame->format;
1286 mReconfiguring = false;
1290 #ifdef LIBAV_CONFIG_H
1292 const uint8_t **in = (const uint8_t **)mFrame->extended_data;
1293 uint8_t *out[] = {mAudioBuffer};
1294 int out_count = sizeof(mAudioBuffer) / mAudioTgtChannels / av_get_bytes_per_sample(mAudioTgtFmt);
1295 int out_size = av_samples_get_buffer_size(NULL, mAudioTgtChannels, out_count, mAudioTgtFmt, 0);
1298 ALOGE("av_samples_get_buffer_size() failed");
1299 return ERR_INVALID_PARAM;
1302 len2 = swr_convert(mSwrCtx, out, out_count, in, mFrame->nb_samples);
1304 ALOGE("audio_resample() failed");
1305 return ERR_RESAMPLE_FAILED;
1307 if (len2 == out_count) {
1308 ALOGE("warning: audio buffer is probably too small");
1311 mResampledData = mAudioBuffer;
1312 mResampledDataSize = len2 * mAudioTgtChannels * av_get_bytes_per_sample(mAudioTgtFmt);
1315 ALOGV("ffmpeg audio decoder(resample), mFrame->nb_samples:%d, len2:%d, mResampledDataSize:%d, "
1316 "src channel:%u, src fmt:%s, tgt channel:%u, tgt fmt:%s",
1317 mFrame->nb_samples, len2, mResampledDataSize,
1318 av_frame_get_channels(mFrame),
1319 av_get_sample_fmt_name((enum AVSampleFormat)mFrame->format),
1321 av_get_sample_fmt_name(mAudioTgtFmt));
1325 mResampledData = mFrame->data[0];
1326 mResampledDataSize = dataSize;
1329 ALOGV("ffmpeg audio decoder(no resample),"
1330 "nb_samples(before resample):%d, mResampledDataSize:%d",
1331 mFrame->nb_samples, mResampledDataSize);
1338 void SoftFFmpegAudio::drainOneOutputBuffer() {
1339 List<BufferInfo *> &outQueue = getPortQueue(kOutputPortIndex);
1340 BufferInfo *outInfo = *outQueue.begin();
1341 CHECK(outInfo != NULL);
1342 OMX_BUFFERHEADERTYPE *outHeader = outInfo->mHeader;
1343 List<BufferInfo *> &inQueue = getPortQueue(kInputPortIndex);
1344 BufferInfo *inInfo = *inQueue.begin();
1345 OMX_BUFFERHEADERTYPE *inHeader = NULL;
1347 if (inHeader != NULL) {
1348 inHeader = inInfo->mHeader;
1351 CHECK_GT(mResampledDataSize, 0);
1353 size_t copy = mResampledDataSize;
1354 if (mResampledDataSize > kOutputBufferSize) {
1355 copy = kOutputBufferSize;
1358 outHeader->nOffset = 0;
1359 outHeader->nFilledLen = copy;
1360 outHeader->nTimeStamp = getAudioClock();
1361 memcpy(outHeader->pBuffer, mResampledData, copy);
1362 outHeader->nFlags = 0;
1364 //update mResampledSize
1365 mResampledData += copy;
1366 mResampledDataSize -= copy;
1369 size_t frames = copy / (av_get_bytes_per_sample(mAudioTgtFmt) * mAudioTgtChannels);
1370 setAudioClock(getAudioClock() + ((frames * 1000000ll) / mAudioTgtFreq));
1373 ALOGV("ffmpeg audio decoder, fill out buffer, copy:%u, pts: %lld, clock: %lld",
1374 copy, outHeader->nTimeStamp, getAudioClock());
1377 outQueue.erase(outQueue.begin());
1378 outInfo->mOwnedByUs = false;
1379 notifyFillBufferDone(outHeader);
1382 void SoftFFmpegAudio::drainEOSOutputBuffer() {
1383 List<BufferInfo *> &outQueue = getPortQueue(kOutputPortIndex);
1384 BufferInfo *outInfo = *outQueue.begin();
1385 CHECK(outInfo != NULL);
1386 OMX_BUFFERHEADERTYPE *outHeader = outInfo->mHeader;
1388 // CHECK_EQ(mResampledDataSize, 0);
1390 ALOGD("ffmpeg audio decoder fill eos outbuf");
1392 outHeader->nTimeStamp = getAudioClock();
1393 outHeader->nFilledLen = 0;
1394 outHeader->nFlags = OMX_BUFFERFLAG_EOS;
1396 outQueue.erase(outQueue.begin());
1397 outInfo->mOwnedByUs = false;
1398 notifyFillBufferDone(outHeader);
1400 mEOSStatus = OUTPUT_FRAMES_FLUSHED;
1403 void SoftFFmpegAudio::drainAllOutputBuffers() {
1404 List<BufferInfo *> &outQueue = getPortQueue(kOutputPortIndex);
1406 if (!mCodecAlreadyOpened) {
1407 drainEOSOutputBuffer();
1408 mEOSStatus = OUTPUT_FRAMES_FLUSHED;
1412 while (!outQueue.empty()) {
1413 if (mResampledDataSize == 0) {
1414 int32_t err = decodeAudio();
1416 notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL);
1417 mSignalledError = true;
1419 } else if (err == ERR_FLUSHED) {
1420 drainEOSOutputBuffer();
1423 CHECK_EQ(err, ERR_OK);
1427 if (mResampledDataSize > 0) {
1428 drainOneOutputBuffer();
1433 void SoftFFmpegAudio::onQueueFilled(OMX_U32 /* portIndex */) {
1434 BufferInfo *inInfo = NULL;
1435 OMX_BUFFERHEADERTYPE *inHeader = NULL;
1437 if (mSignalledError || mOutputPortSettingsChange != NONE) {
1441 if (mEOSStatus == OUTPUT_FRAMES_FLUSHED) {
1445 List<BufferInfo *> &inQueue = getPortQueue(kInputPortIndex);
1446 List<BufferInfo *> &outQueue = getPortQueue(kOutputPortIndex);
1448 while (((mEOSStatus != INPUT_DATA_AVAILABLE) || !inQueue.empty())
1449 && !outQueue.empty()) {
1451 if (mEOSStatus == INPUT_EOS_SEEN) {
1452 drainAllOutputBuffers();
1456 inInfo = *inQueue.begin();
1457 inHeader = inInfo->mHeader;
1459 if (inHeader->nFlags & OMX_BUFFERFLAG_EOS) {
1460 ALOGD("ffmpeg audio decoder eos");
1461 mEOSStatus = INPUT_EOS_SEEN;
1465 if (inHeader->nFlags & OMX_BUFFERFLAG_CODECCONFIG) {
1466 if (handleExtradata() != ERR_OK) {
1467 notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL);
1468 mSignalledError = true;
1474 if (!mCodecAlreadyOpened) {
1475 if (openDecoder() != ERR_OK) {
1476 notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL);
1477 mSignalledError = true;
1482 if (mResampledDataSize == 0) {
1483 int32_t err = decodeAudio();
1485 notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL);
1486 mSignalledError = true;
1488 } else if (err == ERR_NO_FRM) {
1489 CHECK_EQ(mResampledDataSize, 0);
1492 CHECK_EQ(err, ERR_OK);
1496 if (mResampledDataSize > 0) {
1497 drainOneOutputBuffer();
1502 void SoftFFmpegAudio::onPortFlushCompleted(OMX_U32 portIndex) {
1503 ALOGV("ffmpeg audio decoder flush port(%u)", portIndex);
1504 if (portIndex == kInputPortIndex) {
1505 if (mCtx && avcodec_is_open(mCtx)) {
1506 //Make sure that the next buffer output does not still
1507 //depend on fragments from the last one decoded.
1508 avcodec_flush_buffers(mCtx);
1512 mInputBufferSize = 0;
1513 mResampledDataSize = 0;
1514 mResampledData = NULL;
1515 mEOSStatus = INPUT_DATA_AVAILABLE;
1519 void SoftFFmpegAudio::onPortEnableCompleted(OMX_U32 portIndex, bool enabled) {
1520 if (portIndex != kOutputPortIndex) {
1524 switch (mOutputPortSettingsChange) {
1528 case AWAITING_DISABLED:
1531 mOutputPortSettingsChange = AWAITING_ENABLED;
1537 CHECK_EQ((int)mOutputPortSettingsChange, (int)AWAITING_ENABLED);
1539 mOutputPortSettingsChange = NONE;
1545 int64_t SoftFFmpegAudio::getAudioClock() {
1546 if (sAudioClock == NULL) {
1547 sAudioClock = (int64_t*) malloc(sizeof(int64_t));
1550 ALOGV("getAudioClock: %" PRId64, *sAudioClock);
1551 return *sAudioClock;
1554 void SoftFFmpegAudio::setAudioClock(int64_t ticks) {
1555 if (sAudioClock == NULL) {
1556 sAudioClock = (int64_t*) malloc(sizeof(int64_t));
1558 *sAudioClock = ticks;
1561 void SoftFFmpegAudio::onReset() {
1562 enum AVCodecID codecID = mCtx->codec_id;
1564 initDecoder(codecID);
1565 mSignalledError = false;
1566 mOutputPortSettingsChange = NONE;
1567 mEOSStatus = INPUT_DATA_AVAILABLE;
1570 SoftOMXComponent* SoftFFmpegAudio::createSoftOMXComponent(
1571 const char *name, const OMX_CALLBACKTYPE *callbacks,
1572 OMX_PTR appData, OMX_COMPONENTTYPE **component) {
1573 OMX_AUDIO_CODINGTYPE codingType = OMX_AUDIO_CodingAutoDetect;
1574 char *componentRole = NULL;
1575 enum AVCodecID codecID = AV_CODEC_ID_NONE;
1577 for (size_t i = 0; i < kNumAudioComponents; ++i) {
1578 if (!strcasecmp(name, kAudioComponents[i].mName)) {
1579 componentRole = strdup(kAudioComponents[i].mRole);
1580 codingType = kAudioComponents[i].mAudioCodingType;
1581 codecID = kAudioComponents[i].mCodecID;
1586 return new SoftFFmpegAudio(name, componentRole, codingType, codecID,
1587 callbacks, appData, component);
1590 } // namespace android