1 /*
2 * Copyright 2018, 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 #ifdef __LP64__
18 #define OMX_ANDROID_COMPILE_AS_32BIT_ON_64BIT_PLATFORMS
19 #endif
20
21 //#define LOG_NDEBUG 0
22 #define LOG_TAG "C2OMXNode"
23 #include <log/log.h>
24
25 #include <C2AllocatorGralloc.h>
26 #include <C2BlockInternal.h>
27 #include <C2Component.h>
28 #include <C2PlatformSupport.h>
29
30 #include <OMX_Component.h>
31 #include <OMX_Index.h>
32 #include <OMX_IndexExt.h>
33
34 #include <media/stagefright/omx/OMXUtils.h>
35 #include <media/stagefright/MediaErrors.h>
36 #include <ui/Fence.h>
37 #include <ui/GraphicBuffer.h>
38
39 #include "C2OMXNode.h"
40
41 namespace android {
42
43 namespace {
44
45 class Buffer2D : public C2Buffer {
46 public:
Buffer2D(C2ConstGraphicBlock block)47 explicit Buffer2D(C2ConstGraphicBlock block) : C2Buffer({ block }) {}
48 };
49
50 } // namespace
51
C2OMXNode(const std::shared_ptr<Codec2Client::Component> & comp)52 C2OMXNode::C2OMXNode(const std::shared_ptr<Codec2Client::Component> &comp)
53 : mComp(comp), mFrameIndex(0), mWidth(0), mHeight(0),
54 mAdjustTimestampGapUs(0), mFirstInputFrame(true) {
55 // TODO: read from intf()
56 if (!strncmp(comp->getName().c_str(), "c2.android.", 11)) {
57 mUsage = GRALLOC_USAGE_SW_READ_OFTEN;
58 } else {
59 mUsage = GRALLOC_USAGE_HW_VIDEO_ENCODER;
60 }
61 }
62
freeNode()63 status_t C2OMXNode::freeNode() {
64 mComp.reset();
65 return OK;
66 }
67
sendCommand(OMX_COMMANDTYPE cmd,OMX_S32 param)68 status_t C2OMXNode::sendCommand(OMX_COMMANDTYPE cmd, OMX_S32 param) {
69 if (cmd == OMX_CommandStateSet && param == OMX_StateLoaded) {
70 // Reset first input frame so if C2OMXNode is recycled, the timestamp does not become
71 // negative. This is a workaround for HW codecs that do not handle timestamp rollover.
72 mFirstInputFrame = true;
73 }
74 return ERROR_UNSUPPORTED;
75 }
76
getParameter(OMX_INDEXTYPE index,void * params,size_t size)77 status_t C2OMXNode::getParameter(OMX_INDEXTYPE index, void *params, size_t size) {
78 status_t err = ERROR_UNSUPPORTED;
79 switch ((uint32_t)index) {
80 case OMX_IndexParamConsumerUsageBits: {
81 OMX_U32 *usage = (OMX_U32 *)params;
82 *usage = mUsage;
83 err = OK;
84 break;
85 }
86 case OMX_IndexParamPortDefinition: {
87 if (size < sizeof(OMX_PARAM_PORTDEFINITIONTYPE)) {
88 return BAD_VALUE;
89 }
90 OMX_PARAM_PORTDEFINITIONTYPE *pDef = (OMX_PARAM_PORTDEFINITIONTYPE *)params;
91 // TODO: read these from intf()
92 pDef->nBufferCountActual = 16;
93 pDef->eDomain = OMX_PortDomainVideo;
94 pDef->format.video.nFrameWidth = mWidth;
95 pDef->format.video.nFrameHeight = mHeight;
96 err = OK;
97 break;
98 }
99 default:
100 break;
101 }
102 return err;
103 }
104
setParameter(OMX_INDEXTYPE index,const void * params,size_t size)105 status_t C2OMXNode::setParameter(OMX_INDEXTYPE index, const void *params, size_t size) {
106 // handle max/fixed frame duration control
107 if (index == (OMX_INDEXTYPE)OMX_IndexParamMaxFrameDurationForBitrateControl
108 && params != NULL
109 && size == sizeof(OMX_PARAM_U32TYPE)) {
110 // The incoming number is an int32_t contained in OMX_U32.
111 mAdjustTimestampGapUs = (int32_t)((OMX_PARAM_U32TYPE*)params)->nU32;
112 return OK;
113 }
114 return ERROR_UNSUPPORTED;
115 }
116
getConfig(OMX_INDEXTYPE index,void * config,size_t size)117 status_t C2OMXNode::getConfig(OMX_INDEXTYPE index, void *config, size_t size) {
118 (void)index;
119 (void)config;
120 (void)size;
121 return ERROR_UNSUPPORTED;
122 }
123
setConfig(OMX_INDEXTYPE index,const void * config,size_t size)124 status_t C2OMXNode::setConfig(OMX_INDEXTYPE index, const void *config, size_t size) {
125 (void)index;
126 (void)config;
127 (void)size;
128 return ERROR_UNSUPPORTED;
129 }
130
setPortMode(OMX_U32 portIndex,IOMX::PortMode mode)131 status_t C2OMXNode::setPortMode(OMX_U32 portIndex, IOMX::PortMode mode) {
132 (void)portIndex;
133 (void)mode;
134 return ERROR_UNSUPPORTED;
135 }
136
prepareForAdaptivePlayback(OMX_U32 portIndex,OMX_BOOL enable,OMX_U32 maxFrameWidth,OMX_U32 maxFrameHeight)137 status_t C2OMXNode::prepareForAdaptivePlayback(
138 OMX_U32 portIndex, OMX_BOOL enable,
139 OMX_U32 maxFrameWidth, OMX_U32 maxFrameHeight) {
140 (void)portIndex;
141 (void)enable;
142 (void)maxFrameWidth;
143 (void)maxFrameHeight;
144 return ERROR_UNSUPPORTED;
145 }
146
configureVideoTunnelMode(OMX_U32 portIndex,OMX_BOOL tunneled,OMX_U32 audioHwSync,native_handle_t ** sidebandHandle)147 status_t C2OMXNode::configureVideoTunnelMode(
148 OMX_U32 portIndex, OMX_BOOL tunneled,
149 OMX_U32 audioHwSync, native_handle_t **sidebandHandle) {
150 (void)portIndex;
151 (void)tunneled;
152 (void)audioHwSync;
153 *sidebandHandle = nullptr;
154 return ERROR_UNSUPPORTED;
155 }
156
getGraphicBufferUsage(OMX_U32 portIndex,OMX_U32 * usage)157 status_t C2OMXNode::getGraphicBufferUsage(OMX_U32 portIndex, OMX_U32* usage) {
158 (void)portIndex;
159 *usage = 0;
160 return ERROR_UNSUPPORTED;
161 }
162
setInputSurface(const sp<IOMXBufferSource> & bufferSource)163 status_t C2OMXNode::setInputSurface(const sp<IOMXBufferSource> &bufferSource) {
164 c2_status_t err = GetCodec2PlatformAllocatorStore()->fetchAllocator(
165 C2PlatformAllocatorStore::GRALLOC,
166 &mAllocator);
167 if (err != OK) {
168 return UNKNOWN_ERROR;
169 }
170 mBufferSource = bufferSource;
171 return OK;
172 }
173
allocateSecureBuffer(OMX_U32 portIndex,size_t size,buffer_id * buffer,void ** bufferData,sp<NativeHandle> * nativeHandle)174 status_t C2OMXNode::allocateSecureBuffer(
175 OMX_U32 portIndex, size_t size, buffer_id *buffer,
176 void **bufferData, sp<NativeHandle> *nativeHandle) {
177 (void)portIndex;
178 (void)size;
179 (void)nativeHandle;
180 *buffer = 0;
181 *bufferData = nullptr;
182 return ERROR_UNSUPPORTED;
183 }
184
useBuffer(OMX_U32 portIndex,const OMXBuffer & omxBuf,buffer_id * buffer)185 status_t C2OMXNode::useBuffer(
186 OMX_U32 portIndex, const OMXBuffer &omxBuf, buffer_id *buffer) {
187 (void)portIndex;
188 (void)omxBuf;
189 *buffer = 0;
190 return ERROR_UNSUPPORTED;
191 }
192
freeBuffer(OMX_U32 portIndex,buffer_id buffer)193 status_t C2OMXNode::freeBuffer(OMX_U32 portIndex, buffer_id buffer) {
194 (void)portIndex;
195 (void)buffer;
196 return ERROR_UNSUPPORTED;
197 }
198
fillBuffer(buffer_id buffer,const OMXBuffer & omxBuf,int fenceFd)199 status_t C2OMXNode::fillBuffer(
200 buffer_id buffer, const OMXBuffer &omxBuf, int fenceFd) {
201 (void)buffer;
202 (void)omxBuf;
203 (void)fenceFd;
204 return ERROR_UNSUPPORTED;
205 }
206
emptyBuffer(buffer_id buffer,const OMXBuffer & omxBuf,OMX_U32 flags,OMX_TICKS timestamp,int fenceFd)207 status_t C2OMXNode::emptyBuffer(
208 buffer_id buffer, const OMXBuffer &omxBuf,
209 OMX_U32 flags, OMX_TICKS timestamp, int fenceFd) {
210 // TODO: better fence handling
211 if (fenceFd >= 0) {
212 sp<Fence> fence = new Fence(fenceFd);
213 fence->waitForever(LOG_TAG);
214 }
215 std::shared_ptr<Codec2Client::Component> comp = mComp.lock();
216 if (!comp) {
217 return NO_INIT;
218 }
219
220 uint32_t c2Flags = (flags & OMX_BUFFERFLAG_EOS)
221 ? C2FrameData::FLAG_END_OF_STREAM : 0;
222 std::shared_ptr<C2GraphicBlock> block;
223
224 C2Handle *handle = nullptr;
225 if (omxBuf.mBufferType == OMXBuffer::kBufferTypeANWBuffer
226 && omxBuf.mGraphicBuffer != nullptr) {
227 std::shared_ptr<C2GraphicAllocation> alloc;
228 handle = WrapNativeCodec2GrallocHandle(
229 omxBuf.mGraphicBuffer->handle,
230 omxBuf.mGraphicBuffer->width,
231 omxBuf.mGraphicBuffer->height,
232 omxBuf.mGraphicBuffer->format,
233 omxBuf.mGraphicBuffer->usage,
234 omxBuf.mGraphicBuffer->stride);
235 c2_status_t err = mAllocator->priorGraphicAllocation(handle, &alloc);
236 if (err != OK) {
237 return UNKNOWN_ERROR;
238 }
239 block = _C2BlockFactory::CreateGraphicBlock(alloc);
240 } else if (!(flags & OMX_BUFFERFLAG_EOS)) {
241 return BAD_VALUE;
242 }
243
244 std::unique_ptr<C2Work> work(new C2Work);
245 work->input.flags = (C2FrameData::flags_t)c2Flags;
246 work->input.ordinal.timestamp = timestamp;
247
248 // WORKAROUND: adjust timestamp based on gapUs
249 {
250 work->input.ordinal.customOrdinal = timestamp; // save input timestamp
251 if (mFirstInputFrame) {
252 // grab timestamps on first frame
253 mPrevInputTimestamp = timestamp;
254 mPrevCodecTimestamp = timestamp;
255 mFirstInputFrame = false;
256 } else if (mAdjustTimestampGapUs > 0) {
257 work->input.ordinal.timestamp =
258 mPrevCodecTimestamp
259 + c2_min((timestamp - mPrevInputTimestamp).peek(), mAdjustTimestampGapUs);
260 } else if (mAdjustTimestampGapUs < 0) {
261 work->input.ordinal.timestamp = mPrevCodecTimestamp - mAdjustTimestampGapUs;
262 }
263 mPrevInputTimestamp = work->input.ordinal.customOrdinal;
264 mPrevCodecTimestamp = work->input.ordinal.timestamp;
265 ALOGV("adjusting %lld to %lld (gap=%lld)",
266 work->input.ordinal.customOrdinal.peekll(),
267 work->input.ordinal.timestamp.peekll(),
268 (long long)mAdjustTimestampGapUs);
269 }
270
271 work->input.ordinal.frameIndex = mFrameIndex++;
272 work->input.buffers.clear();
273 if (block) {
274 std::shared_ptr<C2Buffer> c2Buffer(
275 // TODO: fence
276 new Buffer2D(block->share(
277 C2Rect(block->width(), block->height()), ::C2Fence())),
278 [buffer, source = getSource()](C2Buffer *ptr) {
279 delete ptr;
280 // TODO: fence
281 (void)source->onInputBufferEmptied(buffer, -1);
282 });
283 work->input.buffers.push_back(c2Buffer);
284 }
285 work->worklets.clear();
286 work->worklets.emplace_back(new C2Worklet);
287 std::list<std::unique_ptr<C2Work>> items;
288 items.push_back(std::move(work));
289
290 c2_status_t err = comp->queue(&items);
291 if (err != C2_OK) {
292 return UNKNOWN_ERROR;
293 }
294
295 return OK;
296 }
297
getExtensionIndex(const char * parameterName,OMX_INDEXTYPE * index)298 status_t C2OMXNode::getExtensionIndex(
299 const char *parameterName, OMX_INDEXTYPE *index) {
300 (void)parameterName;
301 *index = OMX_IndexMax;
302 return ERROR_UNSUPPORTED;
303 }
304
dispatchMessage(const omx_message & msg)305 status_t C2OMXNode::dispatchMessage(const omx_message& msg) {
306 if (msg.type != omx_message::EVENT) {
307 return ERROR_UNSUPPORTED;
308 }
309 if (msg.u.event_data.event != OMX_EventDataSpaceChanged) {
310 return ERROR_UNSUPPORTED;
311 }
312 android_dataspace dataSpace = (android_dataspace)msg.u.event_data.data1;
313 uint32_t pixelFormat = msg.u.event_data.data3;
314
315 // TODO: set dataspace on component to see if it impacts color aspects
316 ALOGD("dataspace changed to %#x pixel format: %#x", dataSpace, pixelFormat);
317 return OK;
318 }
319
getSource()320 sp<IOMXBufferSource> C2OMXNode::getSource() {
321 return mBufferSource;
322 }
323
setFrameSize(uint32_t width,uint32_t height)324 void C2OMXNode::setFrameSize(uint32_t width, uint32_t height) {
325 mWidth = width;
326 mHeight = height;
327 }
328
329 } // namespace android
330