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