Lajos Molnar | 60b1c0e | 2014-08-06 16:55:46 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014, 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 "MediaCodecInfo" |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include <media/IOMX.h> |
| 22 | |
| 23 | #include <media/MediaCodecInfo.h> |
| 24 | |
| 25 | #include <media/stagefright/foundation/ADebug.h> |
| 26 | #include <media/stagefright/foundation/AMessage.h> |
| 27 | #include <binder/Parcel.h> |
| 28 | |
Lajos Molnar | 60b1c0e | 2014-08-06 16:55:46 -0700 | [diff] [blame] | 29 | namespace android { |
| 30 | |
| 31 | void MediaCodecInfo::Capabilities::getSupportedProfileLevels( |
| 32 | Vector<ProfileLevel> *profileLevels) const { |
| 33 | profileLevels->clear(); |
| 34 | profileLevels->appendVector(mProfileLevels); |
| 35 | } |
| 36 | |
| 37 | void MediaCodecInfo::Capabilities::getSupportedColorFormats( |
| 38 | Vector<uint32_t> *colorFormats) const { |
| 39 | colorFormats->clear(); |
| 40 | colorFormats->appendVector(mColorFormats); |
| 41 | } |
| 42 | |
| 43 | uint32_t MediaCodecInfo::Capabilities::getFlags() const { |
| 44 | return mFlags; |
| 45 | } |
| 46 | |
Lajos Molnar | 2461e0c | 2014-08-12 08:55:25 -0700 | [diff] [blame] | 47 | const sp<AMessage> MediaCodecInfo::Capabilities::getDetails() const { |
Lajos Molnar | 60b1c0e | 2014-08-06 16:55:46 -0700 | [diff] [blame] | 48 | return mDetails; |
| 49 | } |
| 50 | |
| 51 | MediaCodecInfo::Capabilities::Capabilities() |
| 52 | : mFlags(0) { |
| 53 | mDetails = new AMessage; |
| 54 | } |
| 55 | |
| 56 | // static |
| 57 | sp<MediaCodecInfo::Capabilities> MediaCodecInfo::Capabilities::FromParcel( |
| 58 | const Parcel &parcel) { |
| 59 | sp<MediaCodecInfo::Capabilities> caps = new Capabilities(); |
| 60 | size_t size = static_cast<size_t>(parcel.readInt32()); |
| 61 | for (size_t i = 0; i < size; i++) { |
| 62 | ProfileLevel profileLevel; |
| 63 | profileLevel.mProfile = static_cast<uint32_t>(parcel.readInt32()); |
| 64 | profileLevel.mLevel = static_cast<uint32_t>(parcel.readInt32()); |
| 65 | if (caps != NULL) { |
| 66 | caps->mProfileLevels.push_back(profileLevel); |
| 67 | } |
| 68 | } |
| 69 | size = static_cast<size_t>(parcel.readInt32()); |
| 70 | for (size_t i = 0; i < size; i++) { |
| 71 | uint32_t color = static_cast<uint32_t>(parcel.readInt32()); |
| 72 | if (caps != NULL) { |
| 73 | caps->mColorFormats.push_back(color); |
| 74 | } |
| 75 | } |
| 76 | uint32_t flags = static_cast<uint32_t>(parcel.readInt32()); |
| 77 | sp<AMessage> details = AMessage::FromParcel(parcel); |
Pawin Vongmasa | 8dab1730 | 2016-05-02 16:08:39 -0700 | [diff] [blame] | 78 | if (details == NULL) |
| 79 | return NULL; |
Lajos Molnar | 60b1c0e | 2014-08-06 16:55:46 -0700 | [diff] [blame] | 80 | if (caps != NULL) { |
| 81 | caps->mFlags = flags; |
| 82 | caps->mDetails = details; |
| 83 | } |
| 84 | return caps; |
| 85 | } |
| 86 | |
| 87 | status_t MediaCodecInfo::Capabilities::writeToParcel(Parcel *parcel) const { |
Colin Cross | b8c35f9 | 2017-04-27 16:15:51 -0700 | [diff] [blame] | 88 | CHECK_LE(mProfileLevels.size(), static_cast<size_t>(INT32_MAX)); |
Lajos Molnar | 60b1c0e | 2014-08-06 16:55:46 -0700 | [diff] [blame] | 89 | parcel->writeInt32(mProfileLevels.size()); |
| 90 | for (size_t i = 0; i < mProfileLevels.size(); i++) { |
| 91 | parcel->writeInt32(mProfileLevels.itemAt(i).mProfile); |
| 92 | parcel->writeInt32(mProfileLevels.itemAt(i).mLevel); |
| 93 | } |
Colin Cross | b8c35f9 | 2017-04-27 16:15:51 -0700 | [diff] [blame] | 94 | CHECK_LE(mColorFormats.size(), static_cast<size_t>(INT32_MAX)); |
Lajos Molnar | 60b1c0e | 2014-08-06 16:55:46 -0700 | [diff] [blame] | 95 | parcel->writeInt32(mColorFormats.size()); |
| 96 | for (size_t i = 0; i < mColorFormats.size(); i++) { |
| 97 | parcel->writeInt32(mColorFormats.itemAt(i)); |
| 98 | } |
| 99 | parcel->writeInt32(mFlags); |
| 100 | mDetails->writeToParcel(parcel); |
| 101 | return OK; |
| 102 | } |
| 103 | |
Pawin Vongmasa | fbe70ae | 2017-07-27 02:21:57 -0700 | [diff] [blame^] | 104 | void MediaCodecInfo::CapabilitiesWriter::addDetail( |
| 105 | const char* key, const char* value) { |
| 106 | mCap->mDetails->setString(key, value); |
| 107 | } |
| 108 | |
| 109 | void MediaCodecInfo::CapabilitiesWriter::addDetail( |
| 110 | const char* key, int32_t value) { |
| 111 | mCap->mDetails->setInt32(key, value); |
| 112 | } |
| 113 | |
| 114 | void MediaCodecInfo::CapabilitiesWriter::addProfileLevel( |
| 115 | uint32_t profile, uint32_t level) { |
Lajos Molnar | 5b05e49 | 2016-02-04 18:57:45 -0800 | [diff] [blame] | 116 | ProfileLevel profileLevel; |
| 117 | profileLevel.mProfile = profile; |
| 118 | profileLevel.mLevel = level; |
Pawin Vongmasa | fbe70ae | 2017-07-27 02:21:57 -0700 | [diff] [blame^] | 119 | if (mCap->mProfileLevelsSorted.indexOf(profileLevel) < 0) { |
| 120 | mCap->mProfileLevels.push_back(profileLevel); |
| 121 | mCap->mProfileLevelsSorted.add(profileLevel); |
Lajos Molnar | 06e9836 | 2017-08-14 15:57:57 -0700 | [diff] [blame] | 122 | } |
Lajos Molnar | 5b05e49 | 2016-02-04 18:57:45 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Pawin Vongmasa | fbe70ae | 2017-07-27 02:21:57 -0700 | [diff] [blame^] | 125 | void MediaCodecInfo::CapabilitiesWriter::addColorFormat(uint32_t format) { |
| 126 | if (mCap->mColorFormatsSorted.indexOf(format) < 0) { |
| 127 | mCap->mColorFormats.push(format); |
| 128 | mCap->mColorFormatsSorted.add(format); |
Lajos Molnar | 06e9836 | 2017-08-14 15:57:57 -0700 | [diff] [blame] | 129 | } |
Lajos Molnar | 5b05e49 | 2016-02-04 18:57:45 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Pawin Vongmasa | fbe70ae | 2017-07-27 02:21:57 -0700 | [diff] [blame^] | 132 | void MediaCodecInfo::CapabilitiesWriter::addFlags(uint32_t flags) { |
| 133 | mCap->mFlags |= flags; |
| 134 | } |
| 135 | |
| 136 | MediaCodecInfo::CapabilitiesWriter::CapabilitiesWriter( |
| 137 | MediaCodecInfo::Capabilities* cap) : mCap(cap) { |
Lajos Molnar | 5b05e49 | 2016-02-04 18:57:45 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Lajos Molnar | 60b1c0e | 2014-08-06 16:55:46 -0700 | [diff] [blame] | 140 | bool MediaCodecInfo::isEncoder() const { |
| 141 | return mIsEncoder; |
| 142 | } |
| 143 | |
Lajos Molnar | 60b1c0e | 2014-08-06 16:55:46 -0700 | [diff] [blame] | 144 | void MediaCodecInfo::getSupportedMimes(Vector<AString> *mimes) const { |
| 145 | mimes->clear(); |
| 146 | for (size_t ix = 0; ix < mCaps.size(); ix++) { |
| 147 | mimes->push_back(mCaps.keyAt(ix)); |
| 148 | } |
| 149 | } |
| 150 | |
Lajos Molnar | 2461e0c | 2014-08-12 08:55:25 -0700 | [diff] [blame] | 151 | const sp<MediaCodecInfo::Capabilities> |
Lajos Molnar | 60b1c0e | 2014-08-06 16:55:46 -0700 | [diff] [blame] | 152 | MediaCodecInfo::getCapabilitiesFor(const char *mime) const { |
| 153 | ssize_t ix = getCapabilityIndex(mime); |
| 154 | if (ix >= 0) { |
| 155 | return mCaps.valueAt(ix); |
| 156 | } |
| 157 | return NULL; |
| 158 | } |
| 159 | |
| 160 | const char *MediaCodecInfo::getCodecName() const { |
| 161 | return mName.c_str(); |
| 162 | } |
| 163 | |
Pawin Vongmasa | fbe70ae | 2017-07-27 02:21:57 -0700 | [diff] [blame^] | 164 | const char *MediaCodecInfo::getOwnerName() const { |
| 165 | return mOwner.c_str(); |
| 166 | } |
| 167 | |
Lajos Molnar | 60b1c0e | 2014-08-06 16:55:46 -0700 | [diff] [blame] | 168 | // static |
| 169 | sp<MediaCodecInfo> MediaCodecInfo::FromParcel(const Parcel &parcel) { |
| 170 | AString name = AString::FromParcel(parcel); |
Pawin Vongmasa | fbe70ae | 2017-07-27 02:21:57 -0700 | [diff] [blame^] | 171 | AString owner = AString::FromParcel(parcel); |
Lajos Molnar | 60b1c0e | 2014-08-06 16:55:46 -0700 | [diff] [blame] | 172 | bool isEncoder = static_cast<bool>(parcel.readInt32()); |
Pawin Vongmasa | fbe70ae | 2017-07-27 02:21:57 -0700 | [diff] [blame^] | 173 | sp<MediaCodecInfo> info = new MediaCodecInfo; |
| 174 | info->mName = name; |
| 175 | info->mOwner = owner; |
| 176 | info->mIsEncoder = isEncoder; |
Lajos Molnar | 60b1c0e | 2014-08-06 16:55:46 -0700 | [diff] [blame] | 177 | size_t size = static_cast<size_t>(parcel.readInt32()); |
| 178 | for (size_t i = 0; i < size; i++) { |
Lajos Molnar | 60b1c0e | 2014-08-06 16:55:46 -0700 | [diff] [blame] | 179 | AString mime = AString::FromParcel(parcel); |
| 180 | sp<Capabilities> caps = Capabilities::FromParcel(parcel); |
Pawin Vongmasa | 8dab1730 | 2016-05-02 16:08:39 -0700 | [diff] [blame] | 181 | if (caps == NULL) |
| 182 | return NULL; |
Lajos Molnar | 60b1c0e | 2014-08-06 16:55:46 -0700 | [diff] [blame] | 183 | if (info != NULL) { |
| 184 | info->mCaps.add(mime, caps); |
| 185 | } |
| 186 | } |
| 187 | return info; |
| 188 | } |
| 189 | |
| 190 | status_t MediaCodecInfo::writeToParcel(Parcel *parcel) const { |
| 191 | mName.writeToParcel(parcel); |
Pawin Vongmasa | fbe70ae | 2017-07-27 02:21:57 -0700 | [diff] [blame^] | 192 | mOwner.writeToParcel(parcel); |
Lajos Molnar | 60b1c0e | 2014-08-06 16:55:46 -0700 | [diff] [blame] | 193 | parcel->writeInt32(mIsEncoder); |
Lajos Molnar | 60b1c0e | 2014-08-06 16:55:46 -0700 | [diff] [blame] | 194 | parcel->writeInt32(mCaps.size()); |
| 195 | for (size_t i = 0; i < mCaps.size(); i++) { |
| 196 | mCaps.keyAt(i).writeToParcel(parcel); |
| 197 | mCaps.valueAt(i)->writeToParcel(parcel); |
| 198 | } |
| 199 | return OK; |
| 200 | } |
| 201 | |
| 202 | ssize_t MediaCodecInfo::getCapabilityIndex(const char *mime) const { |
Marco Nelissen | 89b2a0a | 2016-05-03 11:10:42 -0700 | [diff] [blame] | 203 | if (mime) { |
| 204 | for (size_t ix = 0; ix < mCaps.size(); ix++) { |
| 205 | if (mCaps.keyAt(ix).equalsIgnoreCase(mime)) { |
| 206 | return ix; |
| 207 | } |
Lajos Molnar | 60b1c0e | 2014-08-06 16:55:46 -0700 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | return -1; |
| 211 | } |
| 212 | |
Pawin Vongmasa | fbe70ae | 2017-07-27 02:21:57 -0700 | [diff] [blame^] | 213 | MediaCodecInfo::MediaCodecInfo() { |
Lajos Molnar | 60b1c0e | 2014-08-06 16:55:46 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Pawin Vongmasa | fbe70ae | 2017-07-27 02:21:57 -0700 | [diff] [blame^] | 216 | void MediaCodecInfoWriter::setName(const char* name) { |
| 217 | mInfo->mName = name; |
| 218 | } |
| 219 | |
| 220 | void MediaCodecInfoWriter::setOwner(const char* owner) { |
| 221 | mInfo->mOwner = owner; |
| 222 | } |
| 223 | |
| 224 | void MediaCodecInfoWriter::setEncoder(bool isEncoder) { |
| 225 | mInfo->mIsEncoder = isEncoder; |
| 226 | } |
| 227 | |
| 228 | std::unique_ptr<MediaCodecInfo::CapabilitiesWriter> |
| 229 | MediaCodecInfoWriter::addMime(const char *mime) { |
| 230 | ssize_t ix = mInfo->getCapabilityIndex(mime); |
Lajos Molnar | 60b1c0e | 2014-08-06 16:55:46 -0700 | [diff] [blame] | 231 | if (ix >= 0) { |
Pawin Vongmasa | fbe70ae | 2017-07-27 02:21:57 -0700 | [diff] [blame^] | 232 | return std::unique_ptr<MediaCodecInfo::CapabilitiesWriter>( |
| 233 | new MediaCodecInfo::CapabilitiesWriter( |
| 234 | mInfo->mCaps.valueAt(ix).get())); |
Lajos Molnar | 60b1c0e | 2014-08-06 16:55:46 -0700 | [diff] [blame] | 235 | } |
Pawin Vongmasa | fbe70ae | 2017-07-27 02:21:57 -0700 | [diff] [blame^] | 236 | sp<MediaCodecInfo::Capabilities> caps = new MediaCodecInfo::Capabilities(); |
| 237 | mInfo->mCaps.add(AString(mime), caps); |
| 238 | return std::unique_ptr<MediaCodecInfo::CapabilitiesWriter>( |
| 239 | new MediaCodecInfo::CapabilitiesWriter(caps.get())); |
Lajos Molnar | 60b1c0e | 2014-08-06 16:55:46 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Pawin Vongmasa | fbe70ae | 2017-07-27 02:21:57 -0700 | [diff] [blame^] | 242 | bool MediaCodecInfoWriter::removeMime(const char *mime) { |
| 243 | ssize_t ix = mInfo->getCapabilityIndex(mime); |
Lajos Molnar | 6ff58f0 | 2014-08-11 16:46:15 -0700 | [diff] [blame] | 244 | if (ix >= 0) { |
Pawin Vongmasa | fbe70ae | 2017-07-27 02:21:57 -0700 | [diff] [blame^] | 245 | mInfo->mCaps.removeItemsAt(ix); |
| 246 | return true; |
Lajos Molnar | 6ff58f0 | 2014-08-11 16:46:15 -0700 | [diff] [blame] | 247 | } |
Pawin Vongmasa | fbe70ae | 2017-07-27 02:21:57 -0700 | [diff] [blame^] | 248 | return false; |
Lajos Molnar | 6ff58f0 | 2014-08-11 16:46:15 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Pawin Vongmasa | fbe70ae | 2017-07-27 02:21:57 -0700 | [diff] [blame^] | 251 | MediaCodecInfoWriter::MediaCodecInfoWriter(MediaCodecInfo* info) : |
| 252 | mInfo(info) { |
Lajos Molnar | 732c6d9 | 2014-08-14 19:54:08 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Lajos Molnar | 60b1c0e | 2014-08-06 16:55:46 -0700 | [diff] [blame] | 255 | } // namespace android |