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