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