VideoCodec_to_android.cpp revision 3888f4ba1b3c391104c104ce054f7ad4ec71556c
16e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi/*
26e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi * Copyright (C) 2011 The Android Open Source Project
36e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi *
46e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi * Licensed under the Apache License, Version 2.0 (the "License");
56e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi * you may not use this file except in compliance with the License.
66e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi * You may obtain a copy of the License at
76e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi *
86e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi *      http://www.apache.org/licenses/LICENSE-2.0
96e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi *
106e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi * Unless required by applicable law or agreed to in writing, software
116e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi * distributed under the License is distributed on an "AS IS" BASIS,
126e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
136e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi * See the License for the specific language governing permissions and
146e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi * limitations under the License.
156e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi */
166e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
176e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi#include "sles_allinclusive.h"
186e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
196e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi#include <media/IMediaPlayerService.h>
206e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi#include <media/stagefright/OMXClient.h>
216e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi#include <media/stagefright/OMXCodec.h>
226e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi#include <media/IOMX.h>
236e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi#include <media/stagefright/MediaDefs.h>
246e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
256e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
266e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivinamespace android {
276e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
286e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivistatic sp<IOMX> omx;
296e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
306e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi// listed in same order as VideoCodecIds[] in file "../devices.c" with ANDROID defined
316e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivistatic const char *kVideoMimeTypes[] = {
326e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        MEDIA_MIMETYPE_VIDEO_MPEG2,
336e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        MEDIA_MIMETYPE_VIDEO_H263,
346e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        MEDIA_MIMETYPE_VIDEO_MPEG4,
356e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        MEDIA_MIMETYPE_VIDEO_AVC,
366e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        MEDIA_MIMETYPE_VIDEO_VPX
376e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi};
386e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivistatic const size_t kNbVideoMimeTypes = sizeof(kVideoMimeTypes) / sizeof(kVideoMimeTypes[0]);
396e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
406e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi// codec capabilities in the following arrays maps to the mime types defined in kVideoMimeTypes
416e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivistatic Vector<CodecCapabilities> VideoDecoderCapabilities[kNbVideoMimeTypes];
426e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivistatic XAuint32 VideoDecoderNbProfLevel[kNbVideoMimeTypes];
436e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
446e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivistatic XAuint32 NbSupportedDecoderTypes = 0;
456e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
466e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
476e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel TriviXAuint32 convertOpenMaxIlToAl(OMX_U32 ilVideoProfileOrLevel) {
486e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    // For video codec profiles and levels, the number of trailing zeroes in OpenMAX IL
496e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    // are equal to the matching OpenMAX AL constant value plus 1, for example:
506e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    //    XA_VIDEOPROFILE_H263_BACKWARDCOMPATIBLE ((XAuint32) 0x00000003)
516e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    //        matches
526e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    //    OMX_VIDEO_H263ProfileBackwardCompatible  = 0x04
536e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    return (XAuint32) (__builtin_ctz(ilVideoProfileOrLevel) + 1);
546e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi}
556e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
566e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
576e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivibool android_videoCodec_expose() {
586e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    SL_LOGV("android_videoCodec_expose()");
596e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
603888f4ba1b3c391104c104ce054f7ad4ec71556cGlenn Kasten    sp<IMediaPlayerService> service(IMediaDeathNotifier::getMediaPlayerService());
613888f4ba1b3c391104c104ce054f7ad4ec71556cGlenn Kasten    if (service == NULL) {
623888f4ba1b3c391104c104ce054f7ad4ec71556cGlenn Kasten        // no need to SL_LOGE; getMediaPlayerService already will have done so
636e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        return false;
646e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    }
656e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
666e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    omx = service->getOMX();
676e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    if (omx.get() == NULL) {
686e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        LOGE("android_videoCodec_expose() couldn't access OMX interface");
696e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        return false;
706e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    }
716e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
726e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    // used to check whether no codecs were found, which is a sign of failure
736e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    NbSupportedDecoderTypes = 0;
746e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    for (size_t m = 0 ; m < kNbVideoMimeTypes ; m++) {
756e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        if (OK == QueryCodecs(omx, kVideoMimeTypes[m], true /* queryDecoders */,
766e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi                true /* hwCodecOnly */, &VideoDecoderCapabilities[m])) {
776e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi            if (!VideoDecoderCapabilities[m].empty()) {
786e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi                NbSupportedDecoderTypes++;
796e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi            }
806e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi            // for each decoder of the given decoder ID, verify it is a hardware decoder
816e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi            for (size_t c = 0 ; c < VideoDecoderCapabilities[m].size() ; c++) {
826e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi                VideoDecoderNbProfLevel[c] = 0;
836e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi                const String8& compName =
846e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi                        VideoDecoderCapabilities[m].itemAt(c).mComponentName;
856e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi                // get the number of profiles and levels for this decoder
866e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi                VideoDecoderNbProfLevel[m] =
876e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi                        VideoDecoderCapabilities[m].itemAt(c).mProfileLevels.size();
886e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi                if (VideoDecoderNbProfLevel[m] != 0) {
896e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi                    SL_LOGV("codec %d nb prof/level=%d", m, VideoDecoderNbProfLevel[m]);
906e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi                    break;
916e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi                }
926e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi            }
936e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        }
946e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    }
956e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
966e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    return (NbSupportedDecoderTypes > 0);
976e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi}
986e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
996e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
1006e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivivoid android_videoCodec_deinit() {
1016e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    SL_LOGV("android_videoCodec_deinit()");
1026e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    for (size_t m = 0 ; m < kNbVideoMimeTypes ; m++) {
1036e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        VideoDecoderCapabilities[m].clear();
1046e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    }
1056e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi}
1066e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
1076e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
1086e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel TriviXAuint32 android_videoCodec_getNbDecoders() {
1096e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    return NbSupportedDecoderTypes;
1106e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi}
1116e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
1126e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
1136e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivivoid android_videoCodec_getDecoderIds(XAuint32 nbDecoders, XAuint32 *pDecoderIds) {
1146e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    XAuint32 *pIds = pDecoderIds;
1156e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    XAuint32 nbFound = 0;
1166e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    for (size_t m = 0 ; m < kNbVideoMimeTypes ; m++) {
1176e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        if (!VideoDecoderCapabilities[m].empty()) {
1186e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi            *pIds = VideoDecoderIds[m];
1196e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi            pIds++;
1206e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi            nbFound++;
1216e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        }
1226e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        // range check: function can be called for fewer codecs than there are
1236e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        if (nbFound == nbDecoders) {
1246e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi            break;
1256e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        }
1266e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    }
1276e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi}
1286e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
1296e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
1306e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel TriviSLresult android_videoCodec_getProfileLevelCombinationNb(XAuint32 decoderId, XAuint32 *pNb)
1316e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi{
1326e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    // translate a decoder ID to an index in the codec table
1336e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    size_t decoderIndex = 0;
1346e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    *pNb = 0;
1356e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    while (decoderIndex < kNbVideoMimeTypes) {
1366e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        if (decoderId == VideoDecoderIds[decoderIndex]) {
1376e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi            *pNb = VideoDecoderNbProfLevel[decoderIndex];
138947ccd3690e84649878f2583c701ca8de1b19ed4Glenn Kasten            break;
1396e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        }
1406e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        decoderIndex++;
1416e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    }
1426e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
143947ccd3690e84649878f2583c701ca8de1b19ed4Glenn Kasten    return XA_RESULT_SUCCESS;
1446e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi}
1456e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
1466e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
1476e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel TriviSLresult android_videoCodec_getProfileLevelCombination(XAuint32 decoderId, XAuint32 plIndex,
1486e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        XAVideoCodecDescriptor *pDescr)
1496e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi{
1506e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    // translate a decoder ID to an index in the codec table
1516e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    size_t decoderIndex = 0;
1526e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    while (decoderIndex < kNbVideoMimeTypes) {
1536e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        if (decoderId == VideoDecoderIds[decoderIndex]) {
1546e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi            if (!(plIndex < VideoDecoderCapabilities[decoderIndex].itemAt(0).mProfileLevels.size()))
1556e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi            {
1566e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi                // asking for invalid profile/level
1576e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi                return XA_RESULT_PARAMETER_INVALID;
1586e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi            }
1596e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi            // we only look at the first codec, OpenMAX AL doesn't let you expose the capabilities
1606e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi            //  of multiple codecs
161106a99988093bd3b3b3aafb2da0fbc0d35634787Jean-Michel Trivi            //     set the fields we know about
1626e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi            pDescr->codecId = decoderId;
1636e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi            pDescr->profileSetting = convertOpenMaxIlToAl(VideoDecoderCapabilities[decoderIndex].
1646e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi                    itemAt(0).mProfileLevels.itemAt(plIndex).mProfile);
1656e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi            pDescr->levelSetting =  convertOpenMaxIlToAl(VideoDecoderCapabilities[decoderIndex].
1666e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi                    itemAt(0).mProfileLevels.itemAt(plIndex).mLevel);
167106a99988093bd3b3b3aafb2da0fbc0d35634787Jean-Michel Trivi            //     initialize the fields we don't know about
168106a99988093bd3b3b3aafb2da0fbc0d35634787Jean-Michel Trivi            pDescr->maxWidth = 0;
169106a99988093bd3b3b3aafb2da0fbc0d35634787Jean-Michel Trivi            pDescr->maxHeight = 0;
170106a99988093bd3b3b3aafb2da0fbc0d35634787Jean-Michel Trivi            pDescr->maxFrameRate = 0;
171106a99988093bd3b3b3aafb2da0fbc0d35634787Jean-Michel Trivi            pDescr->maxBitRate = 0;
172106a99988093bd3b3b3aafb2da0fbc0d35634787Jean-Michel Trivi            pDescr->rateControlSupported = 0;
1736e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi            break;
1746e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        }
1756e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        decoderIndex++;
1766e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    }
1776e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    return (decoderIndex < kNbVideoMimeTypes) ? XA_RESULT_SUCCESS : XA_RESULT_PARAMETER_INVALID;
1786e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi}
1796e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
1806e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi} // namespace android
181