OMXCodec.h revision 7f498b90a4300ef9badf14d202b0a67c26e20931
1/* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#ifndef OMX_CODEC_H_ 18 19#define OMX_CODEC_H_ 20 21#include <media/IOMX.h> 22#include <media/stagefright/MediaBuffer.h> 23#include <media/stagefright/MediaSource.h> 24#include <utils/threads.h> 25 26namespace android { 27 28class MemoryDealer; 29struct OMXCodecObserver; 30 31struct OMXCodec : public MediaSource, 32 public MediaBufferObserver { 33 enum CreationFlags { 34 kPreferSoftwareCodecs = 1, 35 }; 36 static sp<MediaSource> Create( 37 const sp<IOMX> &omx, 38 const sp<MetaData> &meta, bool createEncoder, 39 const sp<MediaSource> &source, 40 const char *matchComponentName = NULL, 41 uint32_t flags = 0); 42 43 static void setComponentRole( 44 const sp<IOMX> &omx, IOMX::node_id node, bool isEncoder, 45 const char *mime); 46 47 virtual status_t start(MetaData *params = NULL); 48 virtual status_t stop(); 49 50 virtual sp<MetaData> getFormat(); 51 52 virtual status_t read( 53 MediaBuffer **buffer, const ReadOptions *options = NULL); 54 55 void on_message(const omx_message &msg); 56 57 // from MediaBufferObserver 58 virtual void signalBufferReturned(MediaBuffer *buffer); 59 60protected: 61 virtual ~OMXCodec(); 62 63private: 64 enum State { 65 DEAD, 66 LOADED, 67 LOADED_TO_IDLE, 68 IDLE_TO_EXECUTING, 69 EXECUTING, 70 EXECUTING_TO_IDLE, 71 IDLE_TO_LOADED, 72 RECONFIGURING, 73 ERROR 74 }; 75 76 enum { 77 kPortIndexInput = 0, 78 kPortIndexOutput = 1 79 }; 80 81 enum PortStatus { 82 ENABLED, 83 DISABLING, 84 DISABLED, 85 ENABLING, 86 SHUTTING_DOWN, 87 }; 88 89 enum Quirks { 90 kNeedsFlushBeforeDisable = 1, 91 kWantsNALFragments = 2, 92 kRequiresLoadedToIdleAfterAllocation = 4, 93 kRequiresAllocateBufferOnInputPorts = 8, 94 kRequiresFlushCompleteEmulation = 16, 95 kRequiresAllocateBufferOnOutputPorts = 32, 96 kRequiresFlushBeforeShutdown = 64, 97 kDefersOutputBufferAllocation = 128, 98 kDecoderLiesAboutNumberOfChannels = 256, 99 kInputBufferSizesAreBogus = 512, 100 kSupportsMultipleFramesPerInputBuffer = 1024, 101 }; 102 103 struct BufferInfo { 104 IOMX::buffer_id mBuffer; 105 bool mOwnedByComponent; 106 sp<IMemory> mMem; 107 size_t mSize; 108 void *mData; 109 MediaBuffer *mMediaBuffer; 110 }; 111 112 struct CodecSpecificData { 113 size_t mSize; 114 uint8_t mData[1]; 115 }; 116 117 sp<IOMX> mOMX; 118 bool mOMXLivesLocally; 119 IOMX::node_id mNode; 120 uint32_t mQuirks; 121 bool mIsEncoder; 122 char *mMIME; 123 char *mComponentName; 124 sp<MetaData> mOutputFormat; 125 sp<MediaSource> mSource; 126 Vector<CodecSpecificData *> mCodecSpecificData; 127 size_t mCodecSpecificDataIndex; 128 129 sp<MemoryDealer> mDealer[2]; 130 131 State mState; 132 Vector<BufferInfo> mPortBuffers[2]; 133 PortStatus mPortStatus[2]; 134 bool mInitialBufferSubmit; 135 bool mSignalledEOS; 136 status_t mFinalStatus; 137 bool mNoMoreOutputData; 138 bool mOutputPortSettingsHaveChanged; 139 int64_t mSeekTimeUs; 140 141 MediaBuffer *mLeftOverBuffer; 142 143 Mutex mLock; 144 Condition mAsyncCompletion; 145 146 // A list of indices into mPortStatus[kPortIndexOutput] filled with data. 147 List<size_t> mFilledBuffers; 148 Condition mBufferFilled; 149 150 OMXCodec(const sp<IOMX> &omx, IOMX::node_id node, uint32_t quirks, 151 bool isEncoder, const char *mime, const char *componentName, 152 const sp<MediaSource> &source); 153 154 void addCodecSpecificData(const void *data, size_t size); 155 void clearCodecSpecificData(); 156 157 void setComponentRole(); 158 159 void setAMRFormat(bool isWAMR); 160 void setAACFormat(int32_t numChannels, int32_t sampleRate); 161 162 status_t setVideoPortFormatType( 163 OMX_U32 portIndex, 164 OMX_VIDEO_CODINGTYPE compressionFormat, 165 OMX_COLOR_FORMATTYPE colorFormat); 166 167 void setVideoInputFormat( 168 const char *mime, OMX_U32 width, OMX_U32 height); 169 170 status_t setupMPEG4EncoderParameters(); 171 status_t setupAVCEncoderParameters(); 172 173 status_t setVideoOutputFormat( 174 const char *mime, OMX_U32 width, OMX_U32 height); 175 176 void setImageOutputFormat( 177 OMX_COLOR_FORMATTYPE format, OMX_U32 width, OMX_U32 height); 178 179 void setJPEGInputFormat( 180 OMX_U32 width, OMX_U32 height, OMX_U32 compressedSize); 181 182 void setMinBufferSize(OMX_U32 portIndex, OMX_U32 size); 183 184 void setRawAudioFormat( 185 OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels); 186 187 status_t allocateBuffers(); 188 status_t allocateBuffersOnPort(OMX_U32 portIndex); 189 190 status_t freeBuffersOnPort( 191 OMX_U32 portIndex, bool onlyThoseWeOwn = false); 192 193 void drainInputBuffer(IOMX::buffer_id buffer); 194 void fillOutputBuffer(IOMX::buffer_id buffer); 195 void drainInputBuffer(BufferInfo *info); 196 void fillOutputBuffer(BufferInfo *info); 197 198 void drainInputBuffers(); 199 void fillOutputBuffers(); 200 201 // Returns true iff a flush was initiated and a completion event is 202 // upcoming, false otherwise (A flush was not necessary as we own all 203 // the buffers on that port). 204 // This method will ONLY ever return false for a component with quirk 205 // "kRequiresFlushCompleteEmulation". 206 bool flushPortAsync(OMX_U32 portIndex); 207 208 void disablePortAsync(OMX_U32 portIndex); 209 void enablePortAsync(OMX_U32 portIndex); 210 211 static size_t countBuffersWeOwn(const Vector<BufferInfo> &buffers); 212 static bool isIntermediateState(State state); 213 214 void onEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2); 215 void onCmdComplete(OMX_COMMANDTYPE cmd, OMX_U32 data); 216 void onStateChange(OMX_STATETYPE newState); 217 void onPortSettingsChanged(OMX_U32 portIndex); 218 219 void setState(State newState); 220 221 status_t init(); 222 void initOutputFormat(const sp<MetaData> &inputFormat); 223 224 void dumpPortStatus(OMX_U32 portIndex); 225 226 status_t configureCodec(const sp<MetaData> &meta); 227 228 static uint32_t getComponentQuirks(const char *componentName); 229 230 static void findMatchingCodecs( 231 const char *mime, 232 bool createEncoder, const char *matchComponentName, 233 uint32_t flags, 234 Vector<String8> *matchingCodecs); 235 236 OMXCodec(const OMXCodec &); 237 OMXCodec &operator=(const OMXCodec &); 238}; 239 240struct CodecProfileLevel { 241 OMX_U32 mProfile; 242 OMX_U32 mLevel; 243}; 244 245struct CodecCapabilities { 246 String8 mComponentName; 247 Vector<CodecProfileLevel> mProfileLevels; 248}; 249 250// Return a vector of componentNames with supported profile/level pairs 251// supporting the given mime type, if queryDecoders==true, returns components 252// that decode content of the given type, otherwise returns components 253// that encode content of the given type. 254// profile and level indications only make sense for h.263, mpeg4 and avc 255// video. 256// The profile/level values correspond to 257// OMX_VIDEO_H263PROFILETYPE, OMX_VIDEO_MPEG4PROFILETYPE, 258// OMX_VIDEO_AVCPROFILETYPE, OMX_VIDEO_H263LEVELTYPE, OMX_VIDEO_MPEG4LEVELTYPE 259// and OMX_VIDEO_AVCLEVELTYPE respectively. 260 261status_t QueryCodecs( 262 const sp<IOMX> &omx, 263 const char *mimeType, bool queryDecoders, 264 Vector<CodecCapabilities> *results); 265 266} // namespace android 267 268#endif // OMX_CODEC_H_ 269