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 "Camera3-HeicCompositeStream" |
| 18 | #define ATRACE_TAG ATRACE_TAG_CAMERA |
Susmitha Gummalla | e911f2e | 2020-11-23 09:57:03 -0800 | [diff] [blame] | 19 | #define ALIGN(x, mask) ( ((x) + (mask) - 1) & ~((mask) - 1) ) |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 20 | //#define LOG_NDEBUG 0 |
| 21 | |
| 22 | #include <linux/memfd.h> |
| 23 | #include <pthread.h> |
| 24 | #include <sys/syscall.h> |
| 25 | |
Jayant Chowdhary | c67af1b | 2022-04-07 18:05:04 +0000 | [diff] [blame] | 26 | #include <aidl/android/hardware/camera/device/CameraBlob.h> |
| 27 | #include <aidl/android/hardware/camera/device/CameraBlobId.h> |
Shuzhen Wang | 219c299 | 2019-02-15 17:24:28 -0800 | [diff] [blame] | 28 | #include <libyuv.h> |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 29 | #include <gui/Surface.h> |
| 30 | #include <utils/Log.h> |
| 31 | #include <utils/Trace.h> |
Austin Borger | 0fb3ad9 | 2023-06-01 16:51:35 -0700 | [diff] [blame^] | 32 | #include <camera/StringUtils.h> |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 33 | |
Marco Nelissen | 13aa1a4 | 2019-09-27 10:21:55 -0700 | [diff] [blame] | 34 | #include <mediadrm/ICrypto.h> |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 35 | #include <media/MediaCodecBuffer.h> |
| 36 | #include <media/stagefright/foundation/ABuffer.h> |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 37 | #include <media/stagefright/foundation/MediaDefs.h> |
| 38 | #include <media/stagefright/MediaCodecConstants.h> |
| 39 | |
| 40 | #include "common/CameraDeviceBase.h" |
| 41 | #include "utils/ExifUtils.h" |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 42 | #include "utils/SessionConfigurationUtils.h" |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 43 | #include "HeicEncoderInfoManager.h" |
| 44 | #include "HeicCompositeStream.h" |
| 45 | |
Jayant Chowdhary | c67af1b | 2022-04-07 18:05:04 +0000 | [diff] [blame] | 46 | using aidl::android::hardware::camera::device::CameraBlob; |
| 47 | using aidl::android::hardware::camera::device::CameraBlobId; |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 48 | |
| 49 | namespace android { |
| 50 | namespace camera3 { |
| 51 | |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 52 | HeicCompositeStream::HeicCompositeStream(sp<CameraDeviceBase> device, |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 53 | wp<hardware::camera2::ICameraDeviceCallbacks> cb) : |
| 54 | CompositeStream(device, cb), |
| 55 | mUseHeic(false), |
| 56 | mNumOutputTiles(1), |
| 57 | mOutputWidth(0), |
| 58 | mOutputHeight(0), |
| 59 | mMaxHeicBufferSize(0), |
| 60 | mGridWidth(HeicEncoderInfoManager::kGridWidth), |
| 61 | mGridHeight(HeicEncoderInfoManager::kGridHeight), |
| 62 | mGridRows(1), |
| 63 | mGridCols(1), |
| 64 | mUseGrid(false), |
| 65 | mAppSegmentStreamId(-1), |
| 66 | mAppSegmentSurfaceId(-1), |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 67 | mMainImageStreamId(-1), |
| 68 | mMainImageSurfaceId(-1), |
| 69 | mYuvBufferAcquired(false), |
| 70 | mProducerListener(new ProducerListener()), |
Shuzhen Wang | 3d00ee5 | 2019-09-25 14:19:28 -0700 | [diff] [blame] | 71 | mDequeuedOutputBufferCnt(0), |
| 72 | mCodecOutputCounter(0), |
Shuzhen Wang | 62f49ed | 2019-09-04 14:07:53 -0700 | [diff] [blame] | 73 | mQuality(-1), |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 74 | mGridTimestampUs(0), |
| 75 | mStatusId(StatusTracker::NO_STATUS_ID) { |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | HeicCompositeStream::~HeicCompositeStream() { |
| 79 | // Call deinitCodec in case stream hasn't been deleted yet to avoid any |
| 80 | // memory/resource leak. |
| 81 | deinitCodec(); |
| 82 | |
| 83 | mInputAppSegmentBuffers.clear(); |
| 84 | mCodecOutputBuffers.clear(); |
| 85 | |
| 86 | mAppSegmentStreamId = -1; |
| 87 | mAppSegmentSurfaceId = -1; |
| 88 | mAppSegmentConsumer.clear(); |
| 89 | mAppSegmentSurface.clear(); |
| 90 | |
| 91 | mMainImageStreamId = -1; |
| 92 | mMainImageSurfaceId = -1; |
| 93 | mMainImageConsumer.clear(); |
| 94 | mMainImageSurface.clear(); |
| 95 | } |
| 96 | |
| 97 | bool HeicCompositeStream::isHeicCompositeStream(const sp<Surface> &surface) { |
| 98 | ANativeWindow *anw = surface.get(); |
| 99 | status_t err; |
| 100 | int format; |
| 101 | if ((err = anw->query(anw, NATIVE_WINDOW_FORMAT, &format)) != OK) { |
Austin Borger | 0fb3ad9 | 2023-06-01 16:51:35 -0700 | [diff] [blame^] | 102 | std::string msg = fmt::sprintf("Failed to query Surface format: %s (%d)", strerror(-err), |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 103 | err); |
Austin Borger | 0fb3ad9 | 2023-06-01 16:51:35 -0700 | [diff] [blame^] | 104 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 105 | return false; |
| 106 | } |
| 107 | |
| 108 | int dataspace; |
| 109 | if ((err = anw->query(anw, NATIVE_WINDOW_DEFAULT_DATASPACE, &dataspace)) != OK) { |
Austin Borger | 0fb3ad9 | 2023-06-01 16:51:35 -0700 | [diff] [blame^] | 110 | std::string msg = fmt::sprintf("Failed to query Surface dataspace: %s (%d)", strerror(-err), |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 111 | err); |
Austin Borger | 0fb3ad9 | 2023-06-01 16:51:35 -0700 | [diff] [blame^] | 112 | ALOGE("%s: %s", __FUNCTION__, msg.c_str()); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 113 | return false; |
| 114 | } |
| 115 | |
| 116 | return ((format == HAL_PIXEL_FORMAT_BLOB) && (dataspace == HAL_DATASPACE_HEIF)); |
| 117 | } |
| 118 | |
| 119 | status_t HeicCompositeStream::createInternalStreams(const std::vector<sp<Surface>>& consumers, |
| 120 | bool /*hasDeferredConsumer*/, uint32_t width, uint32_t height, int format, |
Austin Borger | 0fb3ad9 | 2023-06-01 16:51:35 -0700 | [diff] [blame^] | 121 | camera_stream_rotation_t rotation, int *id, const std::string& physicalCameraId, |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 122 | const std::unordered_set<int32_t> &sensorPixelModesUsed, |
| 123 | std::vector<int> *surfaceIds, |
| 124 | int /*streamSetId*/, bool /*isShared*/) { |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 125 | |
| 126 | sp<CameraDeviceBase> device = mDevice.promote(); |
| 127 | if (!device.get()) { |
| 128 | ALOGE("%s: Invalid camera device!", __FUNCTION__); |
| 129 | return NO_INIT; |
| 130 | } |
| 131 | |
| 132 | status_t res = initializeCodec(width, height, device); |
| 133 | if (res != OK) { |
| 134 | ALOGE("%s: Failed to initialize HEIC/HEVC codec: %s (%d)", |
| 135 | __FUNCTION__, strerror(-res), res); |
| 136 | return NO_INIT; |
| 137 | } |
| 138 | |
| 139 | sp<IGraphicBufferProducer> producer; |
| 140 | sp<IGraphicBufferConsumer> consumer; |
| 141 | BufferQueue::createBufferQueue(&producer, &consumer); |
Michael Gonzalez | b5986a3 | 2019-10-09 15:38:17 -0700 | [diff] [blame] | 142 | mAppSegmentConsumer = new CpuConsumer(consumer, kMaxAcquiredAppSegment); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 143 | mAppSegmentConsumer->setFrameAvailableListener(this); |
| 144 | mAppSegmentConsumer->setName(String8("Camera3-HeicComposite-AppSegmentStream")); |
| 145 | mAppSegmentSurface = new Surface(producer); |
| 146 | |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 147 | mStaticInfo = device->info(); |
| 148 | |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 149 | res = device->createStream(mAppSegmentSurface, mAppSegmentMaxSize, 1, format, |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 150 | kAppSegmentDataSpace, rotation, &mAppSegmentStreamId, physicalCameraId, |
| 151 | sensorPixelModesUsed,surfaceIds); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 152 | if (res == OK) { |
| 153 | mAppSegmentSurfaceId = (*surfaceIds)[0]; |
| 154 | } else { |
| 155 | ALOGE("%s: Failed to create JPEG App segment stream: %s (%d)", __FUNCTION__, |
| 156 | strerror(-res), res); |
| 157 | return res; |
| 158 | } |
| 159 | |
| 160 | if (!mUseGrid) { |
| 161 | res = mCodec->createInputSurface(&producer); |
| 162 | if (res != OK) { |
| 163 | ALOGE("%s: Failed to create input surface for Heic codec: %s (%d)", |
| 164 | __FUNCTION__, strerror(-res), res); |
| 165 | return res; |
| 166 | } |
| 167 | } else { |
| 168 | BufferQueue::createBufferQueue(&producer, &consumer); |
| 169 | mMainImageConsumer = new CpuConsumer(consumer, 1); |
| 170 | mMainImageConsumer->setFrameAvailableListener(this); |
| 171 | mMainImageConsumer->setName(String8("Camera3-HeicComposite-HevcInputYUVStream")); |
| 172 | } |
| 173 | mMainImageSurface = new Surface(producer); |
| 174 | |
| 175 | res = mCodec->start(); |
| 176 | if (res != OK) { |
| 177 | ALOGE("%s: Failed to start codec: %s (%d)", __FUNCTION__, |
| 178 | strerror(-res), res); |
| 179 | return res; |
| 180 | } |
| 181 | |
| 182 | std::vector<int> sourceSurfaceId; |
| 183 | //Use YUV_888 format if framework tiling is needed. |
| 184 | int srcStreamFmt = mUseGrid ? HAL_PIXEL_FORMAT_YCbCr_420_888 : |
| 185 | HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED; |
| 186 | res = device->createStream(mMainImageSurface, width, height, srcStreamFmt, kHeifDataSpace, |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 187 | rotation, id, physicalCameraId, sensorPixelModesUsed, &sourceSurfaceId); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 188 | if (res == OK) { |
| 189 | mMainImageSurfaceId = sourceSurfaceId[0]; |
| 190 | mMainImageStreamId = *id; |
| 191 | } else { |
| 192 | ALOGE("%s: Failed to create main image stream: %s (%d)", __FUNCTION__, |
| 193 | strerror(-res), res); |
| 194 | return res; |
| 195 | } |
| 196 | |
| 197 | mOutputSurface = consumers[0]; |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 198 | res = registerCompositeStreamListener(mMainImageStreamId); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 199 | if (res != OK) { |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 200 | ALOGE("%s: Failed to register HAL main image stream: %s (%d)", __FUNCTION__, |
| 201 | strerror(-res), res); |
| 202 | return res; |
| 203 | } |
| 204 | |
| 205 | res = registerCompositeStreamListener(mAppSegmentStreamId); |
| 206 | if (res != OK) { |
| 207 | ALOGE("%s: Failed to register HAL app segment stream: %s (%d)", __FUNCTION__, |
| 208 | strerror(-res), res); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 209 | return res; |
| 210 | } |
| 211 | |
Shuzhen Wang | 219c299 | 2019-02-15 17:24:28 -0800 | [diff] [blame] | 212 | initCopyRowFunction(width); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 213 | return res; |
| 214 | } |
| 215 | |
| 216 | status_t HeicCompositeStream::deleteInternalStreams() { |
| 217 | requestExit(); |
| 218 | auto res = join(); |
| 219 | if (res != OK) { |
| 220 | ALOGE("%s: Failed to join with the main processing thread: %s (%d)", __FUNCTION__, |
| 221 | strerror(-res), res); |
| 222 | } |
| 223 | |
| 224 | deinitCodec(); |
| 225 | |
| 226 | if (mAppSegmentStreamId >= 0) { |
Emilian Peev | c0fe54c | 2020-03-11 14:05:07 -0700 | [diff] [blame] | 227 | // Camera devices may not be valid after switching to offline mode. |
| 228 | // In this case, all offline streams including internal composite streams |
| 229 | // are managed and released by the offline session. |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 230 | sp<CameraDeviceBase> device = mDevice.promote(); |
Emilian Peev | c0fe54c | 2020-03-11 14:05:07 -0700 | [diff] [blame] | 231 | if (device.get() != nullptr) { |
| 232 | res = device->deleteStream(mAppSegmentStreamId); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 233 | } |
| 234 | |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 235 | mAppSegmentStreamId = -1; |
| 236 | } |
| 237 | |
Shuzhen Wang | 2c54504 | 2019-02-07 10:27:35 -0800 | [diff] [blame] | 238 | if (mOutputSurface != nullptr) { |
| 239 | mOutputSurface->disconnect(NATIVE_WINDOW_API_CAMERA); |
| 240 | mOutputSurface.clear(); |
| 241 | } |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 242 | |
| 243 | sp<StatusTracker> statusTracker = mStatusTracker.promote(); |
| 244 | if (statusTracker != nullptr && mStatusId != StatusTracker::NO_STATUS_ID) { |
| 245 | statusTracker->removeComponent(mStatusId); |
| 246 | mStatusId = StatusTracker::NO_STATUS_ID; |
| 247 | } |
| 248 | |
| 249 | if (mPendingInputFrames.size() > 0) { |
| 250 | ALOGW("%s: mPendingInputFrames has %zu stale entries", |
| 251 | __FUNCTION__, mPendingInputFrames.size()); |
| 252 | mPendingInputFrames.clear(); |
| 253 | } |
| 254 | |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 255 | return res; |
| 256 | } |
| 257 | |
| 258 | void HeicCompositeStream::onBufferReleased(const BufferInfo& bufferInfo) { |
| 259 | Mutex::Autolock l(mMutex); |
| 260 | |
| 261 | if (bufferInfo.mError) return; |
| 262 | |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 263 | if (bufferInfo.mStreamId == mMainImageStreamId) { |
| 264 | mMainImageFrameNumbers.push(bufferInfo.mFrameNumber); |
| 265 | mCodecOutputBufferFrameNumbers.push(bufferInfo.mFrameNumber); |
| 266 | ALOGV("%s: [%" PRId64 "]: Adding main image frame number (%zu frame numbers in total)", |
| 267 | __FUNCTION__, bufferInfo.mFrameNumber, mMainImageFrameNumbers.size()); |
| 268 | } else if (bufferInfo.mStreamId == mAppSegmentStreamId) { |
| 269 | mAppSegmentFrameNumbers.push(bufferInfo.mFrameNumber); |
| 270 | ALOGV("%s: [%" PRId64 "]: Adding app segment frame number (%zu frame numbers in total)", |
| 271 | __FUNCTION__, bufferInfo.mFrameNumber, mAppSegmentFrameNumbers.size()); |
| 272 | } |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | // We need to get the settings early to handle the case where the codec output |
| 276 | // arrives earlier than result metadata. |
| 277 | void HeicCompositeStream::onBufferRequestForFrameNumber(uint64_t frameNumber, int streamId, |
| 278 | const CameraMetadata& settings) { |
| 279 | ATRACE_ASYNC_BEGIN("HEIC capture", frameNumber); |
| 280 | |
| 281 | Mutex::Autolock l(mMutex); |
| 282 | if (mErrorState || (streamId != getStreamId())) { |
| 283 | return; |
| 284 | } |
| 285 | |
| 286 | mPendingCaptureResults.emplace(frameNumber, CameraMetadata()); |
| 287 | |
| 288 | camera_metadata_ro_entry entry; |
| 289 | |
| 290 | int32_t orientation = 0; |
| 291 | entry = settings.find(ANDROID_JPEG_ORIENTATION); |
| 292 | if (entry.count == 1) { |
| 293 | orientation = entry.data.i32[0]; |
| 294 | } |
| 295 | |
| 296 | int32_t quality = kDefaultJpegQuality; |
| 297 | entry = settings.find(ANDROID_JPEG_QUALITY); |
| 298 | if (entry.count == 1) { |
| 299 | quality = entry.data.i32[0]; |
| 300 | } |
| 301 | |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 302 | mSettingsByFrameNumber[frameNumber] = {orientation, quality}; |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | void HeicCompositeStream::onFrameAvailable(const BufferItem& item) { |
| 306 | if (item.mDataSpace == static_cast<android_dataspace>(kAppSegmentDataSpace)) { |
| 307 | ALOGV("%s: JPEG APP segments buffer with ts: %" PRIu64 " ms. arrived!", |
| 308 | __func__, ns2ms(item.mTimestamp)); |
| 309 | |
| 310 | Mutex::Autolock l(mMutex); |
| 311 | if (!mErrorState) { |
| 312 | mInputAppSegmentBuffers.push_back(item.mTimestamp); |
| 313 | mInputReadyCondition.signal(); |
| 314 | } |
| 315 | } else if (item.mDataSpace == kHeifDataSpace) { |
| 316 | ALOGV("%s: YUV_888 buffer with ts: %" PRIu64 " ms. arrived!", |
| 317 | __func__, ns2ms(item.mTimestamp)); |
| 318 | |
| 319 | Mutex::Autolock l(mMutex); |
| 320 | if (!mUseGrid) { |
| 321 | ALOGE("%s: YUV_888 internal stream is only supported for HEVC tiling", |
| 322 | __FUNCTION__); |
| 323 | return; |
| 324 | } |
| 325 | if (!mErrorState) { |
| 326 | mInputYuvBuffers.push_back(item.mTimestamp); |
| 327 | mInputReadyCondition.signal(); |
| 328 | } |
| 329 | } else { |
| 330 | ALOGE("%s: Unexpected data space: 0x%x", __FUNCTION__, item.mDataSpace); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | status_t HeicCompositeStream::getCompositeStreamInfo(const OutputStreamInfo &streamInfo, |
| 335 | const CameraMetadata& ch, std::vector<OutputStreamInfo>* compositeOutput /*out*/) { |
| 336 | if (compositeOutput == nullptr) { |
| 337 | return BAD_VALUE; |
| 338 | } |
| 339 | |
| 340 | compositeOutput->clear(); |
| 341 | |
| 342 | bool useGrid, useHeic; |
| 343 | bool isSizeSupported = isSizeSupportedByHeifEncoder( |
| 344 | streamInfo.width, streamInfo.height, &useHeic, &useGrid, nullptr); |
| 345 | if (!isSizeSupported) { |
| 346 | // Size is not supported by either encoder. |
| 347 | return OK; |
| 348 | } |
| 349 | |
| 350 | compositeOutput->insert(compositeOutput->end(), 2, streamInfo); |
| 351 | |
| 352 | // JPEG APPS segments Blob stream info |
| 353 | (*compositeOutput)[0].width = calcAppSegmentMaxSize(ch); |
| 354 | (*compositeOutput)[0].height = 1; |
| 355 | (*compositeOutput)[0].format = HAL_PIXEL_FORMAT_BLOB; |
| 356 | (*compositeOutput)[0].dataSpace = kAppSegmentDataSpace; |
| 357 | (*compositeOutput)[0].consumerUsage = GRALLOC_USAGE_SW_READ_OFTEN; |
| 358 | |
| 359 | // YUV/IMPLEMENTATION_DEFINED stream info |
| 360 | (*compositeOutput)[1].width = streamInfo.width; |
| 361 | (*compositeOutput)[1].height = streamInfo.height; |
| 362 | (*compositeOutput)[1].format = useGrid ? HAL_PIXEL_FORMAT_YCbCr_420_888 : |
| 363 | HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED; |
| 364 | (*compositeOutput)[1].dataSpace = kHeifDataSpace; |
| 365 | (*compositeOutput)[1].consumerUsage = useHeic ? GRALLOC_USAGE_HW_IMAGE_ENCODER : |
| 366 | useGrid ? GRALLOC_USAGE_SW_READ_OFTEN : GRALLOC_USAGE_HW_VIDEO_ENCODER; |
| 367 | |
| 368 | return NO_ERROR; |
| 369 | } |
| 370 | |
| 371 | bool HeicCompositeStream::isSizeSupportedByHeifEncoder(int32_t width, int32_t height, |
Chong Zhang | 688abaa | 2019-05-17 16:32:23 -0700 | [diff] [blame] | 372 | bool* useHeic, bool* useGrid, int64_t* stall, AString* hevcName) { |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 373 | static HeicEncoderInfoManager& heicManager = HeicEncoderInfoManager::getInstance(); |
Chong Zhang | 688abaa | 2019-05-17 16:32:23 -0700 | [diff] [blame] | 374 | return heicManager.isSizeSupported(width, height, useHeic, useGrid, stall, hevcName); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | bool HeicCompositeStream::isInMemoryTempFileSupported() { |
| 378 | int memfd = syscall(__NR_memfd_create, "HEIF-try-memfd", MFD_CLOEXEC); |
| 379 | if (memfd == -1) { |
| 380 | if (errno != ENOSYS) { |
| 381 | ALOGE("%s: Failed to create tmpfs file. errno %d", __FUNCTION__, errno); |
| 382 | } |
| 383 | return false; |
| 384 | } |
| 385 | close(memfd); |
| 386 | return true; |
| 387 | } |
| 388 | |
| 389 | void HeicCompositeStream::onHeicOutputFrameAvailable( |
| 390 | const CodecOutputBufferInfo& outputBufferInfo) { |
| 391 | Mutex::Autolock l(mMutex); |
| 392 | |
| 393 | ALOGV("%s: index %d, offset %d, size %d, time %" PRId64 ", flags 0x%x", |
| 394 | __FUNCTION__, outputBufferInfo.index, outputBufferInfo.offset, |
| 395 | outputBufferInfo.size, outputBufferInfo.timeUs, outputBufferInfo.flags); |
| 396 | |
| 397 | if (!mErrorState) { |
| 398 | if ((outputBufferInfo.size > 0) && |
| 399 | ((outputBufferInfo.flags & MediaCodec::BUFFER_FLAG_CODECCONFIG) == 0)) { |
| 400 | mCodecOutputBuffers.push_back(outputBufferInfo); |
| 401 | mInputReadyCondition.signal(); |
| 402 | } else { |
Shuzhen Wang | 3d00ee5 | 2019-09-25 14:19:28 -0700 | [diff] [blame] | 403 | ALOGV("%s: Releasing output buffer: size %d flags: 0x%x ", __FUNCTION__, |
| 404 | outputBufferInfo.size, outputBufferInfo.flags); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 405 | mCodec->releaseOutputBuffer(outputBufferInfo.index); |
| 406 | } |
| 407 | } else { |
| 408 | mCodec->releaseOutputBuffer(outputBufferInfo.index); |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | void HeicCompositeStream::onHeicInputFrameAvailable(int32_t index) { |
| 413 | Mutex::Autolock l(mMutex); |
| 414 | |
| 415 | if (!mUseGrid) { |
| 416 | ALOGE("%s: Codec YUV input mode must only be used for Hevc tiling mode", __FUNCTION__); |
| 417 | return; |
| 418 | } |
| 419 | |
| 420 | mCodecInputBuffers.push_back(index); |
| 421 | mInputReadyCondition.signal(); |
| 422 | } |
| 423 | |
| 424 | void HeicCompositeStream::onHeicFormatChanged(sp<AMessage>& newFormat) { |
| 425 | if (newFormat == nullptr) { |
| 426 | ALOGE("%s: newFormat must not be null!", __FUNCTION__); |
| 427 | return; |
| 428 | } |
| 429 | |
| 430 | Mutex::Autolock l(mMutex); |
| 431 | |
| 432 | AString mime; |
| 433 | AString mimeHeic(MIMETYPE_IMAGE_ANDROID_HEIC); |
| 434 | newFormat->findString(KEY_MIME, &mime); |
| 435 | if (mime != mimeHeic) { |
| 436 | // For HEVC codec, below keys need to be filled out or overwritten so that the |
| 437 | // muxer can handle them as HEIC output image. |
| 438 | newFormat->setString(KEY_MIME, mimeHeic); |
| 439 | newFormat->setInt32(KEY_WIDTH, mOutputWidth); |
| 440 | newFormat->setInt32(KEY_HEIGHT, mOutputHeight); |
Shuzhen Wang | 1403d95 | 2023-07-31 20:42:19 +0000 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | if (mUseGrid || mUseHeic) { |
| 444 | int32_t gridRows, gridCols, tileWidth, tileHeight; |
| 445 | if (newFormat->findInt32(KEY_GRID_ROWS, &gridRows) && |
| 446 | newFormat->findInt32(KEY_GRID_COLUMNS, &gridCols) && |
| 447 | newFormat->findInt32(KEY_TILE_WIDTH, &tileWidth) && |
| 448 | newFormat->findInt32(KEY_TILE_HEIGHT, &tileHeight)) { |
| 449 | mGridWidth = tileWidth; |
| 450 | mGridHeight = tileHeight; |
| 451 | mGridRows = gridRows; |
| 452 | mGridCols = gridCols; |
| 453 | } else { |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 454 | newFormat->setInt32(KEY_TILE_WIDTH, mGridWidth); |
| 455 | newFormat->setInt32(KEY_TILE_HEIGHT, mGridHeight); |
| 456 | newFormat->setInt32(KEY_GRID_ROWS, mGridRows); |
| 457 | newFormat->setInt32(KEY_GRID_COLUMNS, mGridCols); |
Shuzhen Wang | 1403d95 | 2023-07-31 20:42:19 +0000 | [diff] [blame] | 458 | } |
| 459 | int32_t left, top, right, bottom; |
| 460 | if (newFormat->findRect("crop", &left, &top, &right, &bottom)) { |
| 461 | newFormat->setRect("crop", 0, 0, mOutputWidth - 1, mOutputHeight - 1); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 462 | } |
| 463 | } |
| 464 | newFormat->setInt32(KEY_IS_DEFAULT, 1 /*isPrimary*/); |
| 465 | |
| 466 | int32_t gridRows, gridCols; |
| 467 | if (newFormat->findInt32(KEY_GRID_ROWS, &gridRows) && |
| 468 | newFormat->findInt32(KEY_GRID_COLUMNS, &gridCols)) { |
| 469 | mNumOutputTiles = gridRows * gridCols; |
| 470 | } else { |
| 471 | mNumOutputTiles = 1; |
| 472 | } |
| 473 | |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 474 | mFormat = newFormat; |
Shuzhen Wang | 3d00ee5 | 2019-09-25 14:19:28 -0700 | [diff] [blame] | 475 | |
| 476 | ALOGV("%s: mNumOutputTiles is %zu", __FUNCTION__, mNumOutputTiles); |
| 477 | mInputReadyCondition.signal(); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | void HeicCompositeStream::onHeicCodecError() { |
| 481 | Mutex::Autolock l(mMutex); |
| 482 | mErrorState = true; |
| 483 | } |
| 484 | |
| 485 | status_t HeicCompositeStream::configureStream() { |
| 486 | if (isRunning()) { |
| 487 | // Processing thread is already running, nothing more to do. |
| 488 | return NO_ERROR; |
| 489 | } |
| 490 | |
| 491 | if (mOutputSurface.get() == nullptr) { |
| 492 | ALOGE("%s: No valid output surface set!", __FUNCTION__); |
| 493 | return NO_INIT; |
| 494 | } |
| 495 | |
| 496 | auto res = mOutputSurface->connect(NATIVE_WINDOW_API_CAMERA, mProducerListener); |
| 497 | if (res != OK) { |
| 498 | ALOGE("%s: Unable to connect to native window for stream %d", |
| 499 | __FUNCTION__, mMainImageStreamId); |
| 500 | return res; |
| 501 | } |
| 502 | |
| 503 | if ((res = native_window_set_buffers_format(mOutputSurface.get(), HAL_PIXEL_FORMAT_BLOB)) |
| 504 | != OK) { |
| 505 | ALOGE("%s: Unable to configure stream buffer format for stream %d", __FUNCTION__, |
| 506 | mMainImageStreamId); |
| 507 | return res; |
| 508 | } |
| 509 | |
| 510 | ANativeWindow *anwConsumer = mOutputSurface.get(); |
| 511 | int maxConsumerBuffers; |
| 512 | if ((res = anwConsumer->query(anwConsumer, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, |
| 513 | &maxConsumerBuffers)) != OK) { |
| 514 | ALOGE("%s: Unable to query consumer undequeued" |
| 515 | " buffer count for stream %d", __FUNCTION__, mMainImageStreamId); |
| 516 | return res; |
| 517 | } |
| 518 | |
| 519 | // Cannot use SourceSurface buffer count since it could be codec's 512*512 tile |
| 520 | // buffer count. |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 521 | if ((res = native_window_set_buffer_count( |
Shuzhen Wang | 3d00ee5 | 2019-09-25 14:19:28 -0700 | [diff] [blame] | 522 | anwConsumer, kMaxOutputSurfaceProducerCount + maxConsumerBuffers)) != OK) { |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 523 | ALOGE("%s: Unable to set buffer count for stream %d", __FUNCTION__, mMainImageStreamId); |
| 524 | return res; |
| 525 | } |
| 526 | |
| 527 | if ((res = native_window_set_buffers_dimensions(anwConsumer, mMaxHeicBufferSize, 1)) != OK) { |
| 528 | ALOGE("%s: Unable to set buffer dimension %zu x 1 for stream %d: %s (%d)", |
| 529 | __FUNCTION__, mMaxHeicBufferSize, mMainImageStreamId, strerror(-res), res); |
| 530 | return res; |
| 531 | } |
| 532 | |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 533 | sp<camera3::StatusTracker> statusTracker = mStatusTracker.promote(); |
| 534 | if (statusTracker != nullptr) { |
Yin-Chia Yeh | 87b3ec0 | 2019-06-03 10:44:39 -0700 | [diff] [blame] | 535 | std::string name = std::string("HeicStream ") + std::to_string(getStreamId()); |
| 536 | mStatusId = statusTracker->addComponent(name); |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 537 | } |
| 538 | |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 539 | run("HeicCompositeStreamProc"); |
| 540 | |
| 541 | return NO_ERROR; |
| 542 | } |
| 543 | |
| 544 | status_t HeicCompositeStream::insertGbp(SurfaceMap* /*out*/outSurfaceMap, |
| 545 | Vector<int32_t>* /*out*/outputStreamIds, int32_t* /*out*/currentStreamId) { |
| 546 | if (outSurfaceMap->find(mAppSegmentStreamId) == outSurfaceMap->end()) { |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 547 | outputStreamIds->push_back(mAppSegmentStreamId); |
| 548 | } |
| 549 | (*outSurfaceMap)[mAppSegmentStreamId].push_back(mAppSegmentSurfaceId); |
| 550 | |
| 551 | if (outSurfaceMap->find(mMainImageStreamId) == outSurfaceMap->end()) { |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 552 | outputStreamIds->push_back(mMainImageStreamId); |
| 553 | } |
| 554 | (*outSurfaceMap)[mMainImageStreamId].push_back(mMainImageSurfaceId); |
| 555 | |
| 556 | if (currentStreamId != nullptr) { |
| 557 | *currentStreamId = mMainImageStreamId; |
| 558 | } |
| 559 | |
| 560 | return NO_ERROR; |
| 561 | } |
| 562 | |
Emilian Peev | 4697b64 | 2019-11-19 17:11:14 -0800 | [diff] [blame] | 563 | status_t HeicCompositeStream::insertCompositeStreamIds( |
| 564 | std::vector<int32_t>* compositeStreamIds /*out*/) { |
| 565 | if (compositeStreamIds == nullptr) { |
| 566 | return BAD_VALUE; |
| 567 | } |
| 568 | |
| 569 | compositeStreamIds->push_back(mAppSegmentStreamId); |
| 570 | compositeStreamIds->push_back(mMainImageStreamId); |
| 571 | |
| 572 | return OK; |
| 573 | } |
| 574 | |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 575 | void HeicCompositeStream::onShutter(const CaptureResultExtras& resultExtras, nsecs_t timestamp) { |
| 576 | Mutex::Autolock l(mMutex); |
| 577 | if (mErrorState) { |
| 578 | return; |
| 579 | } |
| 580 | |
| 581 | if (mSettingsByFrameNumber.find(resultExtras.frameNumber) != mSettingsByFrameNumber.end()) { |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 582 | ALOGV("%s: [%" PRId64 "]: timestamp %" PRId64 ", requestId %d", __FUNCTION__, |
| 583 | resultExtras.frameNumber, timestamp, resultExtras.requestId); |
| 584 | mSettingsByFrameNumber[resultExtras.frameNumber].shutterNotified = true; |
| 585 | mSettingsByFrameNumber[resultExtras.frameNumber].timestamp = timestamp; |
| 586 | mSettingsByFrameNumber[resultExtras.frameNumber].requestId = resultExtras.requestId; |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 587 | mInputReadyCondition.signal(); |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | void HeicCompositeStream::compilePendingInputLocked() { |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 592 | auto i = mSettingsByFrameNumber.begin(); |
| 593 | while (i != mSettingsByFrameNumber.end()) { |
| 594 | if (i->second.shutterNotified) { |
| 595 | mPendingInputFrames[i->first].orientation = i->second.orientation; |
| 596 | mPendingInputFrames[i->first].quality = i->second.quality; |
| 597 | mPendingInputFrames[i->first].timestamp = i->second.timestamp; |
| 598 | mPendingInputFrames[i->first].requestId = i->second.requestId; |
| 599 | ALOGV("%s: [%" PRId64 "]: timestamp is %" PRId64, __FUNCTION__, |
| 600 | i->first, i->second.timestamp); |
| 601 | i = mSettingsByFrameNumber.erase(i); |
Shuzhen Wang | 62f49ed | 2019-09-04 14:07:53 -0700 | [diff] [blame] | 602 | |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 603 | // Set encoder quality if no inflight encoding |
| 604 | if (mPendingInputFrames.size() == 1) { |
| 605 | sp<StatusTracker> statusTracker = mStatusTracker.promote(); |
| 606 | if (statusTracker != nullptr) { |
| 607 | statusTracker->markComponentActive(mStatusId); |
| 608 | ALOGV("%s: Mark component as active", __FUNCTION__); |
| 609 | } |
| 610 | |
| 611 | int32_t newQuality = mPendingInputFrames.begin()->second.quality; |
| 612 | updateCodecQualityLocked(newQuality); |
| 613 | } |
| 614 | } else { |
| 615 | i++; |
Shuzhen Wang | 62f49ed | 2019-09-04 14:07:53 -0700 | [diff] [blame] | 616 | } |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 617 | } |
| 618 | |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 619 | while (!mInputAppSegmentBuffers.empty() && mAppSegmentFrameNumbers.size() > 0) { |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 620 | CpuConsumer::LockedBuffer imgBuffer; |
| 621 | auto it = mInputAppSegmentBuffers.begin(); |
| 622 | auto res = mAppSegmentConsumer->lockNextBuffer(&imgBuffer); |
| 623 | if (res == NOT_ENOUGH_DATA) { |
Michael Gonzalez | b5986a3 | 2019-10-09 15:38:17 -0700 | [diff] [blame] | 624 | // Can not lock any more buffers. |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 625 | break; |
| 626 | } else if ((res != OK) || (*it != imgBuffer.timestamp)) { |
| 627 | if (res != OK) { |
| 628 | ALOGE("%s: Error locking JPEG_APP_SEGMENTS image buffer: %s (%d)", __FUNCTION__, |
| 629 | strerror(-res), res); |
| 630 | } else { |
| 631 | ALOGE("%s: Expecting JPEG_APP_SEGMENTS buffer with time stamp: %" PRId64 |
| 632 | " received buffer with time stamp: %" PRId64, __FUNCTION__, |
| 633 | *it, imgBuffer.timestamp); |
Michael Gonzalez | b5986a3 | 2019-10-09 15:38:17 -0700 | [diff] [blame] | 634 | mAppSegmentConsumer->unlockBuffer(imgBuffer); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 635 | } |
| 636 | mPendingInputFrames[*it].error = true; |
| 637 | mInputAppSegmentBuffers.erase(it); |
| 638 | continue; |
| 639 | } |
| 640 | |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 641 | if (mPendingInputFrames.find(mAppSegmentFrameNumbers.front()) == mPendingInputFrames.end()) { |
| 642 | ALOGE("%s: mPendingInputFrames doesn't contain frameNumber %" PRId64, __FUNCTION__, |
| 643 | mAppSegmentFrameNumbers.front()); |
Shuzhen Wang | 991b7b6 | 2020-06-17 11:39:11 -0700 | [diff] [blame] | 644 | mInputAppSegmentBuffers.erase(it); |
| 645 | mAppSegmentFrameNumbers.pop(); |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 646 | continue; |
| 647 | } |
| 648 | |
| 649 | int64_t frameNumber = mAppSegmentFrameNumbers.front(); |
| 650 | // If mPendingInputFrames doesn't contain the expected frame number, the captured |
| 651 | // input app segment frame must have been dropped via a buffer error. Simply |
| 652 | // return the buffer to the buffer queue. |
| 653 | if ((mPendingInputFrames.find(frameNumber) == mPendingInputFrames.end()) || |
| 654 | (mPendingInputFrames[frameNumber].error)) { |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 655 | mAppSegmentConsumer->unlockBuffer(imgBuffer); |
| 656 | } else { |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 657 | mPendingInputFrames[frameNumber].appSegmentBuffer = imgBuffer; |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 658 | } |
| 659 | mInputAppSegmentBuffers.erase(it); |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 660 | mAppSegmentFrameNumbers.pop(); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 661 | } |
| 662 | |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 663 | while (!mInputYuvBuffers.empty() && !mYuvBufferAcquired && mMainImageFrameNumbers.size() > 0) { |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 664 | CpuConsumer::LockedBuffer imgBuffer; |
| 665 | auto it = mInputYuvBuffers.begin(); |
| 666 | auto res = mMainImageConsumer->lockNextBuffer(&imgBuffer); |
| 667 | if (res == NOT_ENOUGH_DATA) { |
Michael Gonzalez | b5986a3 | 2019-10-09 15:38:17 -0700 | [diff] [blame] | 668 | // Can not lock any more buffers. |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 669 | break; |
| 670 | } else if (res != OK) { |
| 671 | ALOGE("%s: Error locking YUV_888 image buffer: %s (%d)", __FUNCTION__, |
| 672 | strerror(-res), res); |
| 673 | mPendingInputFrames[*it].error = true; |
| 674 | mInputYuvBuffers.erase(it); |
| 675 | continue; |
| 676 | } else if (*it != imgBuffer.timestamp) { |
| 677 | ALOGW("%s: Expecting YUV_888 buffer with time stamp: %" PRId64 " received buffer with " |
| 678 | "time stamp: %" PRId64, __FUNCTION__, *it, imgBuffer.timestamp); |
| 679 | mPendingInputFrames[*it].error = true; |
| 680 | mInputYuvBuffers.erase(it); |
| 681 | continue; |
| 682 | } |
| 683 | |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 684 | if (mPendingInputFrames.find(mMainImageFrameNumbers.front()) == mPendingInputFrames.end()) { |
| 685 | ALOGE("%s: mPendingInputFrames doesn't contain frameNumber %" PRId64, __FUNCTION__, |
| 686 | mMainImageFrameNumbers.front()); |
| 687 | mInputYuvBuffers.erase(it); |
Shuzhen Wang | 991b7b6 | 2020-06-17 11:39:11 -0700 | [diff] [blame] | 688 | mMainImageFrameNumbers.pop(); |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 689 | continue; |
| 690 | } |
| 691 | |
| 692 | int64_t frameNumber = mMainImageFrameNumbers.front(); |
| 693 | // If mPendingInputFrames doesn't contain the expected frame number, the captured |
| 694 | // input main image must have been dropped via a buffer error. Simply |
| 695 | // return the buffer to the buffer queue. |
| 696 | if ((mPendingInputFrames.find(frameNumber) == mPendingInputFrames.end()) || |
| 697 | (mPendingInputFrames[frameNumber].error)) { |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 698 | mMainImageConsumer->unlockBuffer(imgBuffer); |
| 699 | } else { |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 700 | mPendingInputFrames[frameNumber].yuvBuffer = imgBuffer; |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 701 | mYuvBufferAcquired = true; |
| 702 | } |
| 703 | mInputYuvBuffers.erase(it); |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 704 | mMainImageFrameNumbers.pop(); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | while (!mCodecOutputBuffers.empty()) { |
| 708 | auto it = mCodecOutputBuffers.begin(); |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 709 | // Assume encoder input to output is FIFO, use a queue to look up |
| 710 | // frameNumber when handling codec outputs. |
| 711 | int64_t bufferFrameNumber = -1; |
| 712 | if (mCodecOutputBufferFrameNumbers.empty()) { |
| 713 | ALOGV("%s: Failed to find buffer frameNumber for codec output buffer!", __FUNCTION__); |
Michael Gonzalez | 5c103f2 | 2019-10-08 14:30:32 -0700 | [diff] [blame] | 714 | break; |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 715 | } else { |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 716 | // Direct mapping between camera frame number and codec timestamp (in us). |
| 717 | bufferFrameNumber = mCodecOutputBufferFrameNumbers.front(); |
Shuzhen Wang | 3d00ee5 | 2019-09-25 14:19:28 -0700 | [diff] [blame] | 718 | mCodecOutputCounter++; |
| 719 | if (mCodecOutputCounter == mNumOutputTiles) { |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 720 | mCodecOutputBufferFrameNumbers.pop(); |
Shuzhen Wang | 3d00ee5 | 2019-09-25 14:19:28 -0700 | [diff] [blame] | 721 | mCodecOutputCounter = 0; |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 722 | } |
| 723 | |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 724 | mPendingInputFrames[bufferFrameNumber].codecOutputBuffers.push_back(*it); |
| 725 | ALOGV("%s: [%" PRId64 "]: Pushing codecOutputBuffers (frameNumber %" PRId64 ")", |
| 726 | __FUNCTION__, bufferFrameNumber, it->timeUs); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 727 | } |
| 728 | mCodecOutputBuffers.erase(it); |
| 729 | } |
| 730 | |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 731 | while (!mCaptureResults.empty()) { |
| 732 | auto it = mCaptureResults.begin(); |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 733 | // Negative frame number indicates that something went wrong during the capture result |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 734 | // collection process. |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 735 | int64_t frameNumber = std::get<0>(it->second); |
| 736 | if (it->first >= 0 && |
| 737 | mPendingInputFrames.find(frameNumber) != mPendingInputFrames.end()) { |
| 738 | if (mPendingInputFrames[frameNumber].timestamp == it->first) { |
| 739 | mPendingInputFrames[frameNumber].result = |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 740 | std::make_unique<CameraMetadata>(std::get<1>(it->second)); |
| 741 | } else { |
| 742 | ALOGE("%s: Capture result frameNumber/timestamp mapping changed between " |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 743 | "shutter and capture result! before: %" PRId64 ", after: %" PRId64, |
| 744 | __FUNCTION__, mPendingInputFrames[frameNumber].timestamp, |
| 745 | it->first); |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 746 | } |
| 747 | } |
| 748 | mCaptureResults.erase(it); |
| 749 | } |
| 750 | |
| 751 | // mErrorFrameNumbers stores frame number of dropped buffers. |
| 752 | auto it = mErrorFrameNumbers.begin(); |
| 753 | while (it != mErrorFrameNumbers.end()) { |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 754 | if (mPendingInputFrames.find(*it) != mPendingInputFrames.end()) { |
| 755 | mPendingInputFrames[*it].error = true; |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 756 | } else { |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 757 | //Error callback is guaranteed to arrive after shutter notify, which |
| 758 | //results in mPendingInputFrames being populated. |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 759 | ALOGW("%s: Not able to find failing input with frame number: %" PRId64, __FUNCTION__, |
| 760 | *it); |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 761 | } |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 762 | it = mErrorFrameNumbers.erase(it); |
| 763 | } |
| 764 | |
| 765 | // mExifErrorFrameNumbers stores the frame number of dropped APP_SEGMENT buffers |
| 766 | it = mExifErrorFrameNumbers.begin(); |
| 767 | while (it != mExifErrorFrameNumbers.end()) { |
| 768 | if (mPendingInputFrames.find(*it) != mPendingInputFrames.end()) { |
| 769 | mPendingInputFrames[*it].exifError = true; |
| 770 | } |
| 771 | it = mExifErrorFrameNumbers.erase(it); |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 772 | } |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 773 | |
| 774 | // Distribute codec input buffers to be filled out from YUV output |
| 775 | for (auto it = mPendingInputFrames.begin(); |
| 776 | it != mPendingInputFrames.end() && mCodecInputBuffers.size() > 0; it++) { |
| 777 | InputFrame& inputFrame(it->second); |
| 778 | if (inputFrame.codecInputCounter < mGridRows * mGridCols) { |
| 779 | // Available input tiles that are required for the current input |
| 780 | // image. |
| 781 | size_t newInputTiles = std::min(mCodecInputBuffers.size(), |
| 782 | mGridRows * mGridCols - inputFrame.codecInputCounter); |
| 783 | for (size_t i = 0; i < newInputTiles; i++) { |
| 784 | CodecInputBufferInfo inputInfo = |
| 785 | { mCodecInputBuffers[0], mGridTimestampUs++, inputFrame.codecInputCounter }; |
| 786 | inputFrame.codecInputBuffers.push_back(inputInfo); |
| 787 | |
| 788 | mCodecInputBuffers.erase(mCodecInputBuffers.begin()); |
| 789 | inputFrame.codecInputCounter++; |
| 790 | } |
| 791 | break; |
| 792 | } |
| 793 | } |
| 794 | } |
| 795 | |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 796 | bool HeicCompositeStream::getNextReadyInputLocked(int64_t *frameNumber /*out*/) { |
| 797 | if (frameNumber == nullptr) { |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 798 | return false; |
| 799 | } |
| 800 | |
| 801 | bool newInputAvailable = false; |
Shuzhen Wang | 3d00ee5 | 2019-09-25 14:19:28 -0700 | [diff] [blame] | 802 | for (auto& it : mPendingInputFrames) { |
| 803 | // New input is considered to be available only if: |
| 804 | // 1. input buffers are ready, or |
| 805 | // 2. App segment and muxer is created, or |
| 806 | // 3. A codec output tile is ready, and an output buffer is available. |
| 807 | // This makes sure that muxer gets created only when an output tile is |
| 808 | // generated, because right now we only handle 1 HEIC output buffer at a |
| 809 | // time (max dequeued buffer count is 1). |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 810 | bool appSegmentReady = |
| 811 | (it.second.appSegmentBuffer.data != nullptr || it.second.exifError) && |
Shuzhen Wang | 3d00ee5 | 2019-09-25 14:19:28 -0700 | [diff] [blame] | 812 | !it.second.appSegmentWritten && it.second.result != nullptr && |
| 813 | it.second.muxer != nullptr; |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 814 | bool codecOutputReady = !it.second.codecOutputBuffers.empty(); |
| 815 | bool codecInputReady = (it.second.yuvBuffer.data != nullptr) && |
| 816 | (!it.second.codecInputBuffers.empty()); |
Shuzhen Wang | 3d00ee5 | 2019-09-25 14:19:28 -0700 | [diff] [blame] | 817 | bool hasOutputBuffer = it.second.muxer != nullptr || |
| 818 | (mDequeuedOutputBufferCnt < kMaxOutputSurfaceProducerCount); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 819 | if ((!it.second.error) && |
Shuzhen Wang | 3d00ee5 | 2019-09-25 14:19:28 -0700 | [diff] [blame] | 820 | (appSegmentReady || (codecOutputReady && hasOutputBuffer) || codecInputReady)) { |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 821 | *frameNumber = it.first; |
Shuzhen Wang | 3d00ee5 | 2019-09-25 14:19:28 -0700 | [diff] [blame] | 822 | if (it.second.format == nullptr && mFormat != nullptr) { |
| 823 | it.second.format = mFormat->dup(); |
| 824 | } |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 825 | newInputAvailable = true; |
| 826 | break; |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | return newInputAvailable; |
| 831 | } |
| 832 | |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 833 | int64_t HeicCompositeStream::getNextFailingInputLocked() { |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 834 | int64_t res = -1; |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 835 | |
| 836 | for (const auto& it : mPendingInputFrames) { |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 837 | if (it.second.error) { |
| 838 | res = it.first; |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 839 | break; |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | return res; |
| 844 | } |
| 845 | |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 846 | status_t HeicCompositeStream::processInputFrame(int64_t frameNumber, |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 847 | InputFrame &inputFrame) { |
| 848 | ATRACE_CALL(); |
| 849 | status_t res = OK; |
| 850 | |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 851 | bool appSegmentReady = |
| 852 | (inputFrame.appSegmentBuffer.data != nullptr || inputFrame.exifError) && |
Shuzhen Wang | 3d00ee5 | 2019-09-25 14:19:28 -0700 | [diff] [blame] | 853 | !inputFrame.appSegmentWritten && inputFrame.result != nullptr && |
| 854 | inputFrame.muxer != nullptr; |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 855 | bool codecOutputReady = inputFrame.codecOutputBuffers.size() > 0; |
| 856 | bool codecInputReady = inputFrame.yuvBuffer.data != nullptr && |
Shuzhen Wang | 3d00ee5 | 2019-09-25 14:19:28 -0700 | [diff] [blame] | 857 | !inputFrame.codecInputBuffers.empty(); |
| 858 | bool hasOutputBuffer = inputFrame.muxer != nullptr || |
| 859 | (mDequeuedOutputBufferCnt < kMaxOutputSurfaceProducerCount); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 860 | |
Shuzhen Wang | 3d00ee5 | 2019-09-25 14:19:28 -0700 | [diff] [blame] | 861 | ALOGV("%s: [%" PRId64 "]: appSegmentReady %d, codecOutputReady %d, codecInputReady %d," |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 862 | " dequeuedOutputBuffer %d, timestamp %" PRId64, __FUNCTION__, frameNumber, |
| 863 | appSegmentReady, codecOutputReady, codecInputReady, mDequeuedOutputBufferCnt, |
| 864 | inputFrame.timestamp); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 865 | |
| 866 | // Handle inputs for Hevc tiling |
| 867 | if (codecInputReady) { |
| 868 | res = processCodecInputFrame(inputFrame); |
| 869 | if (res != OK) { |
| 870 | ALOGE("%s: Failed to process codec input frame: %s (%d)", __FUNCTION__, |
| 871 | strerror(-res), res); |
| 872 | return res; |
| 873 | } |
| 874 | } |
| 875 | |
Shuzhen Wang | 3d00ee5 | 2019-09-25 14:19:28 -0700 | [diff] [blame] | 876 | if (!(codecOutputReady && hasOutputBuffer) && !appSegmentReady) { |
| 877 | return OK; |
| 878 | } |
| 879 | |
| 880 | // Initialize and start muxer if not yet done so. In this case, |
| 881 | // codecOutputReady must be true. Otherwise, appSegmentReady is guaranteed |
| 882 | // to be false, and the function must have returned early. |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 883 | if (inputFrame.muxer == nullptr) { |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 884 | res = startMuxerForInputFrame(frameNumber, inputFrame); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 885 | if (res != OK) { |
| 886 | ALOGE("%s: Failed to create and start muxer: %s (%d)", __FUNCTION__, |
| 887 | strerror(-res), res); |
| 888 | return res; |
| 889 | } |
| 890 | } |
| 891 | |
| 892 | // Write JPEG APP segments data to the muxer. |
Shuzhen Wang | 3d00ee5 | 2019-09-25 14:19:28 -0700 | [diff] [blame] | 893 | if (appSegmentReady) { |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 894 | res = processAppSegment(frameNumber, inputFrame); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 895 | if (res != OK) { |
| 896 | ALOGE("%s: Failed to process JPEG APP segments: %s (%d)", __FUNCTION__, |
| 897 | strerror(-res), res); |
| 898 | return res; |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | // Write media codec bitstream buffers to muxer. |
| 903 | while (!inputFrame.codecOutputBuffers.empty()) { |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 904 | res = processOneCodecOutputFrame(frameNumber, inputFrame); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 905 | if (res != OK) { |
| 906 | ALOGE("%s: Failed to process codec output frame: %s (%d)", __FUNCTION__, |
| 907 | strerror(-res), res); |
| 908 | return res; |
| 909 | } |
| 910 | } |
| 911 | |
Michael Gonzalez | b5986a3 | 2019-10-09 15:38:17 -0700 | [diff] [blame] | 912 | if (inputFrame.pendingOutputTiles == 0) { |
| 913 | if (inputFrame.appSegmentWritten) { |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 914 | res = processCompletedInputFrame(frameNumber, inputFrame); |
Michael Gonzalez | b5986a3 | 2019-10-09 15:38:17 -0700 | [diff] [blame] | 915 | if (res != OK) { |
| 916 | ALOGE("%s: Failed to process completed input frame: %s (%d)", __FUNCTION__, |
| 917 | strerror(-res), res); |
| 918 | return res; |
| 919 | } |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 920 | } |
| 921 | } |
| 922 | |
| 923 | return res; |
| 924 | } |
| 925 | |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 926 | status_t HeicCompositeStream::startMuxerForInputFrame(int64_t frameNumber, InputFrame &inputFrame) { |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 927 | sp<ANativeWindow> outputANW = mOutputSurface; |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 928 | |
| 929 | auto res = outputANW->dequeueBuffer(mOutputSurface.get(), &inputFrame.anb, &inputFrame.fenceFd); |
| 930 | if (res != OK) { |
| 931 | ALOGE("%s: Error retrieving output buffer: %s (%d)", __FUNCTION__, strerror(-res), |
| 932 | res); |
| 933 | return res; |
| 934 | } |
Shuzhen Wang | 3d00ee5 | 2019-09-25 14:19:28 -0700 | [diff] [blame] | 935 | mDequeuedOutputBufferCnt++; |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 936 | |
| 937 | // Combine current thread id, stream id and timestamp to uniquely identify image. |
| 938 | std::ostringstream tempOutputFile; |
| 939 | tempOutputFile << "HEIF-" << pthread_self() << "-" |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 940 | << getStreamId() << "-" << frameNumber; |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 941 | inputFrame.fileFd = syscall(__NR_memfd_create, tempOutputFile.str().c_str(), MFD_CLOEXEC); |
| 942 | if (inputFrame.fileFd < 0) { |
| 943 | ALOGE("%s: Failed to create file %s. Error no is %d", __FUNCTION__, |
| 944 | tempOutputFile.str().c_str(), errno); |
| 945 | return NO_INIT; |
| 946 | } |
Manisha Jajoo | 022b52f | 2021-10-09 00:51:42 +0530 | [diff] [blame] | 947 | inputFrame.muxer = MediaMuxer::create(inputFrame.fileFd, MediaMuxer::OUTPUT_FORMAT_HEIF); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 948 | if (inputFrame.muxer == nullptr) { |
| 949 | ALOGE("%s: Failed to create MediaMuxer for file fd %d", |
| 950 | __FUNCTION__, inputFrame.fileFd); |
| 951 | return NO_INIT; |
| 952 | } |
| 953 | |
| 954 | res = inputFrame.muxer->setOrientationHint(inputFrame.orientation); |
| 955 | if (res != OK) { |
| 956 | ALOGE("%s: Failed to setOrientationHint: %s (%d)", __FUNCTION__, |
| 957 | strerror(-res), res); |
| 958 | return res; |
| 959 | } |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 960 | |
Shuzhen Wang | 3d00ee5 | 2019-09-25 14:19:28 -0700 | [diff] [blame] | 961 | ssize_t trackId = inputFrame.muxer->addTrack(inputFrame.format); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 962 | if (trackId < 0) { |
| 963 | ALOGE("%s: Failed to addTrack to the muxer: %zd", __FUNCTION__, trackId); |
| 964 | return NO_INIT; |
| 965 | } |
| 966 | |
| 967 | inputFrame.trackIndex = trackId; |
| 968 | inputFrame.pendingOutputTiles = mNumOutputTiles; |
| 969 | |
| 970 | res = inputFrame.muxer->start(); |
| 971 | if (res != OK) { |
| 972 | ALOGE("%s: Failed to start MediaMuxer: %s (%d)", |
| 973 | __FUNCTION__, strerror(-res), res); |
| 974 | return res; |
| 975 | } |
| 976 | |
Shuzhen Wang | 3d00ee5 | 2019-09-25 14:19:28 -0700 | [diff] [blame] | 977 | ALOGV("%s: [%" PRId64 "]: Muxer started for inputFrame", __FUNCTION__, |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 978 | frameNumber); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 979 | return OK; |
| 980 | } |
| 981 | |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 982 | status_t HeicCompositeStream::processAppSegment(int64_t frameNumber, InputFrame &inputFrame) { |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 983 | size_t app1Size = 0; |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 984 | size_t appSegmentSize = 0; |
| 985 | if (!inputFrame.exifError) { |
| 986 | appSegmentSize = findAppSegmentsSize(inputFrame.appSegmentBuffer.data, |
| 987 | inputFrame.appSegmentBuffer.width * inputFrame.appSegmentBuffer.height, |
| 988 | &app1Size); |
| 989 | if (appSegmentSize == 0) { |
| 990 | ALOGE("%s: Failed to find JPEG APP segment size", __FUNCTION__); |
| 991 | return NO_INIT; |
| 992 | } |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | std::unique_ptr<ExifUtils> exifUtils(ExifUtils::create()); |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 996 | auto exifRes = inputFrame.exifError ? |
| 997 | exifUtils->initializeEmpty() : |
| 998 | exifUtils->initialize(inputFrame.appSegmentBuffer.data, app1Size); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 999 | if (!exifRes) { |
| 1000 | ALOGE("%s: Failed to initialize ExifUtils object!", __FUNCTION__); |
| 1001 | return BAD_VALUE; |
| 1002 | } |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 1003 | exifRes = exifUtils->setFromMetadata(*inputFrame.result, mStaticInfo, |
| 1004 | mOutputWidth, mOutputHeight); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1005 | if (!exifRes) { |
| 1006 | ALOGE("%s: Failed to set Exif tags using metadata and main image sizes", __FUNCTION__); |
| 1007 | return BAD_VALUE; |
| 1008 | } |
| 1009 | exifRes = exifUtils->setOrientation(inputFrame.orientation); |
| 1010 | if (!exifRes) { |
| 1011 | ALOGE("%s: ExifUtils failed to set orientation", __FUNCTION__); |
| 1012 | return BAD_VALUE; |
| 1013 | } |
| 1014 | exifRes = exifUtils->generateApp1(); |
| 1015 | if (!exifRes) { |
| 1016 | ALOGE("%s: ExifUtils failed to generate APP1 segment", __FUNCTION__); |
| 1017 | return BAD_VALUE; |
| 1018 | } |
| 1019 | |
| 1020 | unsigned int newApp1Length = exifUtils->getApp1Length(); |
| 1021 | const uint8_t *newApp1Segment = exifUtils->getApp1Buffer(); |
| 1022 | |
| 1023 | //Assemble the APP1 marker buffer required by MediaCodec |
| 1024 | uint8_t kExifApp1Marker[] = {'E', 'x', 'i', 'f', 0xFF, 0xE1, 0x00, 0x00}; |
| 1025 | kExifApp1Marker[6] = static_cast<uint8_t>(newApp1Length >> 8); |
| 1026 | kExifApp1Marker[7] = static_cast<uint8_t>(newApp1Length & 0xFF); |
| 1027 | size_t appSegmentBufferSize = sizeof(kExifApp1Marker) + |
| 1028 | appSegmentSize - app1Size + newApp1Length; |
| 1029 | uint8_t* appSegmentBuffer = new uint8_t[appSegmentBufferSize]; |
| 1030 | memcpy(appSegmentBuffer, kExifApp1Marker, sizeof(kExifApp1Marker)); |
| 1031 | memcpy(appSegmentBuffer + sizeof(kExifApp1Marker), newApp1Segment, newApp1Length); |
| 1032 | if (appSegmentSize - app1Size > 0) { |
| 1033 | memcpy(appSegmentBuffer + sizeof(kExifApp1Marker) + newApp1Length, |
| 1034 | inputFrame.appSegmentBuffer.data + app1Size, appSegmentSize - app1Size); |
| 1035 | } |
| 1036 | |
| 1037 | sp<ABuffer> aBuffer = new ABuffer(appSegmentBuffer, appSegmentBufferSize); |
| 1038 | auto res = inputFrame.muxer->writeSampleData(aBuffer, inputFrame.trackIndex, |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 1039 | inputFrame.timestamp, MediaCodec::BUFFER_FLAG_MUXER_DATA); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1040 | delete[] appSegmentBuffer; |
| 1041 | |
| 1042 | if (res != OK) { |
| 1043 | ALOGE("%s: Failed to write JPEG APP segments to muxer: %s (%d)", |
| 1044 | __FUNCTION__, strerror(-res), res); |
| 1045 | return res; |
| 1046 | } |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1047 | |
Shuzhen Wang | 3d00ee5 | 2019-09-25 14:19:28 -0700 | [diff] [blame] | 1048 | ALOGV("%s: [%" PRId64 "]: appSegmentSize is %zu, width %d, height %d, app1Size %zu", |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 1049 | __FUNCTION__, frameNumber, appSegmentSize, inputFrame.appSegmentBuffer.width, |
Shuzhen Wang | 3d00ee5 | 2019-09-25 14:19:28 -0700 | [diff] [blame] | 1050 | inputFrame.appSegmentBuffer.height, app1Size); |
Michael Gonzalez | b5986a3 | 2019-10-09 15:38:17 -0700 | [diff] [blame] | 1051 | |
| 1052 | inputFrame.appSegmentWritten = true; |
| 1053 | // Release the buffer now so any pending input app segments can be processed |
| 1054 | mAppSegmentConsumer->unlockBuffer(inputFrame.appSegmentBuffer); |
| 1055 | inputFrame.appSegmentBuffer.data = nullptr; |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 1056 | inputFrame.exifError = false; |
Michael Gonzalez | b5986a3 | 2019-10-09 15:38:17 -0700 | [diff] [blame] | 1057 | |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1058 | return OK; |
| 1059 | } |
| 1060 | |
| 1061 | status_t HeicCompositeStream::processCodecInputFrame(InputFrame &inputFrame) { |
| 1062 | for (auto& inputBuffer : inputFrame.codecInputBuffers) { |
| 1063 | sp<MediaCodecBuffer> buffer; |
| 1064 | auto res = mCodec->getInputBuffer(inputBuffer.index, &buffer); |
| 1065 | if (res != OK) { |
| 1066 | ALOGE("%s: Error getting codec input buffer: %s (%d)", __FUNCTION__, |
| 1067 | strerror(-res), res); |
| 1068 | return res; |
| 1069 | } |
| 1070 | |
| 1071 | // Copy one tile from source to destination. |
| 1072 | size_t tileX = inputBuffer.tileIndex % mGridCols; |
| 1073 | size_t tileY = inputBuffer.tileIndex / mGridCols; |
| 1074 | size_t top = mGridHeight * tileY; |
| 1075 | size_t left = mGridWidth * tileX; |
| 1076 | size_t width = (tileX == static_cast<size_t>(mGridCols) - 1) ? |
| 1077 | mOutputWidth - tileX * mGridWidth : mGridWidth; |
| 1078 | size_t height = (tileY == static_cast<size_t>(mGridRows) - 1) ? |
| 1079 | mOutputHeight - tileY * mGridHeight : mGridHeight; |
Shuzhen Wang | 3d00ee5 | 2019-09-25 14:19:28 -0700 | [diff] [blame] | 1080 | ALOGV("%s: inputBuffer tileIndex [%zu, %zu], top %zu, left %zu, width %zu, height %zu," |
| 1081 | " timeUs %" PRId64, __FUNCTION__, tileX, tileY, top, left, width, height, |
| 1082 | inputBuffer.timeUs); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1083 | |
| 1084 | res = copyOneYuvTile(buffer, inputFrame.yuvBuffer, top, left, width, height); |
| 1085 | if (res != OK) { |
| 1086 | ALOGE("%s: Failed to copy YUV tile %s (%d)", __FUNCTION__, |
| 1087 | strerror(-res), res); |
| 1088 | return res; |
| 1089 | } |
| 1090 | |
| 1091 | res = mCodec->queueInputBuffer(inputBuffer.index, 0, buffer->capacity(), |
| 1092 | inputBuffer.timeUs, 0, nullptr /*errorDetailMsg*/); |
| 1093 | if (res != OK) { |
| 1094 | ALOGE("%s: Failed to queueInputBuffer to Codec: %s (%d)", |
| 1095 | __FUNCTION__, strerror(-res), res); |
| 1096 | return res; |
| 1097 | } |
| 1098 | } |
| 1099 | |
| 1100 | inputFrame.codecInputBuffers.clear(); |
| 1101 | return OK; |
| 1102 | } |
| 1103 | |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 1104 | status_t HeicCompositeStream::processOneCodecOutputFrame(int64_t frameNumber, |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1105 | InputFrame &inputFrame) { |
| 1106 | auto it = inputFrame.codecOutputBuffers.begin(); |
| 1107 | sp<MediaCodecBuffer> buffer; |
| 1108 | status_t res = mCodec->getOutputBuffer(it->index, &buffer); |
| 1109 | if (res != OK) { |
| 1110 | ALOGE("%s: Error getting Heic codec output buffer at index %d: %s (%d)", |
| 1111 | __FUNCTION__, it->index, strerror(-res), res); |
| 1112 | return res; |
| 1113 | } |
| 1114 | if (buffer == nullptr) { |
| 1115 | ALOGE("%s: Invalid Heic codec output buffer at index %d", |
| 1116 | __FUNCTION__, it->index); |
| 1117 | return BAD_VALUE; |
| 1118 | } |
| 1119 | |
| 1120 | sp<ABuffer> aBuffer = new ABuffer(buffer->data(), buffer->size()); |
| 1121 | res = inputFrame.muxer->writeSampleData( |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 1122 | aBuffer, inputFrame.trackIndex, inputFrame.timestamp, 0 /*flags*/); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1123 | if (res != OK) { |
| 1124 | ALOGE("%s: Failed to write buffer index %d to muxer: %s (%d)", |
| 1125 | __FUNCTION__, it->index, strerror(-res), res); |
| 1126 | return res; |
| 1127 | } |
| 1128 | |
| 1129 | mCodec->releaseOutputBuffer(it->index); |
| 1130 | if (inputFrame.pendingOutputTiles == 0) { |
| 1131 | ALOGW("%s: Codec generated more tiles than expected!", __FUNCTION__); |
| 1132 | } else { |
| 1133 | inputFrame.pendingOutputTiles--; |
| 1134 | } |
| 1135 | |
| 1136 | inputFrame.codecOutputBuffers.erase(inputFrame.codecOutputBuffers.begin()); |
Shuzhen Wang | 3d00ee5 | 2019-09-25 14:19:28 -0700 | [diff] [blame] | 1137 | |
| 1138 | ALOGV("%s: [%" PRId64 "]: Output buffer index %d", |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 1139 | __FUNCTION__, frameNumber, it->index); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1140 | return OK; |
| 1141 | } |
| 1142 | |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 1143 | status_t HeicCompositeStream::processCompletedInputFrame(int64_t frameNumber, |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1144 | InputFrame &inputFrame) { |
| 1145 | sp<ANativeWindow> outputANW = mOutputSurface; |
| 1146 | inputFrame.muxer->stop(); |
| 1147 | |
| 1148 | // Copy the content of the file to memory. |
| 1149 | sp<GraphicBuffer> gb = GraphicBuffer::from(inputFrame.anb); |
| 1150 | void* dstBuffer; |
Shuzhen Wang | c87315d | 2022-03-17 00:11:20 +0000 | [diff] [blame] | 1151 | GraphicBufferLocker gbLocker(gb); |
| 1152 | auto res = gbLocker.lockAsync(&dstBuffer, inputFrame.fenceFd); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1153 | if (res != OK) { |
| 1154 | ALOGE("%s: Error trying to lock output buffer fence: %s (%d)", __FUNCTION__, |
| 1155 | strerror(-res), res); |
| 1156 | return res; |
| 1157 | } |
| 1158 | |
| 1159 | off_t fSize = lseek(inputFrame.fileFd, 0, SEEK_END); |
| 1160 | if (static_cast<size_t>(fSize) > mMaxHeicBufferSize - sizeof(CameraBlob)) { |
| 1161 | ALOGE("%s: Error: MediaMuxer output size %ld is larger than buffer sizer %zu", |
| 1162 | __FUNCTION__, fSize, mMaxHeicBufferSize - sizeof(CameraBlob)); |
| 1163 | return BAD_VALUE; |
| 1164 | } |
| 1165 | |
| 1166 | lseek(inputFrame.fileFd, 0, SEEK_SET); |
| 1167 | ssize_t bytesRead = read(inputFrame.fileFd, dstBuffer, fSize); |
| 1168 | if (bytesRead < fSize) { |
| 1169 | ALOGE("%s: Only %zd of %ld bytes read", __FUNCTION__, bytesRead, fSize); |
| 1170 | return BAD_VALUE; |
| 1171 | } |
| 1172 | |
| 1173 | close(inputFrame.fileFd); |
| 1174 | inputFrame.fileFd = -1; |
| 1175 | |
| 1176 | // Fill in HEIC header |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1177 | // Must be in sync with CAMERA3_HEIC_BLOB_ID in android_media_Utils.cpp |
Shuzhen Wang | dc51909 | 2022-12-02 12:29:04 -0800 | [diff] [blame] | 1178 | uint8_t *header = static_cast<uint8_t*>(dstBuffer) + mMaxHeicBufferSize - sizeof(CameraBlob); |
| 1179 | CameraBlob blobHeader = { |
| 1180 | .blobId = static_cast<CameraBlobId>(0x00FE), |
| 1181 | .blobSizeBytes = static_cast<int32_t>(fSize) |
| 1182 | }; |
| 1183 | memcpy(header, &blobHeader, sizeof(CameraBlob)); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1184 | |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 1185 | res = native_window_set_buffers_timestamp(mOutputSurface.get(), inputFrame.timestamp); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1186 | if (res != OK) { |
| 1187 | ALOGE("%s: Stream %d: Error setting timestamp: %s (%d)", |
| 1188 | __FUNCTION__, getStreamId(), strerror(-res), res); |
| 1189 | return res; |
| 1190 | } |
| 1191 | |
| 1192 | res = outputANW->queueBuffer(mOutputSurface.get(), inputFrame.anb, /*fence*/ -1); |
| 1193 | if (res != OK) { |
| 1194 | ALOGE("%s: Failed to queueBuffer to Heic stream: %s (%d)", __FUNCTION__, |
| 1195 | strerror(-res), res); |
| 1196 | return res; |
| 1197 | } |
| 1198 | inputFrame.anb = nullptr; |
Shuzhen Wang | 3d00ee5 | 2019-09-25 14:19:28 -0700 | [diff] [blame] | 1199 | mDequeuedOutputBufferCnt--; |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1200 | |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 1201 | ALOGV("%s: [%" PRId64 "]", __FUNCTION__, frameNumber); |
| 1202 | ATRACE_ASYNC_END("HEIC capture", frameNumber); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1203 | return OK; |
| 1204 | } |
| 1205 | |
| 1206 | |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 1207 | void HeicCompositeStream::releaseInputFrameLocked(int64_t frameNumber, |
| 1208 | InputFrame *inputFrame /*out*/) { |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1209 | if (inputFrame == nullptr) { |
| 1210 | return; |
| 1211 | } |
| 1212 | |
| 1213 | if (inputFrame->appSegmentBuffer.data != nullptr) { |
| 1214 | mAppSegmentConsumer->unlockBuffer(inputFrame->appSegmentBuffer); |
| 1215 | inputFrame->appSegmentBuffer.data = nullptr; |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | while (!inputFrame->codecOutputBuffers.empty()) { |
| 1219 | auto it = inputFrame->codecOutputBuffers.begin(); |
| 1220 | ALOGV("%s: releaseOutputBuffer index %d", __FUNCTION__, it->index); |
| 1221 | mCodec->releaseOutputBuffer(it->index); |
| 1222 | inputFrame->codecOutputBuffers.erase(it); |
| 1223 | } |
| 1224 | |
| 1225 | if (inputFrame->yuvBuffer.data != nullptr) { |
| 1226 | mMainImageConsumer->unlockBuffer(inputFrame->yuvBuffer); |
| 1227 | inputFrame->yuvBuffer.data = nullptr; |
| 1228 | mYuvBufferAcquired = false; |
| 1229 | } |
| 1230 | |
| 1231 | while (!inputFrame->codecInputBuffers.empty()) { |
| 1232 | auto it = inputFrame->codecInputBuffers.begin(); |
| 1233 | inputFrame->codecInputBuffers.erase(it); |
| 1234 | } |
| 1235 | |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 1236 | if (inputFrame->error || mErrorState) { |
| 1237 | ALOGV("%s: notifyError called for frameNumber %" PRId64, __FUNCTION__, frameNumber); |
| 1238 | notifyError(frameNumber, inputFrame->requestId); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1239 | } |
| 1240 | |
| 1241 | if (inputFrame->fileFd >= 0) { |
| 1242 | close(inputFrame->fileFd); |
| 1243 | inputFrame->fileFd = -1; |
| 1244 | } |
| 1245 | |
| 1246 | if (inputFrame->anb != nullptr) { |
| 1247 | sp<ANativeWindow> outputANW = mOutputSurface; |
| 1248 | outputANW->cancelBuffer(mOutputSurface.get(), inputFrame->anb, /*fence*/ -1); |
| 1249 | inputFrame->anb = nullptr; |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 1250 | |
| 1251 | mDequeuedOutputBufferCnt--; |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1252 | } |
| 1253 | } |
| 1254 | |
Michael Gonzalez | b5986a3 | 2019-10-09 15:38:17 -0700 | [diff] [blame] | 1255 | void HeicCompositeStream::releaseInputFramesLocked() { |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1256 | auto it = mPendingInputFrames.begin(); |
Shuzhen Wang | 62f49ed | 2019-09-04 14:07:53 -0700 | [diff] [blame] | 1257 | bool inputFrameDone = false; |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1258 | while (it != mPendingInputFrames.end()) { |
Michael Gonzalez | b5986a3 | 2019-10-09 15:38:17 -0700 | [diff] [blame] | 1259 | auto& inputFrame = it->second; |
| 1260 | if (inputFrame.error || |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 1261 | (inputFrame.appSegmentWritten && inputFrame.pendingOutputTiles == 0)) { |
| 1262 | releaseInputFrameLocked(it->first, &inputFrame); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1263 | it = mPendingInputFrames.erase(it); |
Shuzhen Wang | 62f49ed | 2019-09-04 14:07:53 -0700 | [diff] [blame] | 1264 | inputFrameDone = true; |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1265 | } else { |
| 1266 | it++; |
| 1267 | } |
| 1268 | } |
Shuzhen Wang | 62f49ed | 2019-09-04 14:07:53 -0700 | [diff] [blame] | 1269 | |
| 1270 | // Update codec quality based on first upcoming input frame. |
| 1271 | // Note that when encoding is in surface mode, currently there is no |
| 1272 | // way for camera service to synchronize quality setting on a per-frame |
| 1273 | // basis: we don't get notification when codec is ready to consume a new |
| 1274 | // input frame. So we update codec quality on a best-effort basis. |
| 1275 | if (inputFrameDone) { |
| 1276 | auto firstPendingFrame = mPendingInputFrames.begin(); |
| 1277 | if (firstPendingFrame != mPendingInputFrames.end()) { |
| 1278 | updateCodecQualityLocked(firstPendingFrame->second.quality); |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 1279 | } else { |
| 1280 | markTrackerIdle(); |
Shuzhen Wang | 62f49ed | 2019-09-04 14:07:53 -0700 | [diff] [blame] | 1281 | } |
| 1282 | } |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | status_t HeicCompositeStream::initializeCodec(uint32_t width, uint32_t height, |
| 1286 | const sp<CameraDeviceBase>& cameraDevice) { |
| 1287 | ALOGV("%s", __FUNCTION__); |
| 1288 | |
| 1289 | bool useGrid = false; |
Chong Zhang | 688abaa | 2019-05-17 16:32:23 -0700 | [diff] [blame] | 1290 | AString hevcName; |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1291 | bool isSizeSupported = isSizeSupportedByHeifEncoder(width, height, |
Chong Zhang | 688abaa | 2019-05-17 16:32:23 -0700 | [diff] [blame] | 1292 | &mUseHeic, &useGrid, nullptr, &hevcName); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1293 | if (!isSizeSupported) { |
| 1294 | ALOGE("%s: Encoder doesnt' support size %u x %u!", |
| 1295 | __FUNCTION__, width, height); |
| 1296 | return BAD_VALUE; |
| 1297 | } |
| 1298 | |
| 1299 | // Create Looper for MediaCodec. |
| 1300 | auto desiredMime = mUseHeic ? MIMETYPE_IMAGE_ANDROID_HEIC : MIMETYPE_VIDEO_HEVC; |
| 1301 | mCodecLooper = new ALooper; |
| 1302 | mCodecLooper->setName("Camera3-HeicComposite-MediaCodecLooper"); |
| 1303 | status_t res = mCodecLooper->start( |
| 1304 | false, // runOnCallingThread |
| 1305 | false, // canCallJava |
| 1306 | PRIORITY_AUDIO); |
| 1307 | if (res != OK) { |
| 1308 | ALOGE("%s: Failed to start codec looper: %s (%d)", |
| 1309 | __FUNCTION__, strerror(-res), res); |
| 1310 | return NO_INIT; |
| 1311 | } |
| 1312 | |
| 1313 | // Create HEIC/HEVC codec. |
Chong Zhang | 688abaa | 2019-05-17 16:32:23 -0700 | [diff] [blame] | 1314 | if (mUseHeic) { |
| 1315 | mCodec = MediaCodec::CreateByType(mCodecLooper, desiredMime, true /*encoder*/); |
| 1316 | } else { |
| 1317 | mCodec = MediaCodec::CreateByComponentName(mCodecLooper, hevcName); |
| 1318 | } |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1319 | if (mCodec == nullptr) { |
| 1320 | ALOGE("%s: Failed to create codec for %s", __FUNCTION__, desiredMime); |
| 1321 | return NO_INIT; |
| 1322 | } |
| 1323 | |
| 1324 | // Create Looper and handler for Codec callback. |
| 1325 | mCodecCallbackHandler = new CodecCallbackHandler(this); |
| 1326 | if (mCodecCallbackHandler == nullptr) { |
| 1327 | ALOGE("%s: Failed to create codec callback handler", __FUNCTION__); |
| 1328 | return NO_MEMORY; |
| 1329 | } |
| 1330 | mCallbackLooper = new ALooper; |
| 1331 | mCallbackLooper->setName("Camera3-HeicComposite-MediaCodecCallbackLooper"); |
| 1332 | res = mCallbackLooper->start( |
| 1333 | false, // runOnCallingThread |
| 1334 | false, // canCallJava |
| 1335 | PRIORITY_AUDIO); |
| 1336 | if (res != OK) { |
| 1337 | ALOGE("%s: Failed to start media callback looper: %s (%d)", |
| 1338 | __FUNCTION__, strerror(-res), res); |
| 1339 | return NO_INIT; |
| 1340 | } |
| 1341 | mCallbackLooper->registerHandler(mCodecCallbackHandler); |
| 1342 | |
| 1343 | mAsyncNotify = new AMessage(kWhatCallbackNotify, mCodecCallbackHandler); |
| 1344 | res = mCodec->setCallback(mAsyncNotify); |
| 1345 | if (res != OK) { |
| 1346 | ALOGE("%s: Failed to set MediaCodec callback: %s (%d)", __FUNCTION__, |
| 1347 | strerror(-res), res); |
| 1348 | return res; |
| 1349 | } |
| 1350 | |
| 1351 | // Create output format and configure the Codec. |
| 1352 | sp<AMessage> outputFormat = new AMessage(); |
| 1353 | outputFormat->setString(KEY_MIME, desiredMime); |
| 1354 | outputFormat->setInt32(KEY_BITRATE_MODE, BITRATE_MODE_CQ); |
| 1355 | outputFormat->setInt32(KEY_QUALITY, kDefaultJpegQuality); |
| 1356 | // Ask codec to skip timestamp check and encode all frames. |
Chong Zhang | 70bfcec | 2019-03-18 12:52:28 -0700 | [diff] [blame] | 1357 | outputFormat->setInt64(KEY_MAX_PTS_GAP_TO_ENCODER, kNoFrameDropMaxPtsGap); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1358 | |
| 1359 | int32_t gridWidth, gridHeight, gridRows, gridCols; |
| 1360 | if (useGrid || mUseHeic) { |
| 1361 | gridWidth = HeicEncoderInfoManager::kGridWidth; |
| 1362 | gridHeight = HeicEncoderInfoManager::kGridHeight; |
| 1363 | gridRows = (height + gridHeight - 1)/gridHeight; |
| 1364 | gridCols = (width + gridWidth - 1)/gridWidth; |
| 1365 | |
| 1366 | if (mUseHeic) { |
| 1367 | outputFormat->setInt32(KEY_TILE_WIDTH, gridWidth); |
| 1368 | outputFormat->setInt32(KEY_TILE_HEIGHT, gridHeight); |
| 1369 | outputFormat->setInt32(KEY_GRID_COLUMNS, gridCols); |
| 1370 | outputFormat->setInt32(KEY_GRID_ROWS, gridRows); |
| 1371 | } |
| 1372 | |
| 1373 | } else { |
| 1374 | gridWidth = width; |
| 1375 | gridHeight = height; |
| 1376 | gridRows = 1; |
| 1377 | gridCols = 1; |
| 1378 | } |
| 1379 | |
| 1380 | outputFormat->setInt32(KEY_WIDTH, !useGrid ? width : gridWidth); |
| 1381 | outputFormat->setInt32(KEY_HEIGHT, !useGrid ? height : gridHeight); |
| 1382 | outputFormat->setInt32(KEY_I_FRAME_INTERVAL, 0); |
| 1383 | outputFormat->setInt32(KEY_COLOR_FORMAT, |
| 1384 | useGrid ? COLOR_FormatYUV420Flexible : COLOR_FormatSurface); |
Shuzhen Wang | 0ca8152 | 2019-08-30 14:15:16 -0700 | [diff] [blame] | 1385 | outputFormat->setInt32(KEY_FRAME_RATE, useGrid ? gridRows * gridCols : kNoGridOpRate); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1386 | // This only serves as a hint to encoder when encoding is not real-time. |
| 1387 | outputFormat->setInt32(KEY_OPERATING_RATE, useGrid ? kGridOpRate : kNoGridOpRate); |
| 1388 | |
| 1389 | res = mCodec->configure(outputFormat, nullptr /*nativeWindow*/, |
| 1390 | nullptr /*crypto*/, CONFIGURE_FLAG_ENCODE); |
| 1391 | if (res != OK) { |
| 1392 | ALOGE("%s: Failed to configure codec: %s (%d)", __FUNCTION__, |
| 1393 | strerror(-res), res); |
| 1394 | return res; |
| 1395 | } |
| 1396 | |
| 1397 | mGridWidth = gridWidth; |
| 1398 | mGridHeight = gridHeight; |
| 1399 | mGridRows = gridRows; |
| 1400 | mGridCols = gridCols; |
| 1401 | mUseGrid = useGrid; |
| 1402 | mOutputWidth = width; |
| 1403 | mOutputHeight = height; |
| 1404 | mAppSegmentMaxSize = calcAppSegmentMaxSize(cameraDevice->info()); |
Susmitha Gummalla | e911f2e | 2020-11-23 09:57:03 -0800 | [diff] [blame] | 1405 | mMaxHeicBufferSize = |
| 1406 | ALIGN(mOutputWidth, HeicEncoderInfoManager::kGridWidth) * |
| 1407 | ALIGN(mOutputHeight, HeicEncoderInfoManager::kGridHeight) * 3 / 2 + mAppSegmentMaxSize; |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1408 | |
| 1409 | return OK; |
| 1410 | } |
| 1411 | |
| 1412 | void HeicCompositeStream::deinitCodec() { |
| 1413 | ALOGV("%s", __FUNCTION__); |
| 1414 | if (mCodec != nullptr) { |
| 1415 | mCodec->stop(); |
| 1416 | mCodec->release(); |
| 1417 | mCodec.clear(); |
| 1418 | } |
| 1419 | |
| 1420 | if (mCodecLooper != nullptr) { |
| 1421 | mCodecLooper->stop(); |
| 1422 | mCodecLooper.clear(); |
| 1423 | } |
| 1424 | |
| 1425 | if (mCallbackLooper != nullptr) { |
| 1426 | mCallbackLooper->stop(); |
| 1427 | mCallbackLooper.clear(); |
| 1428 | } |
| 1429 | |
| 1430 | mAsyncNotify.clear(); |
| 1431 | mFormat.clear(); |
| 1432 | } |
| 1433 | |
| 1434 | // Return the size of the complete list of app segment, 0 indicates failure |
| 1435 | size_t HeicCompositeStream::findAppSegmentsSize(const uint8_t* appSegmentBuffer, |
| 1436 | size_t maxSize, size_t *app1SegmentSize) { |
| 1437 | if (appSegmentBuffer == nullptr || app1SegmentSize == nullptr) { |
| 1438 | ALOGE("%s: Invalid input appSegmentBuffer %p, app1SegmentSize %p", |
| 1439 | __FUNCTION__, appSegmentBuffer, app1SegmentSize); |
| 1440 | return 0; |
| 1441 | } |
| 1442 | |
| 1443 | size_t expectedSize = 0; |
| 1444 | // First check for EXIF transport header at the end of the buffer |
Jayant Chowdhary | c67af1b | 2022-04-07 18:05:04 +0000 | [diff] [blame] | 1445 | const uint8_t *header = appSegmentBuffer + (maxSize - sizeof(CameraBlob)); |
| 1446 | const CameraBlob *blob = (const CameraBlob*)(header); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1447 | if (blob->blobId != CameraBlobId::JPEG_APP_SEGMENTS) { |
Jayant Chowdhary | c67af1b | 2022-04-07 18:05:04 +0000 | [diff] [blame] | 1448 | ALOGE("%s: Invalid EXIF blobId %d", __FUNCTION__, blob->blobId); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1449 | return 0; |
| 1450 | } |
| 1451 | |
Jayant Chowdhary | c67af1b | 2022-04-07 18:05:04 +0000 | [diff] [blame] | 1452 | expectedSize = blob->blobSizeBytes; |
| 1453 | if (expectedSize == 0 || expectedSize > maxSize - sizeof(CameraBlob)) { |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1454 | ALOGE("%s: Invalid blobSize %zu.", __FUNCTION__, expectedSize); |
| 1455 | return 0; |
| 1456 | } |
| 1457 | |
| 1458 | uint32_t totalSize = 0; |
| 1459 | |
| 1460 | // Verify APP1 marker (mandatory) |
| 1461 | uint8_t app1Marker[] = {0xFF, 0xE1}; |
| 1462 | if (memcmp(appSegmentBuffer, app1Marker, sizeof(app1Marker))) { |
| 1463 | ALOGE("%s: Invalid APP1 marker: %x, %x", __FUNCTION__, |
| 1464 | appSegmentBuffer[0], appSegmentBuffer[1]); |
| 1465 | return 0; |
| 1466 | } |
| 1467 | totalSize += sizeof(app1Marker); |
| 1468 | |
| 1469 | uint16_t app1Size = (static_cast<uint16_t>(appSegmentBuffer[totalSize]) << 8) + |
| 1470 | appSegmentBuffer[totalSize+1]; |
| 1471 | totalSize += app1Size; |
| 1472 | |
| 1473 | ALOGV("%s: Expected APP segments size %zu, APP1 segment size %u", |
| 1474 | __FUNCTION__, expectedSize, app1Size); |
| 1475 | while (totalSize < expectedSize) { |
| 1476 | if (appSegmentBuffer[totalSize] != 0xFF || |
| 1477 | appSegmentBuffer[totalSize+1] <= 0xE1 || |
| 1478 | appSegmentBuffer[totalSize+1] > 0xEF) { |
| 1479 | // Invalid APPn marker |
| 1480 | ALOGE("%s: Invalid APPn marker: %x, %x", __FUNCTION__, |
| 1481 | appSegmentBuffer[totalSize], appSegmentBuffer[totalSize+1]); |
| 1482 | return 0; |
| 1483 | } |
| 1484 | totalSize += 2; |
| 1485 | |
| 1486 | uint16_t appnSize = (static_cast<uint16_t>(appSegmentBuffer[totalSize]) << 8) + |
| 1487 | appSegmentBuffer[totalSize+1]; |
| 1488 | totalSize += appnSize; |
| 1489 | } |
| 1490 | |
| 1491 | if (totalSize != expectedSize) { |
| 1492 | ALOGE("%s: Invalid JPEG APP segments: totalSize %u vs expected size %zu", |
| 1493 | __FUNCTION__, totalSize, expectedSize); |
| 1494 | return 0; |
| 1495 | } |
| 1496 | |
| 1497 | *app1SegmentSize = app1Size + sizeof(app1Marker); |
| 1498 | return expectedSize; |
| 1499 | } |
| 1500 | |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1501 | status_t HeicCompositeStream::copyOneYuvTile(sp<MediaCodecBuffer>& codecBuffer, |
| 1502 | const CpuConsumer::LockedBuffer& yuvBuffer, |
| 1503 | size_t top, size_t left, size_t width, size_t height) { |
| 1504 | ATRACE_CALL(); |
| 1505 | |
| 1506 | // Get stride information for codecBuffer |
| 1507 | sp<ABuffer> imageData; |
| 1508 | if (!codecBuffer->meta()->findBuffer("image-data", &imageData)) { |
| 1509 | ALOGE("%s: Codec input buffer is not for image data!", __FUNCTION__); |
| 1510 | return BAD_VALUE; |
| 1511 | } |
| 1512 | if (imageData->size() != sizeof(MediaImage2)) { |
| 1513 | ALOGE("%s: Invalid codec input image size %zu, expected %zu", |
| 1514 | __FUNCTION__, imageData->size(), sizeof(MediaImage2)); |
| 1515 | return BAD_VALUE; |
| 1516 | } |
| 1517 | MediaImage2* imageInfo = reinterpret_cast<MediaImage2*>(imageData->data()); |
| 1518 | if (imageInfo->mType != MediaImage2::MEDIA_IMAGE_TYPE_YUV || |
| 1519 | imageInfo->mBitDepth != 8 || |
| 1520 | imageInfo->mBitDepthAllocated != 8 || |
| 1521 | imageInfo->mNumPlanes != 3) { |
| 1522 | ALOGE("%s: Invalid codec input image info: mType %d, mBitDepth %d, " |
| 1523 | "mBitDepthAllocated %d, mNumPlanes %d!", __FUNCTION__, |
| 1524 | imageInfo->mType, imageInfo->mBitDepth, |
| 1525 | imageInfo->mBitDepthAllocated, imageInfo->mNumPlanes); |
| 1526 | return BAD_VALUE; |
| 1527 | } |
| 1528 | |
| 1529 | ALOGV("%s: yuvBuffer chromaStep %d, chromaStride %d", |
| 1530 | __FUNCTION__, yuvBuffer.chromaStep, yuvBuffer.chromaStride); |
| 1531 | ALOGV("%s: U offset %u, V offset %u, U rowInc %d, V rowInc %d, U colInc %d, V colInc %d", |
| 1532 | __FUNCTION__, imageInfo->mPlane[MediaImage2::U].mOffset, |
| 1533 | imageInfo->mPlane[MediaImage2::V].mOffset, |
| 1534 | imageInfo->mPlane[MediaImage2::U].mRowInc, |
| 1535 | imageInfo->mPlane[MediaImage2::V].mRowInc, |
| 1536 | imageInfo->mPlane[MediaImage2::U].mColInc, |
| 1537 | imageInfo->mPlane[MediaImage2::V].mColInc); |
| 1538 | |
| 1539 | // Y |
| 1540 | for (auto row = top; row < top+height; row++) { |
| 1541 | uint8_t *dst = codecBuffer->data() + imageInfo->mPlane[MediaImage2::Y].mOffset + |
| 1542 | imageInfo->mPlane[MediaImage2::Y].mRowInc * (row - top); |
Shuzhen Wang | 219c299 | 2019-02-15 17:24:28 -0800 | [diff] [blame] | 1543 | mFnCopyRow(yuvBuffer.data+row*yuvBuffer.stride+left, dst, width); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1544 | } |
| 1545 | |
| 1546 | // U is Cb, V is Cr |
| 1547 | bool codecUPlaneFirst = imageInfo->mPlane[MediaImage2::V].mOffset > |
| 1548 | imageInfo->mPlane[MediaImage2::U].mOffset; |
| 1549 | uint32_t codecUvOffsetDiff = codecUPlaneFirst ? |
| 1550 | imageInfo->mPlane[MediaImage2::V].mOffset - imageInfo->mPlane[MediaImage2::U].mOffset : |
| 1551 | imageInfo->mPlane[MediaImage2::U].mOffset - imageInfo->mPlane[MediaImage2::V].mOffset; |
| 1552 | bool isCodecUvSemiplannar = (codecUvOffsetDiff == 1) && |
| 1553 | (imageInfo->mPlane[MediaImage2::U].mRowInc == |
| 1554 | imageInfo->mPlane[MediaImage2::V].mRowInc) && |
| 1555 | (imageInfo->mPlane[MediaImage2::U].mColInc == 2) && |
| 1556 | (imageInfo->mPlane[MediaImage2::V].mColInc == 2); |
| 1557 | bool isCodecUvPlannar = |
| 1558 | ((codecUPlaneFirst && codecUvOffsetDiff >= |
| 1559 | imageInfo->mPlane[MediaImage2::U].mRowInc * imageInfo->mHeight/2) || |
| 1560 | ((!codecUPlaneFirst && codecUvOffsetDiff >= |
| 1561 | imageInfo->mPlane[MediaImage2::V].mRowInc * imageInfo->mHeight/2))) && |
| 1562 | imageInfo->mPlane[MediaImage2::U].mColInc == 1 && |
| 1563 | imageInfo->mPlane[MediaImage2::V].mColInc == 1; |
| 1564 | bool cameraUPlaneFirst = yuvBuffer.dataCr > yuvBuffer.dataCb; |
| 1565 | |
| 1566 | if (isCodecUvSemiplannar && yuvBuffer.chromaStep == 2 && |
| 1567 | (codecUPlaneFirst == cameraUPlaneFirst)) { |
| 1568 | // UV semiplannar |
| 1569 | // The chrome plane could be either Cb first, or Cr first. Take the |
| 1570 | // smaller address. |
| 1571 | uint8_t *src = std::min(yuvBuffer.dataCb, yuvBuffer.dataCr); |
| 1572 | MediaImage2::PlaneIndex dstPlane = codecUvOffsetDiff > 0 ? MediaImage2::U : MediaImage2::V; |
| 1573 | for (auto row = top/2; row < (top+height)/2; row++) { |
| 1574 | uint8_t *dst = codecBuffer->data() + imageInfo->mPlane[dstPlane].mOffset + |
| 1575 | imageInfo->mPlane[dstPlane].mRowInc * (row - top/2); |
Shuzhen Wang | 219c299 | 2019-02-15 17:24:28 -0800 | [diff] [blame] | 1576 | mFnCopyRow(src+row*yuvBuffer.chromaStride+left, dst, width); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1577 | } |
| 1578 | } else if (isCodecUvPlannar && yuvBuffer.chromaStep == 1) { |
| 1579 | // U plane |
| 1580 | for (auto row = top/2; row < (top+height)/2; row++) { |
| 1581 | uint8_t *dst = codecBuffer->data() + imageInfo->mPlane[MediaImage2::U].mOffset + |
| 1582 | imageInfo->mPlane[MediaImage2::U].mRowInc * (row - top/2); |
Shuzhen Wang | 219c299 | 2019-02-15 17:24:28 -0800 | [diff] [blame] | 1583 | mFnCopyRow(yuvBuffer.dataCb+row*yuvBuffer.chromaStride+left/2, dst, width/2); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1584 | } |
| 1585 | |
| 1586 | // V plane |
| 1587 | for (auto row = top/2; row < (top+height)/2; row++) { |
| 1588 | uint8_t *dst = codecBuffer->data() + imageInfo->mPlane[MediaImage2::V].mOffset + |
| 1589 | imageInfo->mPlane[MediaImage2::V].mRowInc * (row - top/2); |
Shuzhen Wang | 219c299 | 2019-02-15 17:24:28 -0800 | [diff] [blame] | 1590 | mFnCopyRow(yuvBuffer.dataCr+row*yuvBuffer.chromaStride+left/2, dst, width/2); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1591 | } |
| 1592 | } else { |
Shuzhen Wang | 219c299 | 2019-02-15 17:24:28 -0800 | [diff] [blame] | 1593 | // Convert between semiplannar and plannar, or when UV orders are |
| 1594 | // different. |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1595 | uint8_t *dst = codecBuffer->data(); |
| 1596 | for (auto row = top/2; row < (top+height)/2; row++) { |
| 1597 | for (auto col = left/2; col < (left+width)/2; col++) { |
| 1598 | // U/Cb |
| 1599 | int32_t dstIndex = imageInfo->mPlane[MediaImage2::U].mOffset + |
| 1600 | imageInfo->mPlane[MediaImage2::U].mRowInc * (row - top/2) + |
| 1601 | imageInfo->mPlane[MediaImage2::U].mColInc * (col - left/2); |
| 1602 | int32_t srcIndex = row * yuvBuffer.chromaStride + yuvBuffer.chromaStep * col; |
| 1603 | dst[dstIndex] = yuvBuffer.dataCb[srcIndex]; |
| 1604 | |
| 1605 | // V/Cr |
| 1606 | dstIndex = imageInfo->mPlane[MediaImage2::V].mOffset + |
| 1607 | imageInfo->mPlane[MediaImage2::V].mRowInc * (row - top/2) + |
| 1608 | imageInfo->mPlane[MediaImage2::V].mColInc * (col - left/2); |
| 1609 | srcIndex = row * yuvBuffer.chromaStride + yuvBuffer.chromaStep * col; |
| 1610 | dst[dstIndex] = yuvBuffer.dataCr[srcIndex]; |
| 1611 | } |
| 1612 | } |
| 1613 | } |
| 1614 | return OK; |
| 1615 | } |
| 1616 | |
Colin Cross | 14900c9 | 2023-01-13 14:50:17 -0800 | [diff] [blame] | 1617 | void HeicCompositeStream::initCopyRowFunction([[maybe_unused]] int32_t width) |
Shuzhen Wang | 219c299 | 2019-02-15 17:24:28 -0800 | [diff] [blame] | 1618 | { |
| 1619 | using namespace libyuv; |
| 1620 | |
| 1621 | mFnCopyRow = CopyRow_C; |
| 1622 | #if defined(HAS_COPYROW_SSE2) |
| 1623 | if (TestCpuFlag(kCpuHasSSE2)) { |
| 1624 | mFnCopyRow = IS_ALIGNED(width, 32) ? CopyRow_SSE2 : CopyRow_Any_SSE2; |
| 1625 | } |
| 1626 | #endif |
| 1627 | #if defined(HAS_COPYROW_AVX) |
| 1628 | if (TestCpuFlag(kCpuHasAVX)) { |
| 1629 | mFnCopyRow = IS_ALIGNED(width, 64) ? CopyRow_AVX : CopyRow_Any_AVX; |
| 1630 | } |
| 1631 | #endif |
| 1632 | #if defined(HAS_COPYROW_ERMS) |
| 1633 | if (TestCpuFlag(kCpuHasERMS)) { |
| 1634 | mFnCopyRow = CopyRow_ERMS; |
| 1635 | } |
| 1636 | #endif |
| 1637 | #if defined(HAS_COPYROW_NEON) |
| 1638 | if (TestCpuFlag(kCpuHasNEON)) { |
| 1639 | mFnCopyRow = IS_ALIGNED(width, 32) ? CopyRow_NEON : CopyRow_Any_NEON; |
| 1640 | } |
| 1641 | #endif |
| 1642 | #if defined(HAS_COPYROW_MIPS) |
| 1643 | if (TestCpuFlag(kCpuHasMIPS)) { |
| 1644 | mFnCopyRow = CopyRow_MIPS; |
| 1645 | } |
| 1646 | #endif |
| 1647 | } |
| 1648 | |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1649 | size_t HeicCompositeStream::calcAppSegmentMaxSize(const CameraMetadata& info) { |
| 1650 | camera_metadata_ro_entry_t entry = info.find(ANDROID_HEIC_INFO_MAX_JPEG_APP_SEGMENTS_COUNT); |
| 1651 | size_t maxAppsSegment = 1; |
| 1652 | if (entry.count > 0) { |
| 1653 | maxAppsSegment = entry.data.u8[0] < 1 ? 1 : |
| 1654 | entry.data.u8[0] > 16 ? 16 : entry.data.u8[0]; |
| 1655 | } |
Jayant Chowdhary | c67af1b | 2022-04-07 18:05:04 +0000 | [diff] [blame] | 1656 | return maxAppsSegment * (2 + 0xFFFF) + sizeof(CameraBlob); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1657 | } |
| 1658 | |
Shuzhen Wang | 62f49ed | 2019-09-04 14:07:53 -0700 | [diff] [blame] | 1659 | void HeicCompositeStream::updateCodecQualityLocked(int32_t quality) { |
| 1660 | if (quality != mQuality) { |
| 1661 | sp<AMessage> qualityParams = new AMessage; |
| 1662 | qualityParams->setInt32(PARAMETER_KEY_VIDEO_BITRATE, quality); |
| 1663 | status_t res = mCodec->setParameters(qualityParams); |
| 1664 | if (res != OK) { |
| 1665 | ALOGE("%s: Failed to set codec quality: %s (%d)", |
| 1666 | __FUNCTION__, strerror(-res), res); |
| 1667 | } else { |
| 1668 | mQuality = quality; |
| 1669 | } |
| 1670 | } |
| 1671 | } |
| 1672 | |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1673 | bool HeicCompositeStream::threadLoop() { |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 1674 | int64_t frameNumber = -1; |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1675 | bool newInputAvailable = false; |
| 1676 | |
| 1677 | { |
| 1678 | Mutex::Autolock l(mMutex); |
| 1679 | if (mErrorState) { |
| 1680 | // In case we landed in error state, return any pending buffers and |
| 1681 | // halt all further processing. |
| 1682 | compilePendingInputLocked(); |
Michael Gonzalez | b5986a3 | 2019-10-09 15:38:17 -0700 | [diff] [blame] | 1683 | releaseInputFramesLocked(); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1684 | return false; |
| 1685 | } |
| 1686 | |
| 1687 | |
| 1688 | while (!newInputAvailable) { |
| 1689 | compilePendingInputLocked(); |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 1690 | newInputAvailable = getNextReadyInputLocked(&frameNumber); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1691 | |
| 1692 | if (!newInputAvailable) { |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 1693 | auto failingFrameNumber = getNextFailingInputLocked(); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1694 | if (failingFrameNumber >= 0) { |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 1695 | releaseInputFrameLocked(failingFrameNumber, |
| 1696 | &mPendingInputFrames[failingFrameNumber]); |
| 1697 | |
| 1698 | // It's okay to remove the entry from mPendingInputFrames |
| 1699 | // because: |
| 1700 | // 1. Only one internal stream (main input) is critical in |
| 1701 | // backing the output stream. |
| 1702 | // 2. If captureResult/appSegment arrives after the entry is |
| 1703 | // removed, they are simply skipped. |
| 1704 | mPendingInputFrames.erase(failingFrameNumber); |
| 1705 | if (mPendingInputFrames.size() == 0) { |
| 1706 | markTrackerIdle(); |
| 1707 | } |
| 1708 | return true; |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1709 | } |
| 1710 | |
| 1711 | auto ret = mInputReadyCondition.waitRelative(mMutex, kWaitDuration); |
| 1712 | if (ret == TIMED_OUT) { |
| 1713 | return true; |
| 1714 | } else if (ret != OK) { |
| 1715 | ALOGE("%s: Timed wait on condition failed: %s (%d)", __FUNCTION__, |
| 1716 | strerror(-ret), ret); |
| 1717 | return false; |
| 1718 | } |
| 1719 | } |
| 1720 | } |
| 1721 | } |
| 1722 | |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 1723 | auto res = processInputFrame(frameNumber, mPendingInputFrames[frameNumber]); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1724 | Mutex::Autolock l(mMutex); |
| 1725 | if (res != OK) { |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 1726 | ALOGE("%s: Failed processing frame with timestamp: %" PRIu64 ", frameNumber: %" |
| 1727 | PRId64 ": %s (%d)", __FUNCTION__, mPendingInputFrames[frameNumber].timestamp, |
| 1728 | frameNumber, strerror(-res), res); |
| 1729 | mPendingInputFrames[frameNumber].error = true; |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1730 | } |
| 1731 | |
Michael Gonzalez | b5986a3 | 2019-10-09 15:38:17 -0700 | [diff] [blame] | 1732 | releaseInputFramesLocked(); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1733 | |
| 1734 | return true; |
| 1735 | } |
| 1736 | |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 1737 | void HeicCompositeStream::flagAnExifErrorFrameNumber(int64_t frameNumber) { |
| 1738 | Mutex::Autolock l(mMutex); |
| 1739 | mExifErrorFrameNumbers.emplace(frameNumber); |
| 1740 | mInputReadyCondition.signal(); |
| 1741 | } |
| 1742 | |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1743 | bool HeicCompositeStream::onStreamBufferError(const CaptureResultExtras& resultExtras) { |
| 1744 | bool res = false; |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 1745 | int64_t frameNumber = resultExtras.frameNumber; |
| 1746 | |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1747 | // Buffer errors concerning internal composite streams should not be directly visible to |
| 1748 | // camera clients. They must only receive a single buffer error with the public composite |
| 1749 | // stream id. |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 1750 | if (resultExtras.errorStreamId == mAppSegmentStreamId) { |
| 1751 | ALOGV("%s: APP_SEGMENT frameNumber: %" PRId64, __FUNCTION__, frameNumber); |
| 1752 | flagAnExifErrorFrameNumber(frameNumber); |
| 1753 | res = true; |
| 1754 | } else if (resultExtras.errorStreamId == mMainImageStreamId) { |
| 1755 | ALOGV("%s: YUV frameNumber: %" PRId64, __FUNCTION__, frameNumber); |
| 1756 | flagAnErrorFrameNumber(frameNumber); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1757 | res = true; |
| 1758 | } |
| 1759 | |
| 1760 | return res; |
| 1761 | } |
| 1762 | |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 1763 | void HeicCompositeStream::onResultError(const CaptureResultExtras& resultExtras) { |
| 1764 | // For result error, since the APPS_SEGMENT buffer already contains EXIF, |
| 1765 | // simply skip using the capture result metadata to override EXIF. |
| 1766 | Mutex::Autolock l(mMutex); |
| 1767 | |
| 1768 | int64_t timestamp = -1; |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 1769 | for (const auto& fn : mSettingsByFrameNumber) { |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 1770 | if (fn.first == resultExtras.frameNumber) { |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 1771 | timestamp = fn.second.timestamp; |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 1772 | break; |
| 1773 | } |
| 1774 | } |
| 1775 | if (timestamp == -1) { |
| 1776 | for (const auto& inputFrame : mPendingInputFrames) { |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 1777 | if (inputFrame.first == resultExtras.frameNumber) { |
| 1778 | timestamp = inputFrame.second.timestamp; |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 1779 | break; |
| 1780 | } |
| 1781 | } |
| 1782 | } |
| 1783 | |
| 1784 | if (timestamp == -1) { |
| 1785 | ALOGE("%s: Failed to find shutter timestamp for result error!", __FUNCTION__); |
| 1786 | return; |
| 1787 | } |
| 1788 | |
| 1789 | mCaptureResults.emplace(timestamp, std::make_tuple(resultExtras.frameNumber, CameraMetadata())); |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 1790 | ALOGV("%s: timestamp %" PRId64 ", frameNumber %" PRId64, __FUNCTION__, |
| 1791 | timestamp, resultExtras.frameNumber); |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 1792 | mInputReadyCondition.signal(); |
| 1793 | } |
| 1794 | |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 1795 | void HeicCompositeStream::onRequestError(const CaptureResultExtras& resultExtras) { |
| 1796 | auto frameNumber = resultExtras.frameNumber; |
| 1797 | ALOGV("%s: frameNumber: %" PRId64, __FUNCTION__, frameNumber); |
| 1798 | Mutex::Autolock l(mMutex); |
| 1799 | auto numRequests = mSettingsByFrameNumber.erase(frameNumber); |
| 1800 | if (numRequests == 0) { |
| 1801 | // Pending request has been populated into mPendingInputFrames |
| 1802 | mErrorFrameNumbers.emplace(frameNumber); |
| 1803 | mInputReadyCondition.signal(); |
| 1804 | } else { |
| 1805 | // REQUEST_ERROR was received without onShutter. |
| 1806 | } |
| 1807 | } |
| 1808 | |
| 1809 | void HeicCompositeStream::markTrackerIdle() { |
| 1810 | sp<StatusTracker> statusTracker = mStatusTracker.promote(); |
| 1811 | if (statusTracker != nullptr) { |
| 1812 | statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE); |
| 1813 | ALOGV("%s: Mark component as idle", __FUNCTION__); |
| 1814 | } |
| 1815 | } |
| 1816 | |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1817 | void HeicCompositeStream::CodecCallbackHandler::onMessageReceived(const sp<AMessage> &msg) { |
| 1818 | sp<HeicCompositeStream> parent = mParent.promote(); |
| 1819 | if (parent == nullptr) return; |
| 1820 | |
| 1821 | switch (msg->what()) { |
| 1822 | case kWhatCallbackNotify: { |
| 1823 | int32_t cbID; |
| 1824 | if (!msg->findInt32("callbackID", &cbID)) { |
| 1825 | ALOGE("kWhatCallbackNotify: callbackID is expected."); |
| 1826 | break; |
| 1827 | } |
| 1828 | |
| 1829 | ALOGV("kWhatCallbackNotify: cbID = %d", cbID); |
| 1830 | |
| 1831 | switch (cbID) { |
| 1832 | case MediaCodec::CB_INPUT_AVAILABLE: { |
| 1833 | int32_t index; |
| 1834 | if (!msg->findInt32("index", &index)) { |
| 1835 | ALOGE("CB_INPUT_AVAILABLE: index is expected."); |
| 1836 | break; |
| 1837 | } |
| 1838 | parent->onHeicInputFrameAvailable(index); |
| 1839 | break; |
| 1840 | } |
| 1841 | |
| 1842 | case MediaCodec::CB_OUTPUT_AVAILABLE: { |
| 1843 | int32_t index; |
| 1844 | size_t offset; |
| 1845 | size_t size; |
| 1846 | int64_t timeUs; |
| 1847 | int32_t flags; |
| 1848 | |
| 1849 | if (!msg->findInt32("index", &index)) { |
| 1850 | ALOGE("CB_OUTPUT_AVAILABLE: index is expected."); |
| 1851 | break; |
| 1852 | } |
| 1853 | if (!msg->findSize("offset", &offset)) { |
| 1854 | ALOGE("CB_OUTPUT_AVAILABLE: offset is expected."); |
| 1855 | break; |
| 1856 | } |
| 1857 | if (!msg->findSize("size", &size)) { |
| 1858 | ALOGE("CB_OUTPUT_AVAILABLE: size is expected."); |
| 1859 | break; |
| 1860 | } |
| 1861 | if (!msg->findInt64("timeUs", &timeUs)) { |
| 1862 | ALOGE("CB_OUTPUT_AVAILABLE: timeUs is expected."); |
| 1863 | break; |
| 1864 | } |
| 1865 | if (!msg->findInt32("flags", &flags)) { |
| 1866 | ALOGE("CB_OUTPUT_AVAILABLE: flags is expected."); |
| 1867 | break; |
| 1868 | } |
| 1869 | |
| 1870 | CodecOutputBufferInfo bufferInfo = { |
| 1871 | index, |
| 1872 | (int32_t)offset, |
| 1873 | (int32_t)size, |
| 1874 | timeUs, |
| 1875 | (uint32_t)flags}; |
| 1876 | |
| 1877 | parent->onHeicOutputFrameAvailable(bufferInfo); |
| 1878 | break; |
| 1879 | } |
| 1880 | |
| 1881 | case MediaCodec::CB_OUTPUT_FORMAT_CHANGED: { |
| 1882 | sp<AMessage> format; |
| 1883 | if (!msg->findMessage("format", &format)) { |
| 1884 | ALOGE("CB_OUTPUT_FORMAT_CHANGED: format is expected."); |
| 1885 | break; |
| 1886 | } |
Chong Zhang | 860eff1 | 2019-09-16 16:15:00 -0700 | [diff] [blame] | 1887 | // Here format is MediaCodec's internal copy of output format. |
| 1888 | // Make a copy since onHeicFormatChanged() might modify it. |
| 1889 | sp<AMessage> formatCopy; |
| 1890 | if (format != nullptr) { |
| 1891 | formatCopy = format->dup(); |
| 1892 | } |
| 1893 | parent->onHeicFormatChanged(formatCopy); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1894 | break; |
| 1895 | } |
| 1896 | |
| 1897 | case MediaCodec::CB_ERROR: { |
| 1898 | status_t err; |
| 1899 | int32_t actionCode; |
| 1900 | AString detail; |
| 1901 | if (!msg->findInt32("err", &err)) { |
| 1902 | ALOGE("CB_ERROR: err is expected."); |
| 1903 | break; |
| 1904 | } |
| 1905 | if (!msg->findInt32("action", &actionCode)) { |
| 1906 | ALOGE("CB_ERROR: action is expected."); |
| 1907 | break; |
| 1908 | } |
| 1909 | msg->findString("detail", &detail); |
| 1910 | ALOGE("Codec reported error(0x%x), actionCode(%d), detail(%s)", |
| 1911 | err, actionCode, detail.c_str()); |
| 1912 | |
| 1913 | parent->onHeicCodecError(); |
| 1914 | break; |
| 1915 | } |
| 1916 | |
| 1917 | default: { |
| 1918 | ALOGE("kWhatCallbackNotify: callbackID(%d) is unexpected.", cbID); |
| 1919 | break; |
| 1920 | } |
| 1921 | } |
| 1922 | break; |
| 1923 | } |
| 1924 | |
| 1925 | default: |
| 1926 | ALOGE("shouldn't be here"); |
| 1927 | break; |
| 1928 | } |
| 1929 | } |
| 1930 | |
| 1931 | }; // namespace camera3 |
| 1932 | }; // namespace android |