OMXCodec.cpp revision 95301196d59fb88ef96342fd307a2be374b5079b
1/*
2 * Copyright (C) 2009 The Android Open Source Project
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 "OMXCodec"
19#include <utils/Log.h>
20
21#include <binder/IServiceManager.h>
22#include <binder/MemoryDealer.h>
23#include <binder/ProcessState.h>
24#include <media/IMediaPlayerService.h>
25#include <media/stagefright/ESDS.h>
26#include <media/stagefright/MediaBuffer.h>
27#include <media/stagefright/MediaBufferGroup.h>
28#include <media/stagefright/MediaDebug.h>
29#include <media/stagefright/MediaDefs.h>
30#include <media/stagefright/MediaExtractor.h>
31#include <media/stagefright/MetaData.h>
32#include <media/stagefright/MmapSource.h>
33#include <media/stagefright/OMXCodec.h>
34#include <media/stagefright/Utils.h>
35#include <utils/Vector.h>
36
37#include <OMX_Audio.h>
38#include <OMX_Component.h>
39
40namespace android {
41
42struct CodecInfo {
43    const char *mime;
44    const char *codec;
45};
46
47static const CodecInfo kDecoderInfo[] = {
48    { MEDIA_MIMETYPE_IMAGE_JPEG, "OMX.TI.JPEG.decode" },
49    { MEDIA_MIMETYPE_AUDIO_MPEG, "OMX.TI.MP3.decode" },
50    { MEDIA_MIMETYPE_AUDIO_MPEG, "OMX.PV.mp3dec" },
51    { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.TI.AMR.decode" },
52    { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.PV.amrdec" },
53    { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.TI.WBAMR.decode" },
54    { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.PV.amrdec" },
55    { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.TI.AAC.decode" },
56    { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.PV.aacdec" },
57    { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.video.decoder.mpeg4" },
58    { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.TI.Video.Decoder" },
59    { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.PV.mpeg4dec" },
60    { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.video.decoder.h263" },
61    { MEDIA_MIMETYPE_VIDEO_H263, "OMX.TI.Video.Decoder" },
62    { MEDIA_MIMETYPE_VIDEO_H263, "OMX.PV.h263dec" },
63    { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.video.decoder.avc" },
64    { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.TI.Video.Decoder" },
65    { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.PV.avcdec" },
66};
67
68static const CodecInfo kEncoderInfo[] = {
69    { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.TI.AMR.encode" },
70    { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.PV.amrencnb" },
71    { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.TI.WBAMR.encode" },
72    { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.TI.AAC.encode" },
73    { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.PV.aacenc" },
74    { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.video.encoder.mpeg4" },
75    { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.TI.Video.encoder" },
76    { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.PV.mpeg4enc" },
77    { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.video.encoder.h263" },
78    { MEDIA_MIMETYPE_VIDEO_H263, "OMX.TI.Video.encoder" },
79    { MEDIA_MIMETYPE_VIDEO_H263, "OMX.PV.h263enc" },
80    { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.TI.Video.encoder" },
81    { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.PV.avcenc" },
82};
83
84#define CODEC_LOGI(x, ...) LOGI("[%s] "x, mComponentName, ##__VA_ARGS__)
85#define CODEC_LOGV(x, ...) LOGV("[%s] "x, mComponentName, ##__VA_ARGS__)
86
87struct OMXCodecObserver : public BnOMXObserver {
88    OMXCodecObserver(const wp<OMXCodec> &target)
89        : mTarget(target) {
90    }
91
92    // from IOMXObserver
93    virtual void on_message(const omx_message &msg) {
94        sp<OMXCodec> codec = mTarget.promote();
95
96        if (codec.get() != NULL) {
97            codec->on_message(msg);
98        }
99    }
100
101protected:
102    virtual ~OMXCodecObserver() {}
103
104private:
105    wp<OMXCodec> mTarget;
106
107    OMXCodecObserver(const OMXCodecObserver &);
108    OMXCodecObserver &operator=(const OMXCodecObserver &);
109};
110
111static const char *GetCodec(const CodecInfo *info, size_t numInfos,
112                            const char *mime, int index) {
113    CHECK(index >= 0);
114    for(size_t i = 0; i < numInfos; ++i) {
115        if (!strcasecmp(mime, info[i].mime)) {
116            if (index == 0) {
117                return info[i].codec;
118            }
119
120            --index;
121        }
122    }
123
124    return NULL;
125}
126
127enum {
128    kAVCProfileBaseline      = 0x42,
129    kAVCProfileMain          = 0x4d,
130    kAVCProfileExtended      = 0x58,
131    kAVCProfileHigh          = 0x64,
132    kAVCProfileHigh10        = 0x6e,
133    kAVCProfileHigh422       = 0x7a,
134    kAVCProfileHigh444       = 0xf4,
135    kAVCProfileCAVLC444Intra = 0x2c
136};
137
138static const char *AVCProfileToString(uint8_t profile) {
139    switch (profile) {
140        case kAVCProfileBaseline:
141            return "Baseline";
142        case kAVCProfileMain:
143            return "Main";
144        case kAVCProfileExtended:
145            return "Extended";
146        case kAVCProfileHigh:
147            return "High";
148        case kAVCProfileHigh10:
149            return "High 10";
150        case kAVCProfileHigh422:
151            return "High 422";
152        case kAVCProfileHigh444:
153            return "High 444";
154        case kAVCProfileCAVLC444Intra:
155            return "CAVLC 444 Intra";
156        default:   return "Unknown";
157    }
158}
159
160template<class T>
161static void InitOMXParams(T *params) {
162    params->nSize = sizeof(T);
163    params->nVersion.s.nVersionMajor = 1;
164    params->nVersion.s.nVersionMinor = 0;
165    params->nVersion.s.nRevision = 0;
166    params->nVersion.s.nStep = 0;
167}
168
169// static
170sp<OMXCodec> OMXCodec::Create(
171        const sp<IOMX> &omx,
172        const sp<MetaData> &meta, bool createEncoder,
173        const sp<MediaSource> &source,
174        const char *matchComponentName) {
175    const char *mime;
176    bool success = meta->findCString(kKeyMIMEType, &mime);
177    CHECK(success);
178
179    const char *componentName = NULL;
180    IOMX::node_id node = 0;
181    for (int index = 0;; ++index) {
182        if (createEncoder) {
183            componentName = GetCodec(
184                    kEncoderInfo, sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
185                    mime, index);
186        } else {
187            componentName = GetCodec(
188                    kDecoderInfo, sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
189                    mime, index);
190        }
191
192        if (!componentName) {
193            return NULL;
194        }
195
196        // If a specific codec is requested, skip the non-matching ones.
197        if (matchComponentName && strcmp(componentName, matchComponentName)) {
198            continue;
199        }
200
201        LOGV("Attempting to allocate OMX node '%s'", componentName);
202
203        status_t err = omx->allocate_node(componentName, &node);
204        if (err == OK) {
205            LOGI("Successfully allocated OMX node '%s'", componentName);
206            break;
207        }
208    }
209
210    uint32_t quirks = 0;
211    if (!strcmp(componentName, "OMX.PV.avcdec")) {
212        quirks |= kWantsNALFragments;
213    }
214    if (!strcmp(componentName, "OMX.TI.MP3.decode")) {
215        quirks |= kNeedsFlushBeforeDisable;
216    }
217    if (!strcmp(componentName, "OMX.TI.AAC.decode")) {
218        quirks |= kNeedsFlushBeforeDisable;
219        quirks |= kRequiresFlushCompleteEmulation;
220
221        // The following is currently necessary for proper shutdown
222        // behaviour, but NOT enabled by default in order to make the
223        // bug reproducible...
224        // quirks |= kRequiresFlushBeforeShutdown;
225    }
226    if (!strncmp(componentName, "OMX.qcom.video.encoder.", 23)) {
227        quirks |= kRequiresLoadedToIdleAfterAllocation;
228        quirks |= kRequiresAllocateBufferOnInputPorts;
229    }
230    if (!strncmp(componentName, "OMX.qcom.video.decoder.", 23)) {
231        // XXX Required on P....on only.
232        quirks |= kRequiresAllocateBufferOnOutputPorts;
233    }
234
235    if (!strncmp(componentName, "OMX.TI.", 7)) {
236        // Apparently I must not use OMX_UseBuffer on either input or
237        // output ports on any of the TI components or quote:
238        // "(I) may have unexpected problem (sic) which can be timing related
239        //  and hard to reproduce."
240
241        quirks |= kRequiresAllocateBufferOnInputPorts;
242        quirks |= kRequiresAllocateBufferOnOutputPorts;
243    }
244
245    sp<OMXCodec> codec = new OMXCodec(
246            omx, node, quirks, createEncoder, mime, componentName,
247            source);
248
249    uint32_t type;
250    const void *data;
251    size_t size;
252    if (meta->findData(kKeyESDS, &type, &data, &size)) {
253        ESDS esds((const char *)data, size);
254        CHECK_EQ(esds.InitCheck(), OK);
255
256        const void *codec_specific_data;
257        size_t codec_specific_data_size;
258        esds.getCodecSpecificInfo(
259                &codec_specific_data, &codec_specific_data_size);
260
261        printf("found codec-specific data of size %d\n",
262               codec_specific_data_size);
263
264        codec->addCodecSpecificData(
265                codec_specific_data, codec_specific_data_size);
266    } else if (meta->findData(kKeyAVCC, &type, &data, &size)) {
267        printf("found avcc of size %d\n", size);
268
269        // Parse the AVCDecoderConfigurationRecord
270
271        const uint8_t *ptr = (const uint8_t *)data;
272
273        CHECK(size >= 7);
274        CHECK_EQ(ptr[0], 1);  // configurationVersion == 1
275        uint8_t profile = ptr[1];
276        uint8_t level = ptr[3];
277
278        CHECK((ptr[4] >> 2) == 0x3f);  // reserved
279
280        size_t lengthSize = 1 + (ptr[4] & 3);
281
282        // commented out check below as H264_QVGA_500_NO_AUDIO.3gp
283        // violates it...
284        // CHECK((ptr[5] >> 5) == 7);  // reserved
285
286        size_t numSeqParameterSets = ptr[5] & 31;
287
288        ptr += 6;
289        size -= 6;
290
291        for (size_t i = 0; i < numSeqParameterSets; ++i) {
292            CHECK(size >= 2);
293            size_t length = U16_AT(ptr);
294
295            ptr += 2;
296            size -= 2;
297
298            CHECK(size >= length);
299
300            codec->addCodecSpecificData(ptr, length);
301
302            ptr += length;
303            size -= length;
304        }
305
306        CHECK(size >= 1);
307        size_t numPictureParameterSets = *ptr;
308        ++ptr;
309        --size;
310
311        for (size_t i = 0; i < numPictureParameterSets; ++i) {
312            CHECK(size >= 2);
313            size_t length = U16_AT(ptr);
314
315            ptr += 2;
316            size -= 2;
317
318            CHECK(size >= length);
319
320            codec->addCodecSpecificData(ptr, length);
321
322            ptr += length;
323            size -= length;
324        }
325
326        LOGI("AVC profile = %d (%s), level = %d",
327             (int)profile, AVCProfileToString(profile), (int)level / 10);
328
329        if (!strcmp(componentName, "OMX.TI.Video.Decoder")
330            && (profile != kAVCProfileBaseline || level > 39)) {
331            // This stream exceeds the decoder's capabilities.
332
333            LOGE("Profile and/or level exceed the decoder's capabilities.");
334            return NULL;
335        }
336    }
337
338    if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_NB, mime)) {
339        codec->setAMRFormat();
340    }
341    if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_WB, mime)) {
342        codec->setAMRWBFormat();
343    }
344    if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AAC, mime)) {
345        int32_t numChannels, sampleRate;
346        CHECK(meta->findInt32(kKeyChannelCount, &numChannels));
347        CHECK(meta->findInt32(kKeySampleRate, &sampleRate));
348
349        codec->setAACFormat(numChannels, sampleRate);
350    }
351    if (!strncasecmp(mime, "video/", 6)) {
352        int32_t width, height;
353        bool success = meta->findInt32(kKeyWidth, &width);
354        success = success && meta->findInt32(kKeyHeight, &height);
355        CHECK(success);
356
357        if (createEncoder) {
358            codec->setVideoInputFormat(mime, width, height);
359        } else {
360            codec->setVideoOutputFormat(mime, width, height);
361        }
362    }
363    if (!strcasecmp(mime, MEDIA_MIMETYPE_IMAGE_JPEG)
364        && !strcmp(componentName, "OMX.TI.JPEG.decode")) {
365        OMX_COLOR_FORMATTYPE format =
366            OMX_COLOR_Format32bitARGB8888;
367            // OMX_COLOR_FormatYUV420PackedPlanar;
368            // OMX_COLOR_FormatCbYCrY;
369            // OMX_COLOR_FormatYUV411Planar;
370
371        int32_t width, height;
372        bool success = meta->findInt32(kKeyWidth, &width);
373        success = success && meta->findInt32(kKeyHeight, &height);
374
375        int32_t compressedSize;
376        success = success && meta->findInt32(
377                kKeyMaxInputSize, &compressedSize);
378
379        CHECK(success);
380        CHECK(compressedSize > 0);
381
382        codec->setImageOutputFormat(format, width, height);
383        codec->setJPEGInputFormat(width, height, (OMX_U32)compressedSize);
384    }
385
386    int32_t maxInputSize;
387    if (createEncoder && meta->findInt32(kKeyMaxInputSize, &maxInputSize)) {
388        codec->setMinBufferSize(kPortIndexInput, (OMX_U32)maxInputSize);
389    }
390
391    if (!strcmp(componentName, "OMX.TI.AMR.encode")
392        || !strcmp(componentName, "OMX.TI.WBAMR.encode")) {
393        codec->setMinBufferSize(kPortIndexOutput, 8192);  // XXX
394    }
395
396    codec->initOutputFormat(meta);
397
398    return codec;
399}
400
401void OMXCodec::setMinBufferSize(OMX_U32 portIndex, OMX_U32 size) {
402    OMX_PARAM_PORTDEFINITIONTYPE def;
403    InitOMXParams(&def);
404    def.nPortIndex = portIndex;
405
406    status_t err = mOMX->get_parameter(
407            mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
408    CHECK_EQ(err, OK);
409
410    if (def.nBufferSize < size) {
411        def.nBufferSize = size;
412
413    }
414
415    err = mOMX->set_parameter(
416            mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
417    CHECK_EQ(err, OK);
418}
419
420status_t OMXCodec::setVideoPortFormatType(
421        OMX_U32 portIndex,
422        OMX_VIDEO_CODINGTYPE compressionFormat,
423        OMX_COLOR_FORMATTYPE colorFormat) {
424    OMX_VIDEO_PARAM_PORTFORMATTYPE format;
425    InitOMXParams(&format);
426    format.nPortIndex = portIndex;
427    format.nIndex = 0;
428    bool found = false;
429
430    OMX_U32 index = 0;
431    for (;;) {
432        format.nIndex = index;
433        status_t err = mOMX->get_parameter(
434                mNode, OMX_IndexParamVideoPortFormat,
435                &format, sizeof(format));
436
437        if (err != OK) {
438            return err;
439        }
440
441        // The following assertion is violated by TI's video decoder.
442        // CHECK_EQ(format.nIndex, index);
443
444#if 1
445        CODEC_LOGI("portIndex: %ld, index: %ld, eCompressionFormat=%d eColorFormat=%d",
446             portIndex,
447             index, format.eCompressionFormat, format.eColorFormat);
448#endif
449
450        if (!strcmp("OMX.TI.Video.encoder", mComponentName)) {
451            if (portIndex == kPortIndexInput
452                    && colorFormat == format.eColorFormat) {
453                // eCompressionFormat does not seem right.
454                found = true;
455                break;
456            }
457            if (portIndex == kPortIndexOutput
458                    && compressionFormat == format.eCompressionFormat) {
459                // eColorFormat does not seem right.
460                found = true;
461                break;
462            }
463        }
464
465        if (format.eCompressionFormat == compressionFormat
466            && format.eColorFormat == colorFormat) {
467            found = true;
468            break;
469        }
470
471        ++index;
472    }
473
474    if (!found) {
475        return UNKNOWN_ERROR;
476    }
477
478    CODEC_LOGI("found a match.");
479    status_t err = mOMX->set_parameter(
480            mNode, OMX_IndexParamVideoPortFormat,
481            &format, sizeof(format));
482
483    return err;
484}
485
486void OMXCodec::setVideoInputFormat(
487        const char *mime, OMX_U32 width, OMX_U32 height) {
488    CODEC_LOGI("setVideoInputFormat width=%ld, height=%ld", width, height);
489
490    OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
491    if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
492        compressionFormat = OMX_VIDEO_CodingAVC;
493    } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
494        compressionFormat = OMX_VIDEO_CodingMPEG4;
495    } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
496        compressionFormat = OMX_VIDEO_CodingH263;
497    } else {
498        LOGE("Not a supported video mime type: %s", mime);
499        CHECK(!"Should not be here. Not a supported video mime type.");
500    }
501
502    OMX_COLOR_FORMATTYPE colorFormat =
503        0 ? OMX_COLOR_FormatYCbYCr : OMX_COLOR_FormatCbYCrY;
504
505    if (!strncmp("OMX.qcom.video.encoder.", mComponentName, 23)) {
506        colorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
507    }
508
509    setVideoPortFormatType(
510            kPortIndexInput, OMX_VIDEO_CodingUnused,
511            colorFormat);
512
513    setVideoPortFormatType(
514            kPortIndexOutput, compressionFormat, OMX_COLOR_FormatUnused);
515
516    OMX_PARAM_PORTDEFINITIONTYPE def;
517    InitOMXParams(&def);
518    def.nPortIndex = kPortIndexOutput;
519
520    OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
521
522    status_t err = mOMX->get_parameter(
523            mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
524
525    CHECK_EQ(err, OK);
526    CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
527
528    video_def->nFrameWidth = width;
529    video_def->nFrameHeight = height;
530
531    video_def->eCompressionFormat = compressionFormat;
532    video_def->eColorFormat = OMX_COLOR_FormatUnused;
533
534    err = mOMX->set_parameter(
535            mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
536    CHECK_EQ(err, OK);
537
538    ////////////////////////////////////////////////////////////////////////////
539
540    InitOMXParams(&def);
541    def.nPortIndex = kPortIndexInput;
542
543    err = mOMX->get_parameter(
544            mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
545    CHECK_EQ(err, OK);
546
547    def.nBufferSize = (width * height * 2); // (width * height * 3) / 2;
548    CODEC_LOGI("Setting nBufferSize = %ld", def.nBufferSize);
549
550    CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
551
552    video_def->nFrameWidth = width;
553    video_def->nFrameHeight = height;
554    video_def->eCompressionFormat = OMX_VIDEO_CodingUnused;
555    video_def->eColorFormat = colorFormat;
556
557    err = mOMX->set_parameter(
558            mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
559    CHECK_EQ(err, OK);
560}
561
562void OMXCodec::setVideoOutputFormat(
563        const char *mime, OMX_U32 width, OMX_U32 height) {
564    CODEC_LOGI("setVideoOutputFormat width=%ld, height=%ld", width, height);
565
566    OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused;
567    if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
568        compressionFormat = OMX_VIDEO_CodingAVC;
569    } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
570        compressionFormat = OMX_VIDEO_CodingMPEG4;
571    } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
572        compressionFormat = OMX_VIDEO_CodingH263;
573    } else {
574        LOGE("Not a supported video mime type: %s", mime);
575        CHECK(!"Should not be here. Not a supported video mime type.");
576    }
577
578    setVideoPortFormatType(
579            kPortIndexInput, compressionFormat, OMX_COLOR_FormatUnused);
580
581#if 1
582    {
583        OMX_VIDEO_PARAM_PORTFORMATTYPE format;
584        InitOMXParams(&format);
585        format.nPortIndex = kPortIndexOutput;
586        format.nIndex = 0;
587
588        status_t err = mOMX->get_parameter(
589                mNode, OMX_IndexParamVideoPortFormat,
590                &format, sizeof(format));
591        CHECK_EQ(err, OK);
592        CHECK_EQ(format.eCompressionFormat, OMX_VIDEO_CodingUnused);
593
594        static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
595
596        CHECK(format.eColorFormat == OMX_COLOR_FormatYUV420Planar
597               || format.eColorFormat == OMX_COLOR_FormatYUV420SemiPlanar
598               || format.eColorFormat == OMX_COLOR_FormatCbYCrY
599               || format.eColorFormat == OMX_QCOM_COLOR_FormatYVU420SemiPlanar);
600
601        err = mOMX->set_parameter(
602                mNode, OMX_IndexParamVideoPortFormat,
603                &format, sizeof(format));
604        CHECK_EQ(err, OK);
605    }
606#endif
607
608    OMX_PARAM_PORTDEFINITIONTYPE def;
609    InitOMXParams(&def);
610    def.nPortIndex = kPortIndexInput;
611
612    OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
613
614    status_t err = mOMX->get_parameter(
615            mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
616
617    CHECK_EQ(err, OK);
618
619#if 1
620    // XXX Need a (much) better heuristic to compute input buffer sizes.
621    const size_t X = 64 * 1024;
622    if (def.nBufferSize < X) {
623        def.nBufferSize = X;
624    }
625#endif
626
627    CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
628
629    video_def->nFrameWidth = width;
630    video_def->nFrameHeight = height;
631
632    video_def->eColorFormat = OMX_COLOR_FormatUnused;
633
634    err = mOMX->set_parameter(
635            mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
636    CHECK_EQ(err, OK);
637
638    ////////////////////////////////////////////////////////////////////////////
639
640    InitOMXParams(&def);
641    def.nPortIndex = kPortIndexOutput;
642
643    err = mOMX->get_parameter(
644            mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
645    CHECK_EQ(err, OK);
646    CHECK_EQ(def.eDomain, OMX_PortDomainVideo);
647
648#if 0
649    def.nBufferSize =
650        (((width + 15) & -16) * ((height + 15) & -16) * 3) / 2;  // YUV420
651#endif
652
653    video_def->nFrameWidth = width;
654    video_def->nFrameHeight = height;
655
656    err = mOMX->set_parameter(
657            mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
658    CHECK_EQ(err, OK);
659}
660
661
662OMXCodec::OMXCodec(
663        const sp<IOMX> &omx, IOMX::node_id node, uint32_t quirks,
664        bool isEncoder,
665        const char *mime,
666        const char *componentName,
667        const sp<MediaSource> &source)
668    : mOMX(omx),
669      mNode(node),
670      mQuirks(quirks),
671      mIsEncoder(isEncoder),
672      mMIME(strdup(mime)),
673      mComponentName(strdup(componentName)),
674      mSource(source),
675      mCodecSpecificDataIndex(0),
676      mState(LOADED),
677      mInitialBufferSubmit(true),
678      mSignalledEOS(false),
679      mNoMoreOutputData(false),
680      mSeekTimeUs(-1) {
681    mPortStatus[kPortIndexInput] = ENABLED;
682    mPortStatus[kPortIndexOutput] = ENABLED;
683
684    mObserver = new OMXCodecObserver(this);
685    mOMX->observe_node(mNode, mObserver);
686
687    setComponentRole();
688}
689
690// static
691void OMXCodec::setComponentRole(
692        const sp<IOMX> &omx, IOMX::node_id node, bool isEncoder,
693        const char *mime) {
694    struct MimeToRole {
695        const char *mime;
696        const char *decoderRole;
697        const char *encoderRole;
698    };
699
700    static const MimeToRole kMimeToRole[] = {
701        { MEDIA_MIMETYPE_AUDIO_MPEG,
702            "audio_decoder.mp3", "audio_encoder.mp3" },
703        { MEDIA_MIMETYPE_AUDIO_AMR_NB,
704            "audio_decoder.amrnb", "audio_encoder.amrnb" },
705        { MEDIA_MIMETYPE_AUDIO_AMR_WB,
706            "audio_decoder.amrwb", "audio_encoder.amrwb" },
707        { MEDIA_MIMETYPE_AUDIO_AAC,
708            "audio_decoder.aac", "audio_encoder.aac" },
709        { MEDIA_MIMETYPE_VIDEO_AVC,
710            "video_decoder.avc", "video_encoder.avc" },
711        { MEDIA_MIMETYPE_VIDEO_MPEG4,
712            "video_decoder.mpeg4", "video_encoder.mpeg4" },
713        { MEDIA_MIMETYPE_VIDEO_H263,
714            "video_decoder.h263", "video_encoder.h263" },
715    };
716
717    static const size_t kNumMimeToRole =
718        sizeof(kMimeToRole) / sizeof(kMimeToRole[0]);
719
720    size_t i;
721    for (i = 0; i < kNumMimeToRole; ++i) {
722        if (!strcasecmp(mime, kMimeToRole[i].mime)) {
723            break;
724        }
725    }
726
727    if (i == kNumMimeToRole) {
728        return;
729    }
730
731    const char *role =
732        isEncoder ? kMimeToRole[i].encoderRole
733                  : kMimeToRole[i].decoderRole;
734
735    if (role != NULL) {
736        OMX_PARAM_COMPONENTROLETYPE roleParams;
737        InitOMXParams(&roleParams);
738
739        strncpy((char *)roleParams.cRole,
740                role, OMX_MAX_STRINGNAME_SIZE - 1);
741
742        roleParams.cRole[OMX_MAX_STRINGNAME_SIZE - 1] = '\0';
743
744        status_t err = omx->set_parameter(
745                node, OMX_IndexParamStandardComponentRole,
746                &roleParams, sizeof(roleParams));
747
748        if (err != OK) {
749            LOGW("Failed to set standard component role '%s'.", role);
750        }
751    }
752}
753
754void OMXCodec::setComponentRole() {
755    setComponentRole(mOMX, mNode, mIsEncoder, mMIME);
756}
757
758OMXCodec::~OMXCodec() {
759    CHECK(mState == LOADED || mState == ERROR);
760
761    status_t err = mOMX->observe_node(mNode, NULL);
762    CHECK_EQ(err, OK);
763
764    err = mOMX->free_node(mNode);
765    CHECK_EQ(err, OK);
766
767    mNode = NULL;
768    setState(DEAD);
769
770    clearCodecSpecificData();
771
772    free(mComponentName);
773    mComponentName = NULL;
774
775    free(mMIME);
776    mMIME = NULL;
777}
778
779status_t OMXCodec::init() {
780    // mLock is held.
781
782    CHECK_EQ(mState, LOADED);
783
784    status_t err;
785    if (!(mQuirks & kRequiresLoadedToIdleAfterAllocation)) {
786        err = mOMX->send_command(mNode, OMX_CommandStateSet, OMX_StateIdle);
787        CHECK_EQ(err, OK);
788        setState(LOADED_TO_IDLE);
789    }
790
791    err = allocateBuffers();
792    CHECK_EQ(err, OK);
793
794    if (mQuirks & kRequiresLoadedToIdleAfterAllocation) {
795        err = mOMX->send_command(mNode, OMX_CommandStateSet, OMX_StateIdle);
796        CHECK_EQ(err, OK);
797
798        setState(LOADED_TO_IDLE);
799    }
800
801    while (mState != EXECUTING && mState != ERROR) {
802        mAsyncCompletion.wait(mLock);
803    }
804
805    return mState == ERROR ? UNKNOWN_ERROR : OK;
806}
807
808// static
809bool OMXCodec::isIntermediateState(State state) {
810    return state == LOADED_TO_IDLE
811        || state == IDLE_TO_EXECUTING
812        || state == EXECUTING_TO_IDLE
813        || state == IDLE_TO_LOADED
814        || state == RECONFIGURING;
815}
816
817status_t OMXCodec::allocateBuffers() {
818    status_t err = allocateBuffersOnPort(kPortIndexInput);
819
820    if (err != OK) {
821        return err;
822    }
823
824    return allocateBuffersOnPort(kPortIndexOutput);
825}
826
827status_t OMXCodec::allocateBuffersOnPort(OMX_U32 portIndex) {
828    OMX_PARAM_PORTDEFINITIONTYPE def;
829    InitOMXParams(&def);
830    def.nPortIndex = portIndex;
831
832    status_t err = mOMX->get_parameter(
833            mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
834
835    if (err != OK) {
836        return err;
837    }
838
839    size_t totalSize = def.nBufferCountActual * def.nBufferSize;
840    mDealer[portIndex] = new MemoryDealer(totalSize);
841
842    for (OMX_U32 i = 0; i < def.nBufferCountActual; ++i) {
843        sp<IMemory> mem = mDealer[portIndex]->allocate(def.nBufferSize);
844        CHECK(mem.get() != NULL);
845
846        IOMX::buffer_id buffer;
847        if (portIndex == kPortIndexInput
848                && (mQuirks & kRequiresAllocateBufferOnInputPorts)) {
849            err = mOMX->allocate_buffer_with_backup(
850                    mNode, portIndex, mem, &buffer);
851        } else if (portIndex == kPortIndexOutput
852                && (mQuirks & kRequiresAllocateBufferOnOutputPorts)) {
853            err = mOMX->allocate_buffer_with_backup(
854                    mNode, portIndex, mem, &buffer);
855        } else {
856            err = mOMX->use_buffer(mNode, portIndex, mem, &buffer);
857        }
858
859        if (err != OK) {
860            LOGE("allocate_buffer_with_backup failed");
861            return err;
862        }
863
864        BufferInfo info;
865        info.mBuffer = buffer;
866        info.mOwnedByComponent = false;
867        info.mMem = mem;
868        info.mMediaBuffer = NULL;
869
870        if (portIndex == kPortIndexOutput) {
871            info.mMediaBuffer = new MediaBuffer(mem->pointer(), mem->size());
872            info.mMediaBuffer->setObserver(this);
873        }
874
875        mPortBuffers[portIndex].push(info);
876
877        CODEC_LOGV("allocated buffer %p on %s port", buffer,
878             portIndex == kPortIndexInput ? "input" : "output");
879    }
880
881    dumpPortStatus(portIndex);
882
883    return OK;
884}
885
886void OMXCodec::on_message(const omx_message &msg) {
887    Mutex::Autolock autoLock(mLock);
888
889    switch (msg.type) {
890        case omx_message::EVENT:
891        {
892            onEvent(
893                 msg.u.event_data.event, msg.u.event_data.data1,
894                 msg.u.event_data.data2);
895
896            break;
897        }
898
899        case omx_message::EMPTY_BUFFER_DONE:
900        {
901            IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
902
903            CODEC_LOGV("EMPTY_BUFFER_DONE(buffer: %p)", buffer);
904
905            Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
906            size_t i = 0;
907            while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
908                ++i;
909            }
910
911            CHECK(i < buffers->size());
912            if (!(*buffers)[i].mOwnedByComponent) {
913                LOGW("We already own input buffer %p, yet received "
914                     "an EMPTY_BUFFER_DONE.", buffer);
915            }
916
917            buffers->editItemAt(i).mOwnedByComponent = false;
918
919            if (mPortStatus[kPortIndexInput] == DISABLING) {
920                CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
921
922                status_t err =
923                    mOMX->free_buffer(mNode, kPortIndexInput, buffer);
924                CHECK_EQ(err, OK);
925
926                buffers->removeAt(i);
927            } else if (mPortStatus[kPortIndexInput] != SHUTTING_DOWN) {
928                CHECK_EQ(mPortStatus[kPortIndexInput], ENABLED);
929                drainInputBuffer(&buffers->editItemAt(i));
930            }
931
932            break;
933        }
934
935        case omx_message::FILL_BUFFER_DONE:
936        {
937            IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer;
938            OMX_U32 flags = msg.u.extended_buffer_data.flags;
939
940            CODEC_LOGV("FILL_BUFFER_DONE(buffer: %p, size: %ld, flags: 0x%08lx)",
941                 buffer,
942                 msg.u.extended_buffer_data.range_length,
943                 flags);
944
945            CODEC_LOGV("FILL_BUFFER_DONE(timestamp: %lld us (%.2f secs))",
946                 msg.u.extended_buffer_data.timestamp,
947                 msg.u.extended_buffer_data.timestamp / 1E6);
948
949            Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
950            size_t i = 0;
951            while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) {
952                ++i;
953            }
954
955            CHECK(i < buffers->size());
956            BufferInfo *info = &buffers->editItemAt(i);
957
958            if (!info->mOwnedByComponent) {
959                LOGW("We already own output buffer %p, yet received "
960                     "a FILL_BUFFER_DONE.", buffer);
961            }
962
963            info->mOwnedByComponent = false;
964
965            if (mPortStatus[kPortIndexOutput] == DISABLING) {
966                CODEC_LOGV("Port is disabled, freeing buffer %p", buffer);
967
968                status_t err =
969                    mOMX->free_buffer(mNode, kPortIndexOutput, buffer);
970                CHECK_EQ(err, OK);
971
972                buffers->removeAt(i);
973            } else if (mPortStatus[kPortIndexOutput] == ENABLED
974                       && (flags & OMX_BUFFERFLAG_EOS)) {
975                CODEC_LOGV("No more output data.");
976                mNoMoreOutputData = true;
977                mBufferFilled.signal();
978            } else if (mPortStatus[kPortIndexOutput] != SHUTTING_DOWN) {
979                CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
980
981                MediaBuffer *buffer = info->mMediaBuffer;
982
983                buffer->set_range(
984                        msg.u.extended_buffer_data.range_offset,
985                        msg.u.extended_buffer_data.range_length);
986
987                buffer->meta_data()->clear();
988
989                buffer->meta_data()->setInt32(
990                        kKeyTimeUnits,
991                        (msg.u.extended_buffer_data.timestamp + 500) / 1000);
992
993                buffer->meta_data()->setInt32(
994                        kKeyTimeScale, 1000);
995
996                if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_SYNCFRAME) {
997                    buffer->meta_data()->setInt32(kKeyIsSyncFrame, true);
998                }
999
1000                buffer->meta_data()->setPointer(
1001                        kKeyPlatformPrivate,
1002                        msg.u.extended_buffer_data.platform_private);
1003
1004                buffer->meta_data()->setPointer(
1005                        kKeyBufferID,
1006                        msg.u.extended_buffer_data.buffer);
1007
1008                mFilledBuffers.push_back(i);
1009                mBufferFilled.signal();
1010            }
1011
1012            break;
1013        }
1014
1015        default:
1016        {
1017            CHECK(!"should not be here.");
1018            break;
1019        }
1020    }
1021}
1022
1023void OMXCodec::onEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2) {
1024    switch (event) {
1025        case OMX_EventCmdComplete:
1026        {
1027            onCmdComplete((OMX_COMMANDTYPE)data1, data2);
1028            break;
1029        }
1030
1031        case OMX_EventError:
1032        {
1033            LOGE("ERROR(%ld, %ld)", data1, data2);
1034
1035            setState(ERROR);
1036            break;
1037        }
1038
1039        case OMX_EventPortSettingsChanged:
1040        {
1041            onPortSettingsChanged(data1);
1042            break;
1043        }
1044
1045        case OMX_EventBufferFlag:
1046        {
1047            CODEC_LOGV("EVENT_BUFFER_FLAG(%ld)", data1);
1048
1049            if (data1 == kPortIndexOutput) {
1050                mNoMoreOutputData = true;
1051            }
1052            break;
1053        }
1054
1055        default:
1056        {
1057            CODEC_LOGV("EVENT(%d, %ld, %ld)", event, data1, data2);
1058            break;
1059        }
1060    }
1061}
1062
1063void OMXCodec::onCmdComplete(OMX_COMMANDTYPE cmd, OMX_U32 data) {
1064    switch (cmd) {
1065        case OMX_CommandStateSet:
1066        {
1067            onStateChange((OMX_STATETYPE)data);
1068            break;
1069        }
1070
1071        case OMX_CommandPortDisable:
1072        {
1073            OMX_U32 portIndex = data;
1074            CODEC_LOGV("PORT_DISABLED(%ld)", portIndex);
1075
1076            CHECK(mState == EXECUTING || mState == RECONFIGURING);
1077            CHECK_EQ(mPortStatus[portIndex], DISABLING);
1078            CHECK_EQ(mPortBuffers[portIndex].size(), 0);
1079
1080            mPortStatus[portIndex] = DISABLED;
1081
1082            if (mState == RECONFIGURING) {
1083                CHECK_EQ(portIndex, kPortIndexOutput);
1084
1085                enablePortAsync(portIndex);
1086
1087                status_t err = allocateBuffersOnPort(portIndex);
1088                CHECK_EQ(err, OK);
1089            }
1090            break;
1091        }
1092
1093        case OMX_CommandPortEnable:
1094        {
1095            OMX_U32 portIndex = data;
1096            CODEC_LOGV("PORT_ENABLED(%ld)", portIndex);
1097
1098            CHECK(mState == EXECUTING || mState == RECONFIGURING);
1099            CHECK_EQ(mPortStatus[portIndex], ENABLING);
1100
1101            mPortStatus[portIndex] = ENABLED;
1102
1103            if (mState == RECONFIGURING) {
1104                CHECK_EQ(portIndex, kPortIndexOutput);
1105
1106                setState(EXECUTING);
1107
1108                fillOutputBuffers();
1109            }
1110            break;
1111        }
1112
1113        case OMX_CommandFlush:
1114        {
1115            OMX_U32 portIndex = data;
1116
1117            CODEC_LOGV("FLUSH_DONE(%ld)", portIndex);
1118
1119            CHECK_EQ(mPortStatus[portIndex], SHUTTING_DOWN);
1120            mPortStatus[portIndex] = ENABLED;
1121
1122            CHECK_EQ(countBuffersWeOwn(mPortBuffers[portIndex]),
1123                     mPortBuffers[portIndex].size());
1124
1125            if (mState == RECONFIGURING) {
1126                CHECK_EQ(portIndex, kPortIndexOutput);
1127
1128                disablePortAsync(portIndex);
1129            } else if (mState == EXECUTING_TO_IDLE) {
1130                if (mPortStatus[kPortIndexInput] == ENABLED
1131                    && mPortStatus[kPortIndexOutput] == ENABLED) {
1132                    CODEC_LOGV("Finished flushing both ports, now completing "
1133                         "transition from EXECUTING to IDLE.");
1134
1135                    mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
1136                    mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
1137
1138                    status_t err =
1139                        mOMX->send_command(mNode, OMX_CommandStateSet, OMX_StateIdle);
1140                    CHECK_EQ(err, OK);
1141                }
1142            } else {
1143                // We're flushing both ports in preparation for seeking.
1144
1145                if (mPortStatus[kPortIndexInput] == ENABLED
1146                    && mPortStatus[kPortIndexOutput] == ENABLED) {
1147                    CODEC_LOGV("Finished flushing both ports, now continuing from"
1148                         " seek-time.");
1149
1150                    drainInputBuffers();
1151                    fillOutputBuffers();
1152                }
1153            }
1154
1155            break;
1156        }
1157
1158        default:
1159        {
1160            CODEC_LOGV("CMD_COMPLETE(%d, %ld)", cmd, data);
1161            break;
1162        }
1163    }
1164}
1165
1166void OMXCodec::onStateChange(OMX_STATETYPE newState) {
1167    switch (newState) {
1168        case OMX_StateIdle:
1169        {
1170            CODEC_LOGV("Now Idle.");
1171            if (mState == LOADED_TO_IDLE) {
1172                status_t err = mOMX->send_command(
1173                        mNode, OMX_CommandStateSet, OMX_StateExecuting);
1174
1175                CHECK_EQ(err, OK);
1176
1177                setState(IDLE_TO_EXECUTING);
1178            } else {
1179                CHECK_EQ(mState, EXECUTING_TO_IDLE);
1180
1181                CHECK_EQ(
1182                    countBuffersWeOwn(mPortBuffers[kPortIndexInput]),
1183                    mPortBuffers[kPortIndexInput].size());
1184
1185                CHECK_EQ(
1186                    countBuffersWeOwn(mPortBuffers[kPortIndexOutput]),
1187                    mPortBuffers[kPortIndexOutput].size());
1188
1189                status_t err = mOMX->send_command(
1190                        mNode, OMX_CommandStateSet, OMX_StateLoaded);
1191
1192                CHECK_EQ(err, OK);
1193
1194                err = freeBuffersOnPort(kPortIndexInput);
1195                CHECK_EQ(err, OK);
1196
1197                err = freeBuffersOnPort(kPortIndexOutput);
1198                CHECK_EQ(err, OK);
1199
1200                mPortStatus[kPortIndexInput] = ENABLED;
1201                mPortStatus[kPortIndexOutput] = ENABLED;
1202
1203                setState(IDLE_TO_LOADED);
1204            }
1205            break;
1206        }
1207
1208        case OMX_StateExecuting:
1209        {
1210            CHECK_EQ(mState, IDLE_TO_EXECUTING);
1211
1212            CODEC_LOGV("Now Executing.");
1213
1214            setState(EXECUTING);
1215
1216            // Buffers will be submitted to the component in the first
1217            // call to OMXCodec::read as mInitialBufferSubmit is true at
1218            // this point. This ensures that this on_message call returns,
1219            // releases the lock and ::init can notice the state change and
1220            // itself return.
1221            break;
1222        }
1223
1224        case OMX_StateLoaded:
1225        {
1226            CHECK_EQ(mState, IDLE_TO_LOADED);
1227
1228            CODEC_LOGV("Now Loaded.");
1229
1230            setState(LOADED);
1231            break;
1232        }
1233
1234        default:
1235        {
1236            CHECK(!"should not be here.");
1237            break;
1238        }
1239    }
1240}
1241
1242// static
1243size_t OMXCodec::countBuffersWeOwn(const Vector<BufferInfo> &buffers) {
1244    size_t n = 0;
1245    for (size_t i = 0; i < buffers.size(); ++i) {
1246        if (!buffers[i].mOwnedByComponent) {
1247            ++n;
1248        }
1249    }
1250
1251    return n;
1252}
1253
1254status_t OMXCodec::freeBuffersOnPort(
1255        OMX_U32 portIndex, bool onlyThoseWeOwn) {
1256    Vector<BufferInfo> *buffers = &mPortBuffers[portIndex];
1257
1258    status_t stickyErr = OK;
1259
1260    for (size_t i = buffers->size(); i-- > 0;) {
1261        BufferInfo *info = &buffers->editItemAt(i);
1262
1263        if (onlyThoseWeOwn && info->mOwnedByComponent) {
1264            continue;
1265        }
1266
1267        CHECK_EQ(info->mOwnedByComponent, false);
1268
1269        CODEC_LOGV("freeing buffer %p on port %ld", info->mBuffer, portIndex);
1270
1271        status_t err =
1272            mOMX->free_buffer(mNode, portIndex, info->mBuffer);
1273
1274        if (err != OK) {
1275            stickyErr = err;
1276        }
1277
1278        if (info->mMediaBuffer != NULL) {
1279            info->mMediaBuffer->setObserver(NULL);
1280
1281            // Make sure nobody but us owns this buffer at this point.
1282            CHECK_EQ(info->mMediaBuffer->refcount(), 0);
1283
1284            info->mMediaBuffer->release();
1285        }
1286
1287        buffers->removeAt(i);
1288    }
1289
1290    CHECK(onlyThoseWeOwn || buffers->isEmpty());
1291
1292    return stickyErr;
1293}
1294
1295void OMXCodec::onPortSettingsChanged(OMX_U32 portIndex) {
1296    CODEC_LOGV("PORT_SETTINGS_CHANGED(%ld)", portIndex);
1297
1298    CHECK_EQ(mState, EXECUTING);
1299    CHECK_EQ(portIndex, kPortIndexOutput);
1300    setState(RECONFIGURING);
1301
1302    if (mQuirks & kNeedsFlushBeforeDisable) {
1303        if (!flushPortAsync(portIndex)) {
1304            onCmdComplete(OMX_CommandFlush, portIndex);
1305        }
1306    } else {
1307        disablePortAsync(portIndex);
1308    }
1309}
1310
1311bool OMXCodec::flushPortAsync(OMX_U32 portIndex) {
1312    CHECK(mState == EXECUTING || mState == RECONFIGURING
1313            || mState == EXECUTING_TO_IDLE);
1314
1315    CODEC_LOGV("flushPortAsync(%ld): we own %d out of %d buffers already.",
1316         portIndex, countBuffersWeOwn(mPortBuffers[portIndex]),
1317         mPortBuffers[portIndex].size());
1318
1319    CHECK_EQ(mPortStatus[portIndex], ENABLED);
1320    mPortStatus[portIndex] = SHUTTING_DOWN;
1321
1322    if ((mQuirks & kRequiresFlushCompleteEmulation)
1323        && countBuffersWeOwn(mPortBuffers[portIndex])
1324                == mPortBuffers[portIndex].size()) {
1325        // No flush is necessary and this component fails to send a
1326        // flush-complete event in this case.
1327
1328        return false;
1329    }
1330
1331    status_t err =
1332        mOMX->send_command(mNode, OMX_CommandFlush, portIndex);
1333    CHECK_EQ(err, OK);
1334
1335    return true;
1336}
1337
1338void OMXCodec::disablePortAsync(OMX_U32 portIndex) {
1339    CHECK(mState == EXECUTING || mState == RECONFIGURING);
1340
1341    CHECK_EQ(mPortStatus[portIndex], ENABLED);
1342    mPortStatus[portIndex] = DISABLING;
1343
1344    status_t err =
1345        mOMX->send_command(mNode, OMX_CommandPortDisable, portIndex);
1346    CHECK_EQ(err, OK);
1347
1348    freeBuffersOnPort(portIndex, true);
1349}
1350
1351void OMXCodec::enablePortAsync(OMX_U32 portIndex) {
1352    CHECK(mState == EXECUTING || mState == RECONFIGURING);
1353
1354    CHECK_EQ(mPortStatus[portIndex], DISABLED);
1355    mPortStatus[portIndex] = ENABLING;
1356
1357    status_t err =
1358        mOMX->send_command(mNode, OMX_CommandPortEnable, portIndex);
1359    CHECK_EQ(err, OK);
1360}
1361
1362void OMXCodec::fillOutputBuffers() {
1363    CHECK_EQ(mState, EXECUTING);
1364
1365    Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1366    for (size_t i = 0; i < buffers->size(); ++i) {
1367        fillOutputBuffer(&buffers->editItemAt(i));
1368    }
1369}
1370
1371void OMXCodec::drainInputBuffers() {
1372    CHECK(mState == EXECUTING || mState == RECONFIGURING);
1373
1374    Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
1375    for (size_t i = 0; i < buffers->size(); ++i) {
1376        drainInputBuffer(&buffers->editItemAt(i));
1377    }
1378}
1379
1380void OMXCodec::drainInputBuffer(BufferInfo *info) {
1381    CHECK_EQ(info->mOwnedByComponent, false);
1382
1383    if (mSignalledEOS) {
1384        return;
1385    }
1386
1387    if (mCodecSpecificDataIndex < mCodecSpecificData.size()) {
1388        const CodecSpecificData *specific =
1389            mCodecSpecificData[mCodecSpecificDataIndex];
1390
1391        size_t size = specific->mSize;
1392
1393        if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mMIME)
1394                && !(mQuirks & kWantsNALFragments)) {
1395            static const uint8_t kNALStartCode[4] =
1396                    { 0x00, 0x00, 0x00, 0x01 };
1397
1398            CHECK(info->mMem->size() >= specific->mSize + 4);
1399
1400            size += 4;
1401
1402            memcpy(info->mMem->pointer(), kNALStartCode, 4);
1403            memcpy((uint8_t *)info->mMem->pointer() + 4,
1404                   specific->mData, specific->mSize);
1405        } else {
1406            CHECK(info->mMem->size() >= specific->mSize);
1407            memcpy(info->mMem->pointer(), specific->mData, specific->mSize);
1408        }
1409
1410        mOMX->empty_buffer(
1411                mNode, info->mBuffer, 0, size,
1412                OMX_BUFFERFLAG_ENDOFFRAME | OMX_BUFFERFLAG_CODECCONFIG,
1413                0);
1414
1415        info->mOwnedByComponent = true;
1416
1417        ++mCodecSpecificDataIndex;
1418        return;
1419    }
1420
1421    MediaBuffer *srcBuffer;
1422    status_t err;
1423    if (mSeekTimeUs >= 0) {
1424        MediaSource::ReadOptions options;
1425        options.setSeekTo(mSeekTimeUs);
1426        mSeekTimeUs = -1;
1427
1428        err = mSource->read(&srcBuffer, &options);
1429    } else {
1430        err = mSource->read(&srcBuffer);
1431    }
1432
1433    OMX_U32 flags = OMX_BUFFERFLAG_ENDOFFRAME;
1434    OMX_TICKS timestamp = 0;
1435    size_t srcLength = 0;
1436
1437    if (err != OK) {
1438        CODEC_LOGV("signalling end of input stream.");
1439        flags |= OMX_BUFFERFLAG_EOS;
1440
1441        mSignalledEOS = true;
1442    } else {
1443        srcLength = srcBuffer->range_length();
1444
1445        if (info->mMem->size() < srcLength) {
1446            LOGE("info->mMem->size() = %d, srcLength = %d",
1447                 info->mMem->size(), srcLength);
1448        }
1449        CHECK(info->mMem->size() >= srcLength);
1450        memcpy(info->mMem->pointer(),
1451               (const uint8_t *)srcBuffer->data() + srcBuffer->range_offset(),
1452               srcLength);
1453
1454        int32_t units, scale;
1455        if (srcBuffer->meta_data()->findInt32(kKeyTimeUnits, &units)
1456            && srcBuffer->meta_data()->findInt32(kKeyTimeScale, &scale)) {
1457            timestamp = ((OMX_TICKS)units * 1000000) / scale;
1458
1459            CODEC_LOGV("Calling empty_buffer on buffer %p (length %d)",
1460                 info->mBuffer, srcLength);
1461            CODEC_LOGV("Calling empty_buffer with timestamp %lld us (%.2f secs)",
1462                 timestamp, timestamp / 1E6);
1463        }
1464    }
1465
1466    mOMX->empty_buffer(
1467            mNode, info->mBuffer, 0, srcLength,
1468            flags, timestamp);
1469
1470    info->mOwnedByComponent = true;
1471
1472    if (srcBuffer != NULL) {
1473        srcBuffer->release();
1474        srcBuffer = NULL;
1475    }
1476}
1477
1478void OMXCodec::fillOutputBuffer(BufferInfo *info) {
1479    CHECK_EQ(info->mOwnedByComponent, false);
1480
1481    if (mNoMoreOutputData) {
1482        CODEC_LOGV("There is no more output data available, not "
1483             "calling fillOutputBuffer");
1484        return;
1485    }
1486
1487    CODEC_LOGV("Calling fill_buffer on buffer %p", info->mBuffer);
1488    mOMX->fill_buffer(mNode, info->mBuffer);
1489
1490    info->mOwnedByComponent = true;
1491}
1492
1493void OMXCodec::drainInputBuffer(IOMX::buffer_id buffer) {
1494    Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput];
1495    for (size_t i = 0; i < buffers->size(); ++i) {
1496        if ((*buffers)[i].mBuffer == buffer) {
1497            drainInputBuffer(&buffers->editItemAt(i));
1498            return;
1499        }
1500    }
1501
1502    CHECK(!"should not be here.");
1503}
1504
1505void OMXCodec::fillOutputBuffer(IOMX::buffer_id buffer) {
1506    Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1507    for (size_t i = 0; i < buffers->size(); ++i) {
1508        if ((*buffers)[i].mBuffer == buffer) {
1509            fillOutputBuffer(&buffers->editItemAt(i));
1510            return;
1511        }
1512    }
1513
1514    CHECK(!"should not be here.");
1515}
1516
1517void OMXCodec::setState(State newState) {
1518    mState = newState;
1519    mAsyncCompletion.signal();
1520
1521    // This may cause some spurious wakeups but is necessary to
1522    // unblock the reader if we enter ERROR state.
1523    mBufferFilled.signal();
1524}
1525
1526void OMXCodec::setRawAudioFormat(
1527        OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels) {
1528    OMX_AUDIO_PARAM_PCMMODETYPE pcmParams;
1529    InitOMXParams(&pcmParams);
1530    pcmParams.nPortIndex = portIndex;
1531
1532    status_t err = mOMX->get_parameter(
1533            mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
1534
1535    CHECK_EQ(err, OK);
1536
1537    pcmParams.nChannels = numChannels;
1538    pcmParams.eNumData = OMX_NumericalDataSigned;
1539    pcmParams.bInterleaved = OMX_TRUE;
1540    pcmParams.nBitPerSample = 16;
1541    pcmParams.nSamplingRate = sampleRate;
1542    pcmParams.ePCMMode = OMX_AUDIO_PCMModeLinear;
1543
1544    if (numChannels == 1) {
1545        pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelCF;
1546    } else {
1547        CHECK_EQ(numChannels, 2);
1548
1549        pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelLF;
1550        pcmParams.eChannelMapping[1] = OMX_AUDIO_ChannelRF;
1551    }
1552
1553    err = mOMX->set_parameter(
1554            mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
1555
1556    CHECK_EQ(err, OK);
1557}
1558
1559void OMXCodec::setAMRFormat() {
1560    if (!mIsEncoder) {
1561        OMX_AUDIO_PARAM_AMRTYPE def;
1562        InitOMXParams(&def);
1563        def.nPortIndex = kPortIndexInput;
1564
1565        status_t err =
1566            mOMX->get_parameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
1567
1568        CHECK_EQ(err, OK);
1569
1570        def.eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatFSF;
1571        def.eAMRBandMode = OMX_AUDIO_AMRBandModeNB0;
1572
1573        err = mOMX->set_parameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
1574        CHECK_EQ(err, OK);
1575    }
1576
1577    ////////////////////////
1578
1579    if (mIsEncoder) {
1580        sp<MetaData> format = mSource->getFormat();
1581        int32_t sampleRate;
1582        int32_t numChannels;
1583        CHECK(format->findInt32(kKeySampleRate, &sampleRate));
1584        CHECK(format->findInt32(kKeyChannelCount, &numChannels));
1585
1586        setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
1587    }
1588}
1589
1590void OMXCodec::setAMRWBFormat() {
1591    if (!mIsEncoder) {
1592        OMX_AUDIO_PARAM_AMRTYPE def;
1593        InitOMXParams(&def);
1594        def.nPortIndex = kPortIndexInput;
1595
1596        status_t err =
1597            mOMX->get_parameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
1598
1599        CHECK_EQ(err, OK);
1600
1601        def.eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatFSF;
1602        def.eAMRBandMode = OMX_AUDIO_AMRBandModeWB0;
1603
1604        err = mOMX->set_parameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
1605        CHECK_EQ(err, OK);
1606    }
1607
1608    ////////////////////////
1609
1610    if (mIsEncoder) {
1611        sp<MetaData> format = mSource->getFormat();
1612        int32_t sampleRate;
1613        int32_t numChannels;
1614        CHECK(format->findInt32(kKeySampleRate, &sampleRate));
1615        CHECK(format->findInt32(kKeyChannelCount, &numChannels));
1616
1617        setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
1618    }
1619}
1620
1621void OMXCodec::setAACFormat(int32_t numChannels, int32_t sampleRate) {
1622    if (mIsEncoder) {
1623        setRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
1624    } else {
1625        OMX_AUDIO_PARAM_AACPROFILETYPE profile;
1626        InitOMXParams(&profile);
1627        profile.nPortIndex = kPortIndexInput;
1628
1629        status_t err = mOMX->get_parameter(
1630                mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
1631        CHECK_EQ(err, OK);
1632
1633        profile.nChannels = numChannels;
1634        profile.nSampleRate = sampleRate;
1635        profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4ADTS;
1636
1637        err = mOMX->set_parameter(
1638                mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
1639        CHECK_EQ(err, OK);
1640    }
1641}
1642
1643void OMXCodec::setImageOutputFormat(
1644        OMX_COLOR_FORMATTYPE format, OMX_U32 width, OMX_U32 height) {
1645    CODEC_LOGV("setImageOutputFormat(%ld, %ld)", width, height);
1646
1647#if 0
1648    OMX_INDEXTYPE index;
1649    status_t err = mOMX->get_extension_index(
1650            mNode, "OMX.TI.JPEG.decode.Config.OutputColorFormat", &index);
1651    CHECK_EQ(err, OK);
1652
1653    err = mOMX->set_config(mNode, index, &format, sizeof(format));
1654    CHECK_EQ(err, OK);
1655#endif
1656
1657    OMX_PARAM_PORTDEFINITIONTYPE def;
1658    InitOMXParams(&def);
1659    def.nPortIndex = kPortIndexOutput;
1660
1661    status_t err = mOMX->get_parameter(
1662            mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1663    CHECK_EQ(err, OK);
1664
1665    CHECK_EQ(def.eDomain, OMX_PortDomainImage);
1666
1667    OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
1668
1669    CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
1670    imageDef->eColorFormat = format;
1671    imageDef->nFrameWidth = width;
1672    imageDef->nFrameHeight = height;
1673
1674    switch (format) {
1675        case OMX_COLOR_FormatYUV420PackedPlanar:
1676        case OMX_COLOR_FormatYUV411Planar:
1677        {
1678            def.nBufferSize = (width * height * 3) / 2;
1679            break;
1680        }
1681
1682        case OMX_COLOR_FormatCbYCrY:
1683        {
1684            def.nBufferSize = width * height * 2;
1685            break;
1686        }
1687
1688        case OMX_COLOR_Format32bitARGB8888:
1689        {
1690            def.nBufferSize = width * height * 4;
1691            break;
1692        }
1693
1694        case OMX_COLOR_Format16bitARGB4444:
1695        case OMX_COLOR_Format16bitARGB1555:
1696        case OMX_COLOR_Format16bitRGB565:
1697        case OMX_COLOR_Format16bitBGR565:
1698        {
1699            def.nBufferSize = width * height * 2;
1700            break;
1701        }
1702
1703        default:
1704            CHECK(!"Should not be here. Unknown color format.");
1705            break;
1706    }
1707
1708    def.nBufferCountActual = def.nBufferCountMin;
1709
1710    err = mOMX->set_parameter(
1711            mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1712    CHECK_EQ(err, OK);
1713}
1714
1715void OMXCodec::setJPEGInputFormat(
1716        OMX_U32 width, OMX_U32 height, OMX_U32 compressedSize) {
1717    OMX_PARAM_PORTDEFINITIONTYPE def;
1718    InitOMXParams(&def);
1719    def.nPortIndex = kPortIndexInput;
1720
1721    status_t err = mOMX->get_parameter(
1722            mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1723    CHECK_EQ(err, OK);
1724
1725    CHECK_EQ(def.eDomain, OMX_PortDomainImage);
1726    OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
1727
1728    CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingJPEG);
1729    imageDef->nFrameWidth = width;
1730    imageDef->nFrameHeight = height;
1731
1732    def.nBufferSize = compressedSize;
1733    def.nBufferCountActual = def.nBufferCountMin;
1734
1735    err = mOMX->set_parameter(
1736            mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1737    CHECK_EQ(err, OK);
1738}
1739
1740void OMXCodec::addCodecSpecificData(const void *data, size_t size) {
1741    CodecSpecificData *specific =
1742        (CodecSpecificData *)malloc(sizeof(CodecSpecificData) + size - 1);
1743
1744    specific->mSize = size;
1745    memcpy(specific->mData, data, size);
1746
1747    mCodecSpecificData.push(specific);
1748}
1749
1750void OMXCodec::clearCodecSpecificData() {
1751    for (size_t i = 0; i < mCodecSpecificData.size(); ++i) {
1752        free(mCodecSpecificData.editItemAt(i));
1753    }
1754    mCodecSpecificData.clear();
1755    mCodecSpecificDataIndex = 0;
1756}
1757
1758status_t OMXCodec::start(MetaData *) {
1759    Mutex::Autolock autoLock(mLock);
1760
1761    if (mState != LOADED) {
1762        return UNKNOWN_ERROR;
1763    }
1764
1765    sp<MetaData> params = new MetaData;
1766    if (mQuirks & kWantsNALFragments) {
1767        params->setInt32(kKeyWantsNALFragments, true);
1768    }
1769    status_t err = mSource->start(params.get());
1770
1771    if (err != OK) {
1772        return err;
1773    }
1774
1775    mCodecSpecificDataIndex = 0;
1776    mInitialBufferSubmit = true;
1777    mSignalledEOS = false;
1778    mNoMoreOutputData = false;
1779    mSeekTimeUs = -1;
1780    mFilledBuffers.clear();
1781
1782    return init();
1783}
1784
1785status_t OMXCodec::stop() {
1786    CODEC_LOGV("stop");
1787
1788    Mutex::Autolock autoLock(mLock);
1789
1790    while (isIntermediateState(mState)) {
1791        mAsyncCompletion.wait(mLock);
1792    }
1793
1794    switch (mState) {
1795        case LOADED:
1796        case ERROR:
1797            break;
1798
1799        case EXECUTING:
1800        {
1801            setState(EXECUTING_TO_IDLE);
1802
1803            if (mQuirks & kRequiresFlushBeforeShutdown) {
1804                CODEC_LOGV("This component requires a flush before transitioning "
1805                     "from EXECUTING to IDLE...");
1806
1807                bool emulateInputFlushCompletion =
1808                    !flushPortAsync(kPortIndexInput);
1809
1810                bool emulateOutputFlushCompletion =
1811                    !flushPortAsync(kPortIndexOutput);
1812
1813                if (emulateInputFlushCompletion) {
1814                    onCmdComplete(OMX_CommandFlush, kPortIndexInput);
1815                }
1816
1817                if (emulateOutputFlushCompletion) {
1818                    onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
1819                }
1820            } else {
1821                mPortStatus[kPortIndexInput] = SHUTTING_DOWN;
1822                mPortStatus[kPortIndexOutput] = SHUTTING_DOWN;
1823
1824                status_t err =
1825                    mOMX->send_command(mNode, OMX_CommandStateSet, OMX_StateIdle);
1826                CHECK_EQ(err, OK);
1827            }
1828
1829            while (mState != LOADED && mState != ERROR) {
1830                mAsyncCompletion.wait(mLock);
1831            }
1832
1833            break;
1834        }
1835
1836        default:
1837        {
1838            CHECK(!"should not be here.");
1839            break;
1840        }
1841    }
1842
1843    mSource->stop();
1844
1845    return OK;
1846}
1847
1848sp<MetaData> OMXCodec::getFormat() {
1849    return mOutputFormat;
1850}
1851
1852status_t OMXCodec::read(
1853        MediaBuffer **buffer, const ReadOptions *options) {
1854    *buffer = NULL;
1855
1856    Mutex::Autolock autoLock(mLock);
1857
1858    if (mState != EXECUTING && mState != RECONFIGURING) {
1859        return UNKNOWN_ERROR;
1860    }
1861
1862    if (mInitialBufferSubmit) {
1863        mInitialBufferSubmit = false;
1864
1865        drainInputBuffers();
1866
1867        if (mState == EXECUTING) {
1868            // Otherwise mState == RECONFIGURING and this code will trigger
1869            // after the output port is reenabled.
1870            fillOutputBuffers();
1871        }
1872    }
1873
1874    int64_t seekTimeUs;
1875    if (options && options->getSeekTo(&seekTimeUs)) {
1876        CODEC_LOGV("seeking to %lld us (%.2f secs)", seekTimeUs, seekTimeUs / 1E6);
1877
1878        mSignalledEOS = false;
1879        mNoMoreOutputData = false;
1880
1881        CHECK(seekTimeUs >= 0);
1882        mSeekTimeUs = seekTimeUs;
1883
1884        mFilledBuffers.clear();
1885
1886        CHECK_EQ(mState, EXECUTING);
1887
1888        bool emulateInputFlushCompletion = !flushPortAsync(kPortIndexInput);
1889        bool emulateOutputFlushCompletion = !flushPortAsync(kPortIndexOutput);
1890
1891        if (emulateInputFlushCompletion) {
1892            onCmdComplete(OMX_CommandFlush, kPortIndexInput);
1893        }
1894
1895        if (emulateOutputFlushCompletion) {
1896            onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
1897        }
1898    }
1899
1900    while (mState != ERROR && !mNoMoreOutputData && mFilledBuffers.empty()) {
1901        mBufferFilled.wait(mLock);
1902    }
1903
1904    if (mState == ERROR) {
1905        return UNKNOWN_ERROR;
1906    }
1907
1908    if (mFilledBuffers.empty()) {
1909        return ERROR_END_OF_STREAM;
1910    }
1911
1912    size_t index = *mFilledBuffers.begin();
1913    mFilledBuffers.erase(mFilledBuffers.begin());
1914
1915    BufferInfo *info = &mPortBuffers[kPortIndexOutput].editItemAt(index);
1916    info->mMediaBuffer->add_ref();
1917    *buffer = info->mMediaBuffer;
1918
1919    return OK;
1920}
1921
1922void OMXCodec::signalBufferReturned(MediaBuffer *buffer) {
1923    Mutex::Autolock autoLock(mLock);
1924
1925    Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput];
1926    for (size_t i = 0; i < buffers->size(); ++i) {
1927        BufferInfo *info = &buffers->editItemAt(i);
1928
1929        if (info->mMediaBuffer == buffer) {
1930            CHECK_EQ(mPortStatus[kPortIndexOutput], ENABLED);
1931            fillOutputBuffer(info);
1932            return;
1933        }
1934    }
1935
1936    CHECK(!"should not be here.");
1937}
1938
1939static const char *imageCompressionFormatString(OMX_IMAGE_CODINGTYPE type) {
1940    static const char *kNames[] = {
1941        "OMX_IMAGE_CodingUnused",
1942        "OMX_IMAGE_CodingAutoDetect",
1943        "OMX_IMAGE_CodingJPEG",
1944        "OMX_IMAGE_CodingJPEG2K",
1945        "OMX_IMAGE_CodingEXIF",
1946        "OMX_IMAGE_CodingTIFF",
1947        "OMX_IMAGE_CodingGIF",
1948        "OMX_IMAGE_CodingPNG",
1949        "OMX_IMAGE_CodingLZW",
1950        "OMX_IMAGE_CodingBMP",
1951    };
1952
1953    size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
1954
1955    if (type < 0 || (size_t)type >= numNames) {
1956        return "UNKNOWN";
1957    } else {
1958        return kNames[type];
1959    }
1960}
1961
1962static const char *colorFormatString(OMX_COLOR_FORMATTYPE type) {
1963    static const char *kNames[] = {
1964        "OMX_COLOR_FormatUnused",
1965        "OMX_COLOR_FormatMonochrome",
1966        "OMX_COLOR_Format8bitRGB332",
1967        "OMX_COLOR_Format12bitRGB444",
1968        "OMX_COLOR_Format16bitARGB4444",
1969        "OMX_COLOR_Format16bitARGB1555",
1970        "OMX_COLOR_Format16bitRGB565",
1971        "OMX_COLOR_Format16bitBGR565",
1972        "OMX_COLOR_Format18bitRGB666",
1973        "OMX_COLOR_Format18bitARGB1665",
1974        "OMX_COLOR_Format19bitARGB1666",
1975        "OMX_COLOR_Format24bitRGB888",
1976        "OMX_COLOR_Format24bitBGR888",
1977        "OMX_COLOR_Format24bitARGB1887",
1978        "OMX_COLOR_Format25bitARGB1888",
1979        "OMX_COLOR_Format32bitBGRA8888",
1980        "OMX_COLOR_Format32bitARGB8888",
1981        "OMX_COLOR_FormatYUV411Planar",
1982        "OMX_COLOR_FormatYUV411PackedPlanar",
1983        "OMX_COLOR_FormatYUV420Planar",
1984        "OMX_COLOR_FormatYUV420PackedPlanar",
1985        "OMX_COLOR_FormatYUV420SemiPlanar",
1986        "OMX_COLOR_FormatYUV422Planar",
1987        "OMX_COLOR_FormatYUV422PackedPlanar",
1988        "OMX_COLOR_FormatYUV422SemiPlanar",
1989        "OMX_COLOR_FormatYCbYCr",
1990        "OMX_COLOR_FormatYCrYCb",
1991        "OMX_COLOR_FormatCbYCrY",
1992        "OMX_COLOR_FormatCrYCbY",
1993        "OMX_COLOR_FormatYUV444Interleaved",
1994        "OMX_COLOR_FormatRawBayer8bit",
1995        "OMX_COLOR_FormatRawBayer10bit",
1996        "OMX_COLOR_FormatRawBayer8bitcompressed",
1997        "OMX_COLOR_FormatL2",
1998        "OMX_COLOR_FormatL4",
1999        "OMX_COLOR_FormatL8",
2000        "OMX_COLOR_FormatL16",
2001        "OMX_COLOR_FormatL24",
2002        "OMX_COLOR_FormatL32",
2003        "OMX_COLOR_FormatYUV420PackedSemiPlanar",
2004        "OMX_COLOR_FormatYUV422PackedSemiPlanar",
2005        "OMX_COLOR_Format18BitBGR666",
2006        "OMX_COLOR_Format24BitARGB6666",
2007        "OMX_COLOR_Format24BitABGR6666",
2008    };
2009
2010    size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2011
2012    static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
2013
2014    if (type == OMX_QCOM_COLOR_FormatYVU420SemiPlanar) {
2015        return "OMX_QCOM_COLOR_FormatYVU420SemiPlanar";
2016    } else if (type < 0 || (size_t)type >= numNames) {
2017        return "UNKNOWN";
2018    } else {
2019        return kNames[type];
2020    }
2021}
2022
2023static const char *videoCompressionFormatString(OMX_VIDEO_CODINGTYPE type) {
2024    static const char *kNames[] = {
2025        "OMX_VIDEO_CodingUnused",
2026        "OMX_VIDEO_CodingAutoDetect",
2027        "OMX_VIDEO_CodingMPEG2",
2028        "OMX_VIDEO_CodingH263",
2029        "OMX_VIDEO_CodingMPEG4",
2030        "OMX_VIDEO_CodingWMV",
2031        "OMX_VIDEO_CodingRV",
2032        "OMX_VIDEO_CodingAVC",
2033        "OMX_VIDEO_CodingMJPEG",
2034    };
2035
2036    size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2037
2038    if (type < 0 || (size_t)type >= numNames) {
2039        return "UNKNOWN";
2040    } else {
2041        return kNames[type];
2042    }
2043}
2044
2045static const char *audioCodingTypeString(OMX_AUDIO_CODINGTYPE type) {
2046    static const char *kNames[] = {
2047        "OMX_AUDIO_CodingUnused",
2048        "OMX_AUDIO_CodingAutoDetect",
2049        "OMX_AUDIO_CodingPCM",
2050        "OMX_AUDIO_CodingADPCM",
2051        "OMX_AUDIO_CodingAMR",
2052        "OMX_AUDIO_CodingGSMFR",
2053        "OMX_AUDIO_CodingGSMEFR",
2054        "OMX_AUDIO_CodingGSMHR",
2055        "OMX_AUDIO_CodingPDCFR",
2056        "OMX_AUDIO_CodingPDCEFR",
2057        "OMX_AUDIO_CodingPDCHR",
2058        "OMX_AUDIO_CodingTDMAFR",
2059        "OMX_AUDIO_CodingTDMAEFR",
2060        "OMX_AUDIO_CodingQCELP8",
2061        "OMX_AUDIO_CodingQCELP13",
2062        "OMX_AUDIO_CodingEVRC",
2063        "OMX_AUDIO_CodingSMV",
2064        "OMX_AUDIO_CodingG711",
2065        "OMX_AUDIO_CodingG723",
2066        "OMX_AUDIO_CodingG726",
2067        "OMX_AUDIO_CodingG729",
2068        "OMX_AUDIO_CodingAAC",
2069        "OMX_AUDIO_CodingMP3",
2070        "OMX_AUDIO_CodingSBC",
2071        "OMX_AUDIO_CodingVORBIS",
2072        "OMX_AUDIO_CodingWMA",
2073        "OMX_AUDIO_CodingRA",
2074        "OMX_AUDIO_CodingMIDI",
2075    };
2076
2077    size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2078
2079    if (type < 0 || (size_t)type >= numNames) {
2080        return "UNKNOWN";
2081    } else {
2082        return kNames[type];
2083    }
2084}
2085
2086static const char *audioPCMModeString(OMX_AUDIO_PCMMODETYPE type) {
2087    static const char *kNames[] = {
2088        "OMX_AUDIO_PCMModeLinear",
2089        "OMX_AUDIO_PCMModeALaw",
2090        "OMX_AUDIO_PCMModeMULaw",
2091    };
2092
2093    size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2094
2095    if (type < 0 || (size_t)type >= numNames) {
2096        return "UNKNOWN";
2097    } else {
2098        return kNames[type];
2099    }
2100}
2101
2102static const char *amrBandModeString(OMX_AUDIO_AMRBANDMODETYPE type) {
2103    static const char *kNames[] = {
2104        "OMX_AUDIO_AMRBandModeUnused",
2105        "OMX_AUDIO_AMRBandModeNB0",
2106        "OMX_AUDIO_AMRBandModeNB1",
2107        "OMX_AUDIO_AMRBandModeNB2",
2108        "OMX_AUDIO_AMRBandModeNB3",
2109        "OMX_AUDIO_AMRBandModeNB4",
2110        "OMX_AUDIO_AMRBandModeNB5",
2111        "OMX_AUDIO_AMRBandModeNB6",
2112        "OMX_AUDIO_AMRBandModeNB7",
2113        "OMX_AUDIO_AMRBandModeWB0",
2114        "OMX_AUDIO_AMRBandModeWB1",
2115        "OMX_AUDIO_AMRBandModeWB2",
2116        "OMX_AUDIO_AMRBandModeWB3",
2117        "OMX_AUDIO_AMRBandModeWB4",
2118        "OMX_AUDIO_AMRBandModeWB5",
2119        "OMX_AUDIO_AMRBandModeWB6",
2120        "OMX_AUDIO_AMRBandModeWB7",
2121        "OMX_AUDIO_AMRBandModeWB8",
2122    };
2123
2124    size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2125
2126    if (type < 0 || (size_t)type >= numNames) {
2127        return "UNKNOWN";
2128    } else {
2129        return kNames[type];
2130    }
2131}
2132
2133static const char *amrFrameFormatString(OMX_AUDIO_AMRFRAMEFORMATTYPE type) {
2134    static const char *kNames[] = {
2135        "OMX_AUDIO_AMRFrameFormatConformance",
2136        "OMX_AUDIO_AMRFrameFormatIF1",
2137        "OMX_AUDIO_AMRFrameFormatIF2",
2138        "OMX_AUDIO_AMRFrameFormatFSF",
2139        "OMX_AUDIO_AMRFrameFormatRTPPayload",
2140        "OMX_AUDIO_AMRFrameFormatITU",
2141    };
2142
2143    size_t numNames = sizeof(kNames) / sizeof(kNames[0]);
2144
2145    if (type < 0 || (size_t)type >= numNames) {
2146        return "UNKNOWN";
2147    } else {
2148        return kNames[type];
2149    }
2150}
2151
2152void OMXCodec::dumpPortStatus(OMX_U32 portIndex) {
2153    OMX_PARAM_PORTDEFINITIONTYPE def;
2154    InitOMXParams(&def);
2155    def.nPortIndex = portIndex;
2156
2157    status_t err = mOMX->get_parameter(
2158            mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2159    CHECK_EQ(err, OK);
2160
2161    printf("%s Port = {\n", portIndex == kPortIndexInput ? "Input" : "Output");
2162
2163    CHECK((portIndex == kPortIndexInput && def.eDir == OMX_DirInput)
2164          || (portIndex == kPortIndexOutput && def.eDir == OMX_DirOutput));
2165
2166    printf("  nBufferCountActual = %ld\n", def.nBufferCountActual);
2167    printf("  nBufferCountMin = %ld\n", def.nBufferCountMin);
2168    printf("  nBufferSize = %ld\n", def.nBufferSize);
2169
2170    switch (def.eDomain) {
2171        case OMX_PortDomainImage:
2172        {
2173            const OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
2174
2175            printf("\n");
2176            printf("  // Image\n");
2177            printf("  nFrameWidth = %ld\n", imageDef->nFrameWidth);
2178            printf("  nFrameHeight = %ld\n", imageDef->nFrameHeight);
2179            printf("  nStride = %ld\n", imageDef->nStride);
2180
2181            printf("  eCompressionFormat = %s\n",
2182                   imageCompressionFormatString(imageDef->eCompressionFormat));
2183
2184            printf("  eColorFormat = %s\n",
2185                   colorFormatString(imageDef->eColorFormat));
2186
2187            break;
2188        }
2189
2190        case OMX_PortDomainVideo:
2191        {
2192            OMX_VIDEO_PORTDEFINITIONTYPE *videoDef = &def.format.video;
2193
2194            printf("\n");
2195            printf("  // Video\n");
2196            printf("  nFrameWidth = %ld\n", videoDef->nFrameWidth);
2197            printf("  nFrameHeight = %ld\n", videoDef->nFrameHeight);
2198            printf("  nStride = %ld\n", videoDef->nStride);
2199
2200            printf("  eCompressionFormat = %s\n",
2201                   videoCompressionFormatString(videoDef->eCompressionFormat));
2202
2203            printf("  eColorFormat = %s\n",
2204                   colorFormatString(videoDef->eColorFormat));
2205
2206            break;
2207        }
2208
2209        case OMX_PortDomainAudio:
2210        {
2211            OMX_AUDIO_PORTDEFINITIONTYPE *audioDef = &def.format.audio;
2212
2213            printf("\n");
2214            printf("  // Audio\n");
2215            printf("  eEncoding = %s\n",
2216                   audioCodingTypeString(audioDef->eEncoding));
2217
2218            if (audioDef->eEncoding == OMX_AUDIO_CodingPCM) {
2219                OMX_AUDIO_PARAM_PCMMODETYPE params;
2220                InitOMXParams(&params);
2221                params.nPortIndex = portIndex;
2222
2223                err = mOMX->get_parameter(
2224                        mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
2225                CHECK_EQ(err, OK);
2226
2227                printf("  nSamplingRate = %ld\n", params.nSamplingRate);
2228                printf("  nChannels = %ld\n", params.nChannels);
2229                printf("  bInterleaved = %d\n", params.bInterleaved);
2230                printf("  nBitPerSample = %ld\n", params.nBitPerSample);
2231
2232                printf("  eNumData = %s\n",
2233                       params.eNumData == OMX_NumericalDataSigned
2234                        ? "signed" : "unsigned");
2235
2236                printf("  ePCMMode = %s\n", audioPCMModeString(params.ePCMMode));
2237            } else if (audioDef->eEncoding == OMX_AUDIO_CodingAMR) {
2238                OMX_AUDIO_PARAM_AMRTYPE amr;
2239                InitOMXParams(&amr);
2240                amr.nPortIndex = portIndex;
2241
2242                err = mOMX->get_parameter(
2243                        mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
2244                CHECK_EQ(err, OK);
2245
2246                printf("  nChannels = %ld\n", amr.nChannels);
2247                printf("  eAMRBandMode = %s\n",
2248                        amrBandModeString(amr.eAMRBandMode));
2249                printf("  eAMRFrameFormat = %s\n",
2250                        amrFrameFormatString(amr.eAMRFrameFormat));
2251            }
2252
2253            break;
2254        }
2255
2256        default:
2257        {
2258            printf("  // Unknown\n");
2259            break;
2260        }
2261    }
2262
2263    printf("}\n");
2264}
2265
2266void OMXCodec::initOutputFormat(const sp<MetaData> &inputFormat) {
2267    mOutputFormat = new MetaData;
2268    mOutputFormat->setCString(kKeyDecoderComponent, mComponentName);
2269
2270    OMX_PARAM_PORTDEFINITIONTYPE def;
2271    InitOMXParams(&def);
2272    def.nPortIndex = kPortIndexOutput;
2273
2274    status_t err = mOMX->get_parameter(
2275            mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2276    CHECK_EQ(err, OK);
2277
2278    switch (def.eDomain) {
2279        case OMX_PortDomainImage:
2280        {
2281            OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image;
2282            CHECK_EQ(imageDef->eCompressionFormat, OMX_IMAGE_CodingUnused);
2283
2284            mOutputFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
2285            mOutputFormat->setInt32(kKeyColorFormat, imageDef->eColorFormat);
2286            mOutputFormat->setInt32(kKeyWidth, imageDef->nFrameWidth);
2287            mOutputFormat->setInt32(kKeyHeight, imageDef->nFrameHeight);
2288            break;
2289        }
2290
2291        case OMX_PortDomainAudio:
2292        {
2293            OMX_AUDIO_PORTDEFINITIONTYPE *audio_def = &def.format.audio;
2294
2295            if (audio_def->eEncoding == OMX_AUDIO_CodingPCM) {
2296                OMX_AUDIO_PARAM_PCMMODETYPE params;
2297                InitOMXParams(&params);
2298                params.nPortIndex = kPortIndexOutput;
2299
2300                err = mOMX->get_parameter(
2301                        mNode, OMX_IndexParamAudioPcm, &params, sizeof(params));
2302                CHECK_EQ(err, OK);
2303
2304                CHECK_EQ(params.eNumData, OMX_NumericalDataSigned);
2305                CHECK_EQ(params.nBitPerSample, 16);
2306                CHECK_EQ(params.ePCMMode, OMX_AUDIO_PCMModeLinear);
2307
2308                int32_t numChannels, sampleRate;
2309                inputFormat->findInt32(kKeyChannelCount, &numChannels);
2310                inputFormat->findInt32(kKeySampleRate, &sampleRate);
2311
2312                if ((OMX_U32)numChannels != params.nChannels) {
2313                    LOGW("Codec outputs a different number of channels than "
2314                         "the input stream contains.");
2315                }
2316
2317                mOutputFormat->setCString(
2318                        kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
2319
2320                // Use the codec-advertised number of channels, as some
2321                // codecs appear to output stereo even if the input data is
2322                // mono.
2323                mOutputFormat->setInt32(kKeyChannelCount, params.nChannels);
2324
2325                // The codec-reported sampleRate is not reliable...
2326                mOutputFormat->setInt32(kKeySampleRate, sampleRate);
2327            } else if (audio_def->eEncoding == OMX_AUDIO_CodingAMR) {
2328                OMX_AUDIO_PARAM_AMRTYPE amr;
2329                InitOMXParams(&amr);
2330                amr.nPortIndex = kPortIndexOutput;
2331
2332                err = mOMX->get_parameter(
2333                        mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr));
2334                CHECK_EQ(err, OK);
2335
2336                CHECK_EQ(amr.nChannels, 1);
2337                mOutputFormat->setInt32(kKeyChannelCount, 1);
2338
2339                if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeNB0
2340                    && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeNB7) {
2341                    mOutputFormat->setCString(
2342                            kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_NB);
2343                    mOutputFormat->setInt32(kKeySampleRate, 8000);
2344                } else if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeWB0
2345                            && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeWB8) {
2346                    mOutputFormat->setCString(
2347                            kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_WB);
2348                    mOutputFormat->setInt32(kKeySampleRate, 16000);
2349                } else {
2350                    CHECK(!"Unknown AMR band mode.");
2351                }
2352            } else if (audio_def->eEncoding == OMX_AUDIO_CodingAAC) {
2353                mOutputFormat->setCString(
2354                        kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC);
2355            } else {
2356                CHECK(!"Should not be here. Unknown audio encoding.");
2357            }
2358            break;
2359        }
2360
2361        case OMX_PortDomainVideo:
2362        {
2363            OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
2364
2365            if (video_def->eCompressionFormat == OMX_VIDEO_CodingUnused) {
2366                mOutputFormat->setCString(
2367                        kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
2368            } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingMPEG4) {
2369                mOutputFormat->setCString(
2370                        kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4);
2371            } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingH263) {
2372                mOutputFormat->setCString(
2373                        kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263);
2374            } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingAVC) {
2375                mOutputFormat->setCString(
2376                        kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
2377            } else {
2378                CHECK(!"Unknown compression format.");
2379            }
2380
2381            if (!strcmp(mComponentName, "OMX.PV.avcdec")) {
2382                // This component appears to be lying to me.
2383                mOutputFormat->setInt32(
2384                        kKeyWidth, (video_def->nFrameWidth + 15) & -16);
2385                mOutputFormat->setInt32(
2386                        kKeyHeight, (video_def->nFrameHeight + 15) & -16);
2387            } else {
2388                mOutputFormat->setInt32(kKeyWidth, video_def->nFrameWidth);
2389                mOutputFormat->setInt32(kKeyHeight, video_def->nFrameHeight);
2390            }
2391
2392            mOutputFormat->setInt32(kKeyColorFormat, video_def->eColorFormat);
2393            break;
2394        }
2395
2396        default:
2397        {
2398            CHECK(!"should not be here, neither audio nor video.");
2399            break;
2400        }
2401    }
2402}
2403
2404////////////////////////////////////////////////////////////////////////////////
2405
2406status_t QueryCodecs(
2407        const sp<IOMX> &omx,
2408        const char *mime, bool queryDecoders,
2409        Vector<CodecCapabilities> *results) {
2410    results->clear();
2411
2412    for (int index = 0;; ++index) {
2413        const char *componentName;
2414
2415        if (!queryDecoders) {
2416            componentName = GetCodec(
2417                    kEncoderInfo, sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]),
2418                    mime, index);
2419        } else {
2420            componentName = GetCodec(
2421                    kDecoderInfo, sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]),
2422                    mime, index);
2423        }
2424
2425        if (!componentName) {
2426            return OK;
2427        }
2428
2429        IOMX::node_id node;
2430        status_t err = omx->allocate_node(componentName, &node);
2431
2432        if (err != OK) {
2433            continue;
2434        }
2435
2436        OMXCodec::setComponentRole(omx, node, queryDecoders, mime);
2437
2438        results->push();
2439        CodecCapabilities *caps = &results->editItemAt(results->size() - 1);
2440        caps->mComponentName = componentName;
2441
2442        OMX_VIDEO_PARAM_PROFILELEVELTYPE param;
2443        InitOMXParams(&param);
2444
2445        param.nPortIndex = queryDecoders ? 0 : 1;
2446
2447        for (param.nProfileIndex = 0;; ++param.nProfileIndex) {
2448            err = omx->get_parameter(
2449                    node, OMX_IndexParamVideoProfileLevelQuerySupported,
2450                    &param, sizeof(param));
2451
2452            if (err != OK) {
2453                break;
2454            }
2455
2456            CodecProfileLevel profileLevel;
2457            profileLevel.mProfile = param.eProfile;
2458            profileLevel.mLevel = param.eLevel;
2459
2460            caps->mProfileLevels.push(profileLevel);
2461        }
2462
2463        CHECK_EQ(omx->free_node(node), OK);
2464    }
2465}
2466
2467}  // namespace android
2468