VideoCodec_to_android.cpp revision 6e7e174807fc639c49125ced8962aa369370fbf0
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 <binder/IServiceManager.h>
206e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi#include <utils/RefBase.h>
216e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi#include <media/IMediaPlayerService.h>
226e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi#include <media/stagefright/OMXClient.h>
236e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi#include <media/stagefright/OMXCodec.h>
246e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi#include <media/IOMX.h>
256e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi#include <media/stagefright/MediaDefs.h>
266e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
276e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
286e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivinamespace android {
296e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
306e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivistatic sp<IOMX> omx;
316e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
326e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi// listed in same order as VideoCodecIds[] in file "../devices.c" with ANDROID defined
336e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivistatic const char *kVideoMimeTypes[] = {
346e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        MEDIA_MIMETYPE_VIDEO_MPEG2,
356e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        MEDIA_MIMETYPE_VIDEO_H263,
366e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        MEDIA_MIMETYPE_VIDEO_MPEG4,
376e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        MEDIA_MIMETYPE_VIDEO_AVC,
386e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        MEDIA_MIMETYPE_VIDEO_VPX
396e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi};
406e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivistatic const size_t kNbVideoMimeTypes = sizeof(kVideoMimeTypes) / sizeof(kVideoMimeTypes[0]);
416e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
426e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi// codec capabilities in the following arrays maps to the mime types defined in kVideoMimeTypes
436e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivistatic Vector<CodecCapabilities> VideoDecoderCapabilities[kNbVideoMimeTypes];
446e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivistatic XAuint32 VideoDecoderNbProfLevel[kNbVideoMimeTypes];
456e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
466e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivistatic XAuint32 NbSupportedDecoderTypes = 0;
476e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
486e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
496e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel TriviXAuint32 convertOpenMaxIlToAl(OMX_U32 ilVideoProfileOrLevel) {
506e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    // For video codec profiles and levels, the number of trailing zeroes in OpenMAX IL
516e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    // are equal to the matching OpenMAX AL constant value plus 1, for example:
526e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    //    XA_VIDEOPROFILE_H263_BACKWARDCOMPATIBLE ((XAuint32) 0x00000003)
536e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    //        matches
546e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    //    OMX_VIDEO_H263ProfileBackwardCompatible  = 0x04
556e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    return (XAuint32) (__builtin_ctz(ilVideoProfileOrLevel) + 1);
566e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi}
576e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
586e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
596e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivibool android_videoCodec_expose() {
606e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    SL_LOGV("android_videoCodec_expose()");
616e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
626e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    sp<IServiceManager> sm = defaultServiceManager();
636e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    sp<IBinder> binder = sm->getService(String16("media.player"));
646e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    sp<IMediaPlayerService> service = interface_cast<IMediaPlayerService>(binder);
656e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
666e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    if (service.get() == NULL) {
676e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        LOGE("android_videoCodec_expose() couldn't access media player service");
686e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        return false;
696e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    }
706e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
716e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    omx = service->getOMX();
726e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    if (omx.get() == NULL) {
736e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        LOGE("android_videoCodec_expose() couldn't access OMX interface");
746e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        return false;
756e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    }
766e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
776e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    // used to check whether no codecs were found, which is a sign of failure
786e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    NbSupportedDecoderTypes = 0;
796e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    for (size_t m = 0 ; m < kNbVideoMimeTypes ; m++) {
806e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        if (OK == QueryCodecs(omx, kVideoMimeTypes[m], true /* queryDecoders */,
816e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi                true /* hwCodecOnly */, &VideoDecoderCapabilities[m])) {
826e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi            if (!VideoDecoderCapabilities[m].empty()) {
836e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi                NbSupportedDecoderTypes++;
846e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi            }
856e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi            // for each decoder of the given decoder ID, verify it is a hardware decoder
866e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi            for (size_t c = 0 ; c < VideoDecoderCapabilities[m].size() ; c++) {
876e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi                VideoDecoderNbProfLevel[c] = 0;
886e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi                const String8& compName =
896e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi                        VideoDecoderCapabilities[m].itemAt(c).mComponentName;
906e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi                // get the number of profiles and levels for this decoder
916e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi                VideoDecoderNbProfLevel[m] =
926e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi                        VideoDecoderCapabilities[m].itemAt(c).mProfileLevels.size();
936e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi                if (VideoDecoderNbProfLevel[m] != 0) {
946e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi                    SL_LOGV("codec %d nb prof/level=%d", m, VideoDecoderNbProfLevel[m]);
956e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi                    break;
966e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi                }
976e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi            }
986e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        }
996e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    }
1006e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
1016e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    return (NbSupportedDecoderTypes > 0);
1026e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi}
1036e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
1046e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
1056e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivivoid android_videoCodec_deinit() {
1066e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    SL_LOGV("android_videoCodec_deinit()");
1076e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    LOGE("android_videoCodec_deinit()");
1086e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    for (size_t m = 0 ; m < kNbVideoMimeTypes ; m++) {
1096e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        VideoDecoderCapabilities[m].clear();
1106e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    }
1116e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi}
1126e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
1136e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
1146e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel TriviXAuint32 android_videoCodec_getNbDecoders() {
1156e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    return NbSupportedDecoderTypes;
1166e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi}
1176e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
1186e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
1196e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivivoid android_videoCodec_getDecoderIds(XAuint32 nbDecoders, XAuint32 *pDecoderIds) {
1206e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    XAuint32 *pIds = pDecoderIds;
1216e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    XAuint32 nbFound = 0;
1226e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    for (size_t m = 0 ; m < kNbVideoMimeTypes ; m++) {
1236e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        if (!VideoDecoderCapabilities[m].empty()) {
1246e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi            *pIds = VideoDecoderIds[m];
1256e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi            pIds++;
1266e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi            nbFound++;
1276e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        }
1286e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        // range check: function can be called for fewer codecs than there are
1296e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        if (nbFound == nbDecoders) {
1306e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi            break;
1316e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        }
1326e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    }
1336e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi}
1346e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
1356e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
1366e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel TriviSLresult android_videoCodec_getProfileLevelCombinationNb(XAuint32 decoderId, XAuint32 *pNb)
1376e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi{
1386e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    // translate a decoder ID to an index in the codec table
1396e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    size_t decoderIndex = 0;
1406e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    *pNb = 0;
1416e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    while (decoderIndex < kNbVideoMimeTypes) {
1426e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        if (decoderId == VideoDecoderIds[decoderIndex]) {
1436e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi            *pNb = VideoDecoderNbProfLevel[decoderIndex];
1446e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        }
1456e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        decoderIndex++;
1466e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    }
1476e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
1486e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    return *pNb > 0 ? XA_RESULT_SUCCESS : XA_RESULT_PARAMETER_INVALID;
1496e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi}
1506e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
1516e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
1526e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel TriviSLresult android_videoCodec_getProfileLevelCombination(XAuint32 decoderId, XAuint32 plIndex,
1536e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        XAVideoCodecDescriptor *pDescr)
1546e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi{
1556e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    // translate a decoder ID to an index in the codec table
1566e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    size_t decoderIndex = 0;
1576e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    while (decoderIndex < kNbVideoMimeTypes) {
1586e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        if (decoderId == VideoDecoderIds[decoderIndex]) {
1596e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi            if (!(plIndex < VideoDecoderCapabilities[decoderIndex].itemAt(0).mProfileLevels.size()))
1606e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi            {
1616e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi                // asking for invalid profile/level
1626e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi                return XA_RESULT_PARAMETER_INVALID;
1636e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi            }
1646e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi            // we only look at the first codec, OpenMAX AL doesn't let you expose the capabilities
1656e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi            //  of multiple codecs
1666e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi            pDescr->codecId = decoderId;
1676e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi            pDescr->profileSetting = convertOpenMaxIlToAl(VideoDecoderCapabilities[decoderIndex].
1686e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi                    itemAt(0).mProfileLevels.itemAt(plIndex).mProfile);
1696e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi            pDescr->levelSetting =  convertOpenMaxIlToAl(VideoDecoderCapabilities[decoderIndex].
1706e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi                    itemAt(0).mProfileLevels.itemAt(plIndex).mLevel);
1716e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi            break;
1726e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        }
1736e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi        decoderIndex++;
1746e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    }
1756e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi    return (decoderIndex < kNbVideoMimeTypes) ? XA_RESULT_SUCCESS : XA_RESULT_PARAMETER_INVALID;
1766e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi}
1776e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi
1786e7e174807fc639c49125ced8962aa369370fbf0Jean-Michel Trivi} // namespace android
179