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