OMXCodec.h revision 79e23b41fad961008bfde6e26b3c6f86878ca69d
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 enum BufferStatus { 125 OWNED_BY_US, 126 OWNED_BY_COMPONENT, 127 OWNED_BY_NATIVE_WINDOW, 128 OWNED_BY_CLIENT, 129 }; 130 131 struct BufferInfo { 132 IOMX::buffer_id mBuffer; 133 BufferStatus mStatus; 134 sp<IMemory> mMem; 135 size_t mSize; 136 void *mData; 137 MediaBuffer *mMediaBuffer; 138 }; 139 140 struct CodecSpecificData { 141 size_t mSize; 142 uint8_t mData[1]; 143 }; 144 145 sp<IOMX> mOMX; 146 bool mOMXLivesLocally; 147 IOMX::node_id mNode; 148 uint32_t mQuirks; 149 bool mIsEncoder; 150 char *mMIME; 151 char *mComponentName; 152 sp<MetaData> mOutputFormat; 153 sp<MediaSource> mSource; 154 Vector<CodecSpecificData *> mCodecSpecificData; 155 size_t mCodecSpecificDataIndex; 156 157 sp<MemoryDealer> mDealer[2]; 158 159 State mState; 160 Vector<BufferInfo> mPortBuffers[2]; 161 PortStatus mPortStatus[2]; 162 bool mInitialBufferSubmit; 163 bool mSignalledEOS; 164 status_t mFinalStatus; 165 bool mNoMoreOutputData; 166 bool mOutputPortSettingsHaveChanged; 167 int64_t mSeekTimeUs; 168 ReadOptions::SeekMode mSeekMode; 169 int64_t mTargetTimeUs; 170 171 MediaBuffer *mLeftOverBuffer; 172 173 Mutex mLock; 174 Condition mAsyncCompletion; 175 176 bool mPaused; 177 178 sp<ANativeWindow> mNativeWindow; 179 180 // The index in each of the mPortBuffers arrays of the buffer that will be 181 // submitted to OMX next. This only applies when using buffers from a 182 // native window. 183 size_t mNextNativeBufferIndex[2]; 184 185 // A list of indices into mPortStatus[kPortIndexOutput] filled with data. 186 List<size_t> mFilledBuffers; 187 Condition mBufferFilled; 188 189 bool mIsMetaDataStoredInVideoBuffers; 190 191 OMXCodec(const sp<IOMX> &omx, IOMX::node_id node, uint32_t quirks, 192 bool isEncoder, const char *mime, const char *componentName, 193 const sp<MediaSource> &source, 194 const sp<ANativeWindow> &nativeWindow); 195 196 void addCodecSpecificData(const void *data, size_t size); 197 void clearCodecSpecificData(); 198 199 void setComponentRole(); 200 201 void setAMRFormat(bool isWAMR, int32_t bitRate); 202 void setAACFormat(int32_t numChannels, int32_t sampleRate, int32_t bitRate); 203 204 status_t setVideoPortFormatType( 205 OMX_U32 portIndex, 206 OMX_VIDEO_CODINGTYPE compressionFormat, 207 OMX_COLOR_FORMATTYPE colorFormat); 208 209 void setVideoInputFormat( 210 const char *mime, const sp<MetaData>& meta); 211 212 status_t setupBitRate(int32_t bitRate); 213 status_t setupErrorCorrectionParameters(); 214 status_t setupH263EncoderParameters(const sp<MetaData>& meta); 215 status_t setupMPEG4EncoderParameters(const sp<MetaData>& meta); 216 status_t setupAVCEncoderParameters(const sp<MetaData>& meta); 217 status_t findTargetColorFormat( 218 const sp<MetaData>& meta, OMX_COLOR_FORMATTYPE *colorFormat); 219 220 status_t isColorFormatSupported( 221 OMX_COLOR_FORMATTYPE colorFormat, int portIndex); 222 223 // If profile/level is set in the meta data, its value in the meta 224 // data will be used; otherwise, the default value will be used. 225 status_t getVideoProfileLevel(const sp<MetaData>& meta, 226 const CodecProfileLevel& defaultProfileLevel, 227 CodecProfileLevel& profileLevel); 228 229 status_t setVideoOutputFormat( 230 const char *mime, OMX_U32 width, OMX_U32 height); 231 232 void setImageOutputFormat( 233 OMX_COLOR_FORMATTYPE format, OMX_U32 width, OMX_U32 height); 234 235 void setJPEGInputFormat( 236 OMX_U32 width, OMX_U32 height, OMX_U32 compressedSize); 237 238 void setMinBufferSize(OMX_U32 portIndex, OMX_U32 size); 239 240 void setRawAudioFormat( 241 OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels); 242 243 status_t allocateBuffers(); 244 status_t allocateBuffersOnPort(OMX_U32 portIndex); 245 status_t allocateOutputBuffersFromNativeWindow(); 246 247 status_t queueBufferToNativeWindow(BufferInfo *info); 248 status_t cancelBufferToNativeWindow(BufferInfo *info); 249 BufferInfo* dequeueBufferFromNativeWindow(); 250 251 status_t freeBuffersOnPort( 252 OMX_U32 portIndex, bool onlyThoseWeOwn = false); 253 254 status_t freeBuffer(OMX_U32 portIndex, size_t bufIndex); 255 256 bool drainInputBuffer(IOMX::buffer_id buffer); 257 void fillOutputBuffer(IOMX::buffer_id buffer); 258 bool drainInputBuffer(BufferInfo *info); 259 void fillOutputBuffer(BufferInfo *info); 260 261 void drainInputBuffers(); 262 void fillOutputBuffers(); 263 264 // Returns true iff a flush was initiated and a completion event is 265 // upcoming, false otherwise (A flush was not necessary as we own all 266 // the buffers on that port). 267 // This method will ONLY ever return false for a component with quirk 268 // "kRequiresFlushCompleteEmulation". 269 bool flushPortAsync(OMX_U32 portIndex); 270 271 void disablePortAsync(OMX_U32 portIndex); 272 void enablePortAsync(OMX_U32 portIndex); 273 274 static size_t countBuffersWeOwn(const Vector<BufferInfo> &buffers); 275 static bool isIntermediateState(State state); 276 277 void onEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2); 278 void onCmdComplete(OMX_COMMANDTYPE cmd, OMX_U32 data); 279 void onStateChange(OMX_STATETYPE newState); 280 void onPortSettingsChanged(OMX_U32 portIndex); 281 282 void setState(State newState); 283 284 status_t init(); 285 void initOutputFormat(const sp<MetaData> &inputFormat); 286 status_t initNativeWindow(); 287 288 void dumpPortStatus(OMX_U32 portIndex); 289 290 status_t configureCodec(const sp<MetaData> &meta, uint32_t flags); 291 292 static uint32_t getComponentQuirks( 293 const char *componentName, bool isEncoder); 294 295 static void findMatchingCodecs( 296 const char *mime, 297 bool createEncoder, const char *matchComponentName, 298 uint32_t flags, 299 Vector<String8> *matchingCodecs); 300 301 void restorePatchedDataPointer(BufferInfo *info); 302 303 OMXCodec(const OMXCodec &); 304 OMXCodec &operator=(const OMXCodec &); 305}; 306 307struct CodecProfileLevel { 308 OMX_U32 mProfile; 309 OMX_U32 mLevel; 310}; 311 312struct CodecCapabilities { 313 String8 mComponentName; 314 Vector<CodecProfileLevel> mProfileLevels; 315 Vector<OMX_U32> mColorFormats; 316}; 317 318// Return a vector of componentNames with supported profile/level pairs 319// supporting the given mime type, if queryDecoders==true, returns components 320// that decode content of the given type, otherwise returns components 321// that encode content of the given type. 322// profile and level indications only make sense for h.263, mpeg4 and avc 323// video. 324// The profile/level values correspond to 325// OMX_VIDEO_H263PROFILETYPE, OMX_VIDEO_MPEG4PROFILETYPE, 326// OMX_VIDEO_AVCPROFILETYPE, OMX_VIDEO_H263LEVELTYPE, OMX_VIDEO_MPEG4LEVELTYPE 327// and OMX_VIDEO_AVCLEVELTYPE respectively. 328 329status_t QueryCodecs( 330 const sp<IOMX> &omx, 331 const char *mimeType, bool queryDecoders, 332 Vector<CodecCapabilities> *results); 333 334} // namespace android 335 336#endif // OMX_CODEC_H_ 337