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/common_utils.h"
28 #include "utils/ffmpeg_utils.h"
39 static void InitOMXParams(T *params) {
40 params->nSize = sizeof(T);
41 params->nVersion.s.nVersionMajor = 1;
42 params->nVersion.s.nVersionMinor = 0;
43 params->nVersion.s.nRevision = 0;
44 params->nVersion.s.nStep = 0;
47 SoftFFmpegAudio::SoftFFmpegAudio(
49 const OMX_CALLBACKTYPE *callbacks,
51 OMX_COMPONENTTYPE **component)
52 : SimpleSoftOMXComponent(name, callbacks, appData, component),
57 mExtradataReady(false),
58 mIgnoreExtradata(false),
59 mFlushComplete(false),
60 mSignalledError(false),
70 mSamplingFmt(AV_SAMPLE_FMT_S16),
71 mAudioConfigChanged(false),
72 mOutputPortSettingsChange(NONE) {
73 if (!strcmp(name, "OMX.ffmpeg.mp3.decoder")) {
75 mIgnoreExtradata = true;
76 } else if (!strcmp(name, "OMX.ffmpeg.mp1.decoder")) {
78 } else if (!strcmp(name, "OMX.ffmpeg.mp2.decoder")) {
80 } else if (!strcmp(name, "OMX.ffmpeg.aac.decoder")) {
82 } else if (!strcmp(name, "OMX.ffmpeg.ape.decoder")) {
84 } else if (!strcmp(name, "OMX.ffmpeg.wma.decoder")) {
86 } else if (!strcmp(name, "OMX.ffmpeg.dts.decoder")) {
88 } else if (!strcmp(name, "OMX.ffmpeg.ra.decoder")) {
91 CHECK(!strcmp(name, "OMX.ffmpeg.ac3.decoder"));
95 LOGV("SoftFFmpegAudio component: %s", name);
98 CHECK_EQ(initDecoder(), (status_t)OK);
101 SoftFFmpegAudio::~SoftFFmpegAudio() {
103 LOGV("~SoftFFmpegAudio");
108 void SoftFFmpegAudio::initPorts() {
109 OMX_PARAM_PORTDEFINITIONTYPE def;
113 def.eDir = OMX_DirInput;
114 def.nBufferCountMin = kNumBuffers;
115 def.nBufferCountActual = def.nBufferCountMin;
116 def.nBufferSize = 8192;
117 def.bEnabled = OMX_TRUE;
118 def.bPopulated = OMX_FALSE;
119 def.eDomain = OMX_PortDomainAudio;
120 def.bBuffersContiguous = OMX_FALSE;
121 def.nBufferAlignment = 1;
125 def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_MPEG);
126 def.format.audio.eEncoding = OMX_AUDIO_CodingMP3;
129 def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_MPEG_LAYER_I);
130 def.format.audio.eEncoding = OMX_AUDIO_CodingMP3;
133 def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_MPEG_LAYER_II);
134 def.format.audio.eEncoding = OMX_AUDIO_CodingMP3;
137 def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_AAC);
138 def.format.audio.eEncoding = OMX_AUDIO_CodingAAC;
141 def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_AC3);
143 //def.format.audio.eEncoding = OMX_AUDIO_CodingAC3;
144 def.format.audio.eEncoding = OMX_AUDIO_CodingAutoDetect; // right?? orz
147 def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_WMA);
148 def.format.audio.eEncoding = OMX_AUDIO_CodingWMA;
151 CHECK(!"Should not be here. Unsupported mime type and compression format");
155 def.format.audio.pNativeRender = NULL;
156 def.format.audio.bFlagErrorConcealment = OMX_FALSE;
161 def.eDir = OMX_DirOutput;
162 def.nBufferCountMin = kNumBuffers;
163 def.nBufferCountActual = def.nBufferCountMin;
164 def.nBufferSize = kOutputBufferSize;
165 def.bEnabled = OMX_TRUE;
166 def.bPopulated = OMX_FALSE;
167 def.eDomain = OMX_PortDomainAudio;
168 def.bBuffersContiguous = OMX_FALSE;
169 def.nBufferAlignment = 2;
171 def.format.audio.cMIMEType = const_cast<char *>("audio/raw");
172 def.format.audio.pNativeRender = NULL;
173 def.format.audio.bFlagErrorConcealment = OMX_FALSE;
174 def.format.audio.eEncoding = OMX_AUDIO_CodingPCM;
179 void SoftFFmpegAudio::setAVCtxToDefault(AVCodecContext *avctx, const AVCodec *codec) {
182 avctx->workaround_bugs = 1;
184 if(avctx->lowres > codec->max_lowres){
185 LOGW("The maximum value for lowres supported by the decoder is %d",
187 avctx->lowres= codec->max_lowres;
189 avctx->idct_algo = 0;
190 avctx->skip_frame = AVDISCARD_DEFAULT;
191 avctx->skip_idct = AVDISCARD_DEFAULT;
192 avctx->skip_loop_filter = AVDISCARD_DEFAULT;
193 avctx->error_concealment = 3;
195 if(avctx->lowres) avctx->flags |= CODEC_FLAG_EMU_EDGE;
196 if (fast) avctx->flags2 |= CODEC_FLAG2_FAST;
197 if(codec->capabilities & CODEC_CAP_DR1)
198 avctx->flags |= CODEC_FLAG_EMU_EDGE;
201 status_t SoftFFmpegAudio::initDecoder() {
204 status = initFFmpeg();
208 mCtx = avcodec_alloc_context3(NULL);
211 LOGE("avcodec_alloc_context failed.");
215 mCtx->codec_type = AVMEDIA_TYPE_AUDIO;
218 mCtx->codec_id = CODEC_ID_MP3;
221 mCtx->codec_id = CODEC_ID_MP1;
224 mCtx->codec_id = CODEC_ID_MP2;
227 mCtx->codec_id = CODEC_ID_AAC;
230 mCtx->codec_id = CODEC_ID_AC3;
233 mCtx->codec_id = CODEC_ID_WMAV2; // FIXME, CODEC_ID_WMAV1 or CODEC_ID_WMAV2?
236 CHECK(!"Should not be here. Unsupported codec");
240 mCtx->codec = avcodec_find_decoder(mCtx->codec_id);
243 LOGE("find codec failed");
247 setAVCtxToDefault(mCtx, mCtx->codec);
249 mCtx->channels = mNumChannels;
250 mCtx->sample_rate = mSamplingRate;
251 mCtx->bit_rate = mBitRate;
252 mCtx->sample_fmt = mSamplingFmt;
254 mAudioSrcFmt = mAudioTgtFmt = AV_SAMPLE_FMT_S16;
255 mAudioSrcFreq = mAudioTgtFreq = mSamplingRate;
256 mAudioSrcChannels = mAudioTgtChannels = mNumChannels;
257 mAudioSrcChannelLayout = mAudioTgtChannelLayout =
258 av_get_default_channel_layout(mNumChannels);
260 memset(mSilenceBuffer, 0, kOutputBufferSize);
265 void SoftFFmpegAudio::deInitDecoder() {
267 //avcodec_flush_buffers(mCtx); // is it necessary? crash sometimes if call it
268 if (!mCtx->extradata) {
269 av_free(mCtx->extradata);
270 mCtx->extradata = NULL;
271 mCtx->extradata_size = 0;
285 OMX_ERRORTYPE SoftFFmpegAudio::internalGetParameter(
286 OMX_INDEXTYPE index, OMX_PTR params) {
287 int32_t channels = 0;
288 int32_t sampling_rate = 0;
291 case OMX_IndexParamAudioAac:
293 OMX_AUDIO_PARAM_AACPROFILETYPE *aacParams =
294 (OMX_AUDIO_PARAM_AACPROFILETYPE *)params;
296 if (aacParams->nPortIndex != 0) {
297 return OMX_ErrorUndefined;
300 aacParams->nBitRate = 0;
301 aacParams->nAudioBandWidth = 0;
302 aacParams->nAACtools = 0;
303 aacParams->nAACERtools = 0;
304 aacParams->eAACProfile = OMX_AUDIO_AACObjectMain;
305 aacParams->eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4FF;
306 aacParams->eChannelMode = OMX_AUDIO_ChannelModeStereo;
308 aacParams->nChannels = mNumChannels;
309 aacParams->nSampleRate = mSamplingRate;
311 return OMX_ErrorNone;
313 case OMX_IndexParamAudioWma:
315 OMX_AUDIO_PARAM_WMATYPE *wmaParams =
316 (OMX_AUDIO_PARAM_WMATYPE *)params;
318 if (wmaParams->nPortIndex != 0) {
319 return OMX_ErrorUndefined;
322 wmaParams->nChannels = 0;
323 wmaParams->nSamplingRate = 0;
324 wmaParams->nBitRate = 0;
325 wmaParams->eFormat = OMX_AUDIO_WMAFormatUnused;
327 return OMX_ErrorNone;
329 case OMX_IndexParamAudioPcm:
331 OMX_AUDIO_PARAM_PCMMODETYPE *pcmParams =
332 (OMX_AUDIO_PARAM_PCMMODETYPE *)params;
334 if (pcmParams->nPortIndex > 1) {
335 return OMX_ErrorUndefined;
338 pcmParams->eNumData = OMX_NumericalDataSigned;
339 pcmParams->eEndian = OMX_EndianBig;
340 pcmParams->bInterleaved = OMX_TRUE;
341 pcmParams->nBitPerSample = 16;
342 pcmParams->ePCMMode = OMX_AUDIO_PCMModeLinear;
343 pcmParams->eChannelMapping[0] = OMX_AUDIO_ChannelLF;
344 pcmParams->eChannelMapping[1] = OMX_AUDIO_ChannelRF;
346 channels = mNumChannels >= 2 ? 2 : 1;
347 sampling_rate = mSamplingRate;
348 // 4000 <= nSamplingRate <= 48000
349 if (mSamplingRate < 4000) {
350 sampling_rate = 4000;
351 } else if (mSamplingRate > 48000) {
352 sampling_rate = 48000;
355 // update src and target(except aac), only once!
356 mAudioSrcChannels = mAudioTgtChannels = channels;
357 mAudioSrcFreq = mAudioTgtFreq = sampling_rate;
358 mAudioSrcFmt = mAudioTgtFmt = AV_SAMPLE_FMT_S16;
359 mAudioSrcChannelLayout = mAudioTgtChannelLayout = av_get_default_channel_layout(channels);
361 pcmParams->nChannels = channels;
362 pcmParams->nSamplingRate = sampling_rate;
364 return OMX_ErrorNone;
368 return SimpleSoftOMXComponent::internalGetParameter(index, params);
372 OMX_ERRORTYPE SoftFFmpegAudio::internalSetParameter(
373 OMX_INDEXTYPE index, const OMX_PTR params) {
374 int32_t channels = 0;
375 int32_t sampling_rate = 0;
378 case OMX_IndexParamStandardComponentRole:
380 const OMX_PARAM_COMPONENTROLETYPE *roleParams =
381 (const OMX_PARAM_COMPONENTROLETYPE *)params;
383 bool supported = true;
386 if (strncmp((const char *)roleParams->cRole,
387 "audio_decoder.mp3", OMX_MAX_STRINGNAME_SIZE - 1))
391 if (strncmp((const char *)roleParams->cRole,
392 "audio_decoder.mp1", OMX_MAX_STRINGNAME_SIZE - 1))
396 if (strncmp((const char *)roleParams->cRole,
397 "audio_decoder.mp2", OMX_MAX_STRINGNAME_SIZE - 1))
401 if (strncmp((const char *)roleParams->cRole,
402 "audio_decoder.aac", OMX_MAX_STRINGNAME_SIZE - 1))
406 if (strncmp((const char *)roleParams->cRole,
407 "audio_decoder.ac3", OMX_MAX_STRINGNAME_SIZE - 1))
411 if (strncmp((const char *)roleParams->cRole,
412 "audio_decoder.wma", OMX_MAX_STRINGNAME_SIZE - 1))
416 CHECK(!"Should not be here. Unsupported role.");
420 LOGE("unsupported role: %s", (const char *)roleParams->cRole);
421 return OMX_ErrorUndefined;
424 return OMX_ErrorNone;
426 case OMX_IndexParamAudioAac:
428 const OMX_AUDIO_PARAM_AACPROFILETYPE *aacParams =
429 (const OMX_AUDIO_PARAM_AACPROFILETYPE *)params;
431 if (aacParams->nPortIndex != 0) {
432 return OMX_ErrorUndefined;
435 mNumChannels = aacParams->nChannels;
436 mSamplingRate = aacParams->nSampleRate;
438 channels = mNumChannels >= 2 ? 2 : 1;
439 sampling_rate = mSamplingRate;
440 // 4000 <= nSamplingRate <= 48000
441 if (mSamplingRate < 4000) {
442 sampling_rate = 4000;
443 } else if (mSamplingRate > 48000) {
444 sampling_rate = 48000;
447 // update src and target(only aac), only once!
448 mAudioSrcChannels = mAudioTgtChannels = channels;
449 mAudioSrcFreq = mAudioTgtFreq = sampling_rate;
450 mAudioSrcFmt = mAudioTgtFmt = AV_SAMPLE_FMT_S16;
451 mAudioSrcChannelLayout = mAudioTgtChannelLayout = av_get_default_channel_layout(channels);
453 LOGV("got OMX_IndexParamAudioAac, mNumChannels: %d, mSamplingRate: %d",
454 mNumChannels, mSamplingRate);
456 return OMX_ErrorNone;
458 case OMX_IndexParamAudioWma:
460 OMX_AUDIO_PARAM_WMATYPE *wmaParams =
461 (OMX_AUDIO_PARAM_WMATYPE *)params;
463 if (wmaParams->nPortIndex != 0) {
464 return OMX_ErrorUndefined;
467 if (wmaParams->eFormat == OMX_AUDIO_WMAFormat7) {
468 mCtx->codec_id = CODEC_ID_WMAV2;
469 } else if (wmaParams->eFormat == OMX_AUDIO_WMAFormat8) {
470 mCtx->codec_id = CODEC_ID_WMAPRO;
471 } else if (wmaParams->eFormat == OMX_AUDIO_WMAFormat9) {
472 mCtx->codec_id = CODEC_ID_WMALOSSLESS;
474 LOGE("unsupported wma codec: 0x%x", wmaParams->eFormat);
475 return OMX_ErrorUndefined;
478 mNumChannels = wmaParams->nChannels;
479 mSamplingRate = wmaParams->nSamplingRate;
480 mBitRate = wmaParams->nBitRate;
483 mCtx->bit_rate = mBitRate;
485 channels = mNumChannels >= 2 ? 2 : 1;
486 sampling_rate = mSamplingRate;
487 // 4000 <= nSamplingRate <= 48000
488 if (mSamplingRate < 4000) {
489 sampling_rate = 4000;
490 } else if (mSamplingRate > 48000) {
491 sampling_rate = 48000;
494 // update src and target(only wma), only once!
495 mAudioSrcChannels = mAudioTgtChannels = channels;
496 mAudioSrcFreq = mAudioTgtFreq = sampling_rate;
497 mAudioSrcFmt = mAudioTgtFmt = AV_SAMPLE_FMT_S16;
498 mAudioSrcChannelLayout = mAudioTgtChannelLayout = av_get_default_channel_layout(channels);
500 LOGV("got OMX_IndexParamAudioWma, mNumChannels: %d, mSamplingRate: %d, mBitRate: %d",
501 mNumChannels, mSamplingRate, mBitRate);
503 return OMX_ErrorNone;
506 LOGI("internalSetParameter, index: 0x%x", index);
507 return SimpleSoftOMXComponent::internalSetParameter(index, params);
511 void SoftFFmpegAudio::onQueueFilled(OMX_U32 portIndex) {
515 int64_t decChannelLayout;
516 int32_t inputBufferUsedLength = 0;
517 BufferInfo *inInfo = NULL;
518 OMX_BUFFERHEADERTYPE *inHeader = NULL;
520 if (mSignalledError || mOutputPortSettingsChange != NONE) {
524 List<BufferInfo *> &inQueue = getPortQueue(0);
525 List<BufferInfo *> &outQueue = getPortQueue(1);
527 while ((!inQueue.empty() || mInputBufferSize > 0 ||
528 mAudioBufferSize > 0 || mFlushComplete) && !outQueue.empty()) {
529 if (!inQueue.empty() || mInputBufferSize > 0) {
530 inInfo = *inQueue.begin();
531 inHeader = inInfo->mHeader;
537 BufferInfo *outInfo = *outQueue.begin();
538 OMX_BUFFERHEADERTYPE *outHeader = outInfo->mHeader;
540 if (inHeader && inHeader->nFlags & OMX_BUFFERFLAG_EOS) {
541 inQueue.erase(inQueue.begin());
542 inInfo->mOwnedByUs = false;
543 notifyEmptyBufferDone(inHeader);
547 if (mReceivedEOS && (mFlushComplete || !(mCtx->codec->capabilities & CODEC_CAP_DELAY))) {
548 outHeader->nFilledLen = 0;
549 outHeader->nFlags = OMX_BUFFERFLAG_EOS;
551 outQueue.erase(outQueue.begin());
552 outInfo->mOwnedByUs = false;
553 notifyFillBufferDone(outHeader);
557 if (inHeader && inHeader->nFlags & OMX_BUFFERFLAG_CODECCONFIG) {
558 LOGI("got extradata, ignore: %d, size: %lu", mIgnoreExtradata, inHeader->nFilledLen);
559 hexdump(inHeader->pBuffer + inHeader->nOffset, inHeader->nFilledLen);
560 if (!mExtradataReady && !mIgnoreExtradata) {
561 int orig_extradata_size = mCtx->extradata_size;
562 mCtx->extradata_size += inHeader->nFilledLen;
563 mCtx->extradata = (uint8_t *)realloc(mCtx->extradata,
564 mCtx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
565 if (!mCtx->extradata) {
566 LOGE("ffmpeg audio decoder failed to alloc extradata memory.");
567 notify(OMX_EventError, OMX_ErrorInsufficientResources, 0, NULL);
568 mSignalledError = true;
572 memcpy(mCtx->extradata + orig_extradata_size,
573 inHeader->pBuffer + inHeader->nOffset, inHeader->nFilledLen);
574 memset(mCtx->extradata + mCtx->extradata_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
576 inInfo->mOwnedByUs = false;
577 inQueue.erase(inQueue.begin());
579 notifyEmptyBufferDone(inHeader);
584 if (mIgnoreExtradata) {
585 LOGI("got extradata, size: %lu, but ignore it", inHeader->nFilledLen);
586 inInfo->mOwnedByUs = false;
587 inQueue.erase(inQueue.begin());
589 notifyEmptyBufferDone(inHeader);
597 if (!mExtradataReady && !mIgnoreExtradata) {
598 LOGI("extradata is ready");
599 hexdump(mCtx->extradata, mCtx->extradata_size);
600 mExtradataReady = true;
603 // find decoder again as codec_id may have changed
604 mCtx->codec = avcodec_find_decoder(mCtx->codec_id);
606 LOGE("ffmpeg audio decoder failed to find codec");
607 notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL);
608 mSignalledError = true;
612 setAVCtxToDefault(mCtx, mCtx->codec);
614 LOGI("open ffmpeg decoder now");
615 err = avcodec_open2(mCtx, mCtx->codec, NULL);
617 LOGE("ffmpeg audio decoder failed to initialize. (%d)", err);
618 notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL);
619 mSignalledError = true;
625 /* update the audio clock with the pts */
626 if (inHeader && inHeader->nOffset == 0) {
627 mAnchorTimeUs = inHeader->nTimeStamp;
628 mInputBufferSize = inHeader->nFilledLen;
631 if (inHeader && mAudioBufferSize == 0 && !mFlushComplete) {
633 av_init_packet(&pkt);
634 if (!mFlushComplete) {
635 pkt.data = (uint8_t *)inHeader->pBuffer + inHeader->nOffset;
636 pkt.size = inHeader->nFilledLen;
637 pkt.pts = inHeader->nTimeStamp; // ingore it, we will compute it
641 pkt.pts = AV_NOPTS_VALUE;
644 LOGV("pkt size: %d, pts: %lld", pkt.size, pkt.pts);
647 if (!(mFrame = avcodec_alloc_frame())) {
648 LOGE("ffmpeg audio decoder failed to alloc memory.");
649 notify(OMX_EventError, OMX_ErrorInsufficientResources, 0, NULL);
650 mSignalledError = true;
654 avcodec_get_frame_defaults(mFrame);
658 inputBufferUsedLength = 0;
659 len = avcodec_decode_audio4(mCtx, mFrame, &gotFrm, &pkt);
661 LOGE("ffmpeg audio decoder failed to decode frame. consume pkt len: %d", len);
663 /* if !mAudioConfigChanged, Don't fill the out buffer */
664 if (!mAudioConfigChanged) {
665 inInfo->mOwnedByUs = false;
666 inQueue.erase(inQueue.begin());
668 notifyEmptyBufferDone(inHeader);
673 inputBufferUsedLength = inHeader->nFilledLen;
674 /* if error, we skip the frame and play silence instead */
675 mPAudioBuffer = mSilenceBuffer;
676 mAudioBufferSize = kOutputBufferSize;
677 } else if (!gotFrm) {
678 LOGI("ffmpeg audio decoder failed to get frame.");
679 /* stop sending empty packets if the decoder is finished */
680 if (!pkt.data && mCtx->codec->capabilities & CODEC_CAP_DELAY)
681 mFlushComplete = true;
685 * FIXME, check mAudioConfigChanged when the first time you call the audio4!
686 * mCtx->sample_rate and mCtx->channels may be changed by audio decoder later, why???
688 if (!mAudioConfigChanged) {
689 if (mCtx->channels != mNumChannels || mCtx->sample_rate != mSamplingRate || mCtx->sample_fmt != mSamplingFmt) {
690 LOGI("audio OMX_EventPortSettingsChanged, mCtx->channels: %d, mNumChannels: %d, mCtx->sample_rate: %d, mSamplingRate: %d",
691 mCtx->channels, mNumChannels, mCtx->sample_rate, mSamplingRate);
692 mNumChannels = mCtx->channels;
693 mSamplingRate = mCtx->sample_rate;
694 mSamplingFmt = mCtx->sample_fmt;
695 mAudioConfigChanged = true;
696 notify(OMX_EventPortSettingsChanged, 1, 0, NULL);
697 mOutputPortSettingsChange = AWAITING_DISABLED;
700 // match with the default, set mAudioConfigChanged true anyway!
701 mAudioConfigChanged = true;
705 dataSize = av_samples_get_buffer_size(NULL, mNumChannels, mFrame->nb_samples, mSamplingFmt, 1);
707 //LOGV("audio decoder get buffer size: %d", dataSize);
709 decChannelLayout = av_get_default_channel_layout(mNumChannels);
710 if (mSamplingFmt != mAudioSrcFmt ||
711 decChannelLayout != mAudioSrcChannelLayout ||
712 mSamplingRate != mAudioSrcFreq ) {
715 mSwrCtx = swr_alloc_set_opts(NULL,
716 mAudioTgtChannelLayout, mAudioTgtFmt, mAudioTgtFreq,
717 decChannelLayout, mSamplingFmt, mSamplingRate,
719 if (!mSwrCtx || swr_init(mSwrCtx) < 0) {
720 LOGE("Cannot create sample rate converter for conversion of %d Hz %s %d channels to %d Hz %s %d channels!",
722 av_get_sample_fmt_name(mSamplingFmt),
725 av_get_sample_fmt_name(mAudioTgtFmt),
727 notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL);
728 mSignalledError = true;
732 LOGI("Create sample rate converter for conversion of %d Hz %s %d channels to %d Hz %s %d channels!",
734 av_get_sample_fmt_name(mSamplingFmt),
737 av_get_sample_fmt_name(mAudioTgtFmt),
740 mAudioSrcChannelLayout = decChannelLayout;
741 mAudioSrcChannels = mNumChannels;
742 mAudioSrcFreq = mSamplingRate;
743 mAudioSrcFmt = mSamplingFmt;
747 const uint8_t *in[] = { mFrame->data[0] };
748 uint8_t *out[] = {mAudioBuf2};
749 int len2 = swr_convert(mSwrCtx, out, sizeof(mAudioBuf2) / mAudioTgtChannels / av_get_bytes_per_sample(mAudioTgtFmt),
750 in, mFrame->nb_samples);
752 LOGE("audio_resample() failed");
755 if (len2 == sizeof(mAudioBuf2) / mAudioTgtChannels / av_get_bytes_per_sample(mAudioTgtFmt)) {
756 LOGE("warning: audio buffer is probably too small");
759 mPAudioBuffer = mAudioBuf2;
760 mAudioBufferSize = len2 * mAudioTgtChannels * av_get_bytes_per_sample(mAudioTgtFmt);
762 mPAudioBuffer = mFrame->data[0];
763 mAudioBufferSize = dataSize;
766 inputBufferUsedLength = len;
768 LOGV("ffmpeg audio decoder get frame. consume pkt len: %d, nb_samples(before resample): %d, mAudioBufferSize: %d",
769 len, mFrame->nb_samples, mAudioBufferSize);
774 size_t copyToOutputBufferLen = mAudioBufferSize;
775 if (mAudioBufferSize > kOutputBufferSize)
776 copyToOutputBufferLen = kOutputBufferSize;
778 outHeader->nOffset = 0;
779 outHeader->nFilledLen = copyToOutputBufferLen;
780 outHeader->nTimeStamp = mAnchorTimeUs + (mNumFramesOutput * 1000000ll) / mSamplingRate;
781 memcpy(outHeader->pBuffer, mPAudioBuffer, copyToOutputBufferLen);
782 outHeader->nFlags = 0;
785 LOGV("ffmpeg audio decoder, fill out buffer, pts: %lld, mNumFramesOutput: %lld", outHeader->nTimeStamp, mNumFramesOutput);
788 mPAudioBuffer += copyToOutputBufferLen;
789 mAudioBufferSize -= copyToOutputBufferLen;
790 mNumFramesOutput += copyToOutputBufferLen / (av_get_bytes_per_sample(mCtx->sample_fmt) * mNumChannels);
793 // reset mNumFramesOutput
794 if (mAudioBufferSize == 0)
795 mNumFramesOutput = 0;
799 CHECK_GE(inHeader->nFilledLen, inputBufferUsedLength);
800 inHeader->nOffset += inputBufferUsedLength;
801 inHeader->nFilledLen -= inputBufferUsedLength;
802 mInputBufferSize -= inputBufferUsedLength;
803 if (inHeader->nFilledLen == 0) {
804 inInfo->mOwnedByUs = false;
805 inQueue.erase(inQueue.begin());
807 notifyEmptyBufferDone(inHeader);
810 mInputBufferSize = 0;
814 outInfo->mOwnedByUs = false;
815 outQueue.erase(outQueue.begin());
817 notifyFillBufferDone(outHeader);
822 void SoftFFmpegAudio::onPortFlushCompleted(OMX_U32 portIndex) {
823 if (portIndex == 0 && mCtx) {
824 // Make sure that the next buffer output does not still
825 // depend on fragments from the last one decoded.
826 avcodec_flush_buffers(mCtx);
831 void SoftFFmpegAudio::onPortEnableCompleted(OMX_U32 portIndex, bool enabled) {
832 if (portIndex != 1) {
836 switch (mOutputPortSettingsChange) {
840 case AWAITING_DISABLED:
843 mOutputPortSettingsChange = AWAITING_ENABLED;
849 CHECK_EQ((int)mOutputPortSettingsChange, (int)AWAITING_ENABLED);
851 mOutputPortSettingsChange = NONE;
857 } // namespace android
859 android::SoftOMXComponent *createSoftOMXComponent(
860 const char *name, const OMX_CALLBACKTYPE *callbacks,
861 OMX_PTR appData, OMX_COMPONENTTYPE **component) {
862 return new android::SoftFFmpegAudio(name, callbacks, appData, component);