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