OMXCodec.h revision ea7d15629752256f6ea1d5c6ea796e59aefd030f
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;
30
31struct OMXCodec : public MediaSource,
32                  public MediaBufferObserver {
33    static sp<OMXCodec> Create(
34            const sp<IOMX> &omx,
35            const sp<MetaData> &meta, bool createEncoder,
36            const sp<MediaSource> &source);
37
38    virtual status_t start(MetaData *params = NULL);
39    virtual status_t stop();
40
41    virtual sp<MetaData> getFormat();
42
43    virtual status_t read(
44            MediaBuffer **buffer, const ReadOptions *options = NULL);
45
46    void on_message(const omx_message &msg);
47
48    // from MediaBufferObserver
49    virtual void signalBufferReturned(MediaBuffer *buffer);
50
51protected:
52    virtual ~OMXCodec();
53
54private:
55    enum State {
56        DEAD,
57        LOADED,
58        LOADED_TO_IDLE,
59        IDLE_TO_EXECUTING,
60        EXECUTING,
61        EXECUTING_TO_IDLE,
62        IDLE_TO_LOADED,
63        RECONFIGURING,
64        ERROR
65    };
66
67    enum {
68        kPortIndexInput  = 0,
69        kPortIndexOutput = 1
70    };
71
72    enum PortStatus {
73        ENABLED,
74        DISABLING,
75        DISABLED,
76        ENABLING,
77        SHUTTING_DOWN,
78    };
79
80    enum Quirks {
81        kNeedsFlushBeforeDisable             = 1,
82        kWantsNALFragments                   = 2,
83        kRequiresLoadedToIdleAfterAllocation = 4,
84        kRequiresAllocateBufferOnInputPorts  = 8,
85        kRequiresFlushCompleteEmulation      = 16,
86        kRequiresAllocateBufferOnOutputPorts = 32,
87    };
88
89    struct BufferInfo {
90        IOMX::buffer_id mBuffer;
91        bool mOwnedByComponent;
92        sp<IMemory> mMem;
93        MediaBuffer *mMediaBuffer;
94    };
95
96    struct CodecSpecificData {
97        size_t mSize;
98        uint8_t mData[1];
99    };
100
101    sp<IOMX> mOMX;
102    IOMX::node_id mNode;
103    sp<OMXCodecObserver> mObserver;
104    uint32_t mQuirks;
105    bool mIsEncoder;
106    char *mMIME;
107    char *mComponentName;
108    sp<MetaData> mOutputFormat;
109    sp<MediaSource> mSource;
110    Vector<CodecSpecificData *> mCodecSpecificData;
111    size_t mCodecSpecificDataIndex;
112
113    sp<MemoryDealer> mDealer[2];
114
115    State mState;
116    Vector<BufferInfo> mPortBuffers[2];
117    PortStatus mPortStatus[2];
118    bool mSignalledEOS;
119    bool mNoMoreOutputData;
120    int64_t mSeekTimeUs;
121
122    Mutex mLock;
123    Condition mAsyncCompletion;
124
125    // A list of indices into mPortStatus[kPortIndexOutput] filled with data.
126    List<size_t> mFilledBuffers;
127    Condition mBufferFilled;
128
129    OMXCodec(const sp<IOMX> &omx, IOMX::node_id node, uint32_t quirks,
130             bool isEncoder, const char *mime, const char *componentName,
131             const sp<MediaSource> &source);
132
133    void addCodecSpecificData(const void *data, size_t size);
134    void clearCodecSpecificData();
135
136    void setAMRFormat();
137    void setAACFormat();
138
139    status_t setVideoPortFormatType(
140            OMX_U32 portIndex,
141            OMX_VIDEO_CODINGTYPE compressionFormat,
142            OMX_COLOR_FORMATTYPE colorFormat);
143
144    void setVideoInputFormat(
145            const char *mime, OMX_U32 width, OMX_U32 height);
146
147    void setVideoOutputFormat(
148            const char *mime, OMX_U32 width, OMX_U32 height);
149
150    void setImageOutputFormat(
151            OMX_COLOR_FORMATTYPE format, OMX_U32 width, OMX_U32 height);
152
153    void setJPEGInputFormat(
154            OMX_U32 width, OMX_U32 height, OMX_U32 compressedSize);
155
156    status_t allocateBuffers();
157    status_t allocateBuffersOnPort(OMX_U32 portIndex);
158
159    status_t freeBuffersOnPort(
160            OMX_U32 portIndex, bool onlyThoseWeOwn = false);
161
162    void drainInputBuffer(IOMX::buffer_id buffer);
163    void fillOutputBuffer(IOMX::buffer_id buffer);
164    void drainInputBuffer(BufferInfo *info);
165    void fillOutputBuffer(BufferInfo *info);
166
167    void drainInputBuffers();
168    void fillOutputBuffers();
169
170    // Returns true iff a flush was initiated and a completion event is
171    // upcoming, false otherwise (A flush was not necessary as we own all
172    // the buffers on that port).
173    // This method will ONLY ever return false for a component with quirk
174    // "kRequiresFlushCompleteEmulation".
175    bool flushPortAsync(OMX_U32 portIndex);
176
177    void disablePortAsync(OMX_U32 portIndex);
178    void enablePortAsync(OMX_U32 portIndex);
179
180    static size_t countBuffersWeOwn(const Vector<BufferInfo> &buffers);
181    static bool isIntermediateState(State state);
182
183    void onEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2);
184    void onCmdComplete(OMX_COMMANDTYPE cmd, OMX_U32 data);
185    void onStateChange(OMX_STATETYPE newState);
186    void onPortSettingsChanged(OMX_U32 portIndex);
187
188    void setState(State newState);
189
190    status_t init();
191    void initOutputFormat(const sp<MetaData> &inputFormat);
192
193    void dumpPortStatus(OMX_U32 portIndex);
194
195    OMXCodec(const OMXCodec &);
196    OMXCodec &operator=(const OMXCodec &);
197};
198
199}  // namespace android
200
201#endif  // OMX_CODEC_H_
202