OMXCodec.h revision 965e4239ca1cf6c824c1f8ce23116f9ba8cf6ebd
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 // Used to record the decoding time for an output picture from 206 // a video encoder. 207 List<int64_t> mDecodingTimeList; 208 209 OMXCodec(const sp<IOMX> &omx, IOMX::node_id node, uint32_t quirks, 210 bool isEncoder, const char *mime, const char *componentName, 211 const sp<MediaSource> &source, 212 const sp<ANativeWindow> &nativeWindow); 213 214 void addCodecSpecificData(const void *data, size_t size); 215 void clearCodecSpecificData(); 216 217 void setComponentRole(); 218 219 void setAMRFormat(bool isWAMR, int32_t bitRate); 220 void setAACFormat(int32_t numChannels, int32_t sampleRate, int32_t bitRate); 221 void setG711Format(int32_t numChannels); 222 223 status_t setVideoPortFormatType( 224 OMX_U32 portIndex, 225 OMX_VIDEO_CODINGTYPE compressionFormat, 226 OMX_COLOR_FORMATTYPE colorFormat); 227 228 void setVideoInputFormat( 229 const char *mime, const sp<MetaData>& meta); 230 231 status_t setupBitRate(int32_t bitRate); 232 status_t setupErrorCorrectionParameters(); 233 status_t setupH263EncoderParameters(const sp<MetaData>& meta); 234 status_t setupMPEG4EncoderParameters(const sp<MetaData>& meta); 235 status_t setupAVCEncoderParameters(const sp<MetaData>& meta); 236 status_t findTargetColorFormat( 237 const sp<MetaData>& meta, OMX_COLOR_FORMATTYPE *colorFormat); 238 239 status_t isColorFormatSupported( 240 OMX_COLOR_FORMATTYPE colorFormat, int portIndex); 241 242 // If profile/level is set in the meta data, its value in the meta 243 // data will be used; otherwise, the default value will be used. 244 status_t getVideoProfileLevel(const sp<MetaData>& meta, 245 const CodecProfileLevel& defaultProfileLevel, 246 CodecProfileLevel& profileLevel); 247 248 status_t setVideoOutputFormat( 249 const char *mime, OMX_U32 width, OMX_U32 height); 250 251 void setImageOutputFormat( 252 OMX_COLOR_FORMATTYPE format, OMX_U32 width, OMX_U32 height); 253 254 void setJPEGInputFormat( 255 OMX_U32 width, OMX_U32 height, OMX_U32 compressedSize); 256 257 void setMinBufferSize(OMX_U32 portIndex, OMX_U32 size); 258 259 void setRawAudioFormat( 260 OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels); 261 262 status_t allocateBuffers(); 263 status_t allocateBuffersOnPort(OMX_U32 portIndex); 264 status_t allocateOutputBuffersFromNativeWindow(); 265 266 status_t queueBufferToNativeWindow(BufferInfo *info); 267 status_t cancelBufferToNativeWindow(BufferInfo *info); 268 BufferInfo* dequeueBufferFromNativeWindow(); 269 270 status_t freeBuffersOnPort( 271 OMX_U32 portIndex, bool onlyThoseWeOwn = false); 272 273 status_t freeBuffer(OMX_U32 portIndex, size_t bufIndex); 274 275 bool drainInputBuffer(IOMX::buffer_id buffer); 276 void fillOutputBuffer(IOMX::buffer_id buffer); 277 bool drainInputBuffer(BufferInfo *info); 278 void fillOutputBuffer(BufferInfo *info); 279 280 void drainInputBuffers(); 281 void fillOutputBuffers(); 282 283 // Returns true iff a flush was initiated and a completion event is 284 // upcoming, false otherwise (A flush was not necessary as we own all 285 // the buffers on that port). 286 // This method will ONLY ever return false for a component with quirk 287 // "kRequiresFlushCompleteEmulation". 288 bool flushPortAsync(OMX_U32 portIndex); 289 290 void disablePortAsync(OMX_U32 portIndex); 291 void enablePortAsync(OMX_U32 portIndex); 292 293 static size_t countBuffersWeOwn(const Vector<BufferInfo> &buffers); 294 static bool isIntermediateState(State state); 295 296 void onEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2); 297 void onCmdComplete(OMX_COMMANDTYPE cmd, OMX_U32 data); 298 void onStateChange(OMX_STATETYPE newState); 299 void onPortSettingsChanged(OMX_U32 portIndex); 300 301 void setState(State newState); 302 303 status_t init(); 304 void initOutputFormat(const sp<MetaData> &inputFormat); 305 status_t initNativeWindow(); 306 307 void dumpPortStatus(OMX_U32 portIndex); 308 309 status_t configureCodec(const sp<MetaData> &meta, uint32_t flags); 310 311 static uint32_t getComponentQuirks( 312 const char *componentName, bool isEncoder); 313 314 static void findMatchingCodecs( 315 const char *mime, 316 bool createEncoder, const char *matchComponentName, 317 uint32_t flags, 318 Vector<String8> *matchingCodecs); 319 320 void restorePatchedDataPointer(BufferInfo *info); 321 322 status_t applyRotation(); 323 324 int64_t retrieveDecodingTimeUs(bool isCodecSpecific); 325 326 OMXCodec(const OMXCodec &); 327 OMXCodec &operator=(const OMXCodec &); 328}; 329 330struct CodecProfileLevel { 331 OMX_U32 mProfile; 332 OMX_U32 mLevel; 333}; 334 335struct CodecCapabilities { 336 String8 mComponentName; 337 Vector<CodecProfileLevel> mProfileLevels; 338 Vector<OMX_U32> mColorFormats; 339}; 340 341// Return a vector of componentNames with supported profile/level pairs 342// supporting the given mime type, if queryDecoders==true, returns components 343// that decode content of the given type, otherwise returns components 344// that encode content of the given type. 345// profile and level indications only make sense for h.263, mpeg4 and avc 346// video. 347// The profile/level values correspond to 348// OMX_VIDEO_H263PROFILETYPE, OMX_VIDEO_MPEG4PROFILETYPE, 349// OMX_VIDEO_AVCPROFILETYPE, OMX_VIDEO_H263LEVELTYPE, OMX_VIDEO_MPEG4LEVELTYPE 350// and OMX_VIDEO_AVCLEVELTYPE respectively. 351 352status_t QueryCodecs( 353 const sp<IOMX> &omx, 354 const char *mimeType, bool queryDecoders, 355 Vector<CodecCapabilities> *results); 356 357} // namespace android 358 359#endif // OMX_CODEC_H_ 360