OMXCodec.h revision 78d529eb330ab6c04b5c694403f5a7e7de4b702f
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 }; 100 101 struct BufferInfo { 102 IOMX::buffer_id mBuffer; 103 bool mOwnedByComponent; 104 sp<IMemory> mMem; 105 size_t mSize; 106 void *mData; 107 MediaBuffer *mMediaBuffer; 108 }; 109 110 struct CodecSpecificData { 111 size_t mSize; 112 uint8_t mData[1]; 113 }; 114 115 sp<IOMX> mOMX; 116 bool mOMXLivesLocally; 117 IOMX::node_id mNode; 118 uint32_t mQuirks; 119 bool mIsEncoder; 120 char *mMIME; 121 char *mComponentName; 122 sp<MetaData> mOutputFormat; 123 sp<MediaSource> mSource; 124 Vector<CodecSpecificData *> mCodecSpecificData; 125 size_t mCodecSpecificDataIndex; 126 127 sp<MemoryDealer> mDealer[2]; 128 129 State mState; 130 Vector<BufferInfo> mPortBuffers[2]; 131 PortStatus mPortStatus[2]; 132 bool mInitialBufferSubmit; 133 bool mSignalledEOS; 134 bool mNoMoreOutputData; 135 bool mOutputPortSettingsHaveChanged; 136 int64_t mSeekTimeUs; 137 138 Mutex mLock; 139 Condition mAsyncCompletion; 140 141 // A list of indices into mPortStatus[kPortIndexOutput] filled with data. 142 List<size_t> mFilledBuffers; 143 Condition mBufferFilled; 144 145 OMXCodec(const sp<IOMX> &omx, IOMX::node_id node, uint32_t quirks, 146 bool isEncoder, const char *mime, const char *componentName, 147 const sp<MediaSource> &source); 148 149 void addCodecSpecificData(const void *data, size_t size); 150 void clearCodecSpecificData(); 151 152 void setComponentRole(); 153 154 void setAMRFormat(bool isWAMR); 155 void setAACFormat(int32_t numChannels, int32_t sampleRate); 156 157 status_t setVideoPortFormatType( 158 OMX_U32 portIndex, 159 OMX_VIDEO_CODINGTYPE compressionFormat, 160 OMX_COLOR_FORMATTYPE colorFormat); 161 162 void setVideoInputFormat( 163 const char *mime, OMX_U32 width, OMX_U32 height); 164 165 status_t setupMPEG4EncoderParameters(); 166 status_t setupAVCEncoderParameters(); 167 168 void setVideoOutputFormat( 169 const char *mime, OMX_U32 width, OMX_U32 height); 170 171 void setImageOutputFormat( 172 OMX_COLOR_FORMATTYPE format, OMX_U32 width, OMX_U32 height); 173 174 void setJPEGInputFormat( 175 OMX_U32 width, OMX_U32 height, OMX_U32 compressedSize); 176 177 void setMinBufferSize(OMX_U32 portIndex, OMX_U32 size); 178 179 void setRawAudioFormat( 180 OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels); 181 182 status_t allocateBuffers(); 183 status_t allocateBuffersOnPort(OMX_U32 portIndex); 184 185 status_t freeBuffersOnPort( 186 OMX_U32 portIndex, bool onlyThoseWeOwn = false); 187 188 void drainInputBuffer(IOMX::buffer_id buffer); 189 void fillOutputBuffer(IOMX::buffer_id buffer); 190 void drainInputBuffer(BufferInfo *info); 191 void fillOutputBuffer(BufferInfo *info); 192 193 void drainInputBuffers(); 194 void fillOutputBuffers(); 195 196 // Returns true iff a flush was initiated and a completion event is 197 // upcoming, false otherwise (A flush was not necessary as we own all 198 // the buffers on that port). 199 // This method will ONLY ever return false for a component with quirk 200 // "kRequiresFlushCompleteEmulation". 201 bool flushPortAsync(OMX_U32 portIndex); 202 203 void disablePortAsync(OMX_U32 portIndex); 204 void enablePortAsync(OMX_U32 portIndex); 205 206 static size_t countBuffersWeOwn(const Vector<BufferInfo> &buffers); 207 static bool isIntermediateState(State state); 208 209 void onEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2); 210 void onCmdComplete(OMX_COMMANDTYPE cmd, OMX_U32 data); 211 void onStateChange(OMX_STATETYPE newState); 212 void onPortSettingsChanged(OMX_U32 portIndex); 213 214 void setState(State newState); 215 216 status_t init(); 217 void initOutputFormat(const sp<MetaData> &inputFormat); 218 219 void dumpPortStatus(OMX_U32 portIndex); 220 221 static uint32_t getComponentQuirks(const char *componentName); 222 223 static void findMatchingCodecs( 224 const char *mime, 225 bool createEncoder, const char *matchComponentName, 226 uint32_t flags, 227 Vector<String8> *matchingCodecs); 228 229 OMXCodec(const OMXCodec &); 230 OMXCodec &operator=(const OMXCodec &); 231}; 232 233struct CodecProfileLevel { 234 OMX_U32 mProfile; 235 OMX_U32 mLevel; 236}; 237 238struct CodecCapabilities { 239 String8 mComponentName; 240 Vector<CodecProfileLevel> mProfileLevels; 241}; 242 243// Return a vector of componentNames with supported profile/level pairs 244// supporting the given mime type, if queryDecoders==true, returns components 245// that decode content of the given type, otherwise returns components 246// that encode content of the given type. 247// profile and level indications only make sense for h.263, mpeg4 and avc 248// video. 249// The profile/level values correspond to 250// OMX_VIDEO_H263PROFILETYPE, OMX_VIDEO_MPEG4PROFILETYPE, 251// OMX_VIDEO_AVCPROFILETYPE, OMX_VIDEO_H263LEVELTYPE, OMX_VIDEO_MPEG4LEVELTYPE 252// and OMX_VIDEO_AVCLEVELTYPE respectively. 253 254status_t QueryCodecs( 255 const sp<IOMX> &omx, 256 const char *mimeType, bool queryDecoders, 257 Vector<CodecCapabilities> *results); 258 259} // namespace android 260 261#endif // OMX_CODEC_H_ 262