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 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 sampleRate, 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 
    302     status_t freeBuffersOnPort(
    303             OMX_U32 portIndex, bool onlyThoseWeOwn = false);
    304 
    305     status_t freeBuffer(OMX_U32 portIndex, size_t bufIndex);
    306 
    307     bool drainInputBuffer(IOMX::buffer_id buffer);
    308     void fillOutputBuffer(IOMX::buffer_id buffer);
    309     bool drainInputBuffer(BufferInfo *info);
    310     void fillOutputBuffer(BufferInfo *info);
    311 
    312     void drainInputBuffers();
    313     void fillOutputBuffers();
    314 
    315     bool drainAnyInputBuffer();
    316     BufferInfo *findInputBufferByDataPointer(void *ptr);
    317     BufferInfo *findEmptyInputBuffer();
    318 
    319     // Returns true iff a flush was initiated and a completion event is
    320     // upcoming, false otherwise (A flush was not necessary as we own all
    321     // the buffers on that port).
    322     // This method will ONLY ever return false for a component with quirk
    323     // "kRequiresFlushCompleteEmulation".
    324     bool flushPortAsync(OMX_U32 portIndex);
    325 
    326     void disablePortAsync(OMX_U32 portIndex);
    327     status_t enablePortAsync(OMX_U32 portIndex);
    328 
    329     static size_t countBuffersWeOwn(const Vector<BufferInfo> &buffers);
    330     static bool isIntermediateState(State state);
    331 
    332     void onEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2);
    333     void onCmdComplete(OMX_COMMANDTYPE cmd, OMX_U32 data);
    334     void onStateChange(OMX_STATETYPE newState);
    335     void onPortSettingsChanged(OMX_U32 portIndex);
    336 
    337     void setState(State newState);
    338 
    339     status_t init();
    340     void initOutputFormat(const sp<MetaData> &inputFormat);
    341     status_t initNativeWindow();
    342 
    343     void initNativeWindowCrop();
    344 
    345     void dumpPortStatus(OMX_U32 portIndex);
    346 
    347     status_t configureCodec(const sp<MetaData> &meta);
    348 
    349     status_t waitForBufferFilled_l();
    350 
    351     int64_t getDecodingTimeUs();
    352 
    353     status_t parseHEVCCodecSpecificData(
    354             const void *data, size_t size,
    355             unsigned *profile, unsigned *level);
    356     status_t parseAVCCodecSpecificData(
    357             const void *data, size_t size,
    358             unsigned *profile, unsigned *level);
    359 
    360     status_t stopOmxComponent_l();
    361 
    362     OMXCodec(const OMXCodec &);
    363     OMXCodec &operator=(const OMXCodec &);
    364 };
    365 
    366 struct CodecCapabilities {
    367     enum {
    368         kFlagSupportsAdaptivePlayback = 1 << 0,
    369     };
    370 
    371     String8 mComponentName;
    372     Vector<CodecProfileLevel> mProfileLevels;
    373     Vector<OMX_U32> mColorFormats;
    374     uint32_t mFlags;
    375 };
    376 
    377 // Return a vector of componentNames with supported profile/level pairs
    378 // supporting the given mime type, if queryDecoders==true, returns components
    379 // that decode content of the given type, otherwise returns components
    380 // that encode content of the given type.
    381 // profile and level indications only make sense for h.263, mpeg4 and avc
    382 // video.
    383 // If hwCodecOnly==true, only returns hardware-based components, software and
    384 // hardware otherwise.
    385 // The profile/level values correspond to
    386 // OMX_VIDEO_H263PROFILETYPE, OMX_VIDEO_MPEG4PROFILETYPE,
    387 // OMX_VIDEO_AVCPROFILETYPE, OMX_VIDEO_H263LEVELTYPE, OMX_VIDEO_MPEG4LEVELTYPE
    388 // and OMX_VIDEO_AVCLEVELTYPE respectively.
    389 
    390 status_t QueryCodecs(
    391         const sp<IOMX> &omx,
    392         const char *mimeType, bool queryDecoders, bool hwCodecOnly,
    393         Vector<CodecCapabilities> *results);
    394 
    395 status_t QueryCodecs(
    396         const sp<IOMX> &omx,
    397         const char *mimeType, bool queryDecoders,
    398         Vector<CodecCapabilities> *results);
    399 
    400 status_t QueryCodec(
    401         const sp<IOMX> &omx,
    402         const char *componentName, const char *mime,
    403         bool isEncoder,
    404         CodecCapabilities *caps);
    405 
    406 status_t getOMXChannelMapping(size_t numChannels, OMX_AUDIO_CHANNELTYPE map[]);
    407 
    408 }  // namespace android
    409 
    410 #endif  // OMX_CODEC_H_
    411