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