OSDN Git Service

fd49837bc9982af83713a76a11754fc1c8eebf9f
[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 "utils/ffmpeg_utils.h"
28
29 //#undef realloc
30 //#include <stdlib.h>
31
32 #define DEBUG_PKT 0
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       mFFmpegInited(false),
54       mCtx(NULL),
55       mSwrCtx(NULL),
56       mCodecOpened(false),
57       mExtradataReady(false),
58       mIgnoreExtradata(false),
59       mFlushComplete(false),
60       mSignalledError(false),
61       mReceivedEOS(false),
62       mFrame(NULL),
63       mAnchorTimeUs(0),
64       mNumFramesOutput(0),
65       mInputBufferSize(0),
66       mAudioBufferSize(0),
67       mNumChannels(2),
68       mSamplingRate(44100),
69       mBitRate(0),
70       mBlockAlign(0),
71       mSamplingFmt(AV_SAMPLE_FMT_S16),
72       mAudioConfigChanged(false),
73       mOutputPortSettingsChange(NONE) {
74     if (!strcmp(name, "OMX.ffmpeg.mp3.decoder")) {
75         mMode = MODE_MPEG;
76         mIgnoreExtradata = true;
77     } else if (!strcmp(name, "OMX.ffmpeg.mp1.decoder")) {
78         mMode = MODE_MPEGL1;
79     } else if (!strcmp(name, "OMX.ffmpeg.mp2.decoder")) {
80         mMode = MODE_MPEGL2;
81     } else if (!strcmp(name, "OMX.ffmpeg.aac.decoder")) {
82         mMode = MODE_AAC;
83     } else if (!strcmp(name, "OMX.ffmpeg.wma.decoder")) {
84         mMode = MODE_WMA;
85     } else if (!strcmp(name, "OMX.ffmpeg.ra.decoder")) {
86         mMode = MODE_RA;
87     } else if (!strcmp(name, "OMX.ffmpeg.ac3.decoder")) {
88         mMode = MODE_AC3;
89     } else if (!strcmp(name, "OMX.ffmpeg.ape.decoder")) {
90         mMode = MODE_APE;
91     } else if (!strcmp(name, "OMX.ffmpeg.dts.decoder")) {
92         mMode = MODE_DTS;
93     } else if (!strcmp(name, "OMX.ffmpeg.flac.decoder")) {
94         mMode = MODE_FLAC;
95     } else if (!strcmp(name, "OMX.ffmpeg.vorbis.decoder")) {
96         mMode = MODE_VORBIS;
97     } else {
98         TRESPASS();
99     }
100
101     ALOGD("SoftFFmpegAudio component: %s mMode: %d", name, mMode);
102
103     initPorts();
104     CHECK_EQ(initDecoder(), (status_t)OK);
105 }
106
107 SoftFFmpegAudio::~SoftFFmpegAudio() {
108     ALOGV("~SoftFFmpegAudio");
109     av_freep(&mFrame);
110     deInitDecoder();
111     if (mFFmpegInited) {
112         deInitFFmpeg();
113     }
114 }
115
116 void SoftFFmpegAudio::initPorts() {
117     OMX_PARAM_PORTDEFINITIONTYPE def;
118     InitOMXParams(&def);
119
120     def.nPortIndex = 0;
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!
128     } else {
129         def.nBufferSize = 20480; // 8192 is too small
130     }
131     def.bEnabled = OMX_TRUE;
132     def.bPopulated = OMX_FALSE;
133     def.eDomain = OMX_PortDomainAudio;
134     def.bBuffersContiguous = OMX_FALSE;
135     def.nBufferAlignment = 1;
136
137     switch (mMode) {
138     case MODE_MPEG:
139         def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_MPEG);
140         def.format.audio.eEncoding = OMX_AUDIO_CodingMP3;
141         break;
142     case MODE_MPEGL1:
143         def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_MPEG_LAYER_I);
144         def.format.audio.eEncoding = OMX_AUDIO_CodingMP3;
145         break;
146     case MODE_MPEGL2:
147         def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_MPEG_LAYER_II);
148         def.format.audio.eEncoding = OMX_AUDIO_CodingMP3;
149         break;
150     case MODE_AAC:
151         def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_AAC);
152         def.format.audio.eEncoding = OMX_AUDIO_CodingAAC;
153         break;
154     case MODE_AC3:
155         def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_AC3);
156         // TODO
157         //def.format.audio.eEncoding = OMX_AUDIO_CodingAC3;
158         def.format.audio.eEncoding = OMX_AUDIO_CodingAutoDetect; // right?? orz
159         break;
160     case MODE_WMA:
161         def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_WMA);
162         def.format.audio.eEncoding = OMX_AUDIO_CodingWMA;
163         break;
164     case MODE_RA:
165         def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_RA);
166         def.format.audio.eEncoding = OMX_AUDIO_CodingRA;
167         break;
168     case MODE_APE:
169         def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_APE);
170         def.format.audio.eEncoding = OMX_AUDIO_CodingAPE;
171         break;
172     case MODE_DTS:
173         def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_DTS);
174         def.format.audio.eEncoding = OMX_AUDIO_CodingDTS;
175         break;
176     case MODE_FLAC:
177         def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_FLAC);
178         def.format.audio.eEncoding = OMX_AUDIO_CodingFLAC;
179         break;
180     case MODE_VORBIS:
181         def.format.audio.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_AUDIO_VORBIS);
182         def.format.audio.eEncoding = OMX_AUDIO_CodingVORBIS;
183         break;
184     default:
185         CHECK(!"Should not be here. Unsupported mime type and compression format");
186         break;
187     }
188
189     def.format.audio.pNativeRender = NULL;
190     def.format.audio.bFlagErrorConcealment = OMX_FALSE;
191
192     addPort(def);
193
194     def.nPortIndex = 1;
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;
204
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;
209
210     addPort(def);
211 }
212
213 void SoftFFmpegAudio::setAVCtxToDefault(AVCodecContext *avctx, const AVCodec *codec) {
214     int fast = 0;
215
216     avctx->workaround_bugs   = 1;
217     avctx->lowres            = 0;
218     if(avctx->lowres > codec->max_lowres){
219         ALOGW("The maximum value for lowres supported by the decoder is %d",
220                 codec->max_lowres);
221         avctx->lowres= codec->max_lowres;
222     }
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;
228
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;
233 }
234
235
236 void SoftFFmpegAudio::reConfigCtx() {
237     mCtx->channels = mNumChannels;
238     mCtx->sample_rate = mSamplingRate;
239     mCtx->bit_rate = mBitRate;
240     mCtx->sample_fmt = mSamplingFmt;
241
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);
247 }
248
249 status_t SoftFFmpegAudio::initDecoder() {
250     status_t status;
251
252     status = initFFmpeg();
253     if (status != OK)
254         return NO_INIT;
255
256     mFFmpegInited = true;
257
258     mCtx = avcodec_alloc_context3(NULL);
259     if (!mCtx)
260     {
261         ALOGE("avcodec_alloc_context failed.");
262         return NO_MEMORY;
263     }
264
265     mCtx->codec_type = AVMEDIA_TYPE_AUDIO;
266     switch (mMode) {
267     case MODE_MPEG:
268         mCtx->codec_id = CODEC_ID_MP3;
269         break;
270     case MODE_MPEGL1:
271         mCtx->codec_id = CODEC_ID_MP1;
272         break;
273     case MODE_MPEGL2:
274         mCtx->codec_id = CODEC_ID_MP2;
275         break;
276     case MODE_AAC:
277         mCtx->codec_id = CODEC_ID_AAC;
278         break;
279     case MODE_AC3:
280         mCtx->codec_id = CODEC_ID_AC3;
281         break;
282     case MODE_WMA:
283         mCtx->codec_id = CODEC_ID_WMAV2; // FIXME, CODEC_ID_WMAV1 or CODEC_ID_WMAV2?
284         break;
285     case MODE_RA:
286         mCtx->codec_id = CODEC_ID_COOK; // FIXME
287         break;
288     case MODE_APE:
289         mCtx->codec_id = CODEC_ID_APE;
290         break;
291     case MODE_DTS:
292         mCtx->codec_id = CODEC_ID_DTS;
293         break;
294     case MODE_FLAC:
295         mCtx->codec_id = CODEC_ID_FLAC;
296         break;
297     case MODE_VORBIS:
298         mCtx->codec_id = CODEC_ID_VORBIS;
299         break;
300     default:
301         CHECK(!"Should not be here. Unsupported codec");
302         break;
303     }
304
305     mCtx->codec = avcodec_find_decoder(mCtx->codec_id);
306     if (!mCtx->codec)
307     {
308         ALOGE("find codec failed");
309         return BAD_TYPE;
310     }
311
312     setAVCtxToDefault(mCtx, mCtx->codec);
313
314     //default config
315     reConfigCtx();
316
317     memset(mSilenceBuffer, 0, kOutputBufferSize);
318
319     return OK;
320 }
321
322 void SoftFFmpegAudio::deInitDecoder() {
323     if (mCtx) {
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;
329         }
330         avcodec_close(mCtx);
331         av_free(mCtx);
332         mCtx = NULL;
333     }
334
335     if (mSwrCtx) {
336         swr_free(&mSwrCtx);
337         mSwrCtx = NULL;
338     }
339 }
340
341 OMX_ERRORTYPE SoftFFmpegAudio::internalGetParameter(
342         OMX_INDEXTYPE index, OMX_PTR params) {
343     int32_t channels = 0;
344     int32_t sampling_rate = 0;
345
346     switch (index) {
347         case OMX_IndexParamAudioAac:
348         {
349             OMX_AUDIO_PARAM_AACPROFILETYPE *aacParams =
350                 (OMX_AUDIO_PARAM_AACPROFILETYPE *)params;
351
352             if (aacParams->nPortIndex != 0) {
353                 return OMX_ErrorUndefined;
354             }
355
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;
363
364             aacParams->nChannels = mNumChannels;
365             aacParams->nSampleRate = mSamplingRate;
366
367             return OMX_ErrorNone;
368         }
369         case OMX_IndexParamAudioWma:
370         {
371             OMX_AUDIO_PARAM_WMATYPE *wmaParams =
372                 (OMX_AUDIO_PARAM_WMATYPE *)params;
373
374             if (wmaParams->nPortIndex != 0) {
375                 return OMX_ErrorUndefined;
376             }
377
378             wmaParams->nChannels = 0;
379             wmaParams->nSamplingRate = 0;
380             wmaParams->nBitRate = 0;
381             wmaParams->eFormat = OMX_AUDIO_WMAFormatUnused;
382
383             return OMX_ErrorNone;
384         }
385         case OMX_IndexParamAudioRa:
386         {
387             OMX_AUDIO_PARAM_RATYPE *raParams =
388                 (OMX_AUDIO_PARAM_RATYPE *)params;
389
390             if (raParams->nPortIndex != 0) {
391                 return OMX_ErrorUndefined;
392             }
393
394             raParams->nChannels = 0;
395             raParams->nSamplingRate = 0;
396             raParams->eFormat = OMX_AUDIO_RAFormatUnused;
397
398             return OMX_ErrorNone;
399         }
400         case OMX_IndexParamAudioApe:
401         {
402             OMX_AUDIO_PARAM_APETYPE *apeParams =
403                 (OMX_AUDIO_PARAM_APETYPE *)params;
404
405             if (apeParams->nPortIndex != 0) {
406                 return OMX_ErrorUndefined;
407             }
408
409             apeParams->nChannels = 0;
410             apeParams->nSamplingRate = 0;
411
412             return OMX_ErrorNone;
413         }
414         case OMX_IndexParamAudioDts:
415         {
416             OMX_AUDIO_PARAM_DTSTYPE *dtsParams =
417                 (OMX_AUDIO_PARAM_DTSTYPE *)params;
418
419             if (dtsParams->nPortIndex != 0) {
420                 return OMX_ErrorUndefined;
421             }
422
423             dtsParams->nChannels = 0;
424             dtsParams->nSamplingRate = 0;
425
426             return OMX_ErrorNone;
427         }
428         case OMX_IndexParamAudioFlac:
429         {
430             OMX_AUDIO_PARAM_FLACTYPE *flacParams =
431                 (OMX_AUDIO_PARAM_FLACTYPE *)params;
432
433             if (flacParams->nPortIndex != 0) {
434                 return OMX_ErrorUndefined;
435             }
436
437             flacParams->nChannels = 0;
438             flacParams->nSampleRate = 0;
439
440             return OMX_ErrorNone;
441         }
442         case OMX_IndexParamAudioPcm:
443         {
444             OMX_AUDIO_PARAM_PCMMODETYPE *pcmParams =
445                 (OMX_AUDIO_PARAM_PCMMODETYPE *)params;
446
447             if (pcmParams->nPortIndex > 1) {
448                 return OMX_ErrorUndefined;
449             }
450
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;
458
459             // update src and target(except aac), only once!
460             adjustAudioParameter();
461
462             pcmParams->nChannels = channels;
463             pcmParams->nSamplingRate = sampling_rate;
464
465             return OMX_ErrorNone;
466         }
467         case OMX_IndexParamAudioVorbis:
468         {
469             OMX_AUDIO_PARAM_VORBISTYPE *vorbisParams =
470                 (OMX_AUDIO_PARAM_VORBISTYPE *)params;
471
472             if (vorbisParams->nPortIndex != 0) {
473                 return OMX_ErrorUndefined;
474             }
475
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;
483
484             vorbisParams->nChannels = 1;
485             vorbisParams->nSampleRate = 44100;
486 /*
487             if (!isConfigured()) {
488                 vorbisParams->nChannels = 1;
489                 vorbisParams->nSampleRate = 44100;
490             } else {
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;
496             }
497 */
498             return OMX_ErrorNone;
499         }
500
501         default:
502             return SimpleSoftOMXComponent::internalGetParameter(index, params);
503     }
504 }
505
506 OMX_ERRORTYPE SoftFFmpegAudio::isRoleSupported(
507         const OMX_PARAM_COMPONENTROLETYPE *roleParams) {
508     bool supported = true;
509
510     switch (mMode) {
511     case MODE_MPEG:
512         if (strncmp((const char *)roleParams->cRole,
513                 "audio_decoder.mp3", OMX_MAX_STRINGNAME_SIZE - 1))
514             supported =  false;
515         break;
516     case MODE_MPEGL1:
517         if (strncmp((const char *)roleParams->cRole,
518                 "audio_decoder.mp1", OMX_MAX_STRINGNAME_SIZE - 1))
519             supported =  false;
520         break;
521     case MODE_MPEGL2:
522         if (strncmp((const char *)roleParams->cRole,
523                 "audio_decoder.mp2", OMX_MAX_STRINGNAME_SIZE - 1))
524             supported =  false;
525         break;
526     case MODE_AAC:
527         if (strncmp((const char *)roleParams->cRole,
528                 "audio_decoder.aac", OMX_MAX_STRINGNAME_SIZE - 1))
529         supported =  false;
530         break;
531     case MODE_AC3:
532         if (strncmp((const char *)roleParams->cRole,
533                 "audio_decoder.ac3", OMX_MAX_STRINGNAME_SIZE - 1))
534             supported =  false;
535         break;
536     case MODE_WMA:
537         if (strncmp((const char *)roleParams->cRole,
538                 "audio_decoder.wma", OMX_MAX_STRINGNAME_SIZE - 1))
539         supported =  false;
540         break;
541     case MODE_RA:
542         if (strncmp((const char *)roleParams->cRole,
543                 "audio_decoder.ra", OMX_MAX_STRINGNAME_SIZE - 1))
544         supported =  false;
545         break;
546     case MODE_APE:
547         if (strncmp((const char *)roleParams->cRole,
548                 "audio_decoder.ape", OMX_MAX_STRINGNAME_SIZE - 1))
549         supported =  false;
550         break;
551     case MODE_DTS:
552         if (strncmp((const char *)roleParams->cRole,
553                 "audio_decoder.dts", OMX_MAX_STRINGNAME_SIZE - 1))
554         supported =  false;
555         break;
556     case MODE_FLAC:
557         if (strncmp((const char *)roleParams->cRole,
558                 "audio_decoder.flac", OMX_MAX_STRINGNAME_SIZE - 1))
559         supported =  false;
560         break;
561     case MODE_VORBIS:
562         if (strncmp((const char *)roleParams->cRole,
563                 "audio_decoder.vorbis", OMX_MAX_STRINGNAME_SIZE - 1))
564         supported =  false;
565         break;
566     default:
567         CHECK(!"Should not be here. Unsupported role.");
568         break;
569     }
570
571     if (!supported) {
572         ALOGE("unsupported role: %s", (const char *)roleParams->cRole);
573         return OMX_ErrorUndefined;
574     }
575
576     return OMX_ErrorNone;
577 }
578
579 void SoftFFmpegAudio::adjustAudioParameter() {
580     int32_t channels = 0;
581     int32_t sampling_rate = 0;
582
583     // channels support 1 or 2 only
584     channels = mNumChannels >= 2 ? 2 : 1;
585
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;
592     }
593
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);
600 }
601
602 OMX_ERRORTYPE SoftFFmpegAudio::internalSetParameter(
603         OMX_INDEXTYPE index, const OMX_PTR params) {
604     switch (index) {
605         case OMX_IndexParamStandardComponentRole:
606         {
607             const OMX_PARAM_COMPONENTROLETYPE *roleParams =
608                 (const OMX_PARAM_COMPONENTROLETYPE *)params;
609                         return isRoleSupported(roleParams);
610         }
611         case OMX_IndexParamAudioPcm:
612         {
613             const OMX_AUDIO_PARAM_PCMMODETYPE *pcmParams =
614                 (const OMX_AUDIO_PARAM_PCMMODETYPE *)params;
615
616             if (pcmParams->nPortIndex != 1) {
617                 return OMX_ErrorUndefined;
618             }
619
620             mNumChannels = pcmParams->nChannels;
621             mSamplingRate = pcmParams->nSamplingRate;
622
623             return OMX_ErrorNone;
624         }
625         case OMX_IndexParamAudioAac:
626         {
627             const OMX_AUDIO_PARAM_AACPROFILETYPE *aacParams =
628                 (const OMX_AUDIO_PARAM_AACPROFILETYPE *)params;
629
630             if (aacParams->nPortIndex != 0) {
631                 return OMX_ErrorUndefined;
632             }
633
634             mNumChannels = aacParams->nChannels;
635             mSamplingRate = aacParams->nSampleRate;
636
637             // update src and target(only aac), only once!
638             adjustAudioParameter();
639
640             ALOGV("got OMX_IndexParamAudioAac, mNumChannels: %d, mSamplingRate: %d",
641                 mNumChannels, mSamplingRate);
642
643             return OMX_ErrorNone;
644         }
645         case OMX_IndexParamAudioWma:
646         {
647             OMX_AUDIO_PARAM_WMATYPE *wmaParams =
648                 (OMX_AUDIO_PARAM_WMATYPE *)params;
649
650             if (wmaParams->nPortIndex != 0) {
651                 return OMX_ErrorUndefined;
652             }
653
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;
660             } else {
661                 ALOGE("unsupported wma codec: 0x%x", wmaParams->eFormat);
662                 return OMX_ErrorUndefined;
663             }
664
665             mNumChannels = wmaParams->nChannels;
666             mSamplingRate = wmaParams->nSamplingRate;
667             mBitRate = wmaParams->nBitRate;
668             mBlockAlign = wmaParams->nBlockAlign;
669
670             // wmadec needs bitrate, block_align
671             mCtx->bit_rate = mBitRate;
672             mCtx->block_align = mBlockAlign;
673
674             // update src and target(only wma), only once!
675             adjustAudioParameter();
676
677             ALOGV("got OMX_IndexParamAudioWma, mNumChannels: %d, "
678                     "mSamplingRate: %d, mBitRate: %d, mBlockAlign: %d",
679                 mNumChannels, mSamplingRate, mBitRate, mBlockAlign);
680
681             return OMX_ErrorNone;
682         }
683         case OMX_IndexParamAudioRa:
684         {
685             OMX_AUDIO_PARAM_RATYPE *raParams =
686                 (OMX_AUDIO_PARAM_RATYPE *)params;
687
688             if (raParams->nPortIndex != 0) {
689                 return OMX_ErrorUndefined;
690             }
691
692             mCtx->codec_id = CODEC_ID_COOK;
693
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;
699
700             // cook decoder need block_align
701             mCtx->block_align = mBlockAlign;
702
703             // update src and target(only wma), only once!
704             adjustAudioParameter();
705
706             ALOGV("got OMX_IndexParamAudioRa, mNumChannels: %d, "
707                     "mSamplingRate: %d, mBlockAlign: %d",
708                 mNumChannels, mSamplingRate, mBlockAlign);
709
710             return OMX_ErrorNone;
711         }
712         case OMX_IndexParamAudioApe:
713         {
714             OMX_AUDIO_PARAM_APETYPE *apeParams =
715                 (OMX_AUDIO_PARAM_APETYPE *)params;
716
717             if (apeParams->nPortIndex != 0) {
718                 return OMX_ErrorUndefined;
719             }
720
721             mNumChannels = apeParams->nChannels;
722             mSamplingRate = apeParams->nSamplingRate;
723
724             // ape decoder need bits_per_coded_sample
725             mCtx->bits_per_coded_sample = apeParams->nBitsPerSample;
726
727             // update src and target, only once!
728             adjustAudioParameter();
729
730             ALOGV("got OMX_IndexParamAudioApe, mNumChannels: %d, "
731                     "mSamplingRate: %d, nBitsPerSample: %lu",
732                 mNumChannels, mSamplingRate, apeParams->nBitsPerSample);
733
734             return OMX_ErrorNone;
735         }
736         case OMX_IndexParamAudioDts:
737         {
738             OMX_AUDIO_PARAM_DTSTYPE *dtsParams =
739                 (OMX_AUDIO_PARAM_DTSTYPE *)params;
740
741             if (dtsParams->nPortIndex != 0) {
742                 return OMX_ErrorUndefined;
743             }
744
745             mNumChannels = dtsParams->nChannels;
746             mSamplingRate = dtsParams->nSamplingRate;
747
748             // update src and target, only once!
749             adjustAudioParameter();
750
751             ALOGV("got OMX_IndexParamAudioDts, mNumChannels: %d, mSamplingRate: %d",
752                 mNumChannels, mSamplingRate);
753
754             return OMX_ErrorNone;
755         }
756         case OMX_IndexParamAudioFlac:
757         {
758             OMX_AUDIO_PARAM_FLACTYPE *flacParams =
759                 (OMX_AUDIO_PARAM_FLACTYPE *)params;
760
761             if (flacParams->nPortIndex != 0) {
762                 return OMX_ErrorUndefined;
763             }
764
765             mNumChannels = flacParams->nChannels;
766             mSamplingRate = flacParams->nSampleRate;
767
768             // update src and target, only once!
769             adjustAudioParameter();
770
771             ALOGV("got OMX_IndexParamAudioFlac, mNumChannels: %d, mSamplingRate: %d",
772                 mNumChannels, mSamplingRate);
773
774             return OMX_ErrorNone;
775         }
776         case OMX_IndexParamAudioVorbis:
777         {
778             const OMX_AUDIO_PARAM_VORBISTYPE *vorbisParams =
779                 (const OMX_AUDIO_PARAM_VORBISTYPE *)params;
780
781             if (vorbisParams->nPortIndex != 0) {
782                 return OMX_ErrorUndefined;
783             }
784
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);
791
792             return OMX_ErrorNone;
793         }
794         default:
795             ALOGI("internalSetParameter, index: 0x%x", index);
796             return SimpleSoftOMXComponent::internalSetParameter(index, params);
797     }
798 }
799
800 void SoftFFmpegAudio::onQueueFilled(OMX_U32 portIndex) {
801     int len = 0;
802     int err = 0;
803     size_t dataSize = 0;
804     int64_t decChannelLayout;
805     int32_t inputBufferUsedLength = 0;
806     BufferInfo *inInfo = NULL;
807     OMX_BUFFERHEADERTYPE *inHeader = NULL;
808
809     if (mSignalledError || mOutputPortSettingsChange != NONE) {
810         return;
811     }
812
813     List<BufferInfo *> &inQueue = getPortQueue(0);
814     List<BufferInfo *> &outQueue = getPortQueue(1);
815
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;
823         } else {
824             inInfo = NULL;
825             inHeader = NULL;
826         }
827
828         BufferInfo *outInfo = *outQueue.begin();
829         OMX_BUFFERHEADERTYPE *outHeader = outInfo->mHeader;
830
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);
837             mReceivedEOS = true;
838                         continue; //empty all input buffers in inQueue
839         }
840
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;
845
846             outQueue.erase(outQueue.begin());
847             outInfo->mOwnedByUs = false;
848             notifyFillBufferDone(outHeader);
849             return;
850         }
851
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;
864                     return;
865                 }
866
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);
870
871                 inInfo->mOwnedByUs = false;
872                 inQueue.erase(inQueue.begin());
873                 inInfo = NULL;
874                 notifyEmptyBufferDone(inHeader);
875                 inHeader = NULL;
876
877                 continue;
878             }
879             if (mIgnoreExtradata) {
880                 ALOGI("got extradata, size: %lu, but ignore it", inHeader->nFilledLen);
881                 inInfo->mOwnedByUs = false;
882                 inQueue.erase(inQueue.begin());
883                 inInfo = NULL;
884                 notifyEmptyBufferDone(inHeader);
885                 inHeader = NULL;
886
887                 continue;
888             }
889         }
890
891         if (!mCodecOpened) {
892             if (!mExtradataReady && !mIgnoreExtradata) {
893                 ALOGI("extradata is ready, size: %d", mCtx->extradata_size);
894                 hexdump(mCtx->extradata, mCtx->extradata_size);
895                 mExtradataReady = true;
896             }
897
898             // find decoder again as codec_id may have changed
899             mCtx->codec = avcodec_find_decoder(mCtx->codec_id);
900             if (!mCtx->codec) {
901                 ALOGE("ffmpeg audio decoder failed to find codec");
902                 notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL);
903                 mSignalledError = true;
904                 return;
905             }
906
907             setAVCtxToDefault(mCtx, mCtx->codec);
908
909             //we should update mCtx here!
910             reConfigCtx();
911
912             ALOGI("open ffmpeg decoder now");
913             err = avcodec_open2(mCtx, mCtx->codec, NULL);
914             if (err < 0) {
915                 ALOGE("ffmpeg audio decoder failed to initialize. (%d)", err);
916                 notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL);
917                 mSignalledError = true;
918                 return;
919             }
920             mCodecOpened = 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));
925         }
926
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;
933         }
934
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
937             AVPacket pkt;
938             memset(&pkt, 0, sizeof(pkt));
939             av_init_packet(&pkt);
940
941             if (inHeader) {
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
946             } else {
947                                 CHECK(mFlushComplete == false);
948                 pkt.data = NULL;
949                 pkt.size = 0;
950                 pkt.pts = AV_NOPTS_VALUE;
951             }
952 #if DEBUG_PKT
953             ALOGV("pkt size: %d, pts: %lld", pkt.size, pkt.pts);
954 #endif
955             if (!mFrame) {
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;
960                     return;
961                 }
962             } else {
963                 avcodec_get_frame_defaults(mFrame);
964             }
965
966             int gotFrm = false;
967             inputBufferUsedLength = 0;
968             len = avcodec_decode_audio4(mCtx, mFrame, &gotFrm, &pkt);
969             if (len < 0) {
970                 ALOGE("ffmpeg audio decoder failed to decode frame. "
971                         "consume pkt len: %d", len);
972
973                 /* if !mAudioConfigChanged, Don't fill the out buffer */
974                 if (!mAudioConfigChanged) {
975                     inInfo->mOwnedByUs = false;
976                     inQueue.erase(inQueue.begin());
977                     inInfo = NULL;
978                     notifyEmptyBufferDone(inHeader);
979                     inHeader = NULL;
980
981                     mInputBufferSize = 0; // need?
982                     continue;
983                 }
984
985                 //inputBufferUsedLength = inHeader->nFilledLen;
986                 /* if error, we skip the frame and play silence instead */
987                 mPAudioBuffer = mSilenceBuffer;
988                 mAudioBufferSize = kOutputBufferSize;
989             }
990
991 #if DEBUG_PKT
992             ALOGV("ffmpeg audio decoder, consume pkt len: %d", len);
993 #endif
994
995                         if (inHeader) {
996                                 CHECK(mFlushComplete == false);
997
998                 if (len < 0)
999                     inputBufferUsedLength = inHeader->nFilledLen;
1000                 else
1001                     inputBufferUsedLength = len;
1002
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());
1010                     inInfo = NULL;
1011                     notifyEmptyBufferDone(inHeader);
1012                     inHeader = NULL;
1013
1014                                         //reset mInputBufferSize
1015                     mInputBufferSize = 0;
1016                 }
1017                         }
1018
1019             if (!gotFrm) {
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;
1024                 continue;
1025             }
1026
1027             /**
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???
1030              */
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;
1047                     return;
1048                 } else {
1049                     // match with the default, set mAudioConfigChanged true anyway!
1050                     mAudioConfigChanged = true;
1051                     mSamplingFmt = mCtx->sample_fmt;
1052                 }
1053             }
1054
1055             dataSize = av_samples_get_buffer_size(NULL, mNumChannels,
1056                            mFrame->nb_samples, mSamplingFmt, 1);
1057
1058 #if DEBUG_FRM
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));
1065 #endif
1066
1067             decChannelLayout = av_get_default_channel_layout(mNumChannels);
1068             if (mSamplingFmt != mAudioSrcFmt
1069                     || decChannelLayout != mAudioSrcChannelLayout
1070                     || mSamplingRate != mAudioSrcFreq) {
1071                 if (mSwrCtx)
1072                     swr_free(&mSwrCtx);
1073                 mSwrCtx = swr_alloc_set_opts(NULL,
1074                         mAudioTgtChannelLayout, mAudioTgtFmt, mAudioTgtFreq,
1075                         decChannelLayout,       mSamplingFmt, mSamplingRate,
1076                         0, NULL);
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!",
1080                             mSamplingRate,
1081                             av_get_sample_fmt_name(mSamplingFmt),
1082                             mNumChannels,
1083                             mAudioTgtFreq,
1084                             av_get_sample_fmt_name(mAudioTgtFmt),
1085                             mAudioTgtChannels);
1086                     notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL);
1087                     mSignalledError = true;
1088                     return;
1089                 }
1090
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)!",
1094                         mSamplingRate,
1095                         av_get_sample_fmt_name(mSamplingFmt),
1096                         mNumChannels,
1097                         mAudioTgtChannelLayout,
1098                         mAudioTgtFreq,
1099                         av_get_sample_fmt_name(mAudioTgtFmt),
1100                         mAudioTgtChannels,
1101                         decChannelLayout);
1102
1103                 mAudioSrcChannelLayout = decChannelLayout;
1104                 mAudioSrcChannels = mNumChannels;
1105                 mAudioSrcFreq = mSamplingRate;
1106                 mAudioSrcFmt = mSamplingFmt;
1107             }
1108
1109             if (mSwrCtx) {
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);
1115 #if DEBUG_FRM
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));
1121 #endif
1122                 int len2 = swr_convert(mSwrCtx, out, out_count, in, mFrame->nb_samples);
1123                 if (len2 < 0) {
1124                     ALOGE("audio_resample() failed");
1125                     break;
1126                 }
1127                 if (len2 == out_count) {
1128                     ALOGE("warning: audio buffer is probably too small");
1129                     swr_init(mSwrCtx);
1130                 }
1131                 mPAudioBuffer = mAudioBuf2;
1132                 mAudioBufferSize = len2 * mAudioTgtChannels *
1133                     av_get_bytes_per_sample(mAudioTgtFmt);
1134             } else {
1135                 mPAudioBuffer = mFrame->data[0];
1136                 mAudioBufferSize = dataSize;
1137             }
1138
1139 #if DEBUG_FRM
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);
1143 #endif
1144         } //if ((inHeader && mAudioBufferSize == 0) || (!inHeader && !mFlushComplete))
1145
1146         size_t copyToOutputBufferLen = mAudioBufferSize;
1147         if (mAudioBufferSize > kOutputBufferSize)
1148             copyToOutputBufferLen = kOutputBufferSize;
1149
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;
1156
1157 #if DEBUG_FRM
1158         ALOGV("ffmpeg audio decoder, fill out buffer, "
1159                                 "pts: %lld, mNumFramesOutput: %lld",
1160                 outHeader->nTimeStamp, mNumFramesOutput);
1161 #endif
1162
1163         mPAudioBuffer += copyToOutputBufferLen;
1164         mAudioBufferSize -= copyToOutputBufferLen;
1165         mNumFramesOutput += copyToOutputBufferLen /
1166             (av_get_bytes_per_sample(mAudioTgtFmt) * mNumChannels);
1167
1168         // reset mNumFramesOutput
1169         if (mInputBufferSize == 0 && mAudioBufferSize == 0) {
1170 #if DEBUG_FRM
1171             ALOGV("~~~~ reset mNumFramesOutput to 0");
1172 #endif
1173             mNumFramesOutput = 0;
1174         }
1175
1176         outInfo->mOwnedByUs = false;
1177         outQueue.erase(outQueue.begin());
1178         outInfo = NULL;
1179         notifyFillBufferDone(outHeader);
1180         outHeader = NULL;
1181     }
1182 }
1183
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);
1189     }
1190         mReceivedEOS = false;
1191         mFlushComplete = false;
1192         mAnchorTimeUs = 0;
1193         mInputBufferSize = 0;
1194         mNumFramesOutput = 0;
1195         mAudioBufferSize = 0;
1196         mPAudioBuffer = NULL;
1197         //don't free mFrame!
1198 }
1199
1200 void SoftFFmpegAudio::onPortEnableCompleted(OMX_U32 portIndex, bool enabled) {
1201     if (portIndex != 1) {
1202         return;
1203     }
1204
1205     switch (mOutputPortSettingsChange) {
1206         case NONE:
1207             break;
1208
1209         case AWAITING_DISABLED:
1210         {
1211             CHECK(!enabled);
1212             mOutputPortSettingsChange = AWAITING_ENABLED;
1213             break;
1214         }
1215
1216         default:
1217         {
1218             CHECK_EQ((int)mOutputPortSettingsChange, (int)AWAITING_ENABLED);
1219             CHECK(enabled);
1220             mOutputPortSettingsChange = NONE;
1221             break;
1222         }
1223     }
1224 }
1225
1226 }  // namespace android
1227
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);
1232 }