blob: cdcadedabde1f62531bb275bf0c9a410588af5d5 [file] [log] [blame]
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001/*
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 Wang3d00ee52019-09-25 14:19:28 -070028#include <media/stagefright/foundation/AMessage.h>
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -080029#include <media/stagefright/MediaCodec.h>
30#include <media/stagefright/MediaMuxer.h>
31
32#include "CompositeStream.h"
33
34namespace android {
35namespace camera3 {
36
37class HeicCompositeStream : public CompositeStream, public Thread,
38 public CpuConsumer::FrameAvailableListener {
39public:
Shuzhen Wange8675782019-12-05 09:12:14 -080040 HeicCompositeStream(sp<CameraDeviceBase> device,
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -080041 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 Peevf4816702020-04-03 15:44:51 -070048 camera_stream_rotation_t rotation, int *id, const String8& physicalCameraId,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -080049 const std::unordered_set<int32_t> &sensorPixelModesUsed,
50 std::vector<int> *surfaceIds,
Emilian Peev434248e2022-10-06 14:58:54 -070051 int streamSetId, bool isShared, int32_t colorSpace,
52 int64_t dynamicProfile, int64_t streamUseCase) override;
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -080053
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 Peev4697b642019-11-19 17:11:14 -080061 status_t insertCompositeStreamIds(std::vector<int32_t>* compositeStreamIds /*out*/) override;
62
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -080063 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 Zhang688abaa2019-05-17 16:32:23 -070080 bool* useHeic, bool* useGrid, int64_t* stall, AString* hevcName = nullptr);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -080081 static bool isInMemoryTempFileSupported();
82protected:
83
84 bool threadLoop() override;
85 bool onStreamBufferError(const CaptureResultExtras& resultExtras) override;
Shuzhen Wange7f4b462019-02-12 08:43:07 -080086 void onResultError(const CaptureResultExtras& resultExtras) override;
Shuzhen Wange8675782019-12-05 09:12:14 -080087 void onRequestError(const CaptureResultExtras& resultExtras) override;
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -080088
89private:
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 Wange7f4b462019-02-12 08:43:07 -0800155 CpuConsumer::LockedBuffer appSegmentBuffer;
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800156 std::vector<CodecOutputBufferInfo> codecOutputBuffers;
Shuzhen Wange7f4b462019-02-12 08:43:07 -0800157 std::unique_ptr<CameraMetadata> result;
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800158
159 // Fields that are only applicable to HEVC tiling.
Shuzhen Wange7f4b462019-02-12 08:43:07 -0800160 CpuConsumer::LockedBuffer yuvBuffer;
161 std::vector<CodecInputBufferInfo> codecInputBuffers;
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800162
Shuzhen Wange8675782019-12-05 09:12:14 -0800163 bool error; // Main input image buffer error
164 bool exifError; // Exif/APP_SEGMENT buffer error
165 int64_t timestamp;
166 int32_t requestId;
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800167
Shuzhen Wang3d00ee52019-09-25 14:19:28 -0700168 sp<AMessage> format;
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800169 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 Wange8675782019-12-05 09:12:14 -0800180 exifError(false), timestamp(-1), requestId(-1), fenceFd(-1),
181 fileFd(-1), trackIndex(-1), anb(nullptr), appSegmentWritten(false),
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800182 pendingOutputTiles(0), codecInputCounter(0) { }
183 };
184
185 void compilePendingInputLocked();
Shuzhen Wange8675782019-12-05 09:12:14 -0800186 // 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 Wang68ac7ad2019-01-30 14:03:28 -0800190
Shuzhen Wange8675782019-12-05 09:12:14 -0800191 status_t processInputFrame(int64_t frameNumber, InputFrame &inputFrame);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800192 status_t processCodecInputFrame(InputFrame &inputFrame);
Shuzhen Wange8675782019-12-05 09:12:14 -0800193 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 Wang68ac7ad2019-01-30 14:03:28 -0800197
Shuzhen Wange8675782019-12-05 09:12:14 -0800198 void releaseInputFrameLocked(int64_t frameNumber, InputFrame *inputFrame /*out*/);
Michael Gonzalezb5986a32019-10-09 15:38:17 -0700199 void releaseInputFramesLocked();
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800200
201 size_t findAppSegmentsSize(const uint8_t* appSegmentBuffer, size_t maxSize,
202 size_t* app1SegmentSize);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800203 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 Wang219c2992019-02-15 17:24:28 -0800206 void initCopyRowFunction(int32_t width);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800207 static size_t calcAppSegmentMaxSize(const CameraMetadata& info);
Shuzhen Wang62f49ed2019-09-04 14:07:53 -0700208 void updateCodecQualityLocked(int32_t quality);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800209
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 Gonzalezb5986a32019-10-09 15:38:17 -0700217 // 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 Wang68ac7ad2019-01-30 14:03:28 -0800220
221 int mAppSegmentStreamId, mAppSegmentSurfaceId;
222 sp<CpuConsumer> mAppSegmentConsumer;
223 sp<Surface> mAppSegmentSurface;
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800224 size_t mAppSegmentMaxSize;
Shuzhen Wange8675782019-12-05 09:12:14 -0800225 std::queue<int64_t> mAppSegmentFrameNumbers;
Shuzhen Wange7f4b462019-02-12 08:43:07 -0800226 CameraMetadata mStaticInfo;
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800227
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 Wange8675782019-12-05 09:12:14 -0800232 std::queue<int64_t> mMainImageFrameNumbers;
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800233
Shuzhen Wang3d00ee52019-09-25 14:19:28 -0700234 static const int32_t kMaxOutputSurfaceProducerCount = 1;
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800235 sp<Surface> mOutputSurface;
236 sp<ProducerListener> mProducerListener;
Shuzhen Wang3d00ee52019-09-25 14:19:28 -0700237 int32_t mDequeuedOutputBufferCnt;
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800238
239 // Map from frame number to JPEG setting of orientation+quality
Shuzhen Wange8675782019-12-05 09:12:14 -0800240 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 Wang68ac7ad2019-01-30 14:03:28 -0800256
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 Wange8675782019-12-05 09:12:14 -0800262 std::queue<int64_t> mCodecOutputBufferFrameNumbers;
Shuzhen Wang3d00ee52019-09-25 14:19:28 -0700263 size_t mCodecOutputCounter;
Shuzhen Wang62f49ed2019-09-04 14:07:53 -0700264 int32_t mQuality;
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800265
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 Wange8675782019-12-05 09:12:14 -0800274 // Indexed by frame number. In most common use case, entries are accessed in order.
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800275 std::map<int64_t, InputFrame> mPendingInputFrames;
Shuzhen Wang219c2992019-02-15 17:24:28 -0800276
277 // Function pointer of libyuv row copy.
278 void (*mFnCopyRow)(const uint8_t* src, uint8_t* dst, int width);
Shuzhen Wange8675782019-12-05 09:12:14 -0800279
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 Wang68ac7ad2019-01-30 14:03:28 -0800287};
288
289}; // namespace camera3
290}; // namespace android
291
292#endif //ANDROID_SERVERS_CAMERA_CAMERA3_HEIC_COMPOSITE_STREAM_H