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