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