2 * Copyright (C) 2009 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <media/IOMX.h>
22 #include <media/stagefright/MediaBuffer.h>
23 #include <media/stagefright/MediaSource.h>
24 #include <utils/threads.h>
29 struct OMXCodecObserver;
30 struct CodecProfileLevel;
32 struct OMXCodec : public MediaSource,
33 public MediaBufferObserver {
35 kPreferSoftwareCodecs = 1,
36 kIgnoreCodecSpecificData = 2,
38 // The client wants to access the output buffer's video
39 // data for example for thumbnail extraction.
40 kClientNeedsFramebuffer = 4,
42 static sp<MediaSource> Create(
44 const sp<MetaData> &meta, bool createEncoder,
45 const sp<MediaSource> &source,
46 const char *matchComponentName = NULL,
49 static void setComponentRole(
50 const sp<IOMX> &omx, IOMX::node_id node, bool isEncoder,
53 virtual status_t start(MetaData *params = NULL);
54 virtual status_t stop();
56 virtual sp<MetaData> getFormat();
58 virtual status_t read(
59 MediaBuffer **buffer, const ReadOptions *options = NULL);
61 virtual status_t pause();
63 // from MediaBufferObserver
64 virtual void signalBufferReturned(MediaBuffer *buffer);
71 // Make sure mLock is accessible to OMXCodecObserver
72 friend class OMXCodecObserver;
74 // Call this with mLock hold
75 void on_message(const omx_message &msg);
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,
121 IOMX::buffer_id mBuffer;
122 bool mOwnedByComponent;
126 MediaBuffer *mMediaBuffer;
129 struct CodecSpecificData {
135 bool mOMXLivesLocally;
140 char *mComponentName;
141 sp<MetaData> mOutputFormat;
142 sp<MediaSource> mSource;
143 Vector<CodecSpecificData *> mCodecSpecificData;
144 size_t mCodecSpecificDataIndex;
146 sp<MemoryDealer> mDealer[2];
149 Vector<BufferInfo> mPortBuffers[2];
150 PortStatus mPortStatus[2];
151 bool mInitialBufferSubmit;
153 status_t mFinalStatus;
154 bool mNoMoreOutputData;
155 bool mOutputPortSettingsHaveChanged;
157 ReadOptions::SeekMode mSeekMode;
158 int64_t mTargetTimeUs;
161 MediaBuffer *mLeftOverBuffer;
164 Condition mAsyncCompletion;
168 // A list of indices into mPortStatus[kPortIndexOutput] filled with data.
169 List<size_t> mFilledBuffers;
170 Condition mBufferFilled;
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);
176 void addCodecSpecificData(const void *data, size_t size);
177 void clearCodecSpecificData();
179 void setComponentRole();
181 void setAMRFormat(bool isWAMR, int32_t bitRate);
182 void setAACFormat(int32_t numChannels, int32_t sampleRate, int32_t bitRate);
184 status_t setVideoPortFormatType(
186 OMX_VIDEO_CODINGTYPE compressionFormat,
187 OMX_COLOR_FORMATTYPE colorFormat);
189 void setVideoInputFormat(
190 const char *mime, const sp<MetaData>& meta);
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);
200 status_t isColorFormatSupported(
201 OMX_COLOR_FORMATTYPE colorFormat, int portIndex);
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);
209 status_t setVideoOutputFormat(
210 const char *mime, OMX_U32 width, OMX_U32 height);
212 void setImageOutputFormat(
213 OMX_COLOR_FORMATTYPE format, OMX_U32 width, OMX_U32 height);
215 void setJPEGInputFormat(
216 OMX_U32 width, OMX_U32 height, OMX_U32 compressedSize);
218 void setMinBufferSize(OMX_U32 portIndex, OMX_U32 size);
220 void setRawAudioFormat(
221 OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels);
223 status_t allocateBuffers();
224 status_t allocateBuffersOnPort(OMX_U32 portIndex);
226 status_t freeBuffersOnPort(
227 OMX_U32 portIndex, bool onlyThoseWeOwn = false);
229 void drainInputBuffer(IOMX::buffer_id buffer);
230 void fillOutputBuffer(IOMX::buffer_id buffer);
231 void drainInputBuffer(BufferInfo *info);
232 void fillOutputBuffer(BufferInfo *info);
234 void drainInputBuffers();
235 void fillOutputBuffers();
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);
244 void disablePortAsync(OMX_U32 portIndex);
245 void enablePortAsync(OMX_U32 portIndex);
247 static size_t countBuffersWeOwn(const Vector<BufferInfo> &buffers);
248 static bool isIntermediateState(State state);
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);
255 void setState(State newState);
258 void initOutputFormat(const sp<MetaData> &inputFormat);
260 void dumpPortStatus(OMX_U32 portIndex);
262 status_t configureCodec(const sp<MetaData> &meta, uint32_t flags);
264 static uint32_t getComponentQuirks(
265 const char *componentName, bool isEncoder);
267 static void findMatchingCodecs(
269 bool createEncoder, const char *matchComponentName,
271 Vector<String8> *matchingCodecs);
273 OMXCodec(const OMXCodec &);
274 OMXCodec &operator=(const OMXCodec &);
277 struct CodecProfileLevel {
282 struct CodecCapabilities {
283 String8 mComponentName;
284 Vector<CodecProfileLevel> mProfileLevels;
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
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.
298 status_t QueryCodecs(
300 const char *mimeType, bool queryDecoders,
301 Vector<CodecCapabilities> *results);
303 } // namespace android
305 #endif // OMX_CODEC_H_