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