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         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 MediaCodecList *list, size_t index);
    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     void setG711Format(int32_t numChannels);
    252 
    253     status_t setVideoPortFormatType(
    254             OMX_U32 portIndex,
    255             OMX_VIDEO_CODINGTYPE compressionFormat,
    256             OMX_COLOR_FORMATTYPE colorFormat);
    257 
    258     void setVideoInputFormat(
    259             const char *mime, const sp<MetaData>& meta);
    260 
    261     status_t setupBitRate(int32_t bitRate);
    262     status_t setupErrorCorrectionParameters();
    263     status_t setupH263EncoderParameters(const sp<MetaData>& meta);
    264     status_t setupMPEG4EncoderParameters(const sp<MetaData>& meta);
    265     status_t setupAVCEncoderParameters(const sp<MetaData>& meta);
    266     status_t findTargetColorFormat(
    267             const sp<MetaData>& meta, OMX_COLOR_FORMATTYPE *colorFormat);
    268 
    269     status_t isColorFormatSupported(
    270             OMX_COLOR_FORMATTYPE colorFormat, int portIndex);
    271 
    272     // If profile/level is set in the meta data, its value in the meta
    273     // data will be used; otherwise, the default value will be used.
    274     status_t getVideoProfileLevel(const sp<MetaData>& meta,
    275             const CodecProfileLevel& defaultProfileLevel,
    276             CodecProfileLevel& profileLevel);
    277 
    278     status_t setVideoOutputFormat(
    279             const char *mime, const sp<MetaData>& meta);
    280 
    281     void setImageOutputFormat(
    282             OMX_COLOR_FORMATTYPE format, OMX_U32 width, OMX_U32 height);
    283 
    284     void setJPEGInputFormat(
    285             OMX_U32 width, OMX_U32 height, OMX_U32 compressedSize);
    286 
    287     void setMinBufferSize(OMX_U32 portIndex, OMX_U32 size);
    288 
    289     void setRawAudioFormat(
    290             OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels);
    291 
    292     status_t allocateBuffers();
    293     status_t allocateBuffersOnPort(OMX_U32 portIndex);
    294     status_t allocateOutputBuffersFromNativeWindow();
    295 
    296     status_t queueBufferToNativeWindow(BufferInfo *info);
    297     status_t cancelBufferToNativeWindow(BufferInfo *info);
    298     BufferInfo* dequeueBufferFromNativeWindow();
    299     status_t pushBlankBuffersToNativeWindow();
    300 
    301     status_t freeBuffersOnPort(
    302             OMX_U32 portIndex, bool onlyThoseWeOwn = false);
    303 
    304     status_t freeBuffer(OMX_U32 portIndex, size_t bufIndex);
    305 
    306     bool drainInputBuffer(IOMX::buffer_id buffer);
    307     void fillOutputBuffer(IOMX::buffer_id buffer);
    308     bool drainInputBuffer(BufferInfo *info);
    309     void fillOutputBuffer(BufferInfo *info);
    310 
    311     void drainInputBuffers();
    312     void fillOutputBuffers();
    313 
    314     bool drainAnyInputBuffer();
    315     BufferInfo *findInputBufferByDataPointer(void *ptr);
    316     BufferInfo *findEmptyInputBuffer();
    317 
    318     // Returns true iff a flush was initiated and a completion event is
    319     // upcoming, false otherwise (A flush was not necessary as we own all
    320     // the buffers on that port).
    321     // This method will ONLY ever return false for a component with quirk
    322     // "kRequiresFlushCompleteEmulation".
    323     bool flushPortAsync(OMX_U32 portIndex);
    324 
    325     void disablePortAsync(OMX_U32 portIndex);
    326     status_t enablePortAsync(OMX_U32 portIndex);
    327 
    328     static size_t countBuffersWeOwn(const Vector<BufferInfo> &buffers);
    329     static bool isIntermediateState(State state);
    330 
    331     void onEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2);
    332     void onCmdComplete(OMX_COMMANDTYPE cmd, OMX_U32 data);
    333     void onStateChange(OMX_STATETYPE newState);
    334     void onPortSettingsChanged(OMX_U32 portIndex);
    335 
    336     void setState(State newState);
    337 
    338     status_t init();
    339     void initOutputFormat(const sp<MetaData> &inputFormat);
    340     status_t initNativeWindow();
    341 
    342     void initNativeWindowCrop();
    343 
    344     void dumpPortStatus(OMX_U32 portIndex);
    345 
    346     status_t configureCodec(const sp<MetaData> &meta);
    347 
    348     status_t applyRotation();
    349     status_t waitForBufferFilled_l();
    350 
    351     int64_t getDecodingTimeUs();
    352 
    353     status_t parseAVCCodecSpecificData(
    354             const void *data, size_t size,
    355             unsigned *profile, unsigned *level);
    356 
    357     status_t stopOmxComponent_l();
    358 
    359     OMXCodec(const OMXCodec &);
    360     OMXCodec &operator=(const OMXCodec &);
    361 };
    362 
    363 struct CodecCapabilities {
    364     String8 mComponentName;
    365     Vector<CodecProfileLevel> mProfileLevels;
    366     Vector<OMX_U32> mColorFormats;
    367 };
    368 
    369 // Return a vector of componentNames with supported profile/level pairs
    370 // supporting the given mime type, if queryDecoders==true, returns components
    371 // that decode content of the given type, otherwise returns components
    372 // that encode content of the given type.
    373 // profile and level indications only make sense for h.263, mpeg4 and avc
    374 // video.
    375 // If hwCodecOnly==true, only returns hardware-based components, software and
    376 // hardware otherwise.
    377 // The profile/level values correspond to
    378 // OMX_VIDEO_H263PROFILETYPE, OMX_VIDEO_MPEG4PROFILETYPE,
    379 // OMX_VIDEO_AVCPROFILETYPE, OMX_VIDEO_H263LEVELTYPE, OMX_VIDEO_MPEG4LEVELTYPE
    380 // and OMX_VIDEO_AVCLEVELTYPE respectively.
    381 
    382 status_t QueryCodecs(
    383         const sp<IOMX> &omx,
    384         const char *mimeType, bool queryDecoders, bool hwCodecOnly,
    385         Vector<CodecCapabilities> *results);
    386 
    387 status_t QueryCodecs(
    388         const sp<IOMX> &omx,
    389         const char *mimeType, bool queryDecoders,
    390         Vector<CodecCapabilities> *results);
    391 
    392 status_t QueryCodec(
    393         const sp<IOMX> &omx,
    394         const char *componentName, const char *mime,
    395         bool isEncoder,
    396         CodecCapabilities *caps);
    397 
    398 status_t getOMXChannelMapping(size_t numChannels, OMX_AUDIO_CHANNELTYPE map[]);
    399 
    400 }  // namespace android
    401 
    402 #endif  // OMX_CODEC_H_
    403