Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2019 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_TAG "HeicEncoderInfoManager" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | |
| 20 | #include <cstdint> |
| 21 | #include <regex> |
| 22 | |
| 23 | #include <cutils/properties.h> |
| 24 | #include <log/log_main.h> |
| 25 | #include <system/graphics.h> |
| 26 | |
| 27 | #include <media/stagefright/MediaCodecList.h> |
| 28 | #include <media/stagefright/foundation/MediaDefs.h> |
| 29 | #include <media/stagefright/foundation/ABuffer.h> |
| 30 | |
| 31 | #include "HeicEncoderInfoManager.h" |
| 32 | |
| 33 | namespace android { |
| 34 | namespace camera3 { |
| 35 | |
| 36 | HeicEncoderInfoManager::HeicEncoderInfoManager() : |
| 37 | mIsInited(false), |
| 38 | mMinSizeHeic(0, 0), |
| 39 | mMaxSizeHeic(INT32_MAX, INT32_MAX), |
| 40 | mHasHEVC(false), |
| 41 | mHasHEIC(false), |
| 42 | mDisableGrid(false) { |
| 43 | if (initialize() == OK) { |
| 44 | mIsInited = true; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | HeicEncoderInfoManager::~HeicEncoderInfoManager() { |
| 49 | } |
| 50 | |
| 51 | bool HeicEncoderInfoManager::isSizeSupported(int32_t width, int32_t height, bool* useHeic, |
| 52 | bool* useGrid, int64_t* stall) const { |
| 53 | if (useHeic == nullptr || useGrid == nullptr) { |
| 54 | ALOGE("%s: invalid parameters: useHeic %p, useGrid %p", |
| 55 | __FUNCTION__, useHeic, useGrid); |
| 56 | return false; |
| 57 | } |
| 58 | if (!mIsInited) return false; |
| 59 | |
| 60 | bool chooseHeic = false, enableGrid = true; |
| 61 | if (mHasHEIC && width >= mMinSizeHeic.first && |
| 62 | height >= mMinSizeHeic.second && width <= mMaxSizeHeic.first && |
| 63 | height <= mMaxSizeHeic.second) { |
| 64 | chooseHeic = true; |
| 65 | enableGrid = false; |
| 66 | } else if (mHasHEVC) { |
| 67 | bool fullSizeSupportedByHevc = (width >= mMinSizeHevc.first && |
| 68 | height >= mMinSizeHevc.second && |
| 69 | width <= mMaxSizeHevc.first && |
| 70 | height <= mMaxSizeHevc.second); |
| 71 | if (fullSizeSupportedByHevc && (mDisableGrid || |
| 72 | (width <= 1920 && height <= 1080))) { |
| 73 | enableGrid = false; |
| 74 | } |
| 75 | } else { |
| 76 | // No encoder available for the requested size. |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | if (stall != nullptr) { |
| 81 | // Find preferred encoder which advertise |
| 82 | // "measured-frame-rate-WIDTHxHEIGHT-range" key. |
| 83 | const FrameRateMaps& maps = |
| 84 | (chooseHeic && mHeicFrameRateMaps.size() > 0) ? |
| 85 | mHeicFrameRateMaps : mHevcFrameRateMaps; |
| 86 | const auto& closestSize = findClosestSize(maps, width, height); |
| 87 | if (closestSize == maps.end()) { |
| 88 | // The "measured-frame-rate-WIDTHxHEIGHT-range" key is optional. |
| 89 | // Hardcode to some default value (3.33ms * tile count) based on resolution. |
| 90 | *stall = 3333333LL * width * height / (kGridWidth * kGridHeight); |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | // Derive stall durations based on average fps of the closest size. |
| 95 | constexpr int64_t NSEC_PER_SEC = 1000000000LL; |
| 96 | int32_t avgFps = (closestSize->second.first + closestSize->second.second)/2; |
| 97 | float ratio = 1.0f * width * height / |
| 98 | (closestSize->first.first * closestSize->first.second); |
| 99 | *stall = ratio * NSEC_PER_SEC / avgFps; |
| 100 | } |
| 101 | |
| 102 | *useHeic = chooseHeic; |
| 103 | *useGrid = enableGrid; |
| 104 | return true; |
| 105 | } |
| 106 | |
| 107 | status_t HeicEncoderInfoManager::initialize() { |
| 108 | mDisableGrid = property_get_bool("camera.heic.disable_grid", false); |
| 109 | sp<IMediaCodecList> codecsList = MediaCodecList::getInstance(); |
| 110 | if (codecsList == nullptr) { |
| 111 | // No media codec available. |
| 112 | return OK; |
| 113 | } |
| 114 | |
| 115 | sp<AMessage> heicDetails = getCodecDetails(codecsList, MEDIA_MIMETYPE_IMAGE_ANDROID_HEIC); |
| 116 | sp<AMessage> hevcDetails = getCodecDetails(codecsList, MEDIA_MIMETYPE_VIDEO_HEVC); |
| 117 | |
| 118 | if (hevcDetails == nullptr) { |
| 119 | if (heicDetails != nullptr) { |
| 120 | ALOGE("%s: Device must support HEVC codec if HEIC codec is available!", |
| 121 | __FUNCTION__); |
| 122 | return BAD_VALUE; |
| 123 | } |
| 124 | return OK; |
| 125 | } |
| 126 | |
| 127 | // Check CQ mode for HEVC codec |
| 128 | { |
| 129 | AString bitrateModes; |
| 130 | auto hasItem = hevcDetails->findString("feature-bitrate-modes", &bitrateModes); |
| 131 | if (!hasItem) { |
| 132 | ALOGE("%s: Failed to query bitrate modes for HEVC codec", __FUNCTION__); |
| 133 | return BAD_VALUE; |
| 134 | } |
| 135 | ALOGV("%s: HEVC codec's feature-bitrate-modes value is %d, %s", |
| 136 | __FUNCTION__, hasItem, bitrateModes.c_str()); |
| 137 | std::regex pattern("(^|,)CQ($|,)", std::regex_constants::icase); |
| 138 | if (!std::regex_search(bitrateModes.c_str(), pattern)) { |
| 139 | return OK; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | // HEIC size range |
| 144 | if (heicDetails != nullptr) { |
| 145 | auto res = getCodecSizeRange(MEDIA_MIMETYPE_IMAGE_ANDROID_HEIC, |
| 146 | heicDetails, &mMinSizeHeic, &mMaxSizeHeic, &mHeicFrameRateMaps); |
| 147 | if (res != OK) { |
| 148 | ALOGE("%s: Failed to get HEIC codec size range: %s (%d)", __FUNCTION__, |
| 149 | strerror(-res), res); |
| 150 | return BAD_VALUE; |
| 151 | } |
| 152 | mHasHEIC = true; |
| 153 | } |
| 154 | |
| 155 | // HEVC size range |
| 156 | { |
| 157 | auto res = getCodecSizeRange(MEDIA_MIMETYPE_VIDEO_HEVC, |
| 158 | hevcDetails, &mMinSizeHevc, &mMaxSizeHevc, &mHevcFrameRateMaps); |
| 159 | if (res != OK) { |
| 160 | ALOGE("%s: Failed to get HEVC codec size range: %s (%d)", __FUNCTION__, |
| 161 | strerror(-res), res); |
| 162 | return BAD_VALUE; |
| 163 | } |
| 164 | |
| 165 | mHasHEVC = true; |
| 166 | } |
| 167 | |
| 168 | return OK; |
| 169 | } |
| 170 | |
| 171 | status_t HeicEncoderInfoManager::getFrameRateMaps(sp<AMessage> details, FrameRateMaps* maps) { |
| 172 | if (details == nullptr || maps == nullptr) { |
| 173 | ALOGE("%s: Invalid input: details: %p, maps: %p", __FUNCTION__, details.get(), maps); |
| 174 | return BAD_VALUE; |
| 175 | } |
| 176 | |
| 177 | for (size_t i = 0; i < details->countEntries(); i++) { |
| 178 | AMessage::Type type; |
| 179 | const char* entryName = details->getEntryNameAt(i, &type); |
| 180 | if (type != AMessage::kTypeString) continue; |
| 181 | std::regex frameRateNamePattern("measured-frame-rate-([0-9]+)[*x]([0-9]+)-range", |
| 182 | std::regex_constants::icase); |
| 183 | std::cmatch sizeMatch; |
| 184 | if (std::regex_match(entryName, sizeMatch, frameRateNamePattern) && |
| 185 | sizeMatch.size() == 3) { |
| 186 | AMessage::ItemData item = details->getEntryAt(i); |
| 187 | AString fpsRangeStr; |
| 188 | if (item.find(&fpsRangeStr)) { |
| 189 | ALOGV("%s: %s", entryName, fpsRangeStr.c_str()); |
| 190 | std::regex frameRatePattern("([0-9]+)-([0-9]+)"); |
| 191 | std::cmatch fpsMatch; |
| 192 | if (std::regex_match(fpsRangeStr.c_str(), fpsMatch, frameRatePattern) && |
| 193 | fpsMatch.size() == 3) { |
| 194 | maps->emplace( |
| 195 | std::make_pair(stoi(sizeMatch[1]), stoi(sizeMatch[2])), |
| 196 | std::make_pair(stoi(fpsMatch[1]), stoi(fpsMatch[2]))); |
| 197 | } else { |
| 198 | return BAD_VALUE; |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | return OK; |
| 204 | } |
| 205 | |
| 206 | status_t HeicEncoderInfoManager::getCodecSizeRange( |
| 207 | const char* codecName, |
| 208 | sp<AMessage> details, |
| 209 | std::pair<int32_t, int32_t>* minSize, |
| 210 | std::pair<int32_t, int32_t>* maxSize, |
| 211 | FrameRateMaps* frameRateMaps) { |
| 212 | if (codecName == nullptr || minSize == nullptr || maxSize == nullptr || |
| 213 | details == nullptr || frameRateMaps == nullptr) { |
| 214 | return BAD_VALUE; |
| 215 | } |
| 216 | |
| 217 | AString sizeRange; |
| 218 | auto hasItem = details->findString("size-range", &sizeRange); |
| 219 | if (!hasItem) { |
| 220 | ALOGE("%s: Failed to query size range for codec %s", __FUNCTION__, codecName); |
| 221 | return BAD_VALUE; |
| 222 | } |
| 223 | ALOGV("%s: %s codec's size range is %s", __FUNCTION__, codecName, sizeRange.c_str()); |
| 224 | std::regex pattern("([0-9]+)[*x]([0-9]+)-([0-9]+)[*x]([0-9]+)"); |
| 225 | std::cmatch match; |
| 226 | if (std::regex_match(sizeRange.c_str(), match, pattern)) { |
| 227 | if (match.size() == 5) { |
| 228 | minSize->first = stoi(match[1]); |
| 229 | minSize->second = stoi(match[2]); |
| 230 | maxSize->first = stoi(match[3]); |
| 231 | maxSize->second = stoi(match[4]); |
| 232 | if (minSize->first > maxSize->first || |
| 233 | minSize->second > maxSize->second) { |
| 234 | ALOGE("%s: Invalid %s code size range: %s", |
| 235 | __FUNCTION__, codecName, sizeRange.c_str()); |
| 236 | return BAD_VALUE; |
| 237 | } |
| 238 | } else { |
| 239 | return BAD_VALUE; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | auto res = getFrameRateMaps(details, frameRateMaps); |
| 244 | if (res != OK) { |
| 245 | return res; |
| 246 | } |
| 247 | |
| 248 | return OK; |
| 249 | } |
| 250 | |
| 251 | HeicEncoderInfoManager::FrameRateMaps::const_iterator HeicEncoderInfoManager::findClosestSize( |
| 252 | const FrameRateMaps& maps, int32_t width, int32_t height) const { |
| 253 | int32_t minDiff = INT32_MAX; |
| 254 | FrameRateMaps::const_iterator closestIter = maps.begin(); |
| 255 | for (auto iter = maps.begin(); iter != maps.end(); iter++) { |
| 256 | // Use area difference between the sizes to approximate size |
| 257 | // difference. |
| 258 | int32_t diff = abs(iter->first.first * iter->first.second - width * height); |
| 259 | if (diff < minDiff) { |
| 260 | closestIter = iter; |
| 261 | minDiff = diff; |
| 262 | } |
| 263 | } |
| 264 | return closestIter; |
| 265 | } |
| 266 | |
| 267 | sp<AMessage> HeicEncoderInfoManager::getCodecDetails( |
| 268 | sp<IMediaCodecList> codecsList, const char* name) { |
| 269 | ssize_t idx = codecsList->findCodecByType(name, true /*encoder*/); |
| 270 | if (idx < 0) { |
| 271 | return nullptr; |
| 272 | } |
| 273 | |
| 274 | const sp<MediaCodecInfo> info = codecsList->getCodecInfo(idx); |
| 275 | if (info == nullptr) { |
| 276 | ALOGE("%s: Failed to get codec info for %s", __FUNCTION__, name); |
| 277 | return nullptr; |
| 278 | } |
| 279 | const sp<MediaCodecInfo::Capabilities> caps = |
| 280 | info->getCapabilitiesFor(name); |
| 281 | if (caps == nullptr) { |
| 282 | ALOGE("%s: Failed to get capabilities for codec %s", __FUNCTION__, name); |
| 283 | return nullptr; |
| 284 | } |
| 285 | const sp<AMessage> details = caps->getDetails(); |
| 286 | if (details == nullptr) { |
| 287 | ALOGE("%s: Failed to get details for codec %s", __FUNCTION__, name); |
| 288 | return nullptr; |
| 289 | } |
| 290 | |
| 291 | return details; |
| 292 | } |
| 293 | } //namespace camera3 |
| 294 | } // namespace android |