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