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