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