• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
26 namespace android {
27 
28 class MemoryDealer;
29 struct OMXCodecObserver;
30 
31 struct 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             const char *matchComponentName = NULL);
38 
39     static void setComponentRole(
40             const sp<IOMX> &omx, IOMX::node_id node, bool isEncoder,
41             const char *mime);
42 
43     virtual status_t start(MetaData *params = NULL);
44     virtual status_t stop();
45 
46     virtual sp<MetaData> getFormat();
47 
48     virtual status_t read(
49             MediaBuffer **buffer, const ReadOptions *options = NULL);
50 
51     void on_message(const omx_message &msg);
52 
53     // from MediaBufferObserver
54     virtual void signalBufferReturned(MediaBuffer *buffer);
55 
56 protected:
57     virtual ~OMXCodec();
58 
59 private:
60     enum State {
61         DEAD,
62         LOADED,
63         LOADED_TO_IDLE,
64         IDLE_TO_EXECUTING,
65         EXECUTING,
66         EXECUTING_TO_IDLE,
67         IDLE_TO_LOADED,
68         RECONFIGURING,
69         ERROR
70     };
71 
72     enum {
73         kPortIndexInput  = 0,
74         kPortIndexOutput = 1
75     };
76 
77     enum PortStatus {
78         ENABLED,
79         DISABLING,
80         DISABLED,
81         ENABLING,
82         SHUTTING_DOWN,
83     };
84 
85     enum Quirks {
86         kNeedsFlushBeforeDisable             = 1,
87         kWantsNALFragments                   = 2,
88         kRequiresLoadedToIdleAfterAllocation = 4,
89         kRequiresAllocateBufferOnInputPorts  = 8,
90         kRequiresFlushCompleteEmulation      = 16,
91         kRequiresAllocateBufferOnOutputPorts = 32,
92         kRequiresFlushBeforeShutdown         = 64,
93         kOutputDimensionsAre16Aligned        = 128,
94     };
95 
96     struct BufferInfo {
97         IOMX::buffer_id mBuffer;
98         bool mOwnedByComponent;
99         sp<IMemory> mMem;
100         MediaBuffer *mMediaBuffer;
101     };
102 
103     struct CodecSpecificData {
104         size_t mSize;
105         uint8_t mData[1];
106     };
107 
108     sp<IOMX> mOMX;
109     IOMX::node_id mNode;
110     sp<OMXCodecObserver> mObserver;
111     uint32_t mQuirks;
112     bool mIsEncoder;
113     char *mMIME;
114     char *mComponentName;
115     sp<MetaData> mOutputFormat;
116     sp<MediaSource> mSource;
117     Vector<CodecSpecificData *> mCodecSpecificData;
118     size_t mCodecSpecificDataIndex;
119 
120     sp<MemoryDealer> mDealer[2];
121 
122     State mState;
123     Vector<BufferInfo> mPortBuffers[2];
124     PortStatus mPortStatus[2];
125     bool mInitialBufferSubmit;
126     bool mSignalledEOS;
127     bool mNoMoreOutputData;
128     int64_t mSeekTimeUs;
129 
130     Mutex mLock;
131     Condition mAsyncCompletion;
132 
133     // A list of indices into mPortStatus[kPortIndexOutput] filled with data.
134     List<size_t> mFilledBuffers;
135     Condition mBufferFilled;
136 
137     OMXCodec(const sp<IOMX> &omx, IOMX::node_id node, uint32_t quirks,
138              bool isEncoder, const char *mime, const char *componentName,
139              const sp<MediaSource> &source);
140 
141     void addCodecSpecificData(const void *data, size_t size);
142     void clearCodecSpecificData();
143 
144     void setComponentRole();
145 
146     void setAMRFormat();
147     void setAMRWBFormat();
148     void setAACFormat(int32_t numChannels, int32_t sampleRate);
149 
150     status_t setVideoPortFormatType(
151             OMX_U32 portIndex,
152             OMX_VIDEO_CODINGTYPE compressionFormat,
153             OMX_COLOR_FORMATTYPE colorFormat);
154 
155     void setVideoInputFormat(
156             const char *mime, OMX_U32 width, OMX_U32 height);
157 
158     void setVideoOutputFormat(
159             const char *mime, OMX_U32 width, OMX_U32 height);
160 
161     void setImageOutputFormat(
162             OMX_COLOR_FORMATTYPE format, OMX_U32 width, OMX_U32 height);
163 
164     void setJPEGInputFormat(
165             OMX_U32 width, OMX_U32 height, OMX_U32 compressedSize);
166 
167     void setMinBufferSize(OMX_U32 portIndex, OMX_U32 size);
168 
169     void setRawAudioFormat(
170             OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels);
171 
172     status_t allocateBuffers();
173     status_t allocateBuffersOnPort(OMX_U32 portIndex);
174 
175     status_t freeBuffersOnPort(
176             OMX_U32 portIndex, bool onlyThoseWeOwn = false);
177 
178     void drainInputBuffer(IOMX::buffer_id buffer);
179     void fillOutputBuffer(IOMX::buffer_id buffer);
180     void drainInputBuffer(BufferInfo *info);
181     void fillOutputBuffer(BufferInfo *info);
182 
183     void drainInputBuffers();
184     void fillOutputBuffers();
185 
186     // Returns true iff a flush was initiated and a completion event is
187     // upcoming, false otherwise (A flush was not necessary as we own all
188     // the buffers on that port).
189     // This method will ONLY ever return false for a component with quirk
190     // "kRequiresFlushCompleteEmulation".
191     bool flushPortAsync(OMX_U32 portIndex);
192 
193     void disablePortAsync(OMX_U32 portIndex);
194     void enablePortAsync(OMX_U32 portIndex);
195 
196     static size_t countBuffersWeOwn(const Vector<BufferInfo> &buffers);
197     static bool isIntermediateState(State state);
198 
199     void onEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2);
200     void onCmdComplete(OMX_COMMANDTYPE cmd, OMX_U32 data);
201     void onStateChange(OMX_STATETYPE newState);
202     void onPortSettingsChanged(OMX_U32 portIndex);
203 
204     void setState(State newState);
205 
206     status_t init();
207     void initOutputFormat(const sp<MetaData> &inputFormat);
208 
209     void dumpPortStatus(OMX_U32 portIndex);
210 
211     OMXCodec(const OMXCodec &);
212     OMXCodec &operator=(const OMXCodec &);
213 };
214 
215 struct CodecProfileLevel {
216     OMX_U32 mProfile;
217     OMX_U32 mLevel;
218 };
219 
220 struct CodecCapabilities {
221     String8 mComponentName;
222     Vector<CodecProfileLevel> mProfileLevels;
223 };
224 
225 // Return a vector of componentNames with supported profile/level pairs
226 // supporting the given mime type, if queryDecoders==true, returns components
227 // that decode content of the given type, otherwise returns components
228 // that encode content of the given type.
229 // profile and level indications only make sense for h.263, mpeg4 and avc
230 // video.
231 // The profile/level values correspond to
232 // OMX_VIDEO_H263PROFILETYPE, OMX_VIDEO_MPEG4PROFILETYPE,
233 // OMX_VIDEO_AVCPROFILETYPE, OMX_VIDEO_H263LEVELTYPE, OMX_VIDEO_MPEG4LEVELTYPE
234 // and OMX_VIDEO_AVCLEVELTYPE respectively.
235 
236 status_t QueryCodecs(
237         const sp<IOMX> &omx,
238         const char *mimeType, bool queryDecoders,
239         Vector<CodecCapabilities> *results);
240 
241 }  // namespace android
242 
243 #endif  // OMX_CODEC_H_
244