OMXCodec.h revision 693d271e62a3726689ff68f4505ba49228eb94b2
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        kWantsRawNALFrames                   = 2,
83        kRequiresLoadedToIdleAfterAllocation = 4,
84        kRequiresAllocateBufferOnInputPorts  = 8,
85    };
86
87    struct BufferInfo {
88        IOMX::buffer_id mBuffer;
89        bool mOwnedByComponent;
90        sp<IMemory> mMem;
91        MediaBuffer *mMediaBuffer;
92    };
93
94    struct CodecSpecificData {
95        size_t mSize;
96        uint8_t mData[1];
97    };
98
99    sp<IOMX> mOMX;
100    IOMX::node_id mNode;
101    sp<OMXCodecObserver> mObserver;
102    uint32_t mQuirks;
103    bool mIsEncoder;
104    char *mMIME;
105    char *mComponentName;
106    sp<MetaData> mOutputFormat;
107    sp<MediaSource> mSource;
108    Vector<CodecSpecificData *> mCodecSpecificData;
109    size_t mCodecSpecificDataIndex;
110
111    sp<MemoryDealer> mDealer;
112
113    State mState;
114    Vector<BufferInfo> mPortBuffers[2];
115    PortStatus mPortStatus[2];
116    bool mSignalledEOS;
117    bool mNoMoreOutputData;
118    int64_t mSeekTimeUs;
119
120    Mutex mLock;
121    Condition mAsyncCompletion;
122
123    // A list of indices into mPortStatus[kPortIndexOutput] filled with data.
124    List<size_t> mFilledBuffers;
125    Condition mBufferFilled;
126
127    OMXCodec(const sp<IOMX> &omx, IOMX::node_id node, uint32_t quirks,
128             bool isEncoder, const char *mime, const char *componentName,
129             const sp<MediaSource> &source);
130
131    void addCodecSpecificData(const void *data, size_t size);
132    void clearCodecSpecificData();
133
134    void setAMRFormat();
135    void setAACFormat();
136
137    status_t setVideoPortFormatType(
138            OMX_U32 portIndex,
139            OMX_VIDEO_CODINGTYPE compressionFormat,
140            OMX_COLOR_FORMATTYPE colorFormat);
141
142    void setVideoInputFormat(
143            const char *mime, OMX_U32 width, OMX_U32 height);
144
145    void setVideoOutputFormat(
146            const char *mime, OMX_U32 width, OMX_U32 height);
147
148    void setImageOutputFormat(
149            OMX_COLOR_FORMATTYPE format, OMX_U32 width, OMX_U32 height);
150
151    status_t allocateBuffers();
152    status_t allocateBuffersOnPort(OMX_U32 portIndex);
153
154    status_t freeBuffersOnPort(
155            OMX_U32 portIndex, bool onlyThoseWeOwn = false);
156
157    void drainInputBuffer(IOMX::buffer_id buffer);
158    void fillOutputBuffer(IOMX::buffer_id buffer);
159    void drainInputBuffer(BufferInfo *info);
160    void fillOutputBuffer(BufferInfo *info);
161
162    void drainInputBuffers();
163    void fillOutputBuffers();
164
165    void flushPortAsync(OMX_U32 portIndex);
166    void disablePortAsync(OMX_U32 portIndex);
167    void enablePortAsync(OMX_U32 portIndex);
168
169    static size_t countBuffersWeOwn(const Vector<BufferInfo> &buffers);
170    static bool isIntermediateState(State state);
171
172    void onEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2);
173    void onCmdComplete(OMX_COMMANDTYPE cmd, OMX_U32 data);
174    void onStateChange(OMX_STATETYPE newState);
175    void onPortSettingsChanged(OMX_U32 portIndex);
176
177    void setState(State newState);
178
179    status_t init();
180    void initOutputFormat(const sp<MetaData> &inputFormat);
181
182    void dumpPortStatus(OMX_U32 portIndex);
183
184    OMXCodec(const OMXCodec &);
185    OMXCodec &operator=(const OMXCodec &);
186};
187
188}  // namespace android
189
190#endif  // OMX_CODEC_H_
191