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