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