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