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