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