Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 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: |
| 47 | explicit Buffer2D(C2ConstGraphicBlock block) : C2Buffer({ block }) {} |
| 48 | }; |
| 49 | |
| 50 | } // namespace |
| 51 | |
| 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 | |
| 63 | status_t C2OMXNode::freeNode() { |
| 64 | mComp.reset(); |
| 65 | return OK; |
| 66 | } |
| 67 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 131 | status_t C2OMXNode::setPortMode(OMX_U32 portIndex, IOMX::PortMode mode) { |
| 132 | (void)portIndex; |
| 133 | (void)mode; |
| 134 | return ERROR_UNSUPPORTED; |
| 135 | } |
| 136 | |
| 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 | |
| 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 | |
| 157 | status_t C2OMXNode::getGraphicBufferUsage(OMX_U32 portIndex, OMX_U32* usage) { |
| 158 | (void)portIndex; |
| 159 | *usage = 0; |
| 160 | return ERROR_UNSUPPORTED; |
| 161 | } |
| 162 | |
| 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 | |
| 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 | |
| 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 | |
| 193 | status_t C2OMXNode::freeBuffer(OMX_U32 portIndex, buffer_id buffer) { |
| 194 | (void)portIndex; |
| 195 | (void)buffer; |
| 196 | return ERROR_UNSUPPORTED; |
| 197 | } |
| 198 | |
| 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 | |
| 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; |
Sungtak Lee | 2729dcf | 2019-01-18 13:15:07 -0800 | [diff] [blame] | 228 | native_handle_t *clonedHandle = native_handle_clone(omxBuf.mGraphicBuffer->handle); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 229 | handle = WrapNativeCodec2GrallocHandle( |
Sungtak Lee | 2729dcf | 2019-01-18 13:15:07 -0800 | [diff] [blame] | 230 | clonedHandle, |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 231 | omxBuf.mGraphicBuffer->width, |
| 232 | omxBuf.mGraphicBuffer->height, |
| 233 | omxBuf.mGraphicBuffer->format, |
| 234 | omxBuf.mGraphicBuffer->usage, |
| 235 | omxBuf.mGraphicBuffer->stride); |
| 236 | c2_status_t err = mAllocator->priorGraphicAllocation(handle, &alloc); |
Sungtak Lee | 2729dcf | 2019-01-18 13:15:07 -0800 | [diff] [blame] | 237 | if (clonedHandle) { |
| 238 | native_handle_delete(clonedHandle); |
| 239 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 240 | if (err != OK) { |
| 241 | return UNKNOWN_ERROR; |
| 242 | } |
| 243 | block = _C2BlockFactory::CreateGraphicBlock(alloc); |
| 244 | } else if (!(flags & OMX_BUFFERFLAG_EOS)) { |
| 245 | return BAD_VALUE; |
| 246 | } |
| 247 | |
| 248 | std::unique_ptr<C2Work> work(new C2Work); |
| 249 | work->input.flags = (C2FrameData::flags_t)c2Flags; |
| 250 | work->input.ordinal.timestamp = timestamp; |
| 251 | |
| 252 | // WORKAROUND: adjust timestamp based on gapUs |
| 253 | { |
| 254 | work->input.ordinal.customOrdinal = timestamp; // save input timestamp |
| 255 | if (mFirstInputFrame) { |
| 256 | // grab timestamps on first frame |
| 257 | mPrevInputTimestamp = timestamp; |
| 258 | mPrevCodecTimestamp = timestamp; |
| 259 | mFirstInputFrame = false; |
| 260 | } else if (mAdjustTimestampGapUs > 0) { |
| 261 | work->input.ordinal.timestamp = |
| 262 | mPrevCodecTimestamp |
| 263 | + c2_min((timestamp - mPrevInputTimestamp).peek(), mAdjustTimestampGapUs); |
| 264 | } else if (mAdjustTimestampGapUs < 0) { |
| 265 | work->input.ordinal.timestamp = mPrevCodecTimestamp - mAdjustTimestampGapUs; |
| 266 | } |
| 267 | mPrevInputTimestamp = work->input.ordinal.customOrdinal; |
| 268 | mPrevCodecTimestamp = work->input.ordinal.timestamp; |
| 269 | ALOGV("adjusting %lld to %lld (gap=%lld)", |
| 270 | work->input.ordinal.customOrdinal.peekll(), |
| 271 | work->input.ordinal.timestamp.peekll(), |
| 272 | (long long)mAdjustTimestampGapUs); |
| 273 | } |
| 274 | |
| 275 | work->input.ordinal.frameIndex = mFrameIndex++; |
| 276 | work->input.buffers.clear(); |
| 277 | if (block) { |
| 278 | std::shared_ptr<C2Buffer> c2Buffer( |
| 279 | // TODO: fence |
| 280 | new Buffer2D(block->share( |
| 281 | C2Rect(block->width(), block->height()), ::C2Fence())), |
| 282 | [buffer, source = getSource()](C2Buffer *ptr) { |
| 283 | delete ptr; |
| 284 | // TODO: fence |
| 285 | (void)source->onInputBufferEmptied(buffer, -1); |
| 286 | }); |
| 287 | work->input.buffers.push_back(c2Buffer); |
| 288 | } |
| 289 | work->worklets.clear(); |
| 290 | work->worklets.emplace_back(new C2Worklet); |
| 291 | std::list<std::unique_ptr<C2Work>> items; |
| 292 | items.push_back(std::move(work)); |
| 293 | |
| 294 | c2_status_t err = comp->queue(&items); |
| 295 | if (err != C2_OK) { |
| 296 | return UNKNOWN_ERROR; |
| 297 | } |
| 298 | |
| 299 | return OK; |
| 300 | } |
| 301 | |
| 302 | status_t C2OMXNode::getExtensionIndex( |
| 303 | const char *parameterName, OMX_INDEXTYPE *index) { |
| 304 | (void)parameterName; |
| 305 | *index = OMX_IndexMax; |
| 306 | return ERROR_UNSUPPORTED; |
| 307 | } |
| 308 | |
| 309 | status_t C2OMXNode::dispatchMessage(const omx_message& msg) { |
| 310 | if (msg.type != omx_message::EVENT) { |
| 311 | return ERROR_UNSUPPORTED; |
| 312 | } |
| 313 | if (msg.u.event_data.event != OMX_EventDataSpaceChanged) { |
| 314 | return ERROR_UNSUPPORTED; |
| 315 | } |
| 316 | android_dataspace dataSpace = (android_dataspace)msg.u.event_data.data1; |
| 317 | uint32_t pixelFormat = msg.u.event_data.data3; |
| 318 | |
| 319 | // TODO: set dataspace on component to see if it impacts color aspects |
| 320 | ALOGD("dataspace changed to %#x pixel format: %#x", dataSpace, pixelFormat); |
| 321 | return OK; |
| 322 | } |
| 323 | |
| 324 | sp<IOMXBufferSource> C2OMXNode::getSource() { |
| 325 | return mBufferSource; |
| 326 | } |
| 327 | |
| 328 | void C2OMXNode::setFrameSize(uint32_t width, uint32_t height) { |
| 329 | mWidth = width; |
| 330 | mHeight = height; |
| 331 | } |
| 332 | |
| 333 | } // namespace android |