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