blob: 392ea87bc033d4c3fba9c9f3099d55beeb3a1182 [file] [log] [blame]
Andreas Huberbe06d262009-08-14 14:37:10 -07001/*
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
Jamie Gennis58a36ad2010-10-07 14:08:38 -070021#include <android/native_window.h>
Andreas Huberbe06d262009-08-14 14:37:10 -070022#include <media/IOMX.h>
23#include <media/stagefright/MediaBuffer.h>
24#include <media/stagefright/MediaSource.h>
25#include <utils/threads.h>
26
27namespace android {
28
Andreas Huber3d3864f2012-02-29 15:47:17 -080029struct MediaCodecList;
Andreas Huberbe06d262009-08-14 14:37:10 -070030class MemoryDealer;
31struct OMXCodecObserver;
James Dong81c929a2010-07-01 15:02:14 -070032struct CodecProfileLevel;
Andreas Huberbe06d262009-08-14 14:37:10 -070033
34struct OMXCodec : public MediaSource,
35 public MediaBufferObserver {
Andreas Hubere13526a2009-10-22 10:43:34 -070036 enum CreationFlags {
Andreas Huber4c19bf92010-09-08 14:32:20 -070037 kPreferSoftwareCodecs = 1,
Andreas Huber5a40e392010-10-18 09:57:42 -070038 kIgnoreCodecSpecificData = 2,
39
40 // The client wants to access the output buffer's video
41 // data for example for thumbnail extraction.
42 kClientNeedsFramebuffer = 4,
James Dong170a9292010-10-22 17:28:15 -070043
44 // Request for software or hardware codecs. If request
45 // can not be fullfilled, Create() returns NULL.
46 kSoftwareCodecsOnly = 8,
47 kHardwareCodecsOnly = 16,
James Dong05c2fd52010-11-02 13:20:11 -070048
49 // Store meta data in video buffers
50 kStoreMetaDataInVideoBuffers = 32,
James Dong5f3ab062011-01-25 16:31:28 -080051
52 // Only submit one input buffer at one time.
53 kOnlySubmitOneInputBufferAtOneTime = 64,
Glenn Kastenb8763f62011-01-28 12:37:51 -080054
55 // Enable GRALLOC_USAGE_PROTECTED for output buffers from native window
56 kEnableGrallocUsageProtected = 128,
Andreas Huber42fb5d62011-06-29 15:53:28 -070057
58 // Secure decoding mode
59 kUseSecureInputBuffers = 256,
Andreas Hubere13526a2009-10-22 10:43:34 -070060 };
Andreas Huber91eb0352009-12-07 09:43:00 -080061 static sp<MediaSource> Create(
Andreas Huberbe06d262009-08-14 14:37:10 -070062 const sp<IOMX> &omx,
63 const sp<MetaData> &meta, bool createEncoder,
Andreas Hubere6c40962009-09-10 14:13:30 -070064 const sp<MediaSource> &source,
Andreas Hubere13526a2009-10-22 10:43:34 -070065 const char *matchComponentName = NULL,
Jamie Gennis58a36ad2010-10-07 14:08:38 -070066 uint32_t flags = 0,
67 const sp<ANativeWindow> &nativeWindow = NULL);
Andreas Hubere6c40962009-09-10 14:13:30 -070068
69 static void setComponentRole(
70 const sp<IOMX> &omx, IOMX::node_id node, bool isEncoder,
71 const char *mime);
Andreas Huberbe06d262009-08-14 14:37:10 -070072
73 virtual status_t start(MetaData *params = NULL);
74 virtual status_t stop();
75
76 virtual sp<MetaData> getFormat();
77
78 virtual status_t read(
79 MediaBuffer **buffer, const ReadOptions *options = NULL);
80
Andreas Huber1f24b302010-06-10 11:12:39 -070081 virtual status_t pause();
82
Andreas Huberbe06d262009-08-14 14:37:10 -070083 // from MediaBufferObserver
84 virtual void signalBufferReturned(MediaBuffer *buffer);
85
Andreas Huber3d3864f2012-02-29 15:47:17 -080086 enum Quirks {
87 kNeedsFlushBeforeDisable = 1,
88 kWantsNALFragments = 2,
89 kRequiresLoadedToIdleAfterAllocation = 4,
90 kRequiresAllocateBufferOnInputPorts = 8,
91 kRequiresFlushCompleteEmulation = 16,
92 kRequiresAllocateBufferOnOutputPorts = 32,
93 kRequiresFlushBeforeShutdown = 64,
94 kDefersOutputBufferAllocation = 128,
95 kDecoderLiesAboutNumberOfChannels = 256,
96 kInputBufferSizesAreBogus = 512,
97 kSupportsMultipleFramesPerInputBuffer = 1024,
98 kAvoidMemcopyInputRecordingFrames = 2048,
99 kRequiresLargerEncoderOutputBuffer = 4096,
100 kOutputBuffersAreUnreadable = 8192,
101 };
102
Andreas Hubere366f522011-06-28 10:51:41 -0700103 // for use by ACodec
104 static void findMatchingCodecs(
105 const char *mime,
106 bool createEncoder, const char *matchComponentName,
107 uint32_t flags,
Andreas Huber3d3864f2012-02-29 15:47:17 -0800108 Vector<String8> *matchingCodecs,
109 Vector<uint32_t> *matchingCodecQuirks = NULL);
110
111 static uint32_t getComponentQuirks(
112 const MediaCodecList *list, size_t index);
113
114 static bool findCodecQuirks(const char *componentName, uint32_t *quirks);
Andreas Hubere366f522011-06-28 10:51:41 -0700115
Andreas Huberbe06d262009-08-14 14:37:10 -0700116protected:
117 virtual ~OMXCodec();
118
119private:
James Dong681e89c2011-01-10 08:55:02 -0800120
121 // Make sure mLock is accessible to OMXCodecObserver
122 friend class OMXCodecObserver;
123
124 // Call this with mLock hold
125 void on_message(const omx_message &msg);
126
Andreas Huberbe06d262009-08-14 14:37:10 -0700127 enum State {
128 DEAD,
129 LOADED,
130 LOADED_TO_IDLE,
131 IDLE_TO_EXECUTING,
132 EXECUTING,
133 EXECUTING_TO_IDLE,
134 IDLE_TO_LOADED,
135 RECONFIGURING,
136 ERROR
137 };
138
139 enum {
140 kPortIndexInput = 0,
141 kPortIndexOutput = 1
142 };
143
144 enum PortStatus {
145 ENABLED,
146 DISABLING,
147 DISABLED,
148 ENABLING,
149 SHUTTING_DOWN,
150 };
151
Andreas Huberbbbcf652010-12-07 14:25:54 -0800152 enum BufferStatus {
153 OWNED_BY_US,
154 OWNED_BY_COMPONENT,
155 OWNED_BY_NATIVE_WINDOW,
156 OWNED_BY_CLIENT,
157 };
158
Andreas Huberbe06d262009-08-14 14:37:10 -0700159 struct BufferInfo {
160 IOMX::buffer_id mBuffer;
Andreas Huberbbbcf652010-12-07 14:25:54 -0800161 BufferStatus mStatus;
Andreas Huberbe06d262009-08-14 14:37:10 -0700162 sp<IMemory> mMem;
Andreas Huberc712b9f2010-01-20 15:05:46 -0800163 size_t mSize;
164 void *mData;
Andreas Huberbe06d262009-08-14 14:37:10 -0700165 MediaBuffer *mMediaBuffer;
166 };
167
168 struct CodecSpecificData {
169 size_t mSize;
170 uint8_t mData[1];
171 };
172
173 sp<IOMX> mOMX;
Andreas Huberf1fe0642010-01-15 15:28:19 -0800174 bool mOMXLivesLocally;
Andreas Huberbe06d262009-08-14 14:37:10 -0700175 IOMX::node_id mNode;
Andreas Huberbe06d262009-08-14 14:37:10 -0700176 uint32_t mQuirks;
Andreas Huber42fb5d62011-06-29 15:53:28 -0700177
178 // Flags specified in the creation of the codec.
179 uint32_t mFlags;
180
Andreas Huberbe06d262009-08-14 14:37:10 -0700181 bool mIsEncoder;
Andreas Huberafe02df2012-01-26 14:39:50 -0800182 bool mIsVideo;
Andreas Huberbe06d262009-08-14 14:37:10 -0700183 char *mMIME;
184 char *mComponentName;
185 sp<MetaData> mOutputFormat;
186 sp<MediaSource> mSource;
187 Vector<CodecSpecificData *> mCodecSpecificData;
188 size_t mCodecSpecificDataIndex;
189
Andreas Huber5c0a9132009-08-20 11:16:40 -0700190 sp<MemoryDealer> mDealer[2];
Andreas Huberbe06d262009-08-14 14:37:10 -0700191
192 State mState;
193 Vector<BufferInfo> mPortBuffers[2];
194 PortStatus mPortStatus[2];
Andreas Huber42978e52009-08-27 10:08:39 -0700195 bool mInitialBufferSubmit;
Andreas Huberbe06d262009-08-14 14:37:10 -0700196 bool mSignalledEOS;
Andreas Huberd7d22eb2010-02-23 13:45:33 -0800197 status_t mFinalStatus;
Andreas Huberbe06d262009-08-14 14:37:10 -0700198 bool mNoMoreOutputData;
Andreas Hubercfd55572009-10-09 14:11:28 -0700199 bool mOutputPortSettingsHaveChanged;
Andreas Huberbe06d262009-08-14 14:37:10 -0700200 int64_t mSeekTimeUs;
Andreas Huber6624c9f2010-07-20 15:04:28 -0700201 ReadOptions::SeekMode mSeekMode;
202 int64_t mTargetTimeUs;
Andreas Huberb9289832011-02-08 13:10:25 -0800203 bool mOutputPortSettingsChangedPending;
Andreas Huberbe06d262009-08-14 14:37:10 -0700204
Andreas Hubera4357ad2010-04-02 12:49:54 -0700205 MediaBuffer *mLeftOverBuffer;
206
Andreas Huberbe06d262009-08-14 14:37:10 -0700207 Mutex mLock;
208 Condition mAsyncCompletion;
209
Andreas Huber1f24b302010-06-10 11:12:39 -0700210 bool mPaused;
211
Jamie Gennis58a36ad2010-10-07 14:08:38 -0700212 sp<ANativeWindow> mNativeWindow;
213
214 // The index in each of the mPortBuffers arrays of the buffer that will be
215 // submitted to OMX next. This only applies when using buffers from a
216 // native window.
217 size_t mNextNativeBufferIndex[2];
218
Andreas Huberbe06d262009-08-14 14:37:10 -0700219 // A list of indices into mPortStatus[kPortIndexOutput] filled with data.
220 List<size_t> mFilledBuffers;
221 Condition mBufferFilled;
222
James Dong4108b1e2011-06-07 19:45:54 -0700223 // Used to record the decoding time for an output picture from
224 // a video encoder.
225 List<int64_t> mDecodingTimeList;
226
Andreas Huber42fb5d62011-06-29 15:53:28 -0700227 OMXCodec(const sp<IOMX> &omx, IOMX::node_id node,
228 uint32_t quirks, uint32_t flags,
Andreas Huberbe06d262009-08-14 14:37:10 -0700229 bool isEncoder, const char *mime, const char *componentName,
Jamie Gennis58a36ad2010-10-07 14:08:38 -0700230 const sp<MediaSource> &source,
231 const sp<ANativeWindow> &nativeWindow);
Andreas Huberbe06d262009-08-14 14:37:10 -0700232
233 void addCodecSpecificData(const void *data, size_t size);
234 void clearCodecSpecificData();
235
Andreas Huber4c483422009-09-02 16:05:36 -0700236 void setComponentRole();
237
James Dong17299ab2010-05-14 15:45:22 -0700238 void setAMRFormat(bool isWAMR, int32_t bitRate);
Gilles-Arnaud Bleu-Laine9a6ed362011-09-15 21:30:13 -0500239 status_t setAACFormat(int32_t numChannels, int32_t sampleRate, int32_t bitRate);
Andreas Huber4b3913a2011-05-11 14:13:42 -0700240 void setG711Format(int32_t numChannels);
Andreas Huberbe06d262009-08-14 14:37:10 -0700241
242 status_t setVideoPortFormatType(
243 OMX_U32 portIndex,
244 OMX_VIDEO_CODINGTYPE compressionFormat,
245 OMX_COLOR_FORMATTYPE colorFormat);
246
247 void setVideoInputFormat(
James Dong1244eab2010-06-08 11:58:53 -0700248 const char *mime, const sp<MetaData>& meta);
Andreas Huberbe06d262009-08-14 14:37:10 -0700249
James Dongc0ab2a62010-06-29 16:29:19 -0700250 status_t setupBitRate(int32_t bitRate);
251 status_t setupErrorCorrectionParameters();
252 status_t setupH263EncoderParameters(const sp<MetaData>& meta);
James Dong1244eab2010-06-08 11:58:53 -0700253 status_t setupMPEG4EncoderParameters(const sp<MetaData>& meta);
254 status_t setupAVCEncoderParameters(const sp<MetaData>& meta);
James Dongafd97e82010-08-03 17:19:23 -0700255 status_t findTargetColorFormat(
256 const sp<MetaData>& meta, OMX_COLOR_FORMATTYPE *colorFormat);
257
258 status_t isColorFormatSupported(
259 OMX_COLOR_FORMATTYPE colorFormat, int portIndex);
Andreas Huberb482ce82009-10-29 12:02:48 -0700260
James Dong81c929a2010-07-01 15:02:14 -0700261 // If profile/level is set in the meta data, its value in the meta
262 // data will be used; otherwise, the default value will be used.
263 status_t getVideoProfileLevel(const sp<MetaData>& meta,
264 const CodecProfileLevel& defaultProfileLevel,
265 CodecProfileLevel& profileLevel);
266
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700267 status_t setVideoOutputFormat(
Andreas Huberbe06d262009-08-14 14:37:10 -0700268 const char *mime, OMX_U32 width, OMX_U32 height);
269
270 void setImageOutputFormat(
271 OMX_COLOR_FORMATTYPE format, OMX_U32 width, OMX_U32 height);
272
Andreas Huber5c0a9132009-08-20 11:16:40 -0700273 void setJPEGInputFormat(
274 OMX_U32 width, OMX_U32 height, OMX_U32 compressedSize);
275
Andreas Huberda050cf2009-09-02 14:01:43 -0700276 void setMinBufferSize(OMX_U32 portIndex, OMX_U32 size);
277
278 void setRawAudioFormat(
279 OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels);
280
Andreas Huberbe06d262009-08-14 14:37:10 -0700281 status_t allocateBuffers();
282 status_t allocateBuffersOnPort(OMX_U32 portIndex);
Jamie Gennis58a36ad2010-10-07 14:08:38 -0700283 status_t allocateOutputBuffersFromNativeWindow();
284
285 status_t queueBufferToNativeWindow(BufferInfo *info);
286 status_t cancelBufferToNativeWindow(BufferInfo *info);
287 BufferInfo* dequeueBufferFromNativeWindow();
Jamie Gennisc0e42932011-10-25 14:50:16 -0700288 status_t pushBlankBuffersToNativeWindow();
Andreas Huberbe06d262009-08-14 14:37:10 -0700289
290 status_t freeBuffersOnPort(
291 OMX_U32 portIndex, bool onlyThoseWeOwn = false);
292
Jamie Gennisf0c5c1e2010-11-01 16:04:31 -0700293 status_t freeBuffer(OMX_U32 portIndex, size_t bufIndex);
294
Andreas Huberbbbcf652010-12-07 14:25:54 -0800295 bool drainInputBuffer(IOMX::buffer_id buffer);
Andreas Huberbe06d262009-08-14 14:37:10 -0700296 void fillOutputBuffer(IOMX::buffer_id buffer);
Andreas Huberbbbcf652010-12-07 14:25:54 -0800297 bool drainInputBuffer(BufferInfo *info);
Andreas Huberbe06d262009-08-14 14:37:10 -0700298 void fillOutputBuffer(BufferInfo *info);
299
300 void drainInputBuffers();
301 void fillOutputBuffers();
302
Andreas Huber42fb5d62011-06-29 15:53:28 -0700303 bool drainAnyInputBuffer();
304 BufferInfo *findInputBufferByDataPointer(void *ptr);
305 BufferInfo *findEmptyInputBuffer();
306
Andreas Huber404cc412009-08-25 14:26:05 -0700307 // Returns true iff a flush was initiated and a completion event is
308 // upcoming, false otherwise (A flush was not necessary as we own all
309 // the buffers on that port).
310 // This method will ONLY ever return false for a component with quirk
311 // "kRequiresFlushCompleteEmulation".
312 bool flushPortAsync(OMX_U32 portIndex);
313
Andreas Huberbe06d262009-08-14 14:37:10 -0700314 void disablePortAsync(OMX_U32 portIndex);
James Dong0209da12011-09-12 19:56:23 -0700315 status_t enablePortAsync(OMX_U32 portIndex);
Andreas Huberbe06d262009-08-14 14:37:10 -0700316
317 static size_t countBuffersWeOwn(const Vector<BufferInfo> &buffers);
318 static bool isIntermediateState(State state);
319
320 void onEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2);
321 void onCmdComplete(OMX_COMMANDTYPE cmd, OMX_U32 data);
322 void onStateChange(OMX_STATETYPE newState);
323 void onPortSettingsChanged(OMX_U32 portIndex);
324
325 void setState(State newState);
326
327 status_t init();
328 void initOutputFormat(const sp<MetaData> &inputFormat);
Jamie Gennis58a36ad2010-10-07 14:08:38 -0700329 status_t initNativeWindow();
Andreas Huberbe06d262009-08-14 14:37:10 -0700330
Lakshman Gowdadb62a242011-09-29 17:47:35 -0700331 void initNativeWindowCrop();
332
Andreas Huberbe06d262009-08-14 14:37:10 -0700333 void dumpPortStatus(OMX_U32 portIndex);
334
Andreas Huber42fb5d62011-06-29 15:53:28 -0700335 status_t configureCodec(const sp<MetaData> &meta);
Andreas Huber2a09c7e2010-03-16 11:44:07 -0700336
James Dong31b93752010-11-10 21:11:41 -0800337 void restorePatchedDataPointer(BufferInfo *info);
338
Andreas Huber5e9dc942011-01-21 14:32:31 -0800339 status_t applyRotation();
James Dongd9ac6212011-07-15 15:25:36 -0700340 status_t waitForBufferFilled_l();
Andreas Huber5e9dc942011-01-21 14:32:31 -0800341
James Dong72516732012-02-06 23:46:37 -0800342 int64_t getDecodingTimeUs();
James Dong4108b1e2011-06-07 19:45:54 -0700343
Andreas Huber0ba86602011-11-18 12:22:59 -0800344 status_t parseAVCCodecSpecificData(
345 const void *data, size_t size,
346 unsigned *profile, unsigned *level);
347
Andreas Huberbe06d262009-08-14 14:37:10 -0700348 OMXCodec(const OMXCodec &);
349 OMXCodec &operator=(const OMXCodec &);
350};
351
Andreas Hubere6c40962009-09-10 14:13:30 -0700352struct CodecCapabilities {
353 String8 mComponentName;
354 Vector<CodecProfileLevel> mProfileLevels;
James Dongd7810892010-11-11 00:33:05 -0800355 Vector<OMX_U32> mColorFormats;
Andreas Hubere6c40962009-09-10 14:13:30 -0700356};
357
358// Return a vector of componentNames with supported profile/level pairs
359// supporting the given mime type, if queryDecoders==true, returns components
360// that decode content of the given type, otherwise returns components
361// that encode content of the given type.
362// profile and level indications only make sense for h.263, mpeg4 and avc
363// video.
Jean-Michel Trivia05f0992011-07-22 09:52:39 -0700364// If hwCodecOnly==true, only returns hardware-based components, software and
365// hardware otherwise.
Andreas Hubere6c40962009-09-10 14:13:30 -0700366// The profile/level values correspond to
367// OMX_VIDEO_H263PROFILETYPE, OMX_VIDEO_MPEG4PROFILETYPE,
368// OMX_VIDEO_AVCPROFILETYPE, OMX_VIDEO_H263LEVELTYPE, OMX_VIDEO_MPEG4LEVELTYPE
369// and OMX_VIDEO_AVCLEVELTYPE respectively.
370
371status_t QueryCodecs(
372 const sp<IOMX> &omx,
Jean-Michel Trivi56a37b02011-07-17 16:35:11 -0700373 const char *mimeType, bool queryDecoders, bool hwCodecOnly,
Andreas Hubere6c40962009-09-10 14:13:30 -0700374 Vector<CodecCapabilities> *results);
375
Jean-Michel Trivia05f0992011-07-22 09:52:39 -0700376status_t QueryCodecs(
377 const sp<IOMX> &omx,
378 const char *mimeType, bool queryDecoders,
379 Vector<CodecCapabilities> *results);
380
Andreas Huberbe06d262009-08-14 14:37:10 -0700381} // namespace android
382
383#endif // OMX_CODEC_H_