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