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