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