OSDN Git Service

add support for MEDIA_MIMETYPE_AUDIO_MPEG_LAYER_I and MEDIA_MIMETYPE_AUDIO_MPEG_LAYER_II
[android-x86/external-stagefright-plugins.git] / libstagefright / codecs / ffmpegdec / adec / SoftFFmpegAudio.cpp
1 /*
2  * Copyright 2012 Michael Chen <omxcodec@gmail.com>
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "SoftFFmpegAudio"
19 #include <utils/Log.h>
20
21 #include "SoftFFmpegAudio.h"
22
23 #include <media/stagefright/foundation/ADebug.h>
24 #include <media/stagefright/foundation/hexdump.h>
25 #include <media/stagefright/MediaDefs.h>
26
27 #include "ffmpeg_utils/ffmpeg_utils.h"
28
29 #undef realloc
30 #include <stdlib.h>
31
32 #define DEBUG_PKT 1
33 #define DEBUG_FRM 0
34
35 namespace android {
36
37 template<class T>
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;
44 }
45
46 SoftFFmpegAudio::SoftFFmpegAudio(
47         const char *name,
48         const OMX_CALLBACKTYPE *callbacks,
49         OMX_PTR appData,
50         OMX_COMPONENTTYPE **component)
51     : SimpleSoftOMXComponent(name, callbacks, appData, component),
52       mMode(MODE_MPEG),
53       mCtx(NULL),
54       mSwrCtx(NULL),
55       mCodecOpened(false),
56       mExtradataReady(false),
57       mIgnoreExtradata(false),
58       mFlushComplete(false),
59       mSignalledError(false),
60       mReceivedEOS(false),
61       mFrame(NULL),
62       mAnchorTimeUs(0),
63       mNumFramesOutput(0),
64       mInputBufferSize(0),
65       mAudioBufferSize(0),
66       mNumChannels(2),
67       mSamplingRate(44100),
68       mAudioConfigChanged(false),
69       mOutputPortSettingsChange(NONE) {
70     if (!strcmp(name, "OMX.ffmpeg.mp3.decoder")) {
71         mMode = MODE_MPEG;
72         mIgnoreExtradata = true;
73     } else if (!strcmp(name, "OMX.ffmpeg.mp1.decoder")) {
74         mMode = MODE_MPEGL1;
75     } else if (!strcmp(name, "OMX.ffmpeg.mp2.decoder")) {
76         mMode = MODE_MPEGL2;
77     } else if (!strcmp(name, "OMX.ffmpeg.aac.decoder")) {
78         mMode = MODE_AAC;
79     } else {
80         CHECK(!strcmp(name, "OMX.ffmpeg.ac3.decoder"));
81         mMode = MODE_AC3;
82     }
83
84     LOGV("SoftFFmpegAudio component: %s", name);
85
86     initPorts();
87     CHECK_EQ(initDecoder(), (status_t)OK);
88 }
89
90 SoftFFmpegAudio::~SoftFFmpegAudio() {
91     av_freep(&mFrame);
92     LOGV("~SoftFFmpegAudio");
93     deInitDecoder();
94     deInitFFmpeg();
95 }
96
97 void SoftFFmpegAudio::initPorts() {
98     OMX_PARAM_PORTDEFINITIONTYPE def;
99     InitOMXParams(&def);
100
101     def.nPortIndex = 0;
102     def.eDir = OMX_DirInput;
103     def.nBufferCountMin = kNumBuffers;
104     def.nBufferCountActual = def.nBufferCountMin;
105     def.nBufferSize = 8192;
106     def.bEnabled = OMX_TRUE;
107     def.bPopulated = OMX_FALSE;
108     def.eDomain = OMX_PortDomainAudio;
109     def.bBuffersContiguous = OMX_FALSE;
110     def.nBufferAlignment = 1;
111
112     switch (mMode) {
113     case MODE_MPEG:
114         def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_MPEG);
115         def.format.audio.eEncoding = OMX_AUDIO_CodingMP3;
116         break;
117     case MODE_MPEGL1:
118         def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_MPEG_LAYER_I);
119         def.format.audio.eEncoding = OMX_AUDIO_CodingMP3;
120         break;
121     case MODE_MPEGL2:
122         def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_MPEG_LAYER_II);
123         def.format.audio.eEncoding = OMX_AUDIO_CodingMP3;
124         break;
125     case MODE_AAC:
126         def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_AAC);
127         def.format.audio.eEncoding = OMX_AUDIO_CodingAAC;
128         break;
129     case MODE_AC3:
130         def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_AC3);
131         // TODO
132         //def.format.audio.eEncoding = OMX_AUDIO_CodingAC3;
133         break;
134     default:
135         CHECK(!"Should not be here. Unsupported mime type and compression format");
136         break;
137     }
138
139     def.format.audio.pNativeRender = NULL;
140     def.format.audio.bFlagErrorConcealment = OMX_FALSE;
141
142     addPort(def);
143
144     def.nPortIndex = 1;
145     def.eDir = OMX_DirOutput;
146     def.nBufferCountMin = kNumBuffers;
147     def.nBufferCountActual = def.nBufferCountMin;
148     def.nBufferSize = kOutputBufferSize;
149     def.bEnabled = OMX_TRUE;
150     def.bPopulated = OMX_FALSE;
151     def.eDomain = OMX_PortDomainAudio;
152     def.bBuffersContiguous = OMX_FALSE;
153     def.nBufferAlignment = 2;
154
155     def.format.audio.cMIMEType = const_cast<char *>("audio/raw");
156     def.format.audio.pNativeRender = NULL;
157     def.format.audio.bFlagErrorConcealment = OMX_FALSE;
158     def.format.audio.eEncoding = OMX_AUDIO_CodingPCM;
159
160     addPort(def);
161 }
162
163 static int lockmgr(void **mtx, enum AVLockOp op) {
164    switch(op) {
165       case AV_LOCK_CREATE:
166           *mtx = (void *)SDL_CreateMutex();
167           if(!*mtx)
168               return 1;
169           return 0;
170       case AV_LOCK_OBTAIN:
171           return !!SDL_LockMutex((SDL_mutex *)*mtx);
172       case AV_LOCK_RELEASE:
173           return !!SDL_UnlockMutex((SDL_mutex *)*mtx);
174       case AV_LOCK_DESTROY:
175           SDL_DestroyMutex((SDL_mutex *)*mtx);
176           return 0;
177    }
178    return 1;
179 }
180
181 status_t SoftFFmpegAudio::initFFmpeg() {
182     //nam_av_log_set_flags(AV_LOG_SKIP_REPEATED);
183     av_log_set_level(AV_LOG_DEBUG);
184     av_log_set_callback(nam_av_log_callback);
185
186     /* register all codecs, demux and protocols */
187     avcodec_register_all();
188 #if CONFIG_AVDEVICE
189     avdevice_register_all();
190 #endif
191     av_register_all();
192     avformat_network_init();
193
194     init_opts();
195
196     if (av_lockmgr_register(lockmgr)) {
197         LOGE("could not initialize lock manager!");
198         return NO_INIT;
199     }
200
201     return OK;
202 }
203
204 void SoftFFmpegAudio::deInitFFmpeg() {
205     av_lockmgr_register(NULL);
206     uninit_opts();
207     avformat_network_deinit();
208 }
209
210 void SoftFFmpegAudio::setAVCtxToDefault(AVCodecContext *avctx, const AVCodec *codec) {
211     int fast = 0;
212
213     avctx->workaround_bugs   = 1;
214     avctx->lowres            = 0;
215     if(avctx->lowres > codec->max_lowres){
216         LOGW("The maximum value for lowres supported by the decoder is %d",
217                 codec->max_lowres);
218         avctx->lowres= codec->max_lowres;
219     }
220     avctx->idct_algo         = 0;
221     avctx->skip_frame        = AVDISCARD_DEFAULT;
222     avctx->skip_idct         = AVDISCARD_DEFAULT;
223     avctx->skip_loop_filter  = AVDISCARD_DEFAULT;
224     avctx->error_concealment = 3;
225
226     if(avctx->lowres) avctx->flags |= CODEC_FLAG_EMU_EDGE;
227     if (fast)   avctx->flags2 |= CODEC_FLAG2_FAST;
228     if(codec->capabilities & CODEC_CAP_DR1)
229         avctx->flags |= CODEC_FLAG_EMU_EDGE;
230 }
231
232 status_t SoftFFmpegAudio::initDecoder() {
233     status_t status;
234
235     status = initFFmpeg();
236     if (status != OK)
237         return status;
238
239     mCtx = avcodec_alloc_context3(NULL);
240     if (!mCtx)
241     {
242         LOGE("avcodec_alloc_context failed.");
243         return OMX_ErrorInsufficientResources;
244     }
245
246     mCtx->codec_type = AVMEDIA_TYPE_AUDIO;
247     switch (mMode) {
248     case MODE_MPEG:
249         mCtx->codec_id = CODEC_ID_MP3;
250         break;
251     case MODE_MPEGL1:
252         mCtx->codec_id = CODEC_ID_MP1;
253         break;
254     case MODE_MPEGL2:
255         mCtx->codec_id = CODEC_ID_MP2;
256         break;
257     case MODE_AAC:
258         mCtx->codec_id = CODEC_ID_AAC;
259         break;
260     case MODE_AC3:
261         mCtx->codec_id = CODEC_ID_AC3;
262         break;
263     default:
264         CHECK(!"Should not be here. Unsupported codec");
265         break;
266     }
267
268     mCtx->codec = avcodec_find_decoder(mCtx->codec_id);
269     if (!mCtx->codec)
270     {
271         LOGE("find codec failed");
272         return OMX_ErrorNotImplemented;
273     }
274
275     setAVCtxToDefault(mCtx, mCtx->codec);
276
277 #if 0
278     // FIXME, defer to open? ref: OMXCodec.cpp:setAudioOutputFormat
279     err = avcodec_open2(mCtx, mCtx->codec, NULL);
280     if (err < 0) {
281         LOGE("ffmpeg audio decoder failed to  initialize. (%d)", err);
282         return OMX_ErrorUndefined;
283     }
284 #endif
285
286     mCtx->sample_fmt = AV_SAMPLE_FMT_S16;
287
288     mAudioSrcFmt = mAudioTgtFmt = AV_SAMPLE_FMT_S16;
289     mAudioSrcFreq = mAudioTgtFreq = mSamplingRate;
290     mAudioSrcChannels = mAudioTgtChannels = mNumChannels;
291     mAudioSrcChannelLayout = mAudioTgtChannelLayout = 
292         av_get_default_channel_layout(mNumChannels);
293
294     memset(mSilenceBuffer, 0, kOutputBufferSize);
295
296     return OMX_ErrorNone;
297 }
298
299 void SoftFFmpegAudio::deInitDecoder() {
300     if (mCtx) {
301         avcodec_flush_buffers(mCtx);
302         if (!mCtx->extradata) {
303             av_free(mCtx->extradata);
304             mCtx->extradata = NULL;
305             mCtx->extradata_size = 0;
306         }
307         avcodec_close(mCtx);
308         av_free(mCtx);
309         mCtx = NULL;
310     }
311
312     if (mSwrCtx) {
313         swr_free(&mSwrCtx);
314         mSwrCtx = NULL;
315     }
316
317 }
318
319 OMX_ERRORTYPE SoftFFmpegAudio::internalGetParameter(
320         OMX_INDEXTYPE index, OMX_PTR params) {
321     switch (index) {
322         case OMX_IndexParamAudioAac:
323         {
324             OMX_AUDIO_PARAM_AACPROFILETYPE *aacParams =
325                 (OMX_AUDIO_PARAM_AACPROFILETYPE *)params;
326
327             if (aacParams->nPortIndex != 0) {
328                 return OMX_ErrorUndefined;
329             }
330
331             aacParams->nBitRate = 0;
332             aacParams->nAudioBandWidth = 0;
333             aacParams->nAACtools = 0;
334             aacParams->nAACERtools = 0;
335             aacParams->eAACProfile = OMX_AUDIO_AACObjectMain;
336             aacParams->eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4FF;
337             aacParams->eChannelMode = OMX_AUDIO_ChannelModeStereo;
338
339             aacParams->nChannels = mNumChannels;
340             aacParams->nSampleRate = mSamplingRate;
341
342             return OMX_ErrorNone;
343         }
344         case OMX_IndexParamAudioPcm:
345         {
346             OMX_AUDIO_PARAM_PCMMODETYPE *pcmParams =
347                 (OMX_AUDIO_PARAM_PCMMODETYPE *)params;
348
349             if (pcmParams->nPortIndex > 1) {
350                 return OMX_ErrorUndefined;
351             }
352
353             pcmParams->eNumData = OMX_NumericalDataSigned;
354             pcmParams->eEndian = OMX_EndianBig;
355             pcmParams->bInterleaved = OMX_TRUE;
356             pcmParams->nBitPerSample = 16;
357             pcmParams->ePCMMode = OMX_AUDIO_PCMModeLinear;
358             pcmParams->eChannelMapping[0] = OMX_AUDIO_ChannelLF;
359             pcmParams->eChannelMapping[1] = OMX_AUDIO_ChannelRF;
360
361             pcmParams->nChannels = mNumChannels;
362             pcmParams->nSamplingRate = mSamplingRate;
363
364             return OMX_ErrorNone;
365         }
366
367         default:
368             return SimpleSoftOMXComponent::internalGetParameter(index, params);
369     }
370 }
371
372 OMX_ERRORTYPE SoftFFmpegAudio::internalSetParameter(
373         OMX_INDEXTYPE index, const OMX_PTR params) {
374     switch (index) {
375         case OMX_IndexParamStandardComponentRole:
376         {
377             const OMX_PARAM_COMPONENTROLETYPE *roleParams =
378                 (const OMX_PARAM_COMPONENTROLETYPE *)params;
379
380             bool supported = true;
381             switch (mMode) {
382             case MODE_MPEG:
383                 if (strncmp((const char *)roleParams->cRole,
384                         "audio_decoder.mp3", OMX_MAX_STRINGNAME_SIZE - 1))
385                     supported =  false;
386                 break;
387             case MODE_MPEGL1:
388                 if (strncmp((const char *)roleParams->cRole,
389                         "audio_decoder.mp1", OMX_MAX_STRINGNAME_SIZE - 1))
390                     supported =  false;
391                 break;
392             case MODE_MPEGL2:
393                 if (strncmp((const char *)roleParams->cRole,
394                         "audio_decoder.mp2", OMX_MAX_STRINGNAME_SIZE - 1))
395                     supported =  false;
396                 break;
397             case MODE_AAC:
398                 if (strncmp((const char *)roleParams->cRole,
399                         "audio_decoder.aac", OMX_MAX_STRINGNAME_SIZE - 1))
400                     supported =  false;
401                 break;
402             case MODE_AC3:
403                 if (strncmp((const char *)roleParams->cRole,
404                         "audio_decoder.ac3", OMX_MAX_STRINGNAME_SIZE - 1))
405                     supported =  false;
406                 break;
407             default:
408                 CHECK(!"Should not be here. Unsupported role.");
409                 break;
410             }
411             if (!supported) {
412                 LOGE("unsupported role: %s", (const char *)roleParams->cRole);
413                 return OMX_ErrorUndefined;
414             }
415
416             return OMX_ErrorNone;
417         }
418         case OMX_IndexParamAudioAac:
419         {
420             const OMX_AUDIO_PARAM_AACPROFILETYPE *aacParams =
421                 (const OMX_AUDIO_PARAM_AACPROFILETYPE *)params;
422
423             if (aacParams->nPortIndex != 0) {
424                 return OMX_ErrorUndefined;
425             }
426
427             mNumChannels = aacParams->nChannels;
428             mSamplingRate = aacParams->nSampleRate;
429
430             LOGV("got OMX_IndexParamAudioAac, mNumChannels: %d, mSamplingRate: %d",
431                 mNumChannels, mSamplingRate);
432
433             return OMX_ErrorNone;
434         }
435         default:
436             LOGI("internalSetParameter, index: 0x%x", index);
437             return SimpleSoftOMXComponent::internalSetParameter(index, params);
438     }
439 }
440
441 void SoftFFmpegAudio::onQueueFilled(OMX_U32 portIndex) {
442     int len = 0;
443     int err = 0;
444     size_t resampledDataSize = 0;
445     int64_t decChannelLayout;
446     int32_t inputBufferUsedLength = 0;
447     BufferInfo *inInfo = NULL;
448     OMX_BUFFERHEADERTYPE *inHeader = NULL;
449
450     if (mSignalledError || mOutputPortSettingsChange != NONE) {
451         return;
452     }
453
454     List<BufferInfo *> &inQueue = getPortQueue(0);
455     List<BufferInfo *> &outQueue = getPortQueue(1);
456
457     while ((!inQueue.empty() || mInputBufferSize > 0 ||
458             mAudioBufferSize > 0 || mFlushComplete) && !outQueue.empty()) {
459         if (!inQueue.empty() || mInputBufferSize > 0) {
460             inInfo = *inQueue.begin();
461             inHeader = inInfo->mHeader;
462         } else {
463             inInfo = NULL;
464             inHeader = NULL;
465         }
466
467         BufferInfo *outInfo = *outQueue.begin();
468         OMX_BUFFERHEADERTYPE *outHeader = outInfo->mHeader;
469 #if 0
470         if (!mAudioConfigChanged && mMode == MODE_AAC &&
471                 (mCtx->channels != mNumChannels || mCtx->sample_rate != mSamplingRate)) {
472             LOGI("audio OMX_EventPortSettingsChanged, mCtx->channels: %d, mNumChannels: %d, mCtx->sample_rate: %d, mSamplingRate: %d",
473                     mCtx->channels, mNumChannels, mCtx->sample_rate, mSamplingRate);
474             mCtx->channels = mNumChannels;
475             mCtx->sample_rate = mSamplingRate;
476             // AAC only?
477             mAudioConfigChanged = true;
478             notify(OMX_EventPortSettingsChanged, 1, 0, NULL);
479             mOutputPortSettingsChange = AWAITING_DISABLED;
480             return;
481         }
482 #endif
483
484         if (inHeader && inHeader->nFlags & OMX_BUFFERFLAG_EOS) {
485             inQueue.erase(inQueue.begin());
486             inInfo->mOwnedByUs = false;
487             notifyEmptyBufferDone(inHeader);
488             mReceivedEOS = true;
489         }
490
491         if (mReceivedEOS && (mFlushComplete || !(mCtx->codec->capabilities & CODEC_CAP_DELAY))) {
492             outHeader->nFilledLen = 0;
493             outHeader->nFlags = OMX_BUFFERFLAG_EOS;
494
495             outQueue.erase(outQueue.begin());
496             outInfo->mOwnedByUs = false;
497             notifyFillBufferDone(outHeader);
498             return;
499         }
500
501         if (inHeader && inHeader->nFlags & OMX_BUFFERFLAG_CODECCONFIG) {
502             LOGI("got extradata, ignore: %d, size: %lu", mIgnoreExtradata, inHeader->nFilledLen);
503             hexdump(inHeader->pBuffer + inHeader->nOffset, inHeader->nFilledLen);
504             if (!mExtradataReady && !mIgnoreExtradata) {
505                 int orig_extradata_size = mCtx->extradata_size;
506                 mCtx->extradata_size += inHeader->nFilledLen;
507                 mCtx->extradata = (uint8_t *)realloc(mCtx->extradata,
508                         mCtx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
509                 if (!mCtx->extradata) {
510                     LOGE("ffmpeg audio decoder failed to alloc extradata memory.");
511                     notify(OMX_EventError, OMX_ErrorInsufficientResources, 0, NULL);
512                     mSignalledError = true;
513                     return;
514                 }
515
516                 memcpy(mCtx->extradata + orig_extradata_size,
517                         inHeader->pBuffer + inHeader->nOffset, inHeader->nFilledLen);
518                 memset(mCtx->extradata + mCtx->extradata_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
519
520                 inInfo->mOwnedByUs = false;
521                 inQueue.erase(inQueue.begin());
522                 inInfo = NULL;
523                 notifyEmptyBufferDone(inHeader);
524                 inHeader = NULL;
525
526                 continue;
527             }
528             if (mIgnoreExtradata) {
529                 LOGI("got extradata, size: %lu, but ignore it", inHeader->nFilledLen);
530                 inInfo->mOwnedByUs = false;
531                 inQueue.erase(inQueue.begin());
532                 inInfo = NULL;
533                 notifyEmptyBufferDone(inHeader);
534                 inHeader = NULL;
535
536                 continue;
537             }
538         }
539
540         if (!mCodecOpened) {
541             if (!mExtradataReady && !mIgnoreExtradata) {
542                 LOGI("extradata is ready");
543                 hexdump(mCtx->extradata, mCtx->extradata_size);
544                 mExtradataReady = true;
545             }
546             LOGI("open ffmpeg decoder now");
547
548             err = avcodec_open2(mCtx, mCtx->codec, NULL);
549             if (err < 0) {
550                 LOGE("ffmpeg audio decoder failed to initialize. (%d)", err);
551                 notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL);
552                 mSignalledError = true;
553                 return;
554             }
555             mCodecOpened = true;
556         }
557
558         /* update the audio clock with the pts */
559         if (inHeader && inHeader->nOffset == 0) {
560             mAnchorTimeUs = inHeader->nTimeStamp;
561             mNumFramesOutput = 0;
562             mInputBufferSize = inHeader->nFilledLen;
563         }
564
565         if (inHeader && mAudioBufferSize == 0 && !mFlushComplete) {
566             AVPacket pkt;
567             av_init_packet(&pkt);
568             if (!mFlushComplete) {
569                 pkt.data = (uint8_t *)inHeader->pBuffer + inHeader->nOffset;
570                 pkt.size = inHeader->nFilledLen;
571                 pkt.pts = inHeader->nTimeStamp; // ingore it, we will compute it
572             } else {
573                 pkt.data = NULL;
574                 pkt.size = 0;
575                 pkt.pts = AV_NOPTS_VALUE;
576             }
577 #if DEBUG_PKT
578             LOGV("pkt size: %d, pts: %lld", pkt.size, pkt.pts);
579 #endif
580             if (!mFrame) {
581                 if (!(mFrame = avcodec_alloc_frame())) {
582                     LOGE("ffmpeg audio decoder failed to alloc memory.");
583                     notify(OMX_EventError, OMX_ErrorInsufficientResources, 0, NULL);
584                     mSignalledError = true;
585                     return;
586                 }
587             } else {
588                 avcodec_get_frame_defaults(mFrame);
589             }
590
591             int gotFrm = false;
592             inputBufferUsedLength = 0;
593             len = avcodec_decode_audio4(mCtx, mFrame, &gotFrm, &pkt);
594             if (len < 0) {
595                 LOGE("ffmpeg audio decoder failed to decode frame. (0x%x)", len);
596                 inputBufferUsedLength = inHeader->nFilledLen;
597                 /* if error, we skip the frame and play silence instead */
598                 mPAudioBuffer = mSilenceBuffer;
599                 mAudioBufferSize = kOutputBufferSize;
600             } else if (!gotFrm) {
601                 LOGI("ffmpeg audio decoder failed to get frame.");
602                 /* stop sending empty packets if the decoder is finished */
603                 if (!pkt.data && mCtx->codec->capabilities & CODEC_CAP_DELAY)
604                     mFlushComplete = true;
605                 continue;
606             } else {
607                 /**
608                  * FIXME, check mAudioConfigChanged when the first time you call the audio4!
609                  * mCtx->sample_rate and mCtx->channels may be changed by audio decoder later, why???
610                  */
611                 if (!mAudioConfigChanged) {
612                     if (mCtx->channels != mNumChannels || mCtx->sample_rate != mSamplingRate) {
613                         LOGI("audio OMX_EventPortSettingsChanged, mCtx->channels: %d, mNumChannels: %d, mCtx->sample_rate: %d, mSamplingRate: %d",
614                                 mCtx->channels, mNumChannels, mCtx->sample_rate, mSamplingRate);
615                         mNumChannels = mCtx->channels;
616                         mSamplingRate = mCtx->sample_rate;
617                         mAudioConfigChanged = true;
618                         notify(OMX_EventPortSettingsChanged, 1, 0, NULL);
619                         mOutputPortSettingsChange = AWAITING_DISABLED;
620                         return;
621                     } else {
622                         // match with the default, set mAudioConfigChanged true anyway!
623                         mAudioConfigChanged = true;
624                     }
625                 }
626
627                 inputBufferUsedLength = len;
628                 mPAudioBuffer = mFrame->data[0];
629                 mAudioBufferSize = av_samples_get_buffer_size(NULL, mCtx->channels,
630                                                        mFrame->nb_samples,
631                                                        mCtx->sample_fmt, 1);
632                 LOGV("ffmpeg audio decoder get frame. (%d), mAudioBufferSize: %d", len, mAudioBufferSize);
633             }
634         }
635
636         size_t copyToOutputBufferLen = mAudioBufferSize;
637         if (mAudioBufferSize > kOutputBufferSize)
638             copyToOutputBufferLen = kOutputBufferSize;
639
640         outHeader->nOffset = 0;
641         outHeader->nFilledLen = copyToOutputBufferLen;
642         outHeader->nTimeStamp = mAnchorTimeUs
643                 + (mNumFramesOutput * 1000000ll) / mSamplingRate;
644         memcpy(outHeader->pBuffer, mPAudioBuffer, copyToOutputBufferLen);
645         outHeader->nFlags = 0;
646
647         mPAudioBuffer += copyToOutputBufferLen;
648         mAudioBufferSize -= copyToOutputBufferLen;
649         mNumFramesOutput += copyToOutputBufferLen / av_get_bytes_per_sample(mCtx->sample_fmt) / mNumChannels;
650
651         if (inHeader) {
652             CHECK_GE(inHeader->nFilledLen, inputBufferUsedLength);
653             inHeader->nOffset += inputBufferUsedLength;
654             inHeader->nFilledLen -= inputBufferUsedLength;
655             mInputBufferSize -= inputBufferUsedLength;
656             if (inHeader->nFilledLen == 0) {
657                 inInfo->mOwnedByUs = false;
658                 inQueue.erase(inQueue.begin());
659                 inInfo = NULL;
660                 notifyEmptyBufferDone(inHeader);
661                 inHeader = NULL;
662
663                 mInputBufferSize = 0;
664             }
665         }
666
667         outInfo->mOwnedByUs = false;
668         outQueue.erase(outQueue.begin());
669         outInfo = NULL;
670         notifyFillBufferDone(outHeader);
671         outHeader = NULL;
672     }
673 }
674
675 void SoftFFmpegAudio::onPortFlushCompleted(OMX_U32 portIndex) {
676     if (portIndex == 0 && mCtx) {
677         // Make sure that the next buffer output does not still
678         // depend on fragments from the last one decoded.
679         avcodec_flush_buffers(mCtx);
680     }
681
682 }
683
684 void SoftFFmpegAudio::onPortEnableCompleted(OMX_U32 portIndex, bool enabled) {
685     if (portIndex != 1) {
686         return;
687     }
688
689     switch (mOutputPortSettingsChange) {
690         case NONE:
691             break;
692
693         case AWAITING_DISABLED:
694         {
695             CHECK(!enabled);
696             mOutputPortSettingsChange = AWAITING_ENABLED;
697             break;
698         }
699
700         default:
701         {
702             CHECK_EQ((int)mOutputPortSettingsChange, (int)AWAITING_ENABLED);
703             CHECK(enabled);
704             mOutputPortSettingsChange = NONE;
705             break;
706         }
707     }
708 }
709
710 }  // namespace android
711
712 android::SoftOMXComponent *createSoftOMXComponent(
713         const char *name, const OMX_CALLBACKTYPE *callbacks,
714         OMX_PTR appData, OMX_COMPONENTTYPE **component) {
715     return new android::SoftFFmpegAudio(name, callbacks, appData, component);
716 }