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