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> |
Wonsik Kim | 414eb15 | 2020-11-12 11:14:42 -0800 | [diff] [blame] | 28 | #include <C2Config.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 29 | #include <C2PlatformSupport.h> |
| 30 | |
| 31 | #include <OMX_Component.h> |
| 32 | #include <OMX_Index.h> |
| 33 | #include <OMX_IndexExt.h> |
| 34 | |
Chong Zhang | 442be45 | 2019-06-18 10:20:54 -0700 | [diff] [blame] | 35 | #include <android/fdsan.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 36 | #include <media/stagefright/omx/OMXUtils.h> |
| 37 | #include <media/stagefright/MediaErrors.h> |
| 38 | #include <ui/Fence.h> |
| 39 | #include <ui/GraphicBuffer.h> |
Wonsik Kim | 831b8d7 | 2019-06-11 17:52:56 -0700 | [diff] [blame] | 40 | #include <utils/Thread.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 41 | |
| 42 | #include "C2OMXNode.h" |
| 43 | |
| 44 | namespace android { |
| 45 | |
| 46 | namespace { |
| 47 | |
Wonsik Kim | 414eb15 | 2020-11-12 11:14:42 -0800 | [diff] [blame] | 48 | constexpr OMX_U32 kPortIndexInput = 0; |
| 49 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 50 | class Buffer2D : public C2Buffer { |
| 51 | public: |
| 52 | explicit Buffer2D(C2ConstGraphicBlock block) : C2Buffer({ block }) {} |
| 53 | }; |
| 54 | |
| 55 | } // namespace |
| 56 | |
Wonsik Kim | 831b8d7 | 2019-06-11 17:52:56 -0700 | [diff] [blame] | 57 | class C2OMXNode::QueueThread : public Thread { |
| 58 | public: |
| 59 | QueueThread() : Thread(false) {} |
| 60 | ~QueueThread() override = default; |
| 61 | void queue( |
| 62 | const std::shared_ptr<Codec2Client::Component> &comp, |
| 63 | int fenceFd, |
| 64 | std::unique_ptr<C2Work> &&work, |
| 65 | android::base::unique_fd &&fd0, |
| 66 | android::base::unique_fd &&fd1) { |
| 67 | Mutexed<Jobs>::Locked jobs(mJobs); |
Sungtak Lee | 45d8059 | 2020-02-25 13:54:14 -0800 | [diff] [blame] | 68 | auto it = jobs->queues.try_emplace(comp, comp).first; |
Wonsik Kim | 831b8d7 | 2019-06-11 17:52:56 -0700 | [diff] [blame] | 69 | it->second.workList.emplace_back( |
| 70 | std::move(work), fenceFd, std::move(fd0), std::move(fd1)); |
| 71 | jobs->cond.broadcast(); |
| 72 | } |
| 73 | |
| 74 | protected: |
| 75 | bool threadLoop() override { |
| 76 | constexpr nsecs_t kIntervalNs = nsecs_t(10) * 1000 * 1000; // 10ms |
| 77 | constexpr nsecs_t kWaitNs = kIntervalNs * 2; |
| 78 | for (int i = 0; i < 2; ++i) { |
| 79 | Mutexed<Jobs>::Locked jobs(mJobs); |
| 80 | nsecs_t nowNs = systemTime(); |
| 81 | bool queued = false; |
Wonsik Kim | 0ca30c3 | 2019-06-28 14:15:02 -0700 | [diff] [blame] | 82 | for (auto it = jobs->queues.begin(); it != jobs->queues.end(); ) { |
Wonsik Kim | 831b8d7 | 2019-06-11 17:52:56 -0700 | [diff] [blame] | 83 | Queue &queue = it->second; |
| 84 | if (queue.workList.empty() |
Sungtak Lee | 45d8059 | 2020-02-25 13:54:14 -0800 | [diff] [blame] | 85 | || (queue.lastQueuedTimestampNs != 0 && |
| 86 | nowNs - queue.lastQueuedTimestampNs < kIntervalNs)) { |
Wonsik Kim | 0ca30c3 | 2019-06-28 14:15:02 -0700 | [diff] [blame] | 87 | ++it; |
Wonsik Kim | 831b8d7 | 2019-06-11 17:52:56 -0700 | [diff] [blame] | 88 | continue; |
| 89 | } |
| 90 | std::shared_ptr<Codec2Client::Component> comp = queue.component.lock(); |
| 91 | if (!comp) { |
| 92 | it = jobs->queues.erase(it); |
| 93 | continue; |
| 94 | } |
| 95 | std::list<std::unique_ptr<C2Work>> items; |
| 96 | std::vector<int> fenceFds; |
| 97 | std::vector<android::base::unique_fd> uniqueFds; |
| 98 | while (!queue.workList.empty()) { |
| 99 | items.push_back(std::move(queue.workList.front().work)); |
| 100 | fenceFds.push_back(queue.workList.front().fenceFd); |
| 101 | uniqueFds.push_back(std::move(queue.workList.front().fd0)); |
| 102 | uniqueFds.push_back(std::move(queue.workList.front().fd1)); |
| 103 | queue.workList.pop_front(); |
| 104 | } |
| 105 | |
| 106 | jobs.unlock(); |
| 107 | for (int fenceFd : fenceFds) { |
| 108 | sp<Fence> fence(new Fence(fenceFd)); |
| 109 | fence->waitForever(LOG_TAG); |
| 110 | } |
Sungtak Lee | 45d8059 | 2020-02-25 13:54:14 -0800 | [diff] [blame] | 111 | queue.lastQueuedTimestampNs = nowNs; |
Wonsik Kim | 831b8d7 | 2019-06-11 17:52:56 -0700 | [diff] [blame] | 112 | comp->queue(&items); |
| 113 | for (android::base::unique_fd &ufd : uniqueFds) { |
| 114 | (void)ufd.release(); |
| 115 | } |
| 116 | jobs.lock(); |
| 117 | |
Wonsik Kim | 0ca30c3 | 2019-06-28 14:15:02 -0700 | [diff] [blame] | 118 | it = jobs->queues.upper_bound(comp); |
Wonsik Kim | 831b8d7 | 2019-06-11 17:52:56 -0700 | [diff] [blame] | 119 | queued = true; |
| 120 | } |
| 121 | if (queued) { |
| 122 | return true; |
| 123 | } |
| 124 | if (i == 0) { |
| 125 | jobs.waitForConditionRelative(jobs->cond, kWaitNs); |
| 126 | } |
| 127 | } |
| 128 | return true; |
| 129 | } |
| 130 | |
| 131 | private: |
| 132 | struct WorkFence { |
| 133 | WorkFence(std::unique_ptr<C2Work> &&w, int fd) : work(std::move(w)), fenceFd(fd) {} |
| 134 | |
| 135 | WorkFence( |
| 136 | std::unique_ptr<C2Work> &&w, |
| 137 | int fd, |
| 138 | android::base::unique_fd &&uniqueFd0, |
| 139 | android::base::unique_fd &&uniqueFd1) |
| 140 | : work(std::move(w)), |
| 141 | fenceFd(fd), |
| 142 | fd0(std::move(uniqueFd0)), |
| 143 | fd1(std::move(uniqueFd1)) {} |
| 144 | |
| 145 | std::unique_ptr<C2Work> work; |
| 146 | int fenceFd; |
| 147 | android::base::unique_fd fd0; |
| 148 | android::base::unique_fd fd1; |
| 149 | }; |
| 150 | struct Queue { |
Sungtak Lee | 45d8059 | 2020-02-25 13:54:14 -0800 | [diff] [blame] | 151 | Queue(const std::shared_ptr<Codec2Client::Component> &comp) |
| 152 | : component(comp), lastQueuedTimestampNs(0) {} |
Wonsik Kim | 831b8d7 | 2019-06-11 17:52:56 -0700 | [diff] [blame] | 153 | Queue(const Queue &) = delete; |
| 154 | Queue &operator =(const Queue &) = delete; |
| 155 | |
| 156 | std::weak_ptr<Codec2Client::Component> component; |
| 157 | std::list<WorkFence> workList; |
| 158 | nsecs_t lastQueuedTimestampNs; |
| 159 | }; |
| 160 | struct Jobs { |
| 161 | std::map<std::weak_ptr<Codec2Client::Component>, |
| 162 | Queue, |
| 163 | std::owner_less<std::weak_ptr<Codec2Client::Component>>> queues; |
| 164 | Condition cond; |
| 165 | }; |
| 166 | Mutexed<Jobs> mJobs; |
| 167 | }; |
| 168 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 169 | C2OMXNode::C2OMXNode(const std::shared_ptr<Codec2Client::Component> &comp) |
Wonsik Kim | 9eac4d1 | 2019-05-23 12:58:48 -0700 | [diff] [blame] | 170 | : mComp(comp), mFrameIndex(0), mWidth(0), mHeight(0), mUsage(0), |
Wonsik Kim | 831b8d7 | 2019-06-11 17:52:56 -0700 | [diff] [blame] | 171 | mAdjustTimestampGapUs(0), mFirstInputFrame(true), |
| 172 | mQueueThread(new QueueThread) { |
Chong Zhang | 442be45 | 2019-06-18 10:20:54 -0700 | [diff] [blame] | 173 | android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_WARN_ALWAYS); |
Wonsik Kim | 831b8d7 | 2019-06-11 17:52:56 -0700 | [diff] [blame] | 174 | mQueueThread->run("C2OMXNode", PRIORITY_AUDIO); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | status_t C2OMXNode::freeNode() { |
| 178 | mComp.reset(); |
Chong Zhang | 442be45 | 2019-06-18 10:20:54 -0700 | [diff] [blame] | 179 | android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_WARN_ONCE); |
Wonsik Kim | 831b8d7 | 2019-06-11 17:52:56 -0700 | [diff] [blame] | 180 | return mQueueThread->requestExitAndWait(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | status_t C2OMXNode::sendCommand(OMX_COMMANDTYPE cmd, OMX_S32 param) { |
| 184 | if (cmd == OMX_CommandStateSet && param == OMX_StateLoaded) { |
| 185 | // Reset first input frame so if C2OMXNode is recycled, the timestamp does not become |
| 186 | // negative. This is a workaround for HW codecs that do not handle timestamp rollover. |
| 187 | mFirstInputFrame = true; |
| 188 | } |
| 189 | return ERROR_UNSUPPORTED; |
| 190 | } |
| 191 | |
| 192 | status_t C2OMXNode::getParameter(OMX_INDEXTYPE index, void *params, size_t size) { |
| 193 | status_t err = ERROR_UNSUPPORTED; |
| 194 | switch ((uint32_t)index) { |
| 195 | case OMX_IndexParamConsumerUsageBits: { |
| 196 | OMX_U32 *usage = (OMX_U32 *)params; |
| 197 | *usage = mUsage; |
| 198 | err = OK; |
| 199 | break; |
| 200 | } |
| 201 | case OMX_IndexParamPortDefinition: { |
| 202 | if (size < sizeof(OMX_PARAM_PORTDEFINITIONTYPE)) { |
| 203 | return BAD_VALUE; |
| 204 | } |
| 205 | OMX_PARAM_PORTDEFINITIONTYPE *pDef = (OMX_PARAM_PORTDEFINITIONTYPE *)params; |
Wonsik Kim | 414eb15 | 2020-11-12 11:14:42 -0800 | [diff] [blame] | 206 | if (pDef->nPortIndex != kPortIndexInput) { |
| 207 | break; |
| 208 | } |
| 209 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 210 | pDef->nBufferCountActual = 16; |
Wonsik Kim | 414eb15 | 2020-11-12 11:14:42 -0800 | [diff] [blame] | 211 | |
| 212 | std::shared_ptr<Codec2Client::Component> comp = mComp.lock(); |
| 213 | C2PortActualDelayTuning::input inputDelay(0); |
| 214 | C2ActualPipelineDelayTuning pipelineDelay(0); |
| 215 | c2_status_t c2err = comp->query( |
| 216 | {&inputDelay, &pipelineDelay}, {}, C2_DONT_BLOCK, nullptr); |
| 217 | if (c2err == C2_OK || c2err == C2_BAD_INDEX) { |
| 218 | pDef->nBufferCountActual = 4; |
| 219 | pDef->nBufferCountActual += (inputDelay ? inputDelay.value : 0u); |
| 220 | pDef->nBufferCountActual += (pipelineDelay ? pipelineDelay.value : 0u); |
| 221 | } |
| 222 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 223 | pDef->eDomain = OMX_PortDomainVideo; |
| 224 | pDef->format.video.nFrameWidth = mWidth; |
| 225 | pDef->format.video.nFrameHeight = mHeight; |
Wonsik Kim | 414eb15 | 2020-11-12 11:14:42 -0800 | [diff] [blame] | 226 | pDef->format.video.eColorFormat = OMX_COLOR_FormatAndroidOpaque; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 227 | err = OK; |
| 228 | break; |
| 229 | } |
| 230 | default: |
| 231 | break; |
| 232 | } |
| 233 | return err; |
| 234 | } |
| 235 | |
| 236 | status_t C2OMXNode::setParameter(OMX_INDEXTYPE index, const void *params, size_t size) { |
Wonsik Kim | 9eac4d1 | 2019-05-23 12:58:48 -0700 | [diff] [blame] | 237 | if (params == NULL) { |
| 238 | return BAD_VALUE; |
| 239 | } |
| 240 | switch ((uint32_t)index) { |
| 241 | case OMX_IndexParamMaxFrameDurationForBitrateControl: |
| 242 | // handle max/fixed frame duration control |
| 243 | if (size != sizeof(OMX_PARAM_U32TYPE)) { |
| 244 | return BAD_VALUE; |
| 245 | } |
| 246 | // The incoming number is an int32_t contained in OMX_U32. |
| 247 | mAdjustTimestampGapUs = (int32_t)((OMX_PARAM_U32TYPE*)params)->nU32; |
| 248 | return OK; |
| 249 | |
| 250 | case OMX_IndexParamConsumerUsageBits: |
| 251 | if (size != sizeof(OMX_U32)) { |
| 252 | return BAD_VALUE; |
| 253 | } |
| 254 | mUsage = *((OMX_U32 *)params); |
| 255 | return OK; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 256 | } |
| 257 | return ERROR_UNSUPPORTED; |
| 258 | } |
| 259 | |
| 260 | status_t C2OMXNode::getConfig(OMX_INDEXTYPE index, void *config, size_t size) { |
| 261 | (void)index; |
| 262 | (void)config; |
| 263 | (void)size; |
| 264 | return ERROR_UNSUPPORTED; |
| 265 | } |
| 266 | |
| 267 | status_t C2OMXNode::setConfig(OMX_INDEXTYPE index, const void *config, size_t size) { |
| 268 | (void)index; |
| 269 | (void)config; |
| 270 | (void)size; |
| 271 | return ERROR_UNSUPPORTED; |
| 272 | } |
| 273 | |
| 274 | status_t C2OMXNode::setPortMode(OMX_U32 portIndex, IOMX::PortMode mode) { |
| 275 | (void)portIndex; |
| 276 | (void)mode; |
| 277 | return ERROR_UNSUPPORTED; |
| 278 | } |
| 279 | |
| 280 | status_t C2OMXNode::prepareForAdaptivePlayback( |
| 281 | OMX_U32 portIndex, OMX_BOOL enable, |
| 282 | OMX_U32 maxFrameWidth, OMX_U32 maxFrameHeight) { |
| 283 | (void)portIndex; |
| 284 | (void)enable; |
| 285 | (void)maxFrameWidth; |
| 286 | (void)maxFrameHeight; |
| 287 | return ERROR_UNSUPPORTED; |
| 288 | } |
| 289 | |
| 290 | status_t C2OMXNode::configureVideoTunnelMode( |
| 291 | OMX_U32 portIndex, OMX_BOOL tunneled, |
| 292 | OMX_U32 audioHwSync, native_handle_t **sidebandHandle) { |
| 293 | (void)portIndex; |
| 294 | (void)tunneled; |
| 295 | (void)audioHwSync; |
| 296 | *sidebandHandle = nullptr; |
| 297 | return ERROR_UNSUPPORTED; |
| 298 | } |
| 299 | |
| 300 | status_t C2OMXNode::getGraphicBufferUsage(OMX_U32 portIndex, OMX_U32* usage) { |
| 301 | (void)portIndex; |
| 302 | *usage = 0; |
| 303 | return ERROR_UNSUPPORTED; |
| 304 | } |
| 305 | |
| 306 | status_t C2OMXNode::setInputSurface(const sp<IOMXBufferSource> &bufferSource) { |
| 307 | c2_status_t err = GetCodec2PlatformAllocatorStore()->fetchAllocator( |
| 308 | C2PlatformAllocatorStore::GRALLOC, |
| 309 | &mAllocator); |
| 310 | if (err != OK) { |
| 311 | return UNKNOWN_ERROR; |
| 312 | } |
| 313 | mBufferSource = bufferSource; |
| 314 | return OK; |
| 315 | } |
| 316 | |
| 317 | status_t C2OMXNode::allocateSecureBuffer( |
| 318 | OMX_U32 portIndex, size_t size, buffer_id *buffer, |
| 319 | void **bufferData, sp<NativeHandle> *nativeHandle) { |
| 320 | (void)portIndex; |
| 321 | (void)size; |
| 322 | (void)nativeHandle; |
| 323 | *buffer = 0; |
| 324 | *bufferData = nullptr; |
| 325 | return ERROR_UNSUPPORTED; |
| 326 | } |
| 327 | |
| 328 | status_t C2OMXNode::useBuffer( |
| 329 | OMX_U32 portIndex, const OMXBuffer &omxBuf, buffer_id *buffer) { |
| 330 | (void)portIndex; |
| 331 | (void)omxBuf; |
| 332 | *buffer = 0; |
| 333 | return ERROR_UNSUPPORTED; |
| 334 | } |
| 335 | |
| 336 | status_t C2OMXNode::freeBuffer(OMX_U32 portIndex, buffer_id buffer) { |
| 337 | (void)portIndex; |
| 338 | (void)buffer; |
| 339 | return ERROR_UNSUPPORTED; |
| 340 | } |
| 341 | |
| 342 | status_t C2OMXNode::fillBuffer( |
| 343 | buffer_id buffer, const OMXBuffer &omxBuf, int fenceFd) { |
| 344 | (void)buffer; |
| 345 | (void)omxBuf; |
| 346 | (void)fenceFd; |
| 347 | return ERROR_UNSUPPORTED; |
| 348 | } |
| 349 | |
| 350 | status_t C2OMXNode::emptyBuffer( |
| 351 | buffer_id buffer, const OMXBuffer &omxBuf, |
| 352 | OMX_U32 flags, OMX_TICKS timestamp, int fenceFd) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 353 | std::shared_ptr<Codec2Client::Component> comp = mComp.lock(); |
| 354 | if (!comp) { |
| 355 | return NO_INIT; |
| 356 | } |
| 357 | |
| 358 | uint32_t c2Flags = (flags & OMX_BUFFERFLAG_EOS) |
| 359 | ? C2FrameData::FLAG_END_OF_STREAM : 0; |
| 360 | std::shared_ptr<C2GraphicBlock> block; |
| 361 | |
Chong Zhang | 442be45 | 2019-06-18 10:20:54 -0700 | [diff] [blame] | 362 | android::base::unique_fd fd0, fd1; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 363 | C2Handle *handle = nullptr; |
| 364 | if (omxBuf.mBufferType == OMXBuffer::kBufferTypeANWBuffer |
| 365 | && omxBuf.mGraphicBuffer != nullptr) { |
| 366 | std::shared_ptr<C2GraphicAllocation> alloc; |
| 367 | handle = WrapNativeCodec2GrallocHandle( |
Sungtak Lee | a4d13be | 2019-01-23 15:24:46 -0800 | [diff] [blame] | 368 | omxBuf.mGraphicBuffer->handle, |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 369 | omxBuf.mGraphicBuffer->width, |
| 370 | omxBuf.mGraphicBuffer->height, |
| 371 | omxBuf.mGraphicBuffer->format, |
| 372 | omxBuf.mGraphicBuffer->usage, |
| 373 | omxBuf.mGraphicBuffer->stride); |
Chong Zhang | 442be45 | 2019-06-18 10:20:54 -0700 | [diff] [blame] | 374 | if (handle != nullptr) { |
| 375 | // unique_fd takes ownership of the fds, we'll get warning if these |
| 376 | // fds get closed by somebody else. Onwership will be released before |
| 377 | // we return, so that the fds get closed as usually when this function |
| 378 | // goes out of scope (when both items and block are gone). |
| 379 | native_handle_t *nativeHandle = reinterpret_cast<native_handle_t*>(handle); |
| 380 | fd0.reset(nativeHandle->numFds > 0 ? nativeHandle->data[0] : -1); |
| 381 | fd1.reset(nativeHandle->numFds > 1 ? nativeHandle->data[1] : -1); |
| 382 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 383 | c2_status_t err = mAllocator->priorGraphicAllocation(handle, &alloc); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 384 | if (err != OK) { |
Chong Zhang | 442be45 | 2019-06-18 10:20:54 -0700 | [diff] [blame] | 385 | (void)fd0.release(); |
| 386 | (void)fd1.release(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 387 | return UNKNOWN_ERROR; |
| 388 | } |
| 389 | block = _C2BlockFactory::CreateGraphicBlock(alloc); |
| 390 | } else if (!(flags & OMX_BUFFERFLAG_EOS)) { |
| 391 | return BAD_VALUE; |
| 392 | } |
| 393 | |
| 394 | std::unique_ptr<C2Work> work(new C2Work); |
| 395 | work->input.flags = (C2FrameData::flags_t)c2Flags; |
| 396 | work->input.ordinal.timestamp = timestamp; |
| 397 | |
| 398 | // WORKAROUND: adjust timestamp based on gapUs |
| 399 | { |
| 400 | work->input.ordinal.customOrdinal = timestamp; // save input timestamp |
| 401 | if (mFirstInputFrame) { |
| 402 | // grab timestamps on first frame |
| 403 | mPrevInputTimestamp = timestamp; |
| 404 | mPrevCodecTimestamp = timestamp; |
| 405 | mFirstInputFrame = false; |
| 406 | } else if (mAdjustTimestampGapUs > 0) { |
| 407 | work->input.ordinal.timestamp = |
| 408 | mPrevCodecTimestamp |
| 409 | + c2_min((timestamp - mPrevInputTimestamp).peek(), mAdjustTimestampGapUs); |
| 410 | } else if (mAdjustTimestampGapUs < 0) { |
| 411 | work->input.ordinal.timestamp = mPrevCodecTimestamp - mAdjustTimestampGapUs; |
| 412 | } |
| 413 | mPrevInputTimestamp = work->input.ordinal.customOrdinal; |
| 414 | mPrevCodecTimestamp = work->input.ordinal.timestamp; |
| 415 | ALOGV("adjusting %lld to %lld (gap=%lld)", |
| 416 | work->input.ordinal.customOrdinal.peekll(), |
| 417 | work->input.ordinal.timestamp.peekll(), |
| 418 | (long long)mAdjustTimestampGapUs); |
| 419 | } |
| 420 | |
| 421 | work->input.ordinal.frameIndex = mFrameIndex++; |
| 422 | work->input.buffers.clear(); |
| 423 | if (block) { |
| 424 | std::shared_ptr<C2Buffer> c2Buffer( |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 425 | new Buffer2D(block->share( |
Wonsik Kim | 4f3314d | 2019-03-26 17:00:34 -0700 | [diff] [blame] | 426 | C2Rect(block->width(), block->height()), ::C2Fence()))); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 427 | work->input.buffers.push_back(c2Buffer); |
| 428 | } |
| 429 | work->worklets.clear(); |
| 430 | work->worklets.emplace_back(new C2Worklet); |
Wonsik Kim | 831b8d7 | 2019-06-11 17:52:56 -0700 | [diff] [blame] | 431 | mBufferIdsInUse.lock()->emplace(work->input.ordinal.frameIndex.peeku(), buffer); |
| 432 | mQueueThread->queue(comp, fenceFd, std::move(work), std::move(fd0), std::move(fd1)); |
Chong Zhang | 442be45 | 2019-06-18 10:20:54 -0700 | [diff] [blame] | 433 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 434 | return OK; |
| 435 | } |
| 436 | |
| 437 | status_t C2OMXNode::getExtensionIndex( |
| 438 | const char *parameterName, OMX_INDEXTYPE *index) { |
| 439 | (void)parameterName; |
| 440 | *index = OMX_IndexMax; |
| 441 | return ERROR_UNSUPPORTED; |
| 442 | } |
| 443 | |
| 444 | status_t C2OMXNode::dispatchMessage(const omx_message& msg) { |
| 445 | if (msg.type != omx_message::EVENT) { |
| 446 | return ERROR_UNSUPPORTED; |
| 447 | } |
| 448 | if (msg.u.event_data.event != OMX_EventDataSpaceChanged) { |
| 449 | return ERROR_UNSUPPORTED; |
| 450 | } |
| 451 | android_dataspace dataSpace = (android_dataspace)msg.u.event_data.data1; |
| 452 | uint32_t pixelFormat = msg.u.event_data.data3; |
| 453 | |
| 454 | // TODO: set dataspace on component to see if it impacts color aspects |
| 455 | ALOGD("dataspace changed to %#x pixel format: %#x", dataSpace, pixelFormat); |
| 456 | return OK; |
| 457 | } |
| 458 | |
| 459 | sp<IOMXBufferSource> C2OMXNode::getSource() { |
| 460 | return mBufferSource; |
| 461 | } |
| 462 | |
| 463 | void C2OMXNode::setFrameSize(uint32_t width, uint32_t height) { |
| 464 | mWidth = width; |
| 465 | mHeight = height; |
| 466 | } |
| 467 | |
Wonsik Kim | 4f3314d | 2019-03-26 17:00:34 -0700 | [diff] [blame] | 468 | void C2OMXNode::onInputBufferDone(c2_cntr64_t index) { |
| 469 | if (!mBufferSource) { |
| 470 | ALOGD("Buffer source not set (index=%llu)", index.peekull()); |
| 471 | return; |
| 472 | } |
Wonsik Kim | a261e38 | 2019-04-10 11:37:50 -0700 | [diff] [blame] | 473 | |
| 474 | int32_t bufferId = 0; |
| 475 | { |
| 476 | decltype(mBufferIdsInUse)::Locked bufferIds(mBufferIdsInUse); |
| 477 | auto it = bufferIds->find(index.peeku()); |
| 478 | if (it == bufferIds->end()) { |
| 479 | ALOGV("Untracked input index %llu (maybe already removed)", index.peekull()); |
| 480 | return; |
| 481 | } |
| 482 | bufferId = it->second; |
| 483 | (void)bufferIds->erase(it); |
Wonsik Kim | 4f3314d | 2019-03-26 17:00:34 -0700 | [diff] [blame] | 484 | } |
Wonsik Kim | a261e38 | 2019-04-10 11:37:50 -0700 | [diff] [blame] | 485 | (void)mBufferSource->onInputBufferEmptied(bufferId, -1); |
Wonsik Kim | 4f3314d | 2019-03-26 17:00:34 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 488 | } // namespace android |