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