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 | #ifndef ANDROID_SERVERS_CAMERA_CAMERA3_HEIC_COMPOSITE_STREAM_H |
| 18 | #define ANDROID_SERVERS_CAMERA_CAMERA3_HEIC_COMPOSITE_STREAM_H |
| 19 | |
| 20 | #include <queue> |
| 21 | |
| 22 | #include <gui/IProducerListener.h> |
| 23 | #include <gui/CpuConsumer.h> |
| 24 | |
| 25 | #include <media/hardware/VideoAPI.h> |
| 26 | #include <media/MediaCodecBuffer.h> |
| 27 | #include <media/stagefright/foundation/ALooper.h> |
Shuzhen Wang | 3d00ee5 | 2019-09-25 14:19:28 -0700 | [diff] [blame] | 28 | #include <media/stagefright/foundation/AMessage.h> |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 29 | #include <media/stagefright/MediaCodec.h> |
| 30 | #include <media/stagefright/MediaMuxer.h> |
| 31 | |
| 32 | #include "CompositeStream.h" |
| 33 | |
| 34 | namespace android { |
| 35 | namespace camera3 { |
| 36 | |
| 37 | class HeicCompositeStream : public CompositeStream, public Thread, |
| 38 | public CpuConsumer::FrameAvailableListener { |
| 39 | public: |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 40 | HeicCompositeStream(sp<CameraDeviceBase> device, |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 41 | wp<hardware::camera2::ICameraDeviceCallbacks> cb); |
| 42 | ~HeicCompositeStream() override; |
| 43 | |
| 44 | static bool isHeicCompositeStream(const sp<Surface> &surface); |
| 45 | |
| 46 | status_t createInternalStreams(const std::vector<sp<Surface>>& consumers, |
| 47 | bool hasDeferredConsumer, uint32_t width, uint32_t height, int format, |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 48 | camera_stream_rotation_t rotation, int *id, const String8& physicalCameraId, |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 49 | const std::unordered_set<int32_t> &sensorPixelModesUsed, |
| 50 | std::vector<int> *surfaceIds, |
Emilian Peev | 434248e | 2022-10-06 14:58:54 -0700 | [diff] [blame^] | 51 | int streamSetId, bool isShared, int32_t colorSpace, |
| 52 | int64_t dynamicProfile, int64_t streamUseCase) override; |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 53 | |
| 54 | status_t deleteInternalStreams() override; |
| 55 | |
| 56 | status_t configureStream() override; |
| 57 | |
| 58 | status_t insertGbp(SurfaceMap* /*out*/outSurfaceMap, Vector<int32_t>* /*out*/outputStreamIds, |
| 59 | int32_t* /*out*/currentStreamId) override; |
| 60 | |
Emilian Peev | 4697b64 | 2019-11-19 17:11:14 -0800 | [diff] [blame] | 61 | status_t insertCompositeStreamIds(std::vector<int32_t>* compositeStreamIds /*out*/) override; |
| 62 | |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 63 | void onShutter(const CaptureResultExtras& resultExtras, nsecs_t timestamp) override; |
| 64 | |
| 65 | int getStreamId() override { return mMainImageStreamId; } |
| 66 | |
| 67 | // Use onShutter to keep track of frame number <-> timestamp mapping. |
| 68 | void onBufferReleased(const BufferInfo& bufferInfo) override; |
| 69 | void onBufferRequestForFrameNumber(uint64_t frameNumber, int streamId, |
| 70 | const CameraMetadata& settings) override; |
| 71 | |
| 72 | // CpuConsumer listener implementation |
| 73 | void onFrameAvailable(const BufferItem& item) override; |
| 74 | |
| 75 | // Return stream information about the internal camera streams |
| 76 | static status_t getCompositeStreamInfo(const OutputStreamInfo &streamInfo, |
| 77 | const CameraMetadata& ch, std::vector<OutputStreamInfo>* compositeOutput /*out*/); |
| 78 | |
| 79 | static bool isSizeSupportedByHeifEncoder(int32_t width, int32_t height, |
Chong Zhang | 688abaa | 2019-05-17 16:32:23 -0700 | [diff] [blame] | 80 | bool* useHeic, bool* useGrid, int64_t* stall, AString* hevcName = nullptr); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 81 | static bool isInMemoryTempFileSupported(); |
| 82 | protected: |
| 83 | |
| 84 | bool threadLoop() override; |
| 85 | bool onStreamBufferError(const CaptureResultExtras& resultExtras) override; |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 86 | void onResultError(const CaptureResultExtras& resultExtras) override; |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 87 | void onRequestError(const CaptureResultExtras& resultExtras) override; |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 88 | |
| 89 | private: |
| 90 | // |
| 91 | // HEIC/HEVC Codec related structures, utility functions, and callbacks |
| 92 | // |
| 93 | struct CodecOutputBufferInfo { |
| 94 | int32_t index; |
| 95 | int32_t offset; |
| 96 | int32_t size; |
| 97 | int64_t timeUs; |
| 98 | uint32_t flags; |
| 99 | }; |
| 100 | |
| 101 | struct CodecInputBufferInfo { |
| 102 | int32_t index; |
| 103 | int64_t timeUs; |
| 104 | size_t tileIndex; |
| 105 | }; |
| 106 | |
| 107 | class CodecCallbackHandler : public AHandler { |
| 108 | public: |
| 109 | explicit CodecCallbackHandler(wp<HeicCompositeStream> parent) { |
| 110 | mParent = parent; |
| 111 | } |
| 112 | virtual void onMessageReceived(const sp<AMessage> &msg); |
| 113 | private: |
| 114 | wp<HeicCompositeStream> mParent; |
| 115 | }; |
| 116 | |
| 117 | enum { |
| 118 | kWhatCallbackNotify, |
| 119 | }; |
| 120 | |
| 121 | bool mUseHeic; |
| 122 | sp<MediaCodec> mCodec; |
| 123 | sp<ALooper> mCodecLooper, mCallbackLooper; |
| 124 | sp<CodecCallbackHandler> mCodecCallbackHandler; |
| 125 | sp<AMessage> mAsyncNotify; |
| 126 | sp<AMessage> mFormat; |
| 127 | size_t mNumOutputTiles; |
| 128 | |
| 129 | int32_t mOutputWidth, mOutputHeight; |
| 130 | size_t mMaxHeicBufferSize; |
| 131 | int32_t mGridWidth, mGridHeight; |
| 132 | size_t mGridRows, mGridCols; |
| 133 | bool mUseGrid; // Whether to use framework YUV frame tiling. |
| 134 | |
| 135 | static const int64_t kNoFrameDropMaxPtsGap = -1000000; |
| 136 | static const int32_t kNoGridOpRate = 30; |
| 137 | static const int32_t kGridOpRate = 120; |
| 138 | |
| 139 | void onHeicOutputFrameAvailable(const CodecOutputBufferInfo& bufferInfo); |
| 140 | void onHeicInputFrameAvailable(int32_t index); // Only called for YUV input mode. |
| 141 | void onHeicFormatChanged(sp<AMessage>& newFormat); |
| 142 | void onHeicCodecError(); |
| 143 | |
| 144 | status_t initializeCodec(uint32_t width, uint32_t height, |
| 145 | const sp<CameraDeviceBase>& cameraDevice); |
| 146 | void deinitCodec(); |
| 147 | |
| 148 | // |
| 149 | // Composite stream related structures, utility functions and callbacks. |
| 150 | // |
| 151 | struct InputFrame { |
| 152 | int32_t orientation; |
| 153 | int32_t quality; |
| 154 | |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 155 | CpuConsumer::LockedBuffer appSegmentBuffer; |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 156 | std::vector<CodecOutputBufferInfo> codecOutputBuffers; |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 157 | std::unique_ptr<CameraMetadata> result; |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 158 | |
| 159 | // Fields that are only applicable to HEVC tiling. |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 160 | CpuConsumer::LockedBuffer yuvBuffer; |
| 161 | std::vector<CodecInputBufferInfo> codecInputBuffers; |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 162 | |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 163 | bool error; // Main input image buffer error |
| 164 | bool exifError; // Exif/APP_SEGMENT buffer error |
| 165 | int64_t timestamp; |
| 166 | int32_t requestId; |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 167 | |
Shuzhen Wang | 3d00ee5 | 2019-09-25 14:19:28 -0700 | [diff] [blame] | 168 | sp<AMessage> format; |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 169 | sp<MediaMuxer> muxer; |
| 170 | int fenceFd; |
| 171 | int fileFd; |
| 172 | ssize_t trackIndex; |
| 173 | ANativeWindowBuffer *anb; |
| 174 | |
| 175 | bool appSegmentWritten; |
| 176 | size_t pendingOutputTiles; |
| 177 | size_t codecInputCounter; |
| 178 | |
| 179 | InputFrame() : orientation(0), quality(kDefaultJpegQuality), error(false), |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 180 | exifError(false), timestamp(-1), requestId(-1), fenceFd(-1), |
| 181 | fileFd(-1), trackIndex(-1), anb(nullptr), appSegmentWritten(false), |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 182 | pendingOutputTiles(0), codecInputCounter(0) { } |
| 183 | }; |
| 184 | |
| 185 | void compilePendingInputLocked(); |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 186 | // Find first complete and valid frame with smallest frame number |
| 187 | bool getNextReadyInputLocked(int64_t *frameNumber /*out*/); |
| 188 | // Find next failing frame number with smallest frame number and return respective frame number |
| 189 | int64_t getNextFailingInputLocked(); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 190 | |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 191 | status_t processInputFrame(int64_t frameNumber, InputFrame &inputFrame); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 192 | status_t processCodecInputFrame(InputFrame &inputFrame); |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 193 | status_t startMuxerForInputFrame(int64_t frameNumber, InputFrame &inputFrame); |
| 194 | status_t processAppSegment(int64_t frameNumber, InputFrame &inputFrame); |
| 195 | status_t processOneCodecOutputFrame(int64_t frameNumber, InputFrame &inputFrame); |
| 196 | status_t processCompletedInputFrame(int64_t frameNumber, InputFrame &inputFrame); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 197 | |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 198 | void releaseInputFrameLocked(int64_t frameNumber, InputFrame *inputFrame /*out*/); |
Michael Gonzalez | b5986a3 | 2019-10-09 15:38:17 -0700 | [diff] [blame] | 199 | void releaseInputFramesLocked(); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 200 | |
| 201 | size_t findAppSegmentsSize(const uint8_t* appSegmentBuffer, size_t maxSize, |
| 202 | size_t* app1SegmentSize); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 203 | status_t copyOneYuvTile(sp<MediaCodecBuffer>& codecBuffer, |
| 204 | const CpuConsumer::LockedBuffer& yuvBuffer, |
| 205 | size_t top, size_t left, size_t width, size_t height); |
Shuzhen Wang | 219c299 | 2019-02-15 17:24:28 -0800 | [diff] [blame] | 206 | void initCopyRowFunction(int32_t width); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 207 | static size_t calcAppSegmentMaxSize(const CameraMetadata& info); |
Shuzhen Wang | 62f49ed | 2019-09-04 14:07:53 -0700 | [diff] [blame] | 208 | void updateCodecQualityLocked(int32_t quality); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 209 | |
| 210 | static const nsecs_t kWaitDuration = 10000000; // 10 ms |
| 211 | static const int32_t kDefaultJpegQuality = 99; |
| 212 | static const auto kJpegDataSpace = HAL_DATASPACE_V0_JFIF; |
| 213 | static const android_dataspace kAppSegmentDataSpace = |
| 214 | static_cast<android_dataspace>(HAL_DATASPACE_JPEG_APP_SEGMENTS); |
| 215 | static const android_dataspace kHeifDataSpace = |
| 216 | static_cast<android_dataspace>(HAL_DATASPACE_HEIF); |
Michael Gonzalez | b5986a3 | 2019-10-09 15:38:17 -0700 | [diff] [blame] | 217 | // Use the limit of pipeline depth in the API sepc as maximum number of acquired |
| 218 | // app segment buffers. |
| 219 | static const uint32_t kMaxAcquiredAppSegment = 8; |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 220 | |
| 221 | int mAppSegmentStreamId, mAppSegmentSurfaceId; |
| 222 | sp<CpuConsumer> mAppSegmentConsumer; |
| 223 | sp<Surface> mAppSegmentSurface; |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 224 | size_t mAppSegmentMaxSize; |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 225 | std::queue<int64_t> mAppSegmentFrameNumbers; |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 226 | CameraMetadata mStaticInfo; |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 227 | |
| 228 | int mMainImageStreamId, mMainImageSurfaceId; |
| 229 | sp<Surface> mMainImageSurface; |
| 230 | sp<CpuConsumer> mMainImageConsumer; // Only applicable for HEVC codec. |
| 231 | bool mYuvBufferAcquired; // Only applicable to HEVC codec |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 232 | std::queue<int64_t> mMainImageFrameNumbers; |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 233 | |
Shuzhen Wang | 3d00ee5 | 2019-09-25 14:19:28 -0700 | [diff] [blame] | 234 | static const int32_t kMaxOutputSurfaceProducerCount = 1; |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 235 | sp<Surface> mOutputSurface; |
| 236 | sp<ProducerListener> mProducerListener; |
Shuzhen Wang | 3d00ee5 | 2019-09-25 14:19:28 -0700 | [diff] [blame] | 237 | int32_t mDequeuedOutputBufferCnt; |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 238 | |
| 239 | // Map from frame number to JPEG setting of orientation+quality |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 240 | struct HeicSettings { |
| 241 | int32_t orientation; |
| 242 | int32_t quality; |
| 243 | int64_t timestamp; |
| 244 | int32_t requestId; |
| 245 | bool shutterNotified; |
| 246 | |
| 247 | HeicSettings() : orientation(0), quality(95), timestamp(0), |
| 248 | requestId(-1), shutterNotified(false) {} |
| 249 | HeicSettings(int32_t _orientation, int32_t _quality) : |
| 250 | orientation(_orientation), |
| 251 | quality(_quality), timestamp(0), |
| 252 | requestId(-1), shutterNotified(false) {} |
| 253 | |
| 254 | }; |
| 255 | std::map<int64_t, HeicSettings> mSettingsByFrameNumber; |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 256 | |
| 257 | // Keep all incoming APP segment Blob buffer pending further processing. |
| 258 | std::vector<int64_t> mInputAppSegmentBuffers; |
| 259 | |
| 260 | // Keep all incoming HEIC blob buffer pending further processing. |
| 261 | std::vector<CodecOutputBufferInfo> mCodecOutputBuffers; |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 262 | std::queue<int64_t> mCodecOutputBufferFrameNumbers; |
Shuzhen Wang | 3d00ee5 | 2019-09-25 14:19:28 -0700 | [diff] [blame] | 263 | size_t mCodecOutputCounter; |
Shuzhen Wang | 62f49ed | 2019-09-04 14:07:53 -0700 | [diff] [blame] | 264 | int32_t mQuality; |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 265 | |
| 266 | // Keep all incoming Yuv buffer pending tiling and encoding (for HEVC YUV tiling only) |
| 267 | std::vector<int64_t> mInputYuvBuffers; |
| 268 | // Keep all codec input buffers ready to be filled out (for HEVC YUV tiling only) |
| 269 | std::vector<int32_t> mCodecInputBuffers; |
| 270 | |
| 271 | // Artificial strictly incremental YUV grid timestamp to make encoder happy. |
| 272 | int64_t mGridTimestampUs; |
| 273 | |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 274 | // Indexed by frame number. In most common use case, entries are accessed in order. |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 275 | std::map<int64_t, InputFrame> mPendingInputFrames; |
Shuzhen Wang | 219c299 | 2019-02-15 17:24:28 -0800 | [diff] [blame] | 276 | |
| 277 | // Function pointer of libyuv row copy. |
| 278 | void (*mFnCopyRow)(const uint8_t* src, uint8_t* dst, int width); |
Shuzhen Wang | e867578 | 2019-12-05 09:12:14 -0800 | [diff] [blame] | 279 | |
| 280 | // A set of APP_SEGMENT error frame numbers |
| 281 | std::set<int64_t> mExifErrorFrameNumbers; |
| 282 | void flagAnExifErrorFrameNumber(int64_t frameNumber); |
| 283 | |
| 284 | // The status id for tracking the active/idle status of this composite stream |
| 285 | int mStatusId; |
| 286 | void markTrackerIdle(); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 287 | }; |
| 288 | |
| 289 | }; // namespace camera3 |
| 290 | }; // namespace android |
| 291 | |
| 292 | #endif //ANDROID_SERVERS_CAMERA_CAMERA3_HEIC_COMPOSITE_STREAM_H |