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