OMXCodec.h revision d35bd5fb4e09c2cd8608497c279cbb2ef9c3a029
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 setupMPEG4EncoderParameters(const sp<MetaData>& meta);
176    status_t setupAVCEncoderParameters(const sp<MetaData>& meta);
177
178    status_t setVideoOutputFormat(
179            const char *mime, OMX_U32 width, OMX_U32 height);
180
181    void setImageOutputFormat(
182            OMX_COLOR_FORMATTYPE format, OMX_U32 width, OMX_U32 height);
183
184    void setJPEGInputFormat(
185            OMX_U32 width, OMX_U32 height, OMX_U32 compressedSize);
186
187    void setMinBufferSize(OMX_U32 portIndex, OMX_U32 size);
188
189    void setRawAudioFormat(
190            OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels);
191
192    status_t allocateBuffers();
193    status_t allocateBuffersOnPort(OMX_U32 portIndex);
194
195    status_t freeBuffersOnPort(
196            OMX_U32 portIndex, bool onlyThoseWeOwn = false);
197
198    void drainInputBuffer(IOMX::buffer_id buffer);
199    void fillOutputBuffer(IOMX::buffer_id buffer);
200    void drainInputBuffer(BufferInfo *info);
201    void fillOutputBuffer(BufferInfo *info);
202
203    void drainInputBuffers();
204    void fillOutputBuffers();
205
206    // Returns true iff a flush was initiated and a completion event is
207    // upcoming, false otherwise (A flush was not necessary as we own all
208    // the buffers on that port).
209    // This method will ONLY ever return false for a component with quirk
210    // "kRequiresFlushCompleteEmulation".
211    bool flushPortAsync(OMX_U32 portIndex);
212
213    void disablePortAsync(OMX_U32 portIndex);
214    void enablePortAsync(OMX_U32 portIndex);
215
216    static size_t countBuffersWeOwn(const Vector<BufferInfo> &buffers);
217    static bool isIntermediateState(State state);
218
219    void onEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2);
220    void onCmdComplete(OMX_COMMANDTYPE cmd, OMX_U32 data);
221    void onStateChange(OMX_STATETYPE newState);
222    void onPortSettingsChanged(OMX_U32 portIndex);
223
224    void setState(State newState);
225
226    status_t init();
227    void initOutputFormat(const sp<MetaData> &inputFormat);
228
229    void dumpPortStatus(OMX_U32 portIndex);
230
231    status_t configureCodec(const sp<MetaData> &meta);
232
233    static uint32_t getComponentQuirks(const char *componentName);
234
235    static void findMatchingCodecs(
236            const char *mime,
237            bool createEncoder, const char *matchComponentName,
238            uint32_t flags,
239            Vector<String8> *matchingCodecs);
240
241    OMXCodec(const OMXCodec &);
242    OMXCodec &operator=(const OMXCodec &);
243};
244
245struct CodecProfileLevel {
246    OMX_U32 mProfile;
247    OMX_U32 mLevel;
248};
249
250struct CodecCapabilities {
251    String8 mComponentName;
252    Vector<CodecProfileLevel> mProfileLevels;
253};
254
255// Return a vector of componentNames with supported profile/level pairs
256// supporting the given mime type, if queryDecoders==true, returns components
257// that decode content of the given type, otherwise returns components
258// that encode content of the given type.
259// profile and level indications only make sense for h.263, mpeg4 and avc
260// video.
261// The profile/level values correspond to
262// OMX_VIDEO_H263PROFILETYPE, OMX_VIDEO_MPEG4PROFILETYPE,
263// OMX_VIDEO_AVCPROFILETYPE, OMX_VIDEO_H263LEVELTYPE, OMX_VIDEO_MPEG4LEVELTYPE
264// and OMX_VIDEO_AVCLEVELTYPE respectively.
265
266status_t QueryCodecs(
267        const sp<IOMX> &omx,
268        const char *mimeType, bool queryDecoders,
269        Vector<CodecCapabilities> *results);
270
271}  // namespace android
272
273#endif  // OMX_CODEC_H_
274