| /* |
| * Copyright (C) 2010 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| //#define LOG_NDEBUG 0 |
| #define LOG_TAG "ACodec" |
| |
| #ifdef __LP64__ |
| #define OMX_ANDROID_COMPILE_AS_32BIT_ON_64BIT_PLATFORMS |
| #endif |
| |
| #include <inttypes.h> |
| #include <utils/Trace.h> |
| |
| #include <gui/Surface.h> |
| |
| #include <media/stagefright/ACodec.h> |
| |
| #include <binder/MemoryDealer.h> |
| |
| #include <media/stagefright/foundation/hexdump.h> |
| #include <media/stagefright/foundation/ABuffer.h> |
| #include <media/stagefright/foundation/ADebug.h> |
| #include <media/stagefright/foundation/AMessage.h> |
| #include <media/stagefright/foundation/AUtils.h> |
| |
| #include <media/stagefright/BufferProducerWrapper.h> |
| #include <media/stagefright/MediaCodec.h> |
| #include <media/stagefright/MediaCodecList.h> |
| #include <media/stagefright/MediaDefs.h> |
| #include <media/stagefright/OMXClient.h> |
| #include <media/stagefright/OMXCodec.h> |
| #include <media/stagefright/PersistentSurface.h> |
| #include <media/stagefright/SurfaceUtils.h> |
| #include <media/hardware/HardwareAPI.h> |
| |
| #include <OMX_AudioExt.h> |
| #include <OMX_VideoExt.h> |
| #include <OMX_Component.h> |
| #include <OMX_IndexExt.h> |
| #include <OMX_AsString.h> |
| |
| #include "include/avc_utils.h" |
| |
| namespace android { |
| |
| // OMX errors are directly mapped into status_t range if |
| // there is no corresponding MediaError status code. |
| // Use the statusFromOMXError(int32_t omxError) function. |
| // |
| // Currently this is a direct map. |
| // See frameworks/native/include/media/openmax/OMX_Core.h |
| // |
| // Vendor OMX errors from 0x90000000 - 0x9000FFFF |
| // Extension OMX errors from 0x8F000000 - 0x90000000 |
| // Standard OMX errors from 0x80001000 - 0x80001024 (0x80001024 current) |
| // |
| |
| // returns true if err is a recognized OMX error code. |
| // as OMX error is OMX_S32, this is an int32_t type |
| static inline bool isOMXError(int32_t err) { |
| return (ERROR_CODEC_MIN <= err && err <= ERROR_CODEC_MAX); |
| } |
| |
| // converts an OMX error to a status_t |
| static inline status_t statusFromOMXError(int32_t omxError) { |
| switch (omxError) { |
| case OMX_ErrorInvalidComponentName: |
| case OMX_ErrorComponentNotFound: |
| return NAME_NOT_FOUND; // can trigger illegal argument error for provided names. |
| default: |
| return isOMXError(omxError) ? omxError : 0; // no translation required |
| } |
| } |
| |
| // checks and converts status_t to a non-side-effect status_t |
| static inline status_t makeNoSideEffectStatus(status_t err) { |
| switch (err) { |
| // the following errors have side effects and may come |
| // from other code modules. Remap for safety reasons. |
| case INVALID_OPERATION: |
| case DEAD_OBJECT: |
| return UNKNOWN_ERROR; |
| default: |
| return err; |
| } |
| } |
| |
| template<class T> |
| static void InitOMXParams(T *params) { |
| params->nSize = sizeof(T); |
| params->nVersion.s.nVersionMajor = 1; |
| params->nVersion.s.nVersionMinor = 0; |
| params->nVersion.s.nRevision = 0; |
| params->nVersion.s.nStep = 0; |
| } |
| |
| struct MessageList : public RefBase { |
| MessageList() { |
| } |
| virtual ~MessageList() { |
| } |
| std::list<sp<AMessage> > &getList() { return mList; } |
| private: |
| std::list<sp<AMessage> > mList; |
| |
| DISALLOW_EVIL_CONSTRUCTORS(MessageList); |
| }; |
| |
| struct CodecObserver : public BnOMXObserver { |
| CodecObserver() {} |
| |
| void setNotificationMessage(const sp<AMessage> &msg) { |
| mNotify = msg; |
| } |
| |
| // from IOMXObserver |
| virtual void onMessages(const std::list<omx_message> &messages) { |
| if (messages.empty()) { |
| return; |
| } |
| |
| sp<AMessage> notify = mNotify->dup(); |
| bool first = true; |
| sp<MessageList> msgList = new MessageList(); |
| for (std::list<omx_message>::const_iterator it = messages.cbegin(); |
| it != messages.cend(); ++it) { |
| const omx_message &omx_msg = *it; |
| if (first) { |
| notify->setInt32("node", omx_msg.node); |
| first = false; |
| } |
| |
| sp<AMessage> msg = new AMessage; |
| msg->setInt32("type", omx_msg.type); |
| switch (omx_msg.type) { |
| case omx_message::EVENT: |
| { |
| msg->setInt32("event", omx_msg.u.event_data.event); |
| msg->setInt32("data1", omx_msg.u.event_data.data1); |
| msg->setInt32("data2", omx_msg.u.event_data.data2); |
| break; |
| } |
| |
| case omx_message::EMPTY_BUFFER_DONE: |
| { |
| msg->setInt32("buffer", omx_msg.u.buffer_data.buffer); |
| msg->setInt32("fence_fd", omx_msg.fenceFd); |
| break; |
| } |
| |
| case omx_message::FILL_BUFFER_DONE: |
| { |
| msg->setInt32( |
| "buffer", omx_msg.u.extended_buffer_data.buffer); |
| msg->setInt32( |
| "range_offset", |
| omx_msg.u.extended_buffer_data.range_offset); |
| msg->setInt32( |
| "range_length", |
| omx_msg.u.extended_buffer_data.range_length); |
| msg->setInt32( |
| "flags", |
| omx_msg.u.extended_buffer_data.flags); |
| msg->setInt64( |
| "timestamp", |
| omx_msg.u.extended_buffer_data.timestamp); |
| msg->setInt32( |
| "fence_fd", omx_msg.fenceFd); |
| break; |
| } |
| |
| case omx_message::FRAME_RENDERED: |
| { |
| msg->setInt64( |
| "media_time_us", omx_msg.u.render_data.timestamp); |
| msg->setInt64( |
| "system_nano", omx_msg.u.render_data.nanoTime); |
| break; |
| } |
| |
| default: |
| ALOGE("Unrecognized message type: %d", omx_msg.type); |
| break; |
| } |
| msgList->getList().push_back(msg); |
| } |
| notify->setObject("messages", msgList); |
| notify->post(); |
| } |
| |
| protected: |
| virtual ~CodecObserver() {} |
| |
| private: |
| sp<AMessage> mNotify; |
| |
| DISALLOW_EVIL_CONSTRUCTORS(CodecObserver); |
| }; |
| |
| //////////////////////////////////////////////////////////////////////////////// |
| |
| struct ACodec::BaseState : public AState { |
| BaseState(ACodec *codec, const sp<AState> &parentState = NULL); |
| |
| protected: |
| enum PortMode { |
| KEEP_BUFFERS, |
| RESUBMIT_BUFFERS, |
| FREE_BUFFERS, |
| }; |
| |
| ACodec *mCodec; |
| |
| virtual PortMode getPortMode(OMX_U32 portIndex); |
| |
| virtual bool onMessageReceived(const sp<AMessage> &msg); |
| |
| virtual bool onOMXEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2); |
| |
| virtual void onOutputBufferDrained(const sp<AMessage> &msg); |
| virtual void onInputBufferFilled(const sp<AMessage> &msg); |
| |
| void postFillThisBuffer(BufferInfo *info); |
| |
| private: |
| // Handles an OMX message. Returns true iff message was handled. |
| bool onOMXMessage(const sp<AMessage> &msg); |
| |
| // Handles a list of messages. Returns true iff messages were handled. |
| bool onOMXMessageList(const sp<AMessage> &msg); |
| |
| // returns true iff this message is for this component and the component is alive |
| bool checkOMXMessage(const sp<AMessage> &msg); |
| |
| bool onOMXEmptyBufferDone(IOMX::buffer_id bufferID, int fenceFd); |
| |
| bool onOMXFillBufferDone( |
| IOMX::buffer_id bufferID, |
| size_t rangeOffset, size_t rangeLength, |
| OMX_U32 flags, |
| int64_t timeUs, |
| int fenceFd); |
| |
| virtual bool onOMXFrameRendered(int64_t mediaTimeUs, nsecs_t systemNano); |
| |
| void getMoreInputDataIfPossible(); |
| |
| DISALLOW_EVIL_CONSTRUCTORS(BaseState); |
| }; |
| |
| //////////////////////////////////////////////////////////////////////////////// |
| |
| struct ACodec::DeathNotifier : public IBinder::DeathRecipient { |
| DeathNotifier(const sp<AMessage> ¬ify) |
| : mNotify(notify) { |
| } |
| |
| virtual void binderDied(const wp<IBinder> &) { |
| mNotify->post(); |
| } |
| |
| protected: |
| virtual ~DeathNotifier() {} |
| |
| private: |
| sp<AMessage> mNotify; |
| |
| DISALLOW_EVIL_CONSTRUCTORS(DeathNotifier); |
| }; |
| |
| struct ACodec::UninitializedState : public ACodec::BaseState { |
| UninitializedState(ACodec *codec); |
| |
| protected: |
| virtual bool onMessageReceived(const sp<AMessage> &msg); |
| virtual void stateEntered(); |
| |
| private: |
| void onSetup(const sp<AMessage> &msg); |
| bool onAllocateComponent(const sp<AMessage> &msg); |
| |
| sp<DeathNotifier> mDeathNotifier; |
| |
| DISALLOW_EVIL_CONSTRUCTORS(UninitializedState); |
| }; |
| |
| //////////////////////////////////////////////////////////////////////////////// |
| |
| struct ACodec::LoadedState : public ACodec::BaseState { |
| LoadedState(ACodec *codec); |
| |
| protected: |
| virtual bool onMessageReceived(const sp<AMessage> &msg); |
| virtual void stateEntered(); |
| |
| private: |
| friend struct ACodec::UninitializedState; |
| |
| bool onConfigureComponent(const sp<AMessage> &msg); |
| void onCreateInputSurface(const sp<AMessage> &msg); |
| void onSetInputSurface(const sp<AMessage> &msg); |
| void onStart(); |
| void onShutdown(bool keepComponentAllocated); |
| |
| status_t setupInputSurface(); |
| |
| DISALLOW_EVIL_CONSTRUCTORS(LoadedState); |
| }; |
| |
| //////////////////////////////////////////////////////////////////////////////// |
| |
| struct ACodec::LoadedToIdleState : public ACodec::BaseState { |
| LoadedToIdleState(ACodec *codec); |
| |
| protected: |
| virtual bool onMessageReceived(const sp<AMessage> &msg); |
| virtual bool onOMXEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2); |
| virtual void stateEntered(); |
| |
| private: |
| status_t allocateBuffers(); |
| |
| DISALLOW_EVIL_CONSTRUCTORS(LoadedToIdleState); |
| }; |
| |
| //////////////////////////////////////////////////////////////////////////////// |
| |
| struct ACodec::IdleToExecutingState : public ACodec::BaseState { |
| IdleToExecutingState(ACodec *codec); |
| |
| protected: |
| virtual bool onMessageReceived(const sp<AMessage> &msg); |
| virtual bool onOMXEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2); |
| virtual void stateEntered(); |
| |
| private: |
| DISALLOW_EVIL_CONSTRUCTORS(IdleToExecutingState); |
| }; |
| |
| //////////////////////////////////////////////////////////////////////////////// |
| |
| struct ACodec::ExecutingState : public ACodec::BaseState { |
| ExecutingState(ACodec *codec); |
| |
| void submitRegularOutputBuffers(); |
| void submitOutputMetaBuffers(); |
| void submitOutputBuffers(); |
| |
| // Submit output buffers to the decoder, submit input buffers to client |
| // to fill with data. |
| void resume(); |
| |
| // Returns true iff input and output buffers are in play. |
| bool active() const { return mActive; } |
| |
| protected: |
| virtual PortMode getPortMode(OMX_U32 portIndex); |
| virtual bool onMessageReceived(const sp<AMessage> &msg); |
| virtual void stateEntered(); |
| |
| virtual bool onOMXEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2); |
| virtual bool onOMXFrameRendered(int64_t mediaTimeUs, nsecs_t systemNano); |
| |
| private: |
| bool mActive; |
| |
| DISALLOW_EVIL_CONSTRUCTORS(ExecutingState); |
| }; |
| |
| //////////////////////////////////////////////////////////////////////////////// |
| |
| struct ACodec::OutputPortSettingsChangedState : public ACodec::BaseState { |
| OutputPortSettingsChangedState(ACodec *codec); |
| |
| protected: |
| virtual PortMode getPortMode(OMX_U32 portIndex); |
| virtual bool onMessageReceived(const sp<AMessage> &msg); |
| virtual void stateEntered(); |
| |
| virtual bool onOMXEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2); |
| virtual bool onOMXFrameRendered(int64_t mediaTimeUs, nsecs_t systemNano); |
| |
| private: |
| DISALLOW_EVIL_CONSTRUCTORS(OutputPortSettingsChangedState); |
| }; |
| |
| //////////////////////////////////////////////////////////////////////////////// |
| |
| struct ACodec::ExecutingToIdleState : public ACodec::BaseState { |
| ExecutingToIdleState(ACodec *codec); |
| |
| protected: |
| virtual bool onMessageReceived(const sp<AMessage> &msg); |
| virtual void stateEntered(); |
| |
| virtual bool onOMXEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2); |
| |
| virtual void onOutputBufferDrained(const sp<AMessage> &msg); |
| virtual void onInputBufferFilled(const sp<AMessage> &msg); |
| |
| private: |
| void changeStateIfWeOwnAllBuffers(); |
| |
| bool mComponentNowIdle; |
| |
| DISALLOW_EVIL_CONSTRUCTORS(ExecutingToIdleState); |
| }; |
| |
| //////////////////////////////////////////////////////////////////////////////// |
| |
| struct ACodec::IdleToLoadedState : public ACodec::BaseState { |
| IdleToLoadedState(ACodec *codec); |
| |
| protected: |
| virtual bool onMessageReceived(const sp<AMessage> &msg); |
| virtual void stateEntered(); |
| |
| virtual bool onOMXEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2); |
| |
| private: |
| DISALLOW_EVIL_CONSTRUCTORS(IdleToLoadedState); |
| }; |
| |
| //////////////////////////////////////////////////////////////////////////////// |
| |
| struct ACodec::FlushingState : public ACodec::BaseState { |
| FlushingState(ACodec *codec); |
| |
| protected: |
| virtual bool onMessageReceived(const sp<AMessage> &msg); |
| virtual void stateEntered(); |
| |
| virtual bool onOMXEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2); |
| |
| virtual void onOutputBufferDrained(const sp<AMessage> &msg); |
| virtual void onInputBufferFilled(const sp<AMessage> &msg); |
| |
| private: |
| bool mFlushComplete[2]; |
| |
| void changeStateIfWeOwnAllBuffers(); |
| |
| DISALLOW_EVIL_CONSTRUCTORS(FlushingState); |
| }; |
| |
| //////////////////////////////////////////////////////////////////////////////// |
| |
| void ACodec::BufferInfo::setWriteFence(int fenceFd, const char *dbg) { |
| if (mFenceFd >= 0) { |
| ALOGW("OVERWRITE OF %s fence %d by write fence %d in %s", |
| mIsReadFence ? "read" : "write", mFenceFd, fenceFd, dbg); |
| } |
| mFenceFd = fenceFd; |
| mIsReadFence = false; |
| } |
| |
| void ACodec::BufferInfo::setReadFence(int fenceFd, const char *dbg) { |
| if (mFenceFd >= 0) { |
| ALOGW("OVERWRITE OF %s fence %d by read fence %d in %s", |
| mIsReadFence ? "read" : "write", mFenceFd, fenceFd, dbg); |
| } |
| mFenceFd = fenceFd; |
| mIsReadFence = true; |
| } |
| |
| void ACodec::BufferInfo::checkWriteFence(const char *dbg) { |
| if (mFenceFd >= 0 && mIsReadFence) { |
| ALOGD("REUSING read fence %d as write fence in %s", mFenceFd, dbg); |
| } |
| } |
| |
| void ACodec::BufferInfo::checkReadFence(const char *dbg) { |
| if (mFenceFd >= 0 && !mIsReadFence) { |
| ALOGD("REUSING write fence %d as read fence in %s", mFenceFd, dbg); |
| } |
| } |
| |
| //////////////////////////////////////////////////////////////////////////////// |
| |
| ACodec::ACodec() |
| : mQuirks(0), |
| mNode(0), |
| mNativeWindowUsageBits(0), |
| mSentFormat(false), |
| mIsVideo(false), |
| mIsEncoder(false), |
| mFatalError(false), |
| mShutdownInProgress(false), |
| mExplicitShutdown(false), |
| mEncoderDelay(0), |
| mEncoderPadding(0), |
| mRotationDegrees(0), |
| mChannelMaskPresent(false), |
| mChannelMask(0), |
| mDequeueCounter(0), |
| mInputMetadataType(kMetadataBufferTypeInvalid), |
| mOutputMetadataType(kMetadataBufferTypeInvalid), |
| mLegacyAdaptiveExperiment(false), |
| mMetadataBuffersToSubmit(0), |
| mRepeatFrameDelayUs(-1ll), |
| mMaxPtsGapUs(-1ll), |
| mMaxFps(-1), |
| mTimePerFrameUs(-1ll), |
| mTimePerCaptureUs(-1ll), |
| mCreateInputBuffersSuspended(false), |
| mTunneled(false) { |
| mUninitializedState = new UninitializedState(this); |
| mLoadedState = new LoadedState(this); |
| mLoadedToIdleState = new LoadedToIdleState(this); |
| mIdleToExecutingState = new IdleToExecutingState(this); |
| mExecutingState = new ExecutingState(this); |
| |
| mOutputPortSettingsChangedState = |
| new OutputPortSettingsChangedState(this); |
| |
| mExecutingToIdleState = new ExecutingToIdleState(this); |
| mIdleToLoadedState = new IdleToLoadedState(this); |
| mFlushingState = new FlushingState(this); |
| |
| mPortEOS[kPortIndexInput] = mPortEOS[kPortIndexOutput] = false; |
| mInputEOSResult = OK; |
| |
| changeState(mUninitializedState); |
| } |
| |
| ACodec::~ACodec() { |
| } |
| |
| void ACodec::setNotificationMessage(const sp<AMessage> &msg) { |
| mNotify = msg; |
| } |
| |
| void ACodec::initiateSetup(const sp<AMessage> &msg) { |
| msg->setWhat(kWhatSetup); |
| msg->setTarget(this); |
| msg->post(); |
| } |
| |
| void ACodec::signalSetParameters(const sp<AMessage> ¶ms) { |
| sp<AMessage> msg = new AMessage(kWhatSetParameters, this); |
| msg->setMessage("params", params); |
| msg->post(); |
| } |
| |
| void ACodec::initiateAllocateComponent(const sp<AMessage> &msg) { |
| msg->setWhat(kWhatAllocateComponent); |
| msg->setTarget(this); |
| msg->post(); |
| } |
| |
| void ACodec::initiateConfigureComponent(const sp<AMessage> &msg) { |
| msg->setWhat(kWhatConfigureComponent); |
| msg->setTarget(this); |
| msg->post(); |
| } |
| |
| status_t ACodec::setSurface(const sp<Surface> &surface) { |
| sp<AMessage> msg = new AMessage(kWhatSetSurface, this); |
| msg->setObject("surface", surface); |
| |
| sp<AMessage> response; |
| status_t err = msg->postAndAwaitResponse(&response); |
| |
| if (err == OK) { |
| (void)response->findInt32("err", &err); |
| } |
| return err; |
| } |
| |
| void ACodec::initiateCreateInputSurface() { |
| (new AMessage(kWhatCreateInputSurface, this))->post(); |
| } |
| |
| void ACodec::initiateSetInputSurface( |
| const sp<PersistentSurface> &surface) { |
| sp<AMessage> msg = new AMessage(kWhatSetInputSurface, this); |
| msg->setObject("input-surface", surface); |
| msg->post(); |
| } |
| |
| void ACodec::signalEndOfInputStream() { |
| (new AMessage(kWhatSignalEndOfInputStream, this))->post(); |
| } |
| |
| void ACodec::initiateStart() { |
| (new AMessage(kWhatStart, this))->post(); |
| } |
| |
| void ACodec::signalFlush() { |
| ALOGV("[%s] signalFlush", mComponentName.c_str()); |
| (new AMessage(kWhatFlush, this))->post(); |
| } |
| |
| void ACodec::signalResume() { |
| (new AMessage(kWhatResume, this))->post(); |
| } |
| |
| void ACodec::initiateShutdown(bool keepComponentAllocated) { |
| sp<AMessage> msg = new AMessage(kWhatShutdown, this); |
| msg->setInt32("keepComponentAllocated", keepComponentAllocated); |
| msg->post(); |
| if (!keepComponentAllocated) { |
| // ensure shutdown completes in 3 seconds |
| (new AMessage(kWhatReleaseCodecInstance, this))->post(3000000); |
| } |
| } |
| |
| void ACodec::signalRequestIDRFrame() { |
| (new AMessage(kWhatRequestIDRFrame, this))->post(); |
| } |
| |
| // *** NOTE: THE FOLLOWING WORKAROUND WILL BE REMOVED *** |
| // Some codecs may return input buffers before having them processed. |
| // This causes a halt if we already signaled an EOS on the input |
| // port. For now keep submitting an output buffer if there was an |
| // EOS on the input port, but not yet on the output port. |
| void ACodec::signalSubmitOutputMetadataBufferIfEOS_workaround() { |
| if (mPortEOS[kPortIndexInput] && !mPortEOS[kPortIndexOutput] && |
| mMetadataBuffersToSubmit > 0) { |
| (new AMessage(kWhatSubmitOutputMetadataBufferIfEOS, this))->post(); |
| } |
| } |
| |
| status_t ACodec::handleSetSurface(const sp<Surface> &surface) { |
| // allow keeping unset surface |
| if (surface == NULL) { |
| if (mNativeWindow != NULL) { |
| ALOGW("cannot unset a surface"); |
| return INVALID_OPERATION; |
| } |
| return OK; |
| } |
| |
| // cannot switch from bytebuffers to surface |
| if (mNativeWindow == NULL) { |
| ALOGW("component was not configured with a surface"); |
| return INVALID_OPERATION; |
| } |
| |
| ANativeWindow *nativeWindow = surface.get(); |
| // if we have not yet started the codec, we can simply set the native window |
| if (mBuffers[kPortIndexInput].size() == 0) { |
| mNativeWindow = surface; |
| return OK; |
| } |
| |
| // we do not support changing a tunneled surface after start |
| if (mTunneled) { |
| ALOGW("cannot change tunneled surface"); |
| return INVALID_OPERATION; |
| } |
| |
| int usageBits = 0; |
| status_t err = setupNativeWindowSizeFormatAndUsage(nativeWindow, &usageBits); |
| if (err != OK) { |
| return err; |
| } |
| |
| int ignoredFlags = kVideoGrallocUsage; |
| // New output surface is not allowed to add new usage flag except ignored ones. |
| if ((usageBits & ~(mNativeWindowUsageBits | ignoredFlags)) != 0) { |
| ALOGW("cannot change usage from %#x to %#x", mNativeWindowUsageBits, usageBits); |
| return BAD_VALUE; |
| } |
| |
| // get min undequeued count. We cannot switch to a surface that has a higher |
| // undequeued count than we allocated. |
| int minUndequeuedBuffers = 0; |
| err = nativeWindow->query( |
| nativeWindow, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, |
| &minUndequeuedBuffers); |
| if (err != 0) { |
| ALOGE("NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS query failed: %s (%d)", |
| strerror(-err), -err); |
| return err; |
| } |
| if (minUndequeuedBuffers > (int)mNumUndequeuedBuffers) { |
| ALOGE("new surface holds onto more buffers (%d) than planned for (%zu)", |
| minUndequeuedBuffers, mNumUndequeuedBuffers); |
| return BAD_VALUE; |
| } |
| |
| // we cannot change the number of output buffers while OMX is running |
| // set up surface to the same count |
| Vector<BufferInfo> &buffers = mBuffers[kPortIndexOutput]; |
| ALOGV("setting up surface for %zu buffers", buffers.size()); |
| |
| err = native_window_set_buffer_count(nativeWindow, buffers.size()); |
| if (err != 0) { |
| ALOGE("native_window_set_buffer_count failed: %s (%d)", strerror(-err), |
| -err); |
| return err; |
| } |
| |
| // need to enable allocation when attaching |
| surface->getIGraphicBufferProducer()->allowAllocation(true); |
| |
| // for meta data mode, we move dequeud buffers to the new surface. |
| // for non-meta mode, we must move all registered buffers |
| for (size_t i = 0; i < buffers.size(); ++i) { |
| const BufferInfo &info = buffers[i]; |
| // skip undequeued buffers for meta data mode |
| if (storingMetadataInDecodedBuffers() |
| && !mLegacyAdaptiveExperiment |
| && info.mStatus == BufferInfo::OWNED_BY_NATIVE_WINDOW) { |
| ALOGV("skipping buffer %p", info.mGraphicBuffer->getNativeBuffer()); |
| continue; |
| } |
| ALOGV("attaching buffer %p", info.mGraphicBuffer->getNativeBuffer()); |
| |
| err = surface->attachBuffer(info.mGraphicBuffer->getNativeBuffer()); |
| if (err != OK) { |
| ALOGE("failed to attach buffer %p to the new surface: %s (%d)", |
| info.mGraphicBuffer->getNativeBuffer(), |
| strerror(-err), -err); |
| return err; |
| } |
| } |
| |
| // cancel undequeued buffers to new surface |
| if (!storingMetadataInDecodedBuffers() || mLegacyAdaptiveExperiment) { |
| for (size_t i = 0; i < buffers.size(); ++i) { |
| BufferInfo &info = buffers.editItemAt(i); |
| if (info.mStatus == BufferInfo::OWNED_BY_NATIVE_WINDOW) { |
| ALOGV("canceling buffer %p", info.mGraphicBuffer->getNativeBuffer()); |
| err = nativeWindow->cancelBuffer( |
| nativeWindow, info.mGraphicBuffer->getNativeBuffer(), info.mFenceFd); |
| info.mFenceFd = -1; |
| if (err != OK) { |
| ALOGE("failed to cancel buffer %p to the new surface: %s (%d)", |
| info.mGraphicBuffer->getNativeBuffer(), |
| strerror(-err), -err); |
| return err; |
| } |
| } |
| } |
| // disallow further allocation |
| (void)surface->getIGraphicBufferProducer()->allowAllocation(false); |
| } |
| |
| // push blank buffers to previous window if requested |
| if (mFlags & kFlagPushBlankBuffersToNativeWindowOnShutdown) { |
| pushBlankBuffersToNativeWindow(mNativeWindow.get()); |
| } |
| |
| mNativeWindow = nativeWindow; |
| mNativeWindowUsageBits = usageBits; |
| return OK; |
| } |
| |
| status_t ACodec::allocateBuffersOnPort(OMX_U32 portIndex) { |
| CHECK(portIndex == kPortIndexInput || portIndex == kPortIndexOutput); |
| |
| CHECK(mDealer[portIndex] == NULL); |
| CHECK(mBuffers[portIndex].isEmpty()); |
| |
| status_t err; |
| if (mNativeWindow != NULL && portIndex == kPortIndexOutput) { |
| if (storingMetadataInDecodedBuffers()) { |
| err = allocateOutputMetadataBuffers(); |
| } else { |
| err = allocateOutputBuffersFromNativeWindow(); |
| } |
| } else { |
| OMX_PARAM_PORTDEFINITIONTYPE def; |
| InitOMXParams(&def); |
| def.nPortIndex = portIndex; |
| |
| err = mOMX->getParameter( |
| mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); |
| |
| if (err == OK) { |
| MetadataBufferType type = |
| portIndex == kPortIndexOutput ? mOutputMetadataType : mInputMetadataType; |
| int32_t bufSize = def.nBufferSize; |
| if (type == kMetadataBufferTypeGrallocSource) { |
| bufSize = sizeof(VideoGrallocMetadata); |
| } else if (type == kMetadataBufferTypeANWBuffer) { |
| bufSize = sizeof(VideoNativeMetadata); |
| } |
| |
| // If using gralloc or native source input metadata buffers, allocate largest |
| // metadata size as we prefer to generate native source metadata, but component |
| // may require gralloc source. For camera source, allocate at least enough |
| // size for native metadata buffers. |
| int32_t allottedSize = bufSize; |
| if (portIndex == kPortIndexInput && type >= kMetadataBufferTypeGrallocSource) { |
| bufSize = max(sizeof(VideoGrallocMetadata), sizeof(VideoNativeMetadata)); |
| } else if (portIndex == kPortIndexInput && type == kMetadataBufferTypeCameraSource) { |
| bufSize = max(bufSize, (int32_t)sizeof(VideoNativeMetadata)); |
| } |
| |
| ALOGV("[%s] Allocating %u buffers of size %d/%d (from %u using %s) on %s port", |
| mComponentName.c_str(), |
| def.nBufferCountActual, bufSize, allottedSize, def.nBufferSize, asString(type), |
| portIndex == kPortIndexInput ? "input" : "output"); |
| |
| size_t totalSize = def.nBufferCountActual * bufSize; |
| mDealer[portIndex] = new MemoryDealer(totalSize, "ACodec"); |
| |
| for (OMX_U32 i = 0; i < def.nBufferCountActual && err == OK; ++i) { |
| sp<IMemory> mem = mDealer[portIndex]->allocate(bufSize); |
| if (mem == NULL || mem->pointer() == NULL) { |
| return NO_MEMORY; |
| } |
| |
| BufferInfo info; |
| info.mStatus = BufferInfo::OWNED_BY_US; |
| info.mFenceFd = -1; |
| info.mRenderInfo = NULL; |
| |
| uint32_t requiresAllocateBufferBit = |
| (portIndex == kPortIndexInput) |
| ? OMXCodec::kRequiresAllocateBufferOnInputPorts |
| : OMXCodec::kRequiresAllocateBufferOnOutputPorts; |
| |
| if ((portIndex == kPortIndexInput && (mFlags & kFlagIsSecure)) |
| || (portIndex == kPortIndexOutput && usingMetadataOnEncoderOutput())) { |
| mem.clear(); |
| |
| void *ptr; |
| err = mOMX->allocateBuffer( |
| mNode, portIndex, bufSize, &info.mBufferID, |
| &ptr); |
| |
| info.mData = new ABuffer(ptr, bufSize); |
| } else if (mQuirks & requiresAllocateBufferBit) { |
| err = mOMX->allocateBufferWithBackup( |
| mNode, portIndex, mem, &info.mBufferID, allottedSize); |
| } else { |
| err = mOMX->useBuffer(mNode, portIndex, mem, &info.mBufferID, allottedSize); |
| } |
| |
| if (mem != NULL) { |
| info.mData = new ABuffer(mem->pointer(), bufSize); |
| if (type == kMetadataBufferTypeANWBuffer) { |
| ((VideoNativeMetadata *)mem->pointer())->nFenceFd = -1; |
| } |
| } |
| |
| mBuffers[portIndex].push(info); |
| } |
| } |
| } |
| |
| if (err != OK) { |
| return err; |
| } |
| |
| sp<AMessage> notify = mNotify->dup(); |
| notify->setInt32("what", CodecBase::kWhatBuffersAllocated); |
| |
| notify->setInt32("portIndex", portIndex); |
| |
| sp<PortDescription> desc = new PortDescription; |
| |
| for (size_t i = 0; i < mBuffers[portIndex].size(); ++i) { |
| const BufferInfo &info = mBuffers[portIndex][i]; |
| |
| desc->addBuffer(info.mBufferID, info.mData); |
| } |
| |
| notify->setObject("portDesc", desc); |
| notify->post(); |
| |
| return OK; |
| } |
| |
| status_t ACodec::setupNativeWindowSizeFormatAndUsage( |
| ANativeWindow *nativeWindow /* nonnull */, int *finalUsage /* nonnull */) { |
| OMX_PARAM_PORTDEFINITIONTYPE def; |
| InitOMXParams(&def); |
| def.nPortIndex = kPortIndexOutput; |
| |
| status_t err = mOMX->getParameter( |
| mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); |
| |
| if (err != OK) { |
| return err; |
| } |
| |
| OMX_U32 usage = 0; |
| err = mOMX->getGraphicBufferUsage(mNode, kPortIndexOutput, &usage); |
| if (err != 0) { |
| ALOGW("querying usage flags from OMX IL component failed: %d", err); |
| // XXX: Currently this error is logged, but not fatal. |
| usage = 0; |
| } |
| int omxUsage = usage; |
| |
| if (mFlags & kFlagIsGrallocUsageProtected) { |
| usage |= GRALLOC_USAGE_PROTECTED; |
| } |
| |
| usage |= kVideoGrallocUsage; |
| *finalUsage = usage; |
| |
| ALOGV("gralloc usage: %#x(OMX) => %#x(ACodec)", omxUsage, usage); |
| return setNativeWindowSizeFormatAndUsage( |
| nativeWindow, |
| def.format.video.nFrameWidth, |
| def.format.video.nFrameHeight, |
| def.format.video.eColorFormat, |
| mRotationDegrees, |
| usage); |
| } |
| |
| status_t ACodec::configureOutputBuffersFromNativeWindow( |
| OMX_U32 *bufferCount, OMX_U32 *bufferSize, |
| OMX_U32 *minUndequeuedBuffers) { |
| OMX_PARAM_PORTDEFINITIONTYPE def; |
| InitOMXParams(&def); |
| def.nPortIndex = kPortIndexOutput; |
| |
| status_t err = mOMX->getParameter( |
| mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); |
| |
| if (err == OK) { |
| err = setupNativeWindowSizeFormatAndUsage(mNativeWindow.get(), &mNativeWindowUsageBits); |
| } |
| if (err != OK) { |
| mNativeWindowUsageBits = 0; |
| return err; |
| } |
| |
| // Exits here for tunneled video playback codecs -- i.e. skips native window |
| // buffer allocation step as this is managed by the tunneled OMX omponent |
| // itself and explicitly sets def.nBufferCountActual to 0. |
| if (mTunneled) { |
| ALOGV("Tunneled Playback: skipping native window buffer allocation."); |
| def.nBufferCountActual = 0; |
| err = mOMX->setParameter( |
| mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); |
| |
| *minUndequeuedBuffers = 0; |
| *bufferCount = 0; |
| *bufferSize = 0; |
| return err; |
| } |
| |
| *minUndequeuedBuffers = 0; |
| err = mNativeWindow->query( |
| mNativeWindow.get(), NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, |
| (int *)minUndequeuedBuffers); |
| |
| if (err != 0) { |
| ALOGE("NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS query failed: %s (%d)", |
| strerror(-err), -err); |
| return err; |
| } |
| |
| // FIXME: assume that surface is controlled by app (native window |
| // returns the number for the case when surface is not controlled by app) |
| // FIXME2: This means that minUndeqeueudBufs can be 1 larger than reported |
| // For now, try to allocate 1 more buffer, but don't fail if unsuccessful |
| |
| // Use conservative allocation while also trying to reduce starvation |
| // |
| // 1. allocate at least nBufferCountMin + minUndequeuedBuffers - that is the |
| // minimum needed for the consumer to be able to work |
| // 2. try to allocate two (2) additional buffers to reduce starvation from |
| // the consumer |
| // plus an extra buffer to account for incorrect minUndequeuedBufs |
| for (OMX_U32 extraBuffers = 2 + 1; /* condition inside loop */; extraBuffers--) { |
| OMX_U32 newBufferCount = |
| def.nBufferCountMin + *minUndequeuedBuffers + extraBuffers; |
| def.nBufferCountActual = newBufferCount; |
| err = mOMX->setParameter( |
| mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); |
| |
| if (err == OK) { |
| *minUndequeuedBuffers += extraBuffers; |
| break; |
| } |
| |
| ALOGW("[%s] setting nBufferCountActual to %u failed: %d", |
| mComponentName.c_str(), newBufferCount, err); |
| /* exit condition */ |
| if (extraBuffers == 0) { |
| return err; |
| } |
| } |
| |
| err = native_window_set_buffer_count( |
| mNativeWindow.get(), def.nBufferCountActual); |
| |
| if (err != 0) { |
| ALOGE("native_window_set_buffer_count failed: %s (%d)", strerror(-err), |
| -err); |
| return err; |
| } |
| |
| *bufferCount = def.nBufferCountActual; |
| *bufferSize = def.nBufferSize; |
| return err; |
| } |
| |
| status_t ACodec::allocateOutputBuffersFromNativeWindow() { |
| OMX_U32 bufferCount, bufferSize, minUndequeuedBuffers; |
| status_t err = configureOutputBuffersFromNativeWindow( |
| &bufferCount, &bufferSize, &minUndequeuedBuffers); |
| if (err != 0) |
| return err; |
| mNumUndequeuedBuffers = minUndequeuedBuffers; |
| |
| if (!storingMetadataInDecodedBuffers()) { |
| static_cast<Surface*>(mNativeWindow.get()) |
| ->getIGraphicBufferProducer()->allowAllocation(true); |
| } |
| |
| ALOGV("[%s] Allocating %u buffers from a native window of size %u on " |
| "output port", |
| mComponentName.c_str(), bufferCount, bufferSize); |
| |
| // Dequeue buffers and send them to OMX |
| for (OMX_U32 i = 0; i < bufferCount; i++) { |
| ANativeWindowBuffer *buf; |
| int fenceFd; |
| err = mNativeWindow->dequeueBuffer(mNativeWindow.get(), &buf, &fenceFd); |
| if (err != 0) { |
| ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), -err); |
| break; |
| } |
| |
| sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(buf, false)); |
| BufferInfo info; |
| info.mStatus = BufferInfo::OWNED_BY_US; |
| info.mFenceFd = fenceFd; |
| info.mIsReadFence = false; |
| info.mRenderInfo = NULL; |
| info.mData = new ABuffer(NULL /* data */, bufferSize /* capacity */); |
| info.mGraphicBuffer = graphicBuffer; |
| mBuffers[kPortIndexOutput].push(info); |
| |
| IOMX::buffer_id bufferId; |
| err = mOMX->useGraphicBuffer(mNode, kPortIndexOutput, graphicBuffer, |
| &bufferId); |
| if (err != 0) { |
| ALOGE("registering GraphicBuffer %u with OMX IL component failed: " |
| "%d", i, err); |
| break; |
| } |
| |
| mBuffers[kPortIndexOutput].editItemAt(i).mBufferID = bufferId; |
| |
| ALOGV("[%s] Registered graphic buffer with ID %u (pointer = %p)", |
| mComponentName.c_str(), |
| bufferId, graphicBuffer.get()); |
| } |
| |
| OMX_U32 cancelStart; |
| OMX_U32 cancelEnd; |
| |
| if (err != 0) { |
| // If an error occurred while dequeuing we need to cancel any buffers |
| // that were dequeued. |
| cancelStart = 0; |
| cancelEnd = mBuffers[kPortIndexOutput].size(); |
| } else { |
| // Return the required minimum undequeued buffers to the native window. |
| cancelStart = bufferCount - minUndequeuedBuffers; |
| cancelEnd = bufferCount; |
| } |
| |
| for (OMX_U32 i = cancelStart; i < cancelEnd; i++) { |
| BufferInfo *info = &mBuffers[kPortIndexOutput].editItemAt(i); |
| if (info->mStatus == BufferInfo::OWNED_BY_US) { |
| status_t error = cancelBufferToNativeWindow(info); |
| if (err == 0) { |
| err = error; |
| } |
| } |
| } |
| |
| if (!storingMetadataInDecodedBuffers()) { |
| static_cast<Surface*>(mNativeWindow.get()) |
| ->getIGraphicBufferProducer()->allowAllocation(false); |
| } |
| |
| return err; |
| } |
| |
| status_t ACodec::allocateOutputMetadataBuffers() { |
| OMX_U32 bufferCount, bufferSize, minUndequeuedBuffers; |
| status_t err = configureOutputBuffersFromNativeWindow( |
| &bufferCount, &bufferSize, &minUndequeuedBuffers); |
| if (err != 0) |
| return err; |
| mNumUndequeuedBuffers = minUndequeuedBuffers; |
| |
| ALOGV("[%s] Allocating %u meta buffers on output port", |
| mComponentName.c_str(), bufferCount); |
| |
| size_t bufSize = mOutputMetadataType == kMetadataBufferTypeANWBuffer ? |
| sizeof(struct VideoNativeMetadata) : sizeof(struct VideoGrallocMetadata); |
| size_t totalSize = bufferCount * bufSize; |
| mDealer[kPortIndexOutput] = new MemoryDealer(totalSize, "ACodec"); |
| |
| // Dequeue buffers and send them to OMX |
| for (OMX_U32 i = 0; i < bufferCount; i++) { |
| BufferInfo info; |
| info.mStatus = BufferInfo::OWNED_BY_NATIVE_WINDOW; |
| info.mFenceFd = -1; |
| info.mRenderInfo = NULL; |
| info.mGraphicBuffer = NULL; |
| info.mDequeuedAt = mDequeueCounter; |
| |
| sp<IMemory> mem = mDealer[kPortIndexOutput]->allocate(bufSize); |
| if (mem == NULL || mem->pointer() == NULL) { |
| return NO_MEMORY; |
| } |
| if (mOutputMetadataType == kMetadataBufferTypeANWBuffer) { |
| ((VideoNativeMetadata *)mem->pointer())->nFenceFd = -1; |
| } |
| info.mData = new ABuffer(mem->pointer(), mem->size()); |
| |
| // we use useBuffer for metadata regardless of quirks |
| err = mOMX->useBuffer( |
| mNode, kPortIndexOutput, mem, &info.mBufferID, mem->size()); |
| |
| mBuffers[kPortIndexOutput].push(info); |
| |
| ALOGV("[%s] allocated meta buffer with ID %u (pointer = %p)", |
| mComponentName.c_str(), info.mBufferID, mem->pointer()); |
| } |
| |
| if (mLegacyAdaptiveExperiment) { |
| // preallocate and preregister buffers |
| static_cast<Surface *>(mNativeWindow.get()) |
| ->getIGraphicBufferProducer()->allowAllocation(true); |
| |
| ALOGV("[%s] Allocating %u buffers from a native window of size %u on " |
| "output port", |
| mComponentName.c_str(), bufferCount, bufferSize); |
| |
| // Dequeue buffers then cancel them all |
| for (OMX_U32 i = 0; i < bufferCount; i++) { |
| BufferInfo *info = &mBuffers[kPortIndexOutput].editItemAt(i); |
| |
| ANativeWindowBuffer *buf; |
| int fenceFd; |
| err = mNativeWindow->dequeueBuffer(mNativeWindow.get(), &buf, &fenceFd); |
| if (err != 0) { |
| ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), -err); |
| break; |
| } |
| |
| sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(buf, false)); |
| mOMX->updateGraphicBufferInMeta( |
| mNode, kPortIndexOutput, graphicBuffer, info->mBufferID); |
| info->mStatus = BufferInfo::OWNED_BY_US; |
| info->setWriteFence(fenceFd, "allocateOutputMetadataBuffers for legacy"); |
| info->mGraphicBuffer = graphicBuffer; |
| } |
| |
| for (OMX_U32 i = 0; i < mBuffers[kPortIndexOutput].size(); i++) { |
| BufferInfo *info = &mBuffers[kPortIndexOutput].editItemAt(i); |
| if (info->mStatus == BufferInfo::OWNED_BY_US) { |
| status_t error = cancelBufferToNativeWindow(info); |
| if (err == OK) { |
| err = error; |
| } |
| } |
| } |
| |
| static_cast<Surface*>(mNativeWindow.get()) |
| ->getIGraphicBufferProducer()->allowAllocation(false); |
| } |
| |
| mMetadataBuffersToSubmit = bufferCount - minUndequeuedBuffers; |
| return err; |
| } |
| |
| status_t ACodec::submitOutputMetadataBuffer() { |
| CHECK(storingMetadataInDecodedBuffers()); |
| if (mMetadataBuffersToSubmit == 0) |
| return OK; |
| |
| BufferInfo *info = dequeueBufferFromNativeWindow(); |
| if (info == NULL) { |
| return ERROR_IO; |
| } |
| |
| ALOGV("[%s] submitting output meta buffer ID %u for graphic buffer %p", |
| mComponentName.c_str(), info->mBufferID, info->mGraphicBuffer.get()); |
| |
| --mMetadataBuffersToSubmit; |
| info->checkWriteFence("submitOutputMetadataBuffer"); |
| status_t err = mOMX->fillBuffer(mNode, info->mBufferID, info->mFenceFd); |
| info->mFenceFd = -1; |
| if (err == OK) { |
| info->mStatus = BufferInfo::OWNED_BY_COMPONENT; |
| } |
| |
| return err; |
| } |
| |
| status_t ACodec::waitForFence(int fd, const char *dbg ) { |
| status_t res = OK; |
| if (fd >= 0) { |
| sp<Fence> fence = new Fence(fd); |
| res = fence->wait(IOMX::kFenceTimeoutMs); |
| ALOGW_IF(res != OK, "FENCE TIMEOUT for %d in %s", fd, dbg); |
| } |
| return res; |
| } |
| |
| // static |
| const char *ACodec::_asString(BufferInfo::Status s) { |
| switch (s) { |
| case BufferInfo::OWNED_BY_US: return "OUR"; |
| case BufferInfo::OWNED_BY_COMPONENT: return "COMPONENT"; |
| case BufferInfo::OWNED_BY_UPSTREAM: return "UPSTREAM"; |
| case BufferInfo::OWNED_BY_DOWNSTREAM: return "DOWNSTREAM"; |
| case BufferInfo::OWNED_BY_NATIVE_WINDOW: return "SURFACE"; |
| case BufferInfo::UNRECOGNIZED: return "UNRECOGNIZED"; |
| default: return "?"; |
| } |
| } |
| |
| void ACodec::dumpBuffers(OMX_U32 portIndex) { |
| CHECK(portIndex == kPortIndexInput || portIndex == kPortIndexOutput); |
| ALOGI("[%s] %s port has %zu buffers:", mComponentName.c_str(), |
| portIndex == kPortIndexInput ? "input" : "output", mBuffers[portIndex].size()); |
| for (size_t i = 0; i < mBuffers[portIndex].size(); ++i) { |
| const BufferInfo &info = mBuffers[portIndex][i]; |
| ALOGI(" slot %2zu: #%8u %p/%p %s(%d) dequeued:%u", |
| i, info.mBufferID, info.mGraphicBuffer.get(), |
| info.mGraphicBuffer == NULL ? NULL : info.mGraphicBuffer->getNativeBuffer(), |
| _asString(info.mStatus), info.mStatus, info.mDequeuedAt); |
| } |
| } |
| |
| status_t ACodec::cancelBufferToNativeWindow(BufferInfo *info) { |
| CHECK_EQ((int)info->mStatus, (int)BufferInfo::OWNED_BY_US); |
| |
| ALOGV("[%s] Calling cancelBuffer on buffer %u", |
| mComponentName.c_str(), info->mBufferID); |
| |
| info->checkWriteFence("cancelBufferToNativeWindow"); |
| int err = mNativeWindow->cancelBuffer( |
| mNativeWindow.get(), info->mGraphicBuffer.get(), info->mFenceFd); |
| info->mFenceFd = -1; |
| |
| ALOGW_IF(err != 0, "[%s] can not return buffer %u to native window", |
| mComponentName.c_str(), info->mBufferID); |
| // change ownership even if cancelBuffer fails |
| info->mStatus = BufferInfo::OWNED_BY_NATIVE_WINDOW; |
| |
| return err; |
| } |
| |
| void ACodec::updateRenderInfoForDequeuedBuffer( |
| ANativeWindowBuffer *buf, int fenceFd, BufferInfo *info) { |
| |
| info->mRenderInfo = |
| mRenderTracker.updateInfoForDequeuedBuffer( |
| buf, fenceFd, info - &mBuffers[kPortIndexOutput][0]); |
| |
| // check for any fences already signaled |
| notifyOfRenderedFrames(false /* dropIncomplete */, info->mRenderInfo); |
| } |
| |
| void ACodec::onFrameRendered(int64_t mediaTimeUs, nsecs_t systemNano) { |
| if (mRenderTracker.onFrameRendered(mediaTimeUs, systemNano) != OK) { |
| mRenderTracker.dumpRenderQueue(); |
| } |
| } |
| |
| void ACodec::notifyOfRenderedFrames(bool dropIncomplete, FrameRenderTracker::Info *until) { |
| sp<AMessage> msg = mNotify->dup(); |
| msg->setInt32("what", CodecBase::kWhatOutputFramesRendered); |
| std::list<FrameRenderTracker::Info> done = |
| mRenderTracker.checkFencesAndGetRenderedFrames(until, dropIncomplete); |
| |
| // unlink untracked frames |
| for (std::list<FrameRenderTracker::Info>::const_iterator it = done.cbegin(); |
| it != done.cend(); ++it) { |
| ssize_t index = it->getIndex(); |
| if (index >= 0 && (size_t)index < mBuffers[kPortIndexOutput].size()) { |
| mBuffers[kPortIndexOutput].editItemAt(index).mRenderInfo = NULL; |
| } else if (index >= 0) { |
| // THIS SHOULD NEVER HAPPEN |
| ALOGE("invalid index %zd in %zu", index, mBuffers[kPortIndexOutput].size()); |
| } |
| } |
| |
| if (MediaCodec::CreateFramesRenderedMessage(done, msg)) { |
| msg->post(); |
| } |
| } |
| |
| ACodec::BufferInfo *ACodec::dequeueBufferFromNativeWindow() { |
| ANativeWindowBuffer *buf; |
| CHECK(mNativeWindow.get() != NULL); |
| |
| if (mTunneled) { |
| ALOGW("dequeueBufferFromNativeWindow() should not be called in tunnel" |
| " video playback mode mode!"); |
| return NULL; |
| } |
| |
| if (mFatalError) { |
| ALOGW("not dequeuing from native window due to fatal error"); |
| return NULL; |
| } |
| |
| int fenceFd = -1; |
| do { |
| status_t err = mNativeWindow->dequeueBuffer(mNativeWindow.get(), &buf, &fenceFd); |
| if (err != 0) { |
| ALOGE("dequeueBuffer failed: %s(%d).", asString(err), err); |
| return NULL; |
| } |
| |
| bool stale = false; |
| for (size_t i = mBuffers[kPortIndexOutput].size(); i-- > 0;) { |
| BufferInfo *info = &mBuffers[kPortIndexOutput].editItemAt(i); |
| |
| if (info->mGraphicBuffer != NULL && |
| info->mGraphicBuffer->handle == buf->handle) { |
| // Since consumers can attach buffers to BufferQueues, it is possible |
| // that a known yet stale buffer can return from a surface that we |
| // once used. We can simply ignore this as we have already dequeued |
| // this buffer properly. NOTE: this does not eliminate all cases, |
| // e.g. it is possible that we have queued the valid buffer to the |
| // NW, and a stale copy of the same buffer gets dequeued - which will |
| // be treated as the valid buffer by ACodec. |
| if (info->mStatus != BufferInfo::OWNED_BY_NATIVE_WINDOW) { |
| ALOGI("dequeued stale buffer %p. discarding", buf); |
| stale = true; |
| break; |
| } |
| |
| ALOGV("dequeued buffer %p", info->mGraphicBuffer->getNativeBuffer()); |
| info->mStatus = BufferInfo::OWNED_BY_US; |
| info->setWriteFence(fenceFd, "dequeueBufferFromNativeWindow"); |
| updateRenderInfoForDequeuedBuffer(buf, fenceFd, info); |
| return info; |
| } |
| } |
| |
| // It is also possible to receive a previously unregistered buffer |
| // in non-meta mode. These should be treated as stale buffers. The |
| // same is possible in meta mode, in which case, it will be treated |
| // as a normal buffer, which is not desirable. |
| // TODO: fix this. |
| if (!stale && (!storingMetadataInDecodedBuffers() || mLegacyAdaptiveExperiment)) { |
| ALOGI("dequeued unrecognized (stale) buffer %p. discarding", buf); |
| stale = true; |
| } |
| if (stale) { |
| // TODO: detach stale buffer, but there is no API yet to do it. |
| buf = NULL; |
| } |
| } while (buf == NULL); |
| |
| // get oldest undequeued buffer |
| BufferInfo *oldest = NULL; |
| for (size_t i = mBuffers[kPortIndexOutput].size(); i-- > 0;) { |
| BufferInfo *info = |
| &mBuffers[kPortIndexOutput].editItemAt(i); |
| if (info->mStatus == BufferInfo::OWNED_BY_NATIVE_WINDOW && |
| (oldest == NULL || |
| // avoid potential issues from counter rolling over |
| mDequeueCounter - info->mDequeuedAt > |
| mDequeueCounter - oldest->mDequeuedAt)) { |
| oldest = info; |
| } |
| } |
| |
| // it is impossible dequeue a buffer when there are no buffers with ANW |
| CHECK(oldest != NULL); |
| // it is impossible to dequeue an unknown buffer in non-meta mode, as the |
| // while loop above does not complete |
| CHECK(storingMetadataInDecodedBuffers()); |
| |
| // discard buffer in LRU info and replace with new buffer |
| oldest->mGraphicBuffer = new GraphicBuffer(buf, false); |
| oldest->mStatus = BufferInfo::OWNED_BY_US; |
| oldest->setWriteFence(fenceFd, "dequeueBufferFromNativeWindow for oldest"); |
| mRenderTracker.untrackFrame(oldest->mRenderInfo); |
| oldest->mRenderInfo = NULL; |
| |
| mOMX->updateGraphicBufferInMeta( |
| mNode, kPortIndexOutput, oldest->mGraphicBuffer, |
| oldest->mBufferID); |
| |
| if (mOutputMetadataType == kMetadataBufferTypeGrallocSource) { |
| VideoGrallocMetadata *grallocMeta = |
| reinterpret_cast<VideoGrallocMetadata *>(oldest->mData->base()); |
| ALOGV("replaced oldest buffer #%u with age %u (%p/%p stored in %p)", |
| (unsigned)(oldest - &mBuffers[kPortIndexOutput][0]), |
| mDequeueCounter - oldest->mDequeuedAt, |
| (void *)(uintptr_t)grallocMeta->pHandle, |
| oldest->mGraphicBuffer->handle, oldest->mData->base()); |
| } else if (mOutputMetadataType == kMetadataBufferTypeANWBuffer) { |
| VideoNativeMetadata *nativeMeta = |
| reinterpret_cast<VideoNativeMetadata *>(oldest->mData->base()); |
| ALOGV("replaced oldest buffer #%u with age %u (%p/%p stored in %p)", |
| (unsigned)(oldest - &mBuffers[kPortIndexOutput][0]), |
| mDequeueCounter - oldest->mDequeuedAt, |
| (void *)(uintptr_t)nativeMeta->pBuffer, |
| oldest->mGraphicBuffer->getNativeBuffer(), oldest->mData->base()); |
| } |
| |
| updateRenderInfoForDequeuedBuffer(buf, fenceFd, oldest); |
| return oldest; |
| } |
| |
| status_t ACodec::freeBuffersOnPort(OMX_U32 portIndex) { |
| status_t err = OK; |
| for (size_t i = mBuffers[portIndex].size(); i > 0;) { |
| i--; |
| status_t err2 = freeBuffer(portIndex, i); |
| if (err == OK) { |
| err = err2; |
| } |
| } |
| |
| // clear mDealer even on an error |
| mDealer[portIndex].clear(); |
| return err; |
| } |
| |
| status_t ACodec::freeOutputBuffersNotOwnedByComponent() { |
| status_t err = OK; |
| for (size_t i = mBuffers[kPortIndexOutput].size(); i > 0;) { |
| i--; |
| BufferInfo *info = |
| &mBuffers[kPortIndexOutput].editItemAt(i); |
| |
| // At this time some buffers may still be with the component |
| // or being drained. |
| if (info->mStatus != BufferInfo::OWNED_BY_COMPONENT && |
| info->mStatus != BufferInfo::OWNED_BY_DOWNSTREAM) { |
| status_t err2 = freeBuffer(kPortIndexOutput, i); |
| if (err == OK) { |
| err = err2; |
| } |
| } |
| } |
| |
| return err; |
| } |
| |
| status_t ACodec::freeBuffer(OMX_U32 portIndex, size_t i) { |
| BufferInfo *info = &mBuffers[portIndex].editItemAt(i); |
| status_t err = OK; |
| |
| // there should not be any fences in the metadata |
| MetadataBufferType type = |
| portIndex == kPortIndexOutput ? mOutputMetadataType : mInputMetadataType; |
| if (type == kMetadataBufferTypeANWBuffer && info->mData != NULL |
| && info->mData->size() >= sizeof(VideoNativeMetadata)) { |
| int fenceFd = ((VideoNativeMetadata *)info->mData->data())->nFenceFd; |
| if (fenceFd >= 0) { |
| ALOGW("unreleased fence (%d) in %s metadata buffer %zu", |
| fenceFd, portIndex == kPortIndexInput ? "input" : "output", i); |
| } |
| } |
| |
| switch (info->mStatus) { |
| case BufferInfo::OWNED_BY_US: |
| if (portIndex == kPortIndexOutput && mNativeWindow != NULL) { |
| (void)cancelBufferToNativeWindow(info); |
| } |
| // fall through |
| |
| case BufferInfo::OWNED_BY_NATIVE_WINDOW: |
| err = mOMX->freeBuffer(mNode, portIndex, info->mBufferID); |
| break; |
| |
| default: |
| ALOGE("trying to free buffer not owned by us or ANW (%d)", info->mStatus); |
| err = FAILED_TRANSACTION; |
| break; |
| } |
| |
| if (info->mFenceFd >= 0) { |
| ::close(info->mFenceFd); |
| } |
| |
| if (portIndex == kPortIndexOutput) { |
| mRenderTracker.untrackFrame(info->mRenderInfo, i); |
| info->mRenderInfo = NULL; |
| } |
| |
| // remove buffer even if mOMX->freeBuffer fails |
| mBuffers[portIndex].removeAt(i); |
| return err; |
| } |
| |
| ACodec::BufferInfo *ACodec::findBufferByID( |
| uint32_t portIndex, IOMX::buffer_id bufferID, ssize_t *index) { |
| for (size_t i = 0; i < mBuffers[portIndex].size(); ++i) { |
| BufferInfo *info = &mBuffers[portIndex].editItemAt(i); |
| |
| if (info->mBufferID == bufferID) { |
| if (index != NULL) { |
| *index = i; |
| } |
| return info; |
| } |
| } |
| |
| ALOGE("Could not find buffer with ID %u", bufferID); |
| return NULL; |
| } |
| |
| status_t ACodec::setComponentRole( |
| bool isEncoder, const char *mime) { |
| struct MimeToRole { |
| const char *mime; |
| const char *decoderRole; |
| const char *encoderRole; |
| }; |
| |
| static const MimeToRole kMimeToRole[] = { |
| { MEDIA_MIMETYPE_AUDIO_MPEG, |
| "audio_decoder.mp3", "audio_encoder.mp3" }, |
| { MEDIA_MIMETYPE_AUDIO_MPEG_LAYER_I, |
| "audio_decoder.mp1", "audio_encoder.mp1" }, |
| { MEDIA_MIMETYPE_AUDIO_MPEG_LAYER_II, |
| "audio_decoder.mp2", "audio_encoder.mp2" }, |
| { MEDIA_MIMETYPE_AUDIO_AMR_NB, |
| "audio_decoder.amrnb", "audio_encoder.amrnb" }, |
| { MEDIA_MIMETYPE_AUDIO_AMR_WB, |
| "audio_decoder.amrwb", "audio_encoder.amrwb" }, |
| { MEDIA_MIMETYPE_AUDIO_AAC, |
| "audio_decoder.aac", "audio_encoder.aac" }, |
| { MEDIA_MIMETYPE_AUDIO_VORBIS, |
| "audio_decoder.vorbis", "audio_encoder.vorbis" }, |
| { MEDIA_MIMETYPE_AUDIO_OPUS, |
| "audio_decoder.opus", "audio_encoder.opus" }, |
| { MEDIA_MIMETYPE_AUDIO_G711_MLAW, |
| "audio_decoder.g711mlaw", "audio_encoder.g711mlaw" }, |
| { MEDIA_MIMETYPE_AUDIO_G711_ALAW, |
| "audio_decoder.g711alaw", "audio_encoder.g711alaw" }, |
| { MEDIA_MIMETYPE_VIDEO_AVC, |
| "video_decoder.avc", "video_encoder.avc" }, |
| { MEDIA_MIMETYPE_VIDEO_HEVC, |
| "video_decoder.hevc", "video_encoder.hevc" }, |
| { MEDIA_MIMETYPE_VIDEO_MPEG4, |
| "video_decoder.mpeg4", "video_encoder.mpeg4" }, |
| { MEDIA_MIMETYPE_VIDEO_H263, |
| "video_decoder.h263", "video_encoder.h263" }, |
| { MEDIA_MIMETYPE_VIDEO_VP8, |
| "video_decoder.vp8", "video_encoder.vp8" }, |
| { MEDIA_MIMETYPE_VIDEO_VP9, |
| "video_decoder.vp9", "video_encoder.vp9" }, |
| { MEDIA_MIMETYPE_AUDIO_RAW, |
| "audio_decoder.raw", "audio_encoder.raw" }, |
| { MEDIA_MIMETYPE_AUDIO_FLAC, |
| "audio_decoder.flac", "audio_encoder.flac" }, |
| { MEDIA_MIMETYPE_AUDIO_MSGSM, |
| "audio_decoder.gsm", "audio_encoder.gsm" }, |
| { MEDIA_MIMETYPE_VIDEO_MPEG2, |
| "video_decoder.mpeg2", "video_encoder.mpeg2" }, |
| { MEDIA_MIMETYPE_AUDIO_AC3, |
| "audio_decoder.ac3", "audio_encoder.ac3" }, |
| { MEDIA_MIMETYPE_AUDIO_EAC3, |
| "audio_decoder.eac3", "audio_encoder.eac3" }, |
| }; |
| |
| static const size_t kNumMimeToRole = |
| sizeof(kMimeToRole) / sizeof(kMimeToRole[0]); |
| |
| size_t i; |
| for (i = 0; i < kNumMimeToRole; ++i) { |
| if (!strcasecmp(mime, kMimeToRole[i].mime)) { |
| break; |
| } |
| } |
| |
| if (i == kNumMimeToRole) { |
| return ERROR_UNSUPPORTED; |
| } |
| |
| const char *role = |
| isEncoder ? kMimeToRole[i].encoderRole |
| : kMimeToRole[i].decoderRole; |
| |
| if (role != NULL) { |
| OMX_PARAM_COMPONENTROLETYPE roleParams; |
| InitOMXParams(&roleParams); |
| |
| strncpy((char *)roleParams.cRole, |
| role, OMX_MAX_STRINGNAME_SIZE - 1); |
| |
| roleParams.cRole[OMX_MAX_STRINGNAME_SIZE - 1] = '\0'; |
| |
| status_t err = mOMX->setParameter( |
| mNode, OMX_IndexParamStandardComponentRole, |
| &roleParams, sizeof(roleParams)); |
| |
| if (err != OK) { |
| ALOGW("[%s] Failed to set standard component role '%s'.", |
| mComponentName.c_str(), role); |
| |
| return err; |
| } |
| } |
| |
| return OK; |
| } |
| |
| status_t ACodec::configureCodec( |
| const char *mime, const sp<AMessage> &msg) { |
| int32_t encoder; |
| if (!msg->findInt32("encoder", &encoder)) { |
| encoder = false; |
| } |
| |
| sp<AMessage> inputFormat = new AMessage(); |
| sp<AMessage> outputFormat = mNotify->dup(); // will use this for kWhatOutputFormatChanged |
| |
| mIsEncoder = encoder; |
| |
| mInputMetadataType = kMetadataBufferTypeInvalid; |
| mOutputMetadataType = kMetadataBufferTypeInvalid; |
| |
| status_t err = setComponentRole(encoder /* isEncoder */, mime); |
| |
| if (err != OK) { |
| return err; |
| } |
| |
| int32_t bitRate = 0; |
| // FLAC encoder doesn't need a bitrate, other encoders do |
| if (encoder && strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_FLAC) |
| && !msg->findInt32("bitrate", &bitRate)) { |
| return INVALID_OPERATION; |
| } |
| |
| int32_t storeMeta; |
| if (encoder |
| && msg->findInt32("store-metadata-in-buffers", &storeMeta) |
| && storeMeta != 0) { |
| err = mOMX->storeMetaDataInBuffers(mNode, kPortIndexInput, OMX_TRUE, &mInputMetadataType); |
| if (err != OK) { |
| ALOGE("[%s] storeMetaDataInBuffers (input) failed w/ err %d", |
| mComponentName.c_str(), err); |
| |
| return err; |
| } |
| // For this specific case we could be using camera source even if storeMetaDataInBuffers |
| // returns Gralloc source. Pretend that we are; this will force us to use nBufferSize. |
| if (mInputMetadataType == kMetadataBufferTypeGrallocSource) { |
| mInputMetadataType = kMetadataBufferTypeCameraSource; |
| } |
| |
| uint32_t usageBits; |
| if (mOMX->getParameter( |
| mNode, (OMX_INDEXTYPE)OMX_IndexParamConsumerUsageBits, |
| &usageBits, sizeof(usageBits)) == OK) { |
| inputFormat->setInt32( |
| "using-sw-read-often", !!(usageBits & GRALLOC_USAGE_SW_READ_OFTEN)); |
| } |
| } |
| |
| int32_t prependSPSPPS = 0; |
| if (encoder |
| && msg->findInt32("prepend-sps-pps-to-idr-frames", &prependSPSPPS) |
| && prependSPSPPS != 0) { |
| OMX_INDEXTYPE index; |
| err = mOMX->getExtensionIndex( |
| mNode, |
| "OMX.google.android.index.prependSPSPPSToIDRFrames", |
| &index); |
| |
| if (err == OK) { |
| PrependSPSPPSToIDRFramesParams params; |
| InitOMXParams(¶ms); |
| params.bEnable = OMX_TRUE; |
| |
| err = mOMX->setParameter( |
| mNode, index, ¶ms, sizeof(params)); |
| } |
| |
| if (err != OK) { |
| ALOGE("Encoder could not be configured to emit SPS/PPS before " |
| "IDR frames. (err %d)", err); |
| |
| return err; |
| } |
| } |
| |
| // Only enable metadata mode on encoder output if encoder can prepend |
| // sps/pps to idr frames, since in metadata mode the bitstream is in an |
| // opaque handle, to which we don't have access. |
| int32_t video = !strncasecmp(mime, "video/", 6); |
| mIsVideo = video; |
| if (encoder && video) { |
| OMX_BOOL enable = (OMX_BOOL) (prependSPSPPS |
| && msg->findInt32("store-metadata-in-buffers-output", &storeMeta) |
| && storeMeta != 0); |
| |
| err = mOMX->storeMetaDataInBuffers(mNode, kPortIndexOutput, enable, &mOutputMetadataType); |
| if (err != OK) { |
| ALOGE("[%s] storeMetaDataInBuffers (output) failed w/ err %d", |
| mComponentName.c_str(), err); |
| } |
| |
| if (!msg->findInt64( |
| "repeat-previous-frame-after", |
| &mRepeatFrameDelayUs)) { |
| mRepeatFrameDelayUs = -1ll; |
| } |
| |
| if (!msg->findInt64("max-pts-gap-to-encoder", &mMaxPtsGapUs)) { |
| mMaxPtsGapUs = -1ll; |
| } |
| |
| if (!msg->findFloat("max-fps-to-encoder", &mMaxFps)) { |
| mMaxFps = -1; |
| } |
| |
| if (!msg->findInt64("time-lapse", &mTimePerCaptureUs)) { |
| mTimePerCaptureUs = -1ll; |
| } |
| |
| if (!msg->findInt32( |
| "create-input-buffers-suspended", |
| (int32_t*)&mCreateInputBuffersSuspended)) { |
| mCreateInputBuffersSuspended = false; |
| } |
| } |
| |
| // NOTE: we only use native window for video decoders |
| sp<RefBase> obj; |
| bool haveNativeWindow = msg->findObject("native-window", &obj) |
| && obj != NULL && video && !encoder; |
| mLegacyAdaptiveExperiment = false; |
| if (video && !encoder) { |
| inputFormat->setInt32("adaptive-playback", false); |
| |
| int32_t usageProtected; |
| if (msg->findInt32("protected", &usageProtected) && usageProtected) { |
| if (!haveNativeWindow) { |
| ALOGE("protected output buffers must be sent to an ANativeWindow"); |
| return PERMISSION_DENIED; |
| } |
| mFlags |= kFlagIsGrallocUsageProtected; |
| mFlags |= kFlagPushBlankBuffersToNativeWindowOnShutdown; |
| } |
| } |
| if (haveNativeWindow) { |
| sp<ANativeWindow> nativeWindow = |
| static_cast<ANativeWindow *>(static_cast<Surface *>(obj.get())); |
| |
| // START of temporary support for automatic FRC - THIS WILL BE REMOVED |
| int32_t autoFrc; |
| if (msg->findInt32("auto-frc", &autoFrc)) { |
| bool enabled = autoFrc; |
| OMX_CONFIG_BOOLEANTYPE config; |
| InitOMXParams(&config); |
| config.bEnabled = (OMX_BOOL)enabled; |
| status_t temp = mOMX->setConfig( |
| mNode, (OMX_INDEXTYPE)OMX_IndexConfigAutoFramerateConversion, |
| &config, sizeof(config)); |
| if (temp == OK) { |
| outputFormat->setInt32("auto-frc", enabled); |
| } else if (enabled) { |
| ALOGI("codec does not support requested auto-frc (err %d)", temp); |
| } |
| } |
| // END of temporary support for automatic FRC |
| |
| int32_t tunneled; |
| if (msg->findInt32("feature-tunneled-playback", &tunneled) && |
| tunneled != 0) { |
| ALOGI("Configuring TUNNELED video playback."); |
| mTunneled = true; |
| |
| int32_t audioHwSync = 0; |
| if (!msg->findInt32("audio-hw-sync", &audioHwSync)) { |
| ALOGW("No Audio HW Sync provided for video tunnel"); |
| } |
| err = configureTunneledVideoPlayback(audioHwSync, nativeWindow); |
| if (err != OK) { |
| ALOGE("configureTunneledVideoPlayback(%d,%p) failed!", |
| audioHwSync, nativeWindow.get()); |
| return err; |
| } |
| |
| int32_t maxWidth = 0, maxHeight = 0; |
| if (msg->findInt32("max-width", &maxWidth) && |
| msg->findInt32("max-height", &maxHeight)) { |
| |
| err = mOMX->prepareForAdaptivePlayback( |
| mNode, kPortIndexOutput, OMX_TRUE, maxWidth, maxHeight); |
| if (err != OK) { |
| ALOGW("[%s] prepareForAdaptivePlayback failed w/ err %d", |
| mComponentName.c_str(), err); |
| // allow failure |
| err = OK; |
| } else { |
| inputFormat->setInt32("max-width", maxWidth); |
| inputFormat->setInt32("max-height", maxHeight); |
| inputFormat->setInt32("adaptive-playback", true); |
| } |
| } |
| } else { |
| ALOGV("Configuring CPU controlled video playback."); |
| mTunneled = false; |
| |
| // Explicity reset the sideband handle of the window for |
| // non-tunneled video in case the window was previously used |
| // for a tunneled video playback. |
| err = native_window_set_sideband_stream(nativeWindow.get(), NULL); |
| if (err != OK) { |
| ALOGE("set_sideband_stream(NULL) failed! (err %d).", err); |
| return err; |
| } |
| |
| // Always try to enable dynamic output buffers on native surface |
| err = mOMX->storeMetaDataInBuffers( |
| mNode, kPortIndexOutput, OMX_TRUE, &mOutputMetadataType); |
| if (err != OK) { |
| ALOGE("[%s] storeMetaDataInBuffers failed w/ err %d", |
| mComponentName.c_str(), err); |
| |
| // if adaptive playback has been requested, try JB fallback |
| // NOTE: THIS FALLBACK MECHANISM WILL BE REMOVED DUE TO ITS |
| // LARGE MEMORY REQUIREMENT |
| |
| // we will not do adaptive playback on software accessed |
| // surfaces as they never had to respond to changes in the |
| // crop window, and we don't trust that they will be able to. |
| int usageBits = 0; |
| bool canDoAdaptivePlayback; |
| |
| if (nativeWindow->query( |
| nativeWindow.get(), |
| NATIVE_WINDOW_CONSUMER_USAGE_BITS, |
| &usageBits) != OK) { |
| canDoAdaptivePlayback = false; |
| } else { |
| canDoAdaptivePlayback = |
| (usageBits & |
| (GRALLOC_USAGE_SW_READ_MASK | |
| GRALLOC_USAGE_SW_WRITE_MASK)) == 0; |
| } |
| |
| int32_t maxWidth = 0, maxHeight = 0; |
| if (canDoAdaptivePlayback && |
| msg->findInt32("max-width", &maxWidth) && |
| msg->findInt32("max-height", &maxHeight)) { |
| ALOGV("[%s] prepareForAdaptivePlayback(%dx%d)", |
| mComponentName.c_str(), maxWidth, maxHeight); |
| |
| err = mOMX->prepareForAdaptivePlayback( |
| mNode, kPortIndexOutput, OMX_TRUE, maxWidth, |
| maxHeight); |
| ALOGW_IF(err != OK, |
| "[%s] prepareForAdaptivePlayback failed w/ err %d", |
| mComponentName.c_str(), err); |
| |
| if (err == OK) { |
| inputFormat->setInt32("max-width", maxWidth); |
| inputFormat->setInt32("max-height", maxHeight); |
| inputFormat->setInt32("adaptive-playback", true); |
| } |
| } |
| // allow failure |
| err = OK; |
| } else { |
| ALOGV("[%s] storeMetaDataInBuffers succeeded", |
| mComponentName.c_str()); |
| CHECK(storingMetadataInDecodedBuffers()); |
| mLegacyAdaptiveExperiment = ADebug::isExperimentEnabled( |
| "legacy-adaptive", !msg->contains("no-experiments")); |
| |
| inputFormat->setInt32("adaptive-playback", true); |
| } |
| |
| int32_t push; |
| if (msg->findInt32("push-blank-buffers-on-shutdown", &push) |
| && push != 0) { |
| mFlags |= kFlagPushBlankBuffersToNativeWindowOnShutdown; |
| } |
| } |
| |
| int32_t rotationDegrees; |
| if (msg->findInt32("rotation-degrees", &rotationDegrees)) { |
| mRotationDegrees = rotationDegrees; |
| } else { |
| mRotationDegrees = 0; |
| } |
| } |
| |
| if (video) { |
| // determine need for software renderer |
| bool usingSwRenderer = false; |
| if (haveNativeWindow && mComponentName.startsWith("OMX.google.")) { |
| usingSwRenderer = true; |
| haveNativeWindow = false; |
| } |
| |
| if (encoder) { |
| err = setupVideoEncoder(mime, msg); |
| } else { |
| err = setupVideoDecoder(mime, msg, haveNativeWindow); |
| } |
| |
| if (err != OK) { |
| return err; |
| } |
| |
| if (haveNativeWindow) { |
| mNativeWindow = static_cast<Surface *>(obj.get()); |
| } |
| |
| // initialize native window now to get actual output format |
| // TODO: this is needed for some encoders even though they don't use native window |
| err = initNativeWindow(); |
| if (err != OK) { |
| return err; |
| } |
| |
| // fallback for devices that do not handle flex-YUV for native buffers |
| if (haveNativeWindow) { |
| int32_t requestedColorFormat = OMX_COLOR_FormatUnused; |
| if (msg->findInt32("color-format", &requestedColorFormat) && |
| requestedColorFormat == OMX_COLOR_FormatYUV420Flexible) { |
| status_t err = getPortFormat(kPortIndexOutput, outputFormat); |
| if (err != OK) { |
| return err; |
| } |
| int32_t colorFormat = OMX_COLOR_FormatUnused; |
| OMX_U32 flexibleEquivalent = OMX_COLOR_FormatUnused; |
| if (!outputFormat->findInt32("color-format", &colorFormat)) { |
| ALOGE("ouptut port did not have a color format (wrong domain?)"); |
| return BAD_VALUE; |
| } |
| ALOGD("[%s] Requested output format %#x and got %#x.", |
| mComponentName.c_str(), requestedColorFormat, colorFormat); |
| if (!isFlexibleColorFormat( |
| mOMX, mNode, colorFormat, haveNativeWindow, &flexibleEquivalent) |
| || flexibleEquivalent != (OMX_U32)requestedColorFormat) { |
| // device did not handle flex-YUV request for native window, fall back |
| // to SW renderer |
| ALOGI("[%s] Falling back to software renderer", mComponentName.c_str()); |
| mNativeWindow.clear(); |
| mNativeWindowUsageBits = 0; |
| haveNativeWindow = false; |
| usingSwRenderer = true; |
| if (storingMetadataInDecodedBuffers()) { |
| err = mOMX->storeMetaDataInBuffers( |
| mNode, kPortIndexOutput, OMX_FALSE, &mOutputMetadataType); |
| mOutputMetadataType = kMetadataBufferTypeInvalid; // just in case |
| // TODO: implement adaptive-playback support for bytebuffer mode. |
| // This is done by SW codecs, but most HW codecs don't support it. |
| inputFormat->setInt32("adaptive-playback", false); |
| } |
| if (err == OK) { |
| err = mOMX->enableGraphicBuffers(mNode, kPortIndexOutput, OMX_FALSE); |
| } |
| if (mFlags & kFlagIsGrallocUsageProtected) { |
| // fallback is not supported for protected playback |
| err = PERMISSION_DENIED; |
| } else if (err == OK) { |
| err = setupVideoDecoder(mime, msg, false); |
| } |
| } |
| } |
| } |
| |
| if (usingSwRenderer) { |
| outputFormat->setInt32("using-sw-renderer", 1); |
| } |
| } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_MPEG)) { |
| int32_t numChannels, sampleRate; |
| if (!msg->findInt32("channel-count", &numChannels) |
| || !msg->findInt32("sample-rate", &sampleRate)) { |
| // Since we did not always check for these, leave them optional |
| // and have the decoder figure it all out. |
| err = OK; |
| } else { |
| err = setupRawAudioFormat( |
| encoder ? kPortIndexInput : kPortIndexOutput, |
| sampleRate, |
| numChannels); |
| } |
| } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AAC)) { |
| int32_t numChannels, sampleRate; |
| if (!msg->findInt32("channel-count", &numChannels) |
| || !msg->findInt32("sample-rate", &sampleRate)) { |
| err = INVALID_OPERATION; |
| } else { |
| int32_t isADTS, aacProfile; |
| int32_t sbrMode; |
| int32_t maxOutputChannelCount; |
| int32_t pcmLimiterEnable; |
| drcParams_t drc; |
| if (!msg->findInt32("is-adts", &isADTS)) { |
| isADTS = 0; |
| } |
| if (!msg->findInt32("aac-profile", &aacProfile)) { |
| aacProfile = OMX_AUDIO_AACObjectNull; |
| } |
| if (!msg->findInt32("aac-sbr-mode", &sbrMode)) { |
| sbrMode = -1; |
| } |
| |
| if (!msg->findInt32("aac-max-output-channel_count", &maxOutputChannelCount)) { |
| maxOutputChannelCount = -1; |
| } |
| if (!msg->findInt32("aac-pcm-limiter-enable", &pcmLimiterEnable)) { |
| // value is unknown |
| pcmLimiterEnable = -1; |
| } |
| if (!msg->findInt32("aac-encoded-target-level", &drc.encodedTargetLevel)) { |
| // value is unknown |
| drc.encodedTargetLevel = -1; |
| } |
| if (!msg->findInt32("aac-drc-cut-level", &drc.drcCut)) { |
| // value is unknown |
| drc.drcCut = -1; |
| } |
| if (!msg->findInt32("aac-drc-boost-level", &drc.drcBoost)) { |
| // value is unknown |
| drc.drcBoost = -1; |
| } |
| if (!msg->findInt32("aac-drc-heavy-compression", &drc.heavyCompression)) { |
| // value is unknown |
| drc.heavyCompression = -1; |
| } |
| if (!msg->findInt32("aac-target-ref-level", &drc.targetRefLevel)) { |
| // value is unknown |
| drc.targetRefLevel = -1; |
| } |
| |
| err = setupAACCodec( |
| encoder, numChannels, sampleRate, bitRate, aacProfile, |
| isADTS != 0, sbrMode, maxOutputChannelCount, drc, |
| pcmLimiterEnable); |
| } |
| } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AMR_NB)) { |
| err = setupAMRCodec(encoder, false /* isWAMR */, bitRate); |
| } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AMR_WB)) { |
| err = setupAMRCodec(encoder, true /* isWAMR */, bitRate); |
| } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_G711_ALAW) |
| || !strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_G711_MLAW)) { |
| // These are PCM-like formats with a fixed sample rate but |
| // a variable number of channels. |
| |
| int32_t numChannels; |
| if (!msg->findInt32("channel-count", &numChannels)) { |
| err = INVALID_OPERATION; |
| } else { |
| int32_t sampleRate; |
| if (!msg->findInt32("sample-rate", &sampleRate)) { |
| sampleRate = 8000; |
| } |
| err = setupG711Codec(encoder, sampleRate, numChannels); |
| } |
| } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_FLAC)) { |
| int32_t numChannels = 0, sampleRate = 0, compressionLevel = -1; |
| if (encoder && |
| (!msg->findInt32("channel-count", &numChannels) |
| || !msg->findInt32("sample-rate", &sampleRate))) { |
| ALOGE("missing channel count or sample rate for FLAC encoder"); |
| err = INVALID_OPERATION; |
| } else { |
| if (encoder) { |
| if (!msg->findInt32( |
| "complexity", &compressionLevel) && |
| !msg->findInt32( |
| "flac-compression-level", &compressionLevel)) { |
| compressionLevel = 5; // default FLAC compression level |
| } else if (compressionLevel < 0) { |
| ALOGW("compression level %d outside [0..8] range, " |
| "using 0", |
| compressionLevel); |
| compressionLevel = 0; |
| } else if (compressionLevel > 8) { |
| ALOGW("compression level %d outside [0..8] range, " |
| "using 8", |
| compressionLevel); |
| compressionLevel = 8; |
| } |
| } |
| err = setupFlacCodec( |
| encoder, numChannels, sampleRate, compressionLevel); |
| } |
| } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_RAW)) { |
| int32_t numChannels, sampleRate; |
| if (encoder |
| || !msg->findInt32("channel-count", &numChannels) |
| || !msg->findInt32("sample-rate", &sampleRate)) { |
| err = INVALID_OPERATION; |
| } else { |
| err = setupRawAudioFormat(kPortIndexInput, sampleRate, numChannels); |
| } |
| } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AC3)) { |
| int32_t numChannels; |
| int32_t sampleRate; |
| if (!msg->findInt32("channel-count", &numChannels) |
| || !msg->findInt32("sample-rate", &sampleRate)) { |
| err = INVALID_OPERATION; |
| } else { |
| err = setupAC3Codec(encoder, numChannels, sampleRate); |
| } |
| } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_EAC3)) { |
| int32_t numChannels; |
| int32_t sampleRate; |
| if (!msg->findInt32("channel-count", &numChannels) |
| || !msg->findInt32("sample-rate", &sampleRate)) { |
| err = INVALID_OPERATION; |
| } else { |
| err = setupEAC3Codec(encoder, numChannels, sampleRate); |
| } |
| } |
| |
| if (err != OK) { |
| return err; |
| } |
| |
| if (!msg->findInt32("encoder-delay", &mEncoderDelay)) { |
| mEncoderDelay = 0; |
| } |
| |
| if (!msg->findInt32("encoder-padding", &mEncoderPadding)) { |
| mEncoderPadding = 0; |
| } |
| |
| if (msg->findInt32("channel-mask", &mChannelMask)) { |
| mChannelMaskPresent = true; |
| } else { |
| mChannelMaskPresent = false; |
| } |
| |
| int32_t maxInputSize; |
| if (msg->findInt32("max-input-size", &maxInputSize)) { |
| err = setMinBufferSize(kPortIndexInput, (size_t)maxInputSize); |
| } else if (!strcmp("OMX.Nvidia.aac.decoder", mComponentName.c_str())) { |
| err = setMinBufferSize(kPortIndexInput, 8192); // XXX |
| } |
| |
| int32_t priority; |
| if (msg->findInt32("priority", &priority)) { |
| err = setPriority(priority); |
| } |
| |
| int32_t rateInt = -1; |
| float rateFloat = -1; |
| if (!msg->findFloat("operating-rate", &rateFloat)) { |
| msg->findInt32("operating-rate", &rateInt); |
| rateFloat = (float)rateInt; // 16MHz (FLINTMAX) is OK for upper bound. |
| } |
| if (rateFloat > 0) { |
| err = setOperatingRate(rateFloat, video); |
| } |
| |
| mBaseOutputFormat = outputFormat; |
| |
| err = getPortFormat(kPortIndexInput, inputFormat); |
| if (err == OK) { |
| err = getPortFormat(kPortIndexOutput, outputFormat); |
| if (err == OK) { |
| mInputFormat = inputFormat; |
| mOutputFormat = outputFormat; |
| } |
| } |
| return err; |
| } |
| |
| status_t ACodec::setPriority(int32_t priority) { |
| if (priority < 0) { |
| return BAD_VALUE; |
| } |
| OMX_PARAM_U32TYPE config; |
| InitOMXParams(&config); |
| config.nU32 = (OMX_U32)priority; |
| status_t temp = mOMX->setConfig( |
| mNode, (OMX_INDEXTYPE)OMX_IndexConfigPriority, |
| &config, sizeof(config)); |
| if (temp != OK) { |
| ALOGI("codec does not support config priority (err %d)", temp); |
| } |
| return OK; |
| } |
| |
| status_t ACodec::setOperatingRate(float rateFloat, bool isVideo) { |
| if (rateFloat < 0) { |
| return BAD_VALUE; |
| } |
| OMX_U32 rate; |
| if (isVideo) { |
| if (rateFloat > 65535) { |
| return BAD_VALUE; |
| } |
| rate = (OMX_U32)(rateFloat * 65536.0f + 0.5f); |
| } else { |
| if (rateFloat > UINT_MAX) { |
| return BAD_VALUE; |
| } |
| rate = (OMX_U32)(rateFloat); |
| } |
| OMX_PARAM_U32TYPE config; |
| InitOMXParams(&config); |
| config.nU32 = rate; |
| status_t err = mOMX->setConfig( |
| mNode, (OMX_INDEXTYPE)OMX_IndexConfigOperatingRate, |
| &config, sizeof(config)); |
| if (err != OK) { |
| ALOGI("codec does not support config operating rate (err %d)", err); |
| } |
| return OK; |
| } |
| |
| status_t ACodec::setMinBufferSize(OMX_U32 portIndex, size_t size) { |
| OMX_PARAM_PORTDEFINITIONTYPE def; |
| InitOMXParams(&def); |
| def.nPortIndex = portIndex; |
| |
| status_t err = mOMX->getParameter( |
| mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); |
| |
| if (err != OK) { |
| return err; |
| } |
| |
| if (def.nBufferSize >= size) { |
| return OK; |
| } |
| |
| def.nBufferSize = size; |
| |
| err = mOMX->setParameter( |
| mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); |
| |
| if (err != OK) { |
| return err; |
| } |
| |
| err = mOMX->getParameter( |
| mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); |
| |
| if (err != OK) { |
| return err; |
| } |
| |
| if (def.nBufferSize < size) { |
| ALOGE("failed to set min buffer size to %zu (is still %u)", size, def.nBufferSize); |
| return FAILED_TRANSACTION; |
| } |
| |
| return OK; |
| } |
| |
| status_t ACodec::selectAudioPortFormat( |
| OMX_U32 portIndex, OMX_AUDIO_CODINGTYPE desiredFormat) { |
| OMX_AUDIO_PARAM_PORTFORMATTYPE format; |
| InitOMXParams(&format); |
| |
| format.nPortIndex = portIndex; |
| for (OMX_U32 index = 0;; ++index) { |
| format.nIndex = index; |
| |
| status_t err = mOMX->getParameter( |
| mNode, OMX_IndexParamAudioPortFormat, |
| &format, sizeof(format)); |
| |
| if (err != OK) { |
| return err; |
| } |
| |
| if (format.eEncoding == desiredFormat) { |
| break; |
| } |
| } |
| |
| return mOMX->setParameter( |
| mNode, OMX_IndexParamAudioPortFormat, &format, sizeof(format)); |
| } |
| |
| status_t ACodec::setupAACCodec( |
| bool encoder, int32_t numChannels, int32_t sampleRate, |
| int32_t bitRate, int32_t aacProfile, bool isADTS, int32_t sbrMode, |
| int32_t maxOutputChannelCount, const drcParams_t& drc, |
| int32_t pcmLimiterEnable) { |
| if (encoder && isADTS) { |
| return -EINVAL; |
| } |
| |
| status_t err = setupRawAudioFormat( |
| encoder ? kPortIndexInput : kPortIndexOutput, |
| sampleRate, |
| numChannels); |
| |
| if (err != OK) { |
| return err; |
| } |
| |
| if (encoder) { |
| err = selectAudioPortFormat(kPortIndexOutput, OMX_AUDIO_CodingAAC); |
| |
| if (err != OK) { |
| return err; |
| } |
| |
| OMX_PARAM_PORTDEFINITIONTYPE def; |
| InitOMXParams(&def); |
| def.nPortIndex = kPortIndexOutput; |
| |
| err = mOMX->getParameter( |
| mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); |
| |
| if (err != OK) { |
| return err; |
| } |
| |
| def.format.audio.bFlagErrorConcealment = OMX_TRUE; |
| def.format.audio.eEncoding = OMX_AUDIO_CodingAAC; |
| |
| err = mOMX->setParameter( |
| mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); |
| |
| if (err != OK) { |
| return err; |
| } |
| |
| OMX_AUDIO_PARAM_AACPROFILETYPE profile; |
| InitOMXParams(&profile); |
| profile.nPortIndex = kPortIndexOutput; |
| |
| err = mOMX->getParameter( |
| mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile)); |
| |
| if (err != OK) { |
| return err; |
| } |
| |
| profile.nChannels = numChannels; |
| |
| profile.eChannelMode = |
| (numChannels == 1) |
| ? OMX_AUDIO_ChannelModeMono: OMX_AUDIO_ChannelModeStereo; |
| |
| profile.nSampleRate = sampleRate; |
| profile.nBitRate = bitRate; |
| profile.nAudioBandWidth = 0; |
| profile.nFrameLength = 0; |
| profile.nAACtools = OMX_AUDIO_AACToolAll; |
| profile.nAACERtools = OMX_AUDIO_AACERNone; |
| profile.eAACProfile = (OMX_AUDIO_AACPROFILETYPE) aacProfile; |
| profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4FF; |
| switch (sbrMode) { |
| case 0: |
| // disable sbr |
| profile.nAACtools &= ~OMX_AUDIO_AACToolAndroidSSBR; |
| profile.nAACtools &= ~OMX_AUDIO_AACToolAndroidDSBR; |
| break; |
| case 1: |
| // enable single-rate sbr |
| profile.nAACtools |= OMX_AUDIO_AACToolAndroidSSBR; |
| profile.nAACtools &= ~OMX_AUDIO_AACToolAndroidDSBR; |
| break; |
| case 2: |
| // enable dual-rate sbr |
| profile.nAACtools &= ~OMX_AUDIO_AACToolAndroidSSBR; |
| profile.nAACtools |= OMX_AUDIO_AACToolAndroidDSBR; |
| break; |
| case -1: |
| // enable both modes -> the codec will decide which mode should be used |
| profile.nAACtools |= OMX_AUDIO_AACToolAndroidSSBR; |
| profile.nAACtools |= OMX_AUDIO_AACToolAndroidDSBR; |
| break; |
| default: |
| // unsupported sbr mode |
| return BAD_VALUE; |
| } |
| |
| |
| err = mOMX->setParameter( |
| mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile)); |
| |
| if (err != OK) { |
| return err; |
| } |
| |
| return err; |
| } |
| |
| OMX_AUDIO_PARAM_AACPROFILETYPE profile; |
| InitOMXParams(&profile); |
| profile.nPortIndex = kPortIndexInput; |
| |
| err = mOMX->getParameter( |
| mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile)); |
| |
| if (err != OK) { |
| return err; |
| } |
| |
| profile.nChannels = numChannels; |
| profile.nSampleRate = sampleRate; |
| |
| profile.eAACStreamFormat = |
| isADTS |
| ? OMX_AUDIO_AACStreamFormatMP4ADTS |
| : OMX_AUDIO_AACStreamFormatMP4FF; |
| |
| OMX_AUDIO_PARAM_ANDROID_AACPRESENTATIONTYPE presentation; |
| InitOMXParams(&presentation); |
| presentation.nMaxOutputChannels = maxOutputChannelCount; |
| presentation.nDrcCut = drc.drcCut; |
| presentation.nDrcBoost = drc.drcBoost; |
| presentation.nHeavyCompression = drc.heavyCompression; |
| presentation.nTargetReferenceLevel = drc.targetRefLevel; |
| presentation.nEncodedTargetLevel = drc.encodedTargetLevel; |
| presentation.nPCMLimiterEnable = pcmLimiterEnable; |
| |
| status_t res = mOMX->setParameter(mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile)); |
| if (res == OK) { |
| // optional parameters, will not cause configuration failure |
| mOMX->setParameter(mNode, (OMX_INDEXTYPE)OMX_IndexParamAudioAndroidAacPresentation, |
| &presentation, sizeof(presentation)); |
| } else { |
| ALOGW("did not set AudioAndroidAacPresentation due to error %d when setting AudioAac", res); |
| } |
| return res; |
| } |
| |
| status_t ACodec::setupAC3Codec( |
| bool encoder, int32_t numChannels, int32_t sampleRate) { |
| status_t err = setupRawAudioFormat( |
| encoder ? kPortIndexInput : kPortIndexOutput, sampleRate, numChannels); |
| |
| if (err != OK) { |
| return err; |
| } |
| |
| if (encoder) { |
| ALOGW("AC3 encoding is not supported."); |
| return INVALID_OPERATION; |
| } |
| |
| OMX_AUDIO_PARAM_ANDROID_AC3TYPE def; |
| InitOMXParams(&def); |
| def.nPortIndex = kPortIndexInput; |
| |
| err = mOMX->getParameter( |
| mNode, |
| (OMX_INDEXTYPE)OMX_IndexParamAudioAndroidAc3, |
| &def, |
| sizeof(def)); |
| |
| if (err != OK) { |
| return err; |
| } |
| |
| def.nChannels = numChannels; |
| def.nSampleRate = sampleRate; |
| |
| return mOMX->setParameter( |
| mNode, |
| (OMX_INDEXTYPE)OMX_IndexParamAudioAndroidAc3, |
| &def, |
| sizeof(def)); |
| } |
| |
| status_t ACodec::setupEAC3Codec( |
| bool encoder, int32_t numChannels, int32_t sampleRate) { |
| status_t err = setupRawAudioFormat( |
| encoder ? kPortIndexInput : kPortIndexOutput, sampleRate, numChannels); |
| |
| if (err != OK) { |
| return err; |
| } |
| |
| if (encoder) { |
| ALOGW("EAC3 encoding is not supported."); |
| return INVALID_OPERATION; |
| } |
| |
| OMX_AUDIO_PARAM_ANDROID_EAC3TYPE def; |
| InitOMXParams(&def); |
| def.nPortIndex = kPortIndexInput; |
| |
| err = mOMX->getParameter( |
| mNode, |
| (OMX_INDEXTYPE)OMX_IndexParamAudioAndroidEac3, |
| &def, |
| sizeof(def)); |
| |
| if (err != OK) { |
| return err; |
| } |
| |
| def.nChannels = numChannels; |
| def.nSampleRate = sampleRate; |
| |
| return mOMX->setParameter( |
| mNode, |
| (OMX_INDEXTYPE)OMX_IndexParamAudioAndroidEac3, |
| &def, |
| sizeof(def)); |
| } |
| |
| static OMX_AUDIO_AMRBANDMODETYPE pickModeFromBitRate( |
| bool isAMRWB, int32_t bps) { |
| if (isAMRWB) { |
| if (bps <= 6600) { |
| return OMX_AUDIO_AMRBandModeWB0; |
| } else if (bps <= 8850) { |
| return OMX_AUDIO_AMRBandModeWB1; |
| } else if (bps <= 12650) { |
| return OMX_AUDIO_AMRBandModeWB2; |
| } else if (bps <= 14250) { |
| return OMX_AUDIO_AMRBandModeWB3; |
| } else if (bps <= 15850) { |
| return OMX_AUDIO_AMRBandModeWB4; |
| } else if (bps <= 18250) { |
| return OMX_AUDIO_AMRBandModeWB5; |
| } else if (bps <= 19850) { |
| return OMX_AUDIO_AMRBandModeWB6; |
| } else if (bps <= 23050) { |
| return OMX_AUDIO_AMRBandModeWB7; |
| } |
| |
| // 23850 bps |
| return OMX_AUDIO_AMRBandModeWB8; |
| } else { // AMRNB |
| if (bps <= 4750) { |
| return OMX_AUDIO_AMRBandModeNB0; |
| } else if (bps <= 5150) { |
| return OMX_AUDIO_AMRBandModeNB1; |
| } else if (bps <= 5900) { |
| return OMX_AUDIO_AMRBandModeNB2; |
| } else if (bps <= 6700) { |
| return OMX_AUDIO_AMRBandModeNB3; |
| } else if (bps <= 7400) { |
| return OMX_AUDIO_AMRBandModeNB4; |
| } else if (bps <= 7950) { |
| return OMX_AUDIO_AMRBandModeNB5; |
| } else if (bps <= 10200) { |
| return OMX_AUDIO_AMRBandModeNB6; |
| } |
| |
| // 12200 bps |
| return OMX_AUDIO_AMRBandModeNB7; |
| } |
| } |
| |
| status_t ACodec::setupAMRCodec(bool encoder, bool isWAMR, int32_t bitrate) { |
| OMX_AUDIO_PARAM_AMRTYPE def; |
| InitOMXParams(&def); |
| def.nPortIndex = encoder ? kPortIndexOutput : kPortIndexInput; |
| |
| status_t err = |
| mOMX->getParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def)); |
| |
| if (err != OK) { |
| return err; |
| } |
| |
| def.eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatFSF; |
| def.eAMRBandMode = pickModeFromBitRate(isWAMR, bitrate); |
| |
| err = mOMX->setParameter( |
| mNode, OMX_IndexParamAudioAmr, &def, sizeof(def)); |
| |
| if (err != OK) { |
| return err; |
| } |
| |
| return setupRawAudioFormat( |
| encoder ? kPortIndexInput : kPortIndexOutput, |
| isWAMR ? 16000 : 8000 /* sampleRate */, |
| 1 /* numChannels */); |
| } |
| |
| status_t ACodec::setupG711Codec(bool encoder, int32_t sampleRate, int32_t numChannels) { |
| if (encoder) { |
| return INVALID_OPERATION; |
| } |
| |
| return setupRawAudioFormat( |
| kPortIndexInput, sampleRate, numChannels); |
| } |
| |
| status_t ACodec::setupFlacCodec( |
| bool encoder, int32_t numChannels, int32_t sampleRate, int32_t compressionLevel) { |
| |
| if (encoder) { |
| OMX_AUDIO_PARAM_FLACTYPE def; |
| InitOMXParams(&def); |
| def.nPortIndex = kPortIndexOutput; |
| |
| // configure compression level |
| status_t err = mOMX->getParameter(mNode, OMX_IndexParamAudioFlac, &def, sizeof(def)); |
| if (err != OK) { |
| ALOGE("setupFlacCodec(): Error %d getting OMX_IndexParamAudioFlac parameter", err); |
| return err; |
| } |
| def.nCompressionLevel = compressionLevel; |
| err = mOMX->setParameter(mNode, OMX_IndexParamAudioFlac, &def, sizeof(def)); |
| if (err != OK) { |
| ALOGE("setupFlacCodec(): Error %d setting OMX_IndexParamAudioFlac parameter", err); |
| return err; |
| } |
| } |
| |
| return setupRawAudioFormat( |
| encoder ? kPortIndexInput : kPortIndexOutput, |
| sampleRate, |
| numChannels); |
| } |
| |
| status_t ACodec::setupRawAudioFormat( |
| OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels) { |
| OMX_PARAM_PORTDEFINITIONTYPE def; |
| InitOMXParams(&def); |
| def.nPortIndex = portIndex; |
| |
| status_t err = mOMX->getParameter( |
| mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); |
| |
| if (err != OK) { |
| return err; |
| } |
| |
| def.format.audio.eEncoding = OMX_AUDIO_CodingPCM; |
| |
| err = mOMX->setParameter( |
| mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); |
| |
| if (err != OK) { |
| return err; |
| } |
| |
| OMX_AUDIO_PARAM_PCMMODETYPE pcmParams; |
| InitOMXParams(&pcmParams); |
| pcmParams.nPortIndex = portIndex; |
| |
| err = mOMX->getParameter( |
| mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams)); |
| |
| if (err != OK) { |
| return err; |
| } |
| |
| pcmParams.nChannels = numChannels; |
| pcmParams.eNumData = OMX_NumericalDataSigned; |
| pcmParams.bInterleaved = OMX_TRUE; |
| pcmParams.nBitPerSample = 16; |
| pcmParams.nSamplingRate = sampleRate; |
| pcmParams.ePCMMode = OMX_AUDIO_PCMModeLinear; |
| |
| if (getOMXChannelMapping(numChannels, pcmParams.eChannelMapping) != OK) { |
| return OMX_ErrorNone; |
| } |
| |
| return mOMX->setParameter( |
| mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams)); |
| } |
| |
| status_t ACodec::configureTunneledVideoPlayback( |
| int32_t audioHwSync, const sp<ANativeWindow> &nativeWindow) { |
| native_handle_t* sidebandHandle; |
| |
| status_t err = mOMX->configureVideoTunnelMode( |
| mNode, kPortIndexOutput, OMX_TRUE, audioHwSync, &sidebandHandle); |
| if (err != OK) { |
| ALOGE("configureVideoTunnelMode failed! (err %d).", err); |
| return err; |
| } |
| |
| err = native_window_set_sideband_stream(nativeWindow.get(), sidebandHandle); |
| if (err != OK) { |
| ALOGE("native_window_set_sideband_stream(%p) failed! (err %d).", |
| sidebandHandle, err); |
| return err; |
| } |
| |
| return OK; |
| } |
| |
| status_t ACodec::setVideoPortFormatType( |
| OMX_U32 portIndex, |
| OMX_VIDEO_CODINGTYPE compressionFormat, |
| OMX_COLOR_FORMATTYPE colorFormat, |
| bool usingNativeBuffers) { |
| OMX_VIDEO_PARAM_PORTFORMATTYPE format; |
| InitOMXParams(&format); |
| format.nPortIndex = portIndex; |
| format.nIndex = 0; |
| bool found = false; |
| |
| OMX_U32 index = 0; |
| for (;;) { |
| format.nIndex = index; |
| status_t err = mOMX->getParameter( |
| mNode, OMX_IndexParamVideoPortFormat, |
| &format, sizeof(format)); |
| |
| if (err != OK) { |
| return err; |
| } |
| |
| // substitute back flexible color format to codec supported format |
| OMX_U32 flexibleEquivalent; |
| if (compressionFormat == OMX_VIDEO_CodingUnused |
| && isFlexibleColorFormat( |
| mOMX, mNode, format.eColorFormat, usingNativeBuffers, &flexibleEquivalent) |
| && colorFormat == flexibleEquivalent) { |
| ALOGI("[%s] using color format %#x in place of %#x", |
| mComponentName.c_str(), format.eColorFormat, colorFormat); |
| colorFormat = format.eColorFormat; |
| } |
| |
| // The following assertion is violated by TI's video decoder. |
| // CHECK_EQ(format.nIndex, index); |
| |
| if (!strcmp("OMX.TI.Video.encoder", mComponentName.c_str())) { |
| if (portIndex == kPortIndexInput |
| && colorFormat == format.eColorFormat) { |
| // eCompressionFormat does not seem right. |
| found = true; |
| break; |
| } |
| if (portIndex == kPortIndexOutput |
| && compressionFormat == format.eCompressionFormat) { |
| // eColorFormat does not seem right. |
| found = true; |
| break; |
| } |
| } |
| |
| if (format.eCompressionFormat == compressionFormat |
| && format.eColorFormat == colorFormat) { |
| found = true; |
| break; |
| } |
| |
| ++index; |
| } |
| |
| if (!found) { |
| return UNKNOWN_ERROR; |
| } |
| |
| status_t err = mOMX->setParameter( |
| mNode, OMX_IndexParamVideoPortFormat, |
| &format, sizeof(format)); |
| |
| return err; |
| } |
| |
| // Set optimal output format. OMX component lists output formats in the order |
| // of preference, but this got more complicated since the introduction of flexible |
| // YUV formats. We support a legacy behavior for applications that do not use |
| // surface output, do not specify an output format, but expect a "usable" standard |
| // OMX format. SW readable and standard formats must be flex-YUV. |
| // |
| // Suggested preference order: |
| // - optimal format for texture rendering (mediaplayer behavior) |
| // - optimal SW readable & texture renderable format (flex-YUV support) |
| // - optimal SW readable non-renderable format (flex-YUV bytebuffer support) |
| // - legacy "usable" standard formats |
| // |
| // For legacy support, we prefer a standard format, but will settle for a SW readable |
| // flex-YUV format. |
| status_t ACodec::setSupportedOutputFormat(bool getLegacyFlexibleFormat) { |
| OMX_VIDEO_PARAM_PORTFORMATTYPE format, legacyFormat; |
| InitOMXParams(&format); |
| format.nPortIndex = kPortIndexOutput; |
| |
| InitOMXParams(&legacyFormat); |
| // this field will change when we find a suitable legacy format |
| legacyFormat.eColorFormat = OMX_COLOR_FormatUnused; |
| |
| for (OMX_U32 index = 0; ; ++index) { |
| format.nIndex = index; |
| status_t err = mOMX->getParameter( |
| mNode, OMX_IndexParamVideoPortFormat, |
| &format, sizeof(format)); |
| if (err != OK) { |
| // no more formats, pick legacy format if found |
| if (legacyFormat.eColorFormat != OMX_COLOR_FormatUnused) { |
| memcpy(&format, &legacyFormat, sizeof(format)); |
| break; |
| } |
| return err; |
| } |
| if (format.eCompressionFormat != OMX_VIDEO_CodingUnused) { |
| return OMX_ErrorBadParameter; |
| } |
| if (!getLegacyFlexibleFormat) { |
| break; |
| } |
| // standard formats that were exposed to users before |
| if (format.eColorFormat == OMX_COLOR_FormatYUV420Planar |
| || format.eColorFormat == OMX_COLOR_FormatYUV420PackedPlanar |
| || format.eColorFormat == OMX_COLOR_FormatYUV420SemiPlanar |
| || format.eColorFormat == OMX_COLOR_FormatYUV420PackedSemiPlanar |
| || format.eColorFormat == OMX_TI_COLOR_FormatYUV420PackedSemiPlanar) { |
| break; |
| } |
| // find best legacy non-standard format |
| OMX_U32 flexibleEquivalent; |
| if (legacyFormat.eColorFormat == OMX_COLOR_FormatUnused |
| && isFlexibleColorFormat( |
| mOMX, mNode, format.eColorFormat, false /* usingNativeBuffers */, |
| &flexibleEquivalent) |
| && flexibleEquivalent == OMX_COLOR_FormatYUV420Flexible) { |
| memcpy(&legacyFormat, &format, sizeof(format)); |
| } |
| } |
| return mOMX->setParameter( |
| mNode, OMX_IndexParamVideoPortFormat, |
| &format, sizeof(format)); |
| } |
| |
| static const struct VideoCodingMapEntry { |
| const char *mMime; |
| OMX_VIDEO_CODINGTYPE mVideoCodingType; |
| } kVideoCodingMapEntry[] = { |
| { MEDIA_MIMETYPE_VIDEO_AVC, OMX_VIDEO_CodingAVC }, |
| { MEDIA_MIMETYPE_VIDEO_HEVC, OMX_VIDEO_CodingHEVC }, |
| { MEDIA_MIMETYPE_VIDEO_MPEG4, OMX_VIDEO_CodingMPEG4 }, |
| { MEDIA_MIMETYPE_VIDEO_H263, OMX_VIDEO_CodingH263 }, |
| { MEDIA_MIMETYPE_VIDEO_MPEG2, OMX_VIDEO_CodingMPEG2 }, |
| { MEDIA_MIMETYPE_VIDEO_VP8, OMX_VIDEO_CodingVP8 }, |
| { MEDIA_MIMETYPE_VIDEO_VP9, OMX_VIDEO_CodingVP9 }, |
| }; |
| |
| static status_t GetVideoCodingTypeFromMime( |
| const char *mime, OMX_VIDEO_CODINGTYPE *codingType) { |
| for (size_t i = 0; |
| i < sizeof(kVideoCodingMapEntry) / sizeof(kVideoCodingMapEntry[0]); |
| ++i) { |
| if (!strcasecmp(mime, kVideoCodingMapEntry[i].mMime)) { |
| *codingType = kVideoCodingMapEntry[i].mVideoCodingType; |
| return OK; |
| } |
| } |
| |
| *codingType = OMX_VIDEO_CodingUnused; |
| |
| return ERROR_UNSUPPORTED; |
| } |
| |
| static status_t GetMimeTypeForVideoCoding( |
| OMX_VIDEO_CODINGTYPE codingType, AString *mime) { |
| for (size_t i = 0; |
| i < sizeof(kVideoCodingMapEntry) / sizeof(kVideoCodingMapEntry[0]); |
| ++i) { |
| if (codingType == kVideoCodingMapEntry[i].mVideoCodingType) { |
| *mime = kVideoCodingMapEntry[i].mMime; |
| return OK; |
| } |
| } |
| |
| mime->clear(); |
| |
| return ERROR_UNSUPPORTED; |
| } |
| |
| status_t ACodec::setupVideoDecoder( |
| const char *mime, const sp<AMessage> &msg, bool haveNativeWindow) { |
| int32_t width, height; |
| if (!msg->findInt32("width", &width) |
| || !msg->findInt32("height", &height)) { |
| return INVALID_OPERATION; |
| } |
| |
| OMX_VIDEO_CODINGTYPE compressionFormat; |
| status_t err = GetVideoCodingTypeFromMime(mime, &compressionFormat); |
| |
| if (err != OK) { |
| return err; |
| } |
| |
| err = setVideoPortFormatType( |
| kPortIndexInput, compressionFormat, OMX_COLOR_FormatUnused); |
| |
| if (err != OK) { |
| return err; |
| } |
| |
| int32_t tmp; |
| if (msg->findInt32("color-format", &tmp)) { |
| OMX_COLOR_FORMATTYPE colorFormat = |
| static_cast<OMX_COLOR_FORMATTYPE>(tmp); |
| err = setVideoPortFormatType( |
|