OMXCodec.h revision 824c9ff4a55e86faae4f8f158977329909cbfaf6
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; 30struct CodecProfileLevel; 31 32struct OMXCodec : public MediaSource, 33 public MediaBufferObserver { 34 enum CreationFlags { 35 kPreferSoftwareCodecs = 1, 36 }; 37 static sp<MediaSource> Create( 38 const sp<IOMX> &omx, 39 const sp<MetaData> &meta, bool createEncoder, 40 const sp<MediaSource> &source, 41 const char *matchComponentName = NULL, 42 uint32_t flags = 0); 43 44 static void setComponentRole( 45 const sp<IOMX> &omx, IOMX::node_id node, bool isEncoder, 46 const char *mime); 47 48 virtual status_t start(MetaData *params = NULL); 49 virtual status_t stop(); 50 51 virtual sp<MetaData> getFormat(); 52 53 virtual status_t read( 54 MediaBuffer **buffer, const ReadOptions *options = NULL); 55 56 virtual status_t pause(); 57 58 void on_message(const omx_message &msg); 59 60 // from MediaBufferObserver 61 virtual void signalBufferReturned(MediaBuffer *buffer); 62 63protected: 64 virtual ~OMXCodec(); 65 66private: 67 enum State { 68 DEAD, 69 LOADED, 70 LOADED_TO_IDLE, 71 IDLE_TO_EXECUTING, 72 EXECUTING, 73 EXECUTING_TO_IDLE, 74 IDLE_TO_LOADED, 75 RECONFIGURING, 76 ERROR 77 }; 78 79 enum { 80 kPortIndexInput = 0, 81 kPortIndexOutput = 1 82 }; 83 84 enum PortStatus { 85 ENABLED, 86 DISABLING, 87 DISABLED, 88 ENABLING, 89 SHUTTING_DOWN, 90 }; 91 92 enum Quirks { 93 kNeedsFlushBeforeDisable = 1, 94 kWantsNALFragments = 2, 95 kRequiresLoadedToIdleAfterAllocation = 4, 96 kRequiresAllocateBufferOnInputPorts = 8, 97 kRequiresFlushCompleteEmulation = 16, 98 kRequiresAllocateBufferOnOutputPorts = 32, 99 kRequiresFlushBeforeShutdown = 64, 100 kDefersOutputBufferAllocation = 128, 101 kDecoderLiesAboutNumberOfChannels = 256, 102 kInputBufferSizesAreBogus = 512, 103 kSupportsMultipleFramesPerInputBuffer = 1024, 104 kAvoidMemcopyInputRecordingFrames = 2048, 105 kRequiresLargerEncoderOutputBuffer = 4096, 106 }; 107 108 struct BufferInfo { 109 IOMX::buffer_id mBuffer; 110 bool mOwnedByComponent; 111 sp<IMemory> mMem; 112 size_t mSize; 113 void *mData; 114 MediaBuffer *mMediaBuffer; 115 }; 116 117 struct CodecSpecificData { 118 size_t mSize; 119 uint8_t mData[1]; 120 }; 121 122 sp<IOMX> mOMX; 123 bool mOMXLivesLocally; 124 IOMX::node_id mNode; 125 uint32_t mQuirks; 126 bool mIsEncoder; 127 char *mMIME; 128 char *mComponentName; 129 sp<MetaData> mOutputFormat; 130 sp<MediaSource> mSource; 131 Vector<CodecSpecificData *> mCodecSpecificData; 132 size_t mCodecSpecificDataIndex; 133 134 sp<MemoryDealer> mDealer[2]; 135 136 State mState; 137 Vector<BufferInfo> mPortBuffers[2]; 138 PortStatus mPortStatus[2]; 139 bool mInitialBufferSubmit; 140 bool mSignalledEOS; 141 status_t mFinalStatus; 142 bool mNoMoreOutputData; 143 bool mOutputPortSettingsHaveChanged; 144 int64_t mSeekTimeUs; 145 ReadOptions::SeekMode mSeekMode; 146 int64_t mTargetTimeUs; 147 int64_t mSkipTimeUs; 148 149 MediaBuffer *mLeftOverBuffer; 150 151 Mutex mLock; 152 Condition mAsyncCompletion; 153 154 bool mPaused; 155 156 // A list of indices into mPortStatus[kPortIndexOutput] filled with data. 157 List<size_t> mFilledBuffers; 158 Condition mBufferFilled; 159 160 OMXCodec(const sp<IOMX> &omx, IOMX::node_id node, uint32_t quirks, 161 bool isEncoder, const char *mime, const char *componentName, 162 const sp<MediaSource> &source); 163 164 void addCodecSpecificData(const void *data, size_t size); 165 void clearCodecSpecificData(); 166 167 void setComponentRole(); 168 169 void setAMRFormat(bool isWAMR, int32_t bitRate); 170 void setAACFormat(int32_t numChannels, int32_t sampleRate, int32_t bitRate); 171 172 status_t setVideoPortFormatType( 173 OMX_U32 portIndex, 174 OMX_VIDEO_CODINGTYPE compressionFormat, 175 OMX_COLOR_FORMATTYPE colorFormat); 176 177 void setVideoInputFormat( 178 const char *mime, const sp<MetaData>& meta); 179 180 status_t setupBitRate(int32_t bitRate); 181 status_t setupErrorCorrectionParameters(); 182 status_t setupH263EncoderParameters(const sp<MetaData>& meta); 183 status_t setupMPEG4EncoderParameters(const sp<MetaData>& meta); 184 status_t setupAVCEncoderParameters(const sp<MetaData>& meta); 185 status_t findTargetColorFormat( 186 const sp<MetaData>& meta, OMX_COLOR_FORMATTYPE *colorFormat); 187 188 status_t isColorFormatSupported( 189 OMX_COLOR_FORMATTYPE colorFormat, int portIndex); 190 191 // If profile/level is set in the meta data, its value in the meta 192 // data will be used; otherwise, the default value will be used. 193 status_t getVideoProfileLevel(const sp<MetaData>& meta, 194 const CodecProfileLevel& defaultProfileLevel, 195 CodecProfileLevel& profileLevel); 196 197 status_t setVideoOutputFormat( 198 const char *mime, OMX_U32 width, OMX_U32 height); 199 200 void setImageOutputFormat( 201 OMX_COLOR_FORMATTYPE format, OMX_U32 width, OMX_U32 height); 202 203 void setJPEGInputFormat( 204 OMX_U32 width, OMX_U32 height, OMX_U32 compressedSize); 205 206 void setMinBufferSize(OMX_U32 portIndex, OMX_U32 size); 207 208 void setRawAudioFormat( 209 OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels); 210 211 status_t allocateBuffers(); 212 status_t allocateBuffersOnPort(OMX_U32 portIndex); 213 214 status_t freeBuffersOnPort( 215 OMX_U32 portIndex, bool onlyThoseWeOwn = false); 216 217 void drainInputBuffer(IOMX::buffer_id buffer); 218 void fillOutputBuffer(IOMX::buffer_id buffer); 219 void drainInputBuffer(BufferInfo *info); 220 void fillOutputBuffer(BufferInfo *info); 221 222 void drainInputBuffers(); 223 void fillOutputBuffers(); 224 225 // Returns true iff a flush was initiated and a completion event is 226 // upcoming, false otherwise (A flush was not necessary as we own all 227 // the buffers on that port). 228 // This method will ONLY ever return false for a component with quirk 229 // "kRequiresFlushCompleteEmulation". 230 bool flushPortAsync(OMX_U32 portIndex); 231 232 void disablePortAsync(OMX_U32 portIndex); 233 void enablePortAsync(OMX_U32 portIndex); 234 235 static size_t countBuffersWeOwn(const Vector<BufferInfo> &buffers); 236 static bool isIntermediateState(State state); 237 238 void onEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2); 239 void onCmdComplete(OMX_COMMANDTYPE cmd, OMX_U32 data); 240 void onStateChange(OMX_STATETYPE newState); 241 void onPortSettingsChanged(OMX_U32 portIndex); 242 243 void setState(State newState); 244 245 status_t init(); 246 void initOutputFormat(const sp<MetaData> &inputFormat); 247 248 void dumpPortStatus(OMX_U32 portIndex); 249 250 status_t configureCodec(const sp<MetaData> &meta); 251 252 static uint32_t getComponentQuirks(const char *componentName); 253 254 static void findMatchingCodecs( 255 const char *mime, 256 bool createEncoder, const char *matchComponentName, 257 uint32_t flags, 258 Vector<String8> *matchingCodecs); 259 260 OMXCodec(const OMXCodec &); 261 OMXCodec &operator=(const OMXCodec &); 262}; 263 264struct CodecProfileLevel { 265 OMX_U32 mProfile; 266 OMX_U32 mLevel; 267}; 268 269struct CodecCapabilities { 270 String8 mComponentName; 271 Vector<CodecProfileLevel> mProfileLevels; 272}; 273 274// Return a vector of componentNames with supported profile/level pairs 275// supporting the given mime type, if queryDecoders==true, returns components 276// that decode content of the given type, otherwise returns components 277// that encode content of the given type. 278// profile and level indications only make sense for h.263, mpeg4 and avc 279// video. 280// The profile/level values correspond to 281// OMX_VIDEO_H263PROFILETYPE, OMX_VIDEO_MPEG4PROFILETYPE, 282// OMX_VIDEO_AVCPROFILETYPE, OMX_VIDEO_H263LEVELTYPE, OMX_VIDEO_MPEG4LEVELTYPE 283// and OMX_VIDEO_AVCLEVELTYPE respectively. 284 285status_t QueryCodecs( 286 const sp<IOMX> &omx, 287 const char *mimeType, bool queryDecoders, 288 Vector<CodecCapabilities> *results); 289 290} // namespace android 291 292#endif // OMX_CODEC_H_ 293