• 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 <android/native_window.h>
22 #include <media/IOMX.h>
23 #include <media/stagefright/MediaBuffer.h>
24 #include <media/stagefright/MediaSource.h>
25 #include <utils/threads.h>
26 
27 #include <OMX_Audio.h>
28 
29 namespace android {
30 
31 struct MediaCodecInfo;
32 class MemoryDealer;
33 struct OMXCodecObserver;
34 struct CodecProfileLevel;
35 class SkipCutBuffer;
36 
37 struct OMXCodec : public MediaSource,
38                   public MediaBufferObserver {
39     enum CreationFlags {
40         kPreferSoftwareCodecs    = 1,
41         kIgnoreCodecSpecificData = 2,
42 
43         // Request for software or hardware codecs. If request
44         // can not be fullfilled, Create() returns NULL.
45         kSoftwareCodecsOnly      = 8,
46         kHardwareCodecsOnly      = 16,
47 
48         // Store meta data in video buffers
49         kStoreMetaDataInVideoBuffers = 32,
50 
51         // Only submit one input buffer at one time.
52         kOnlySubmitOneInputBufferAtOneTime = 64,
53 
54         // Enable GRALLOC_USAGE_PROTECTED for output buffers from native window
55         kEnableGrallocUsageProtected = 128,
56 
57         // Secure decoding mode
58         kUseSecureInputBuffers = 256,
59     };
60     static sp<MediaSource> Create(
61             const sp<IOMX> &omx,
62             const sp<MetaData> &meta, bool createEncoder,
63             const sp<MediaSource> &source,
64             const char *matchComponentName = NULL,
65             uint32_t flags = 0,
66             const sp<ANativeWindow> &nativeWindow = NULL);
67 
68     static void setComponentRole(
69             const sp<IOMX> &omx, IOMX::node_id node, bool isEncoder,
70             const char *mime);
71 
72     virtual status_t start(MetaData *params = NULL);
73     virtual status_t stop();
74 
75     virtual sp<MetaData> getFormat();
76 
77     virtual status_t read(
78             MediaBuffer **buffer, const ReadOptions *options = NULL);
79 
80     virtual status_t pause();
81 
82     // from MediaBufferObserver
83     virtual void signalBufferReturned(MediaBuffer *buffer);
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         kDefersOutputBufferAllocation         = 128,
94         kDecoderLiesAboutNumberOfChannels     = 256,
95         kInputBufferSizesAreBogus             = 512,
96         kSupportsMultipleFramesPerInputBuffer = 1024,
97         kRequiresLargerEncoderOutputBuffer    = 2048,
98         kOutputBuffersAreUnreadable           = 4096,
99     };
100 
101     struct CodecNameAndQuirks {
102         String8 mName;
103         uint32_t mQuirks;
104     };
105 
106     // for use by ACodec
107     static void findMatchingCodecs(
108             const char *mime,
109             bool createEncoder, const char *matchComponentName,
110             uint32_t flags,
111             Vector<CodecNameAndQuirks> *matchingCodecNamesAndQuirks);
112 
113     static uint32_t getComponentQuirks(
114             const sp<MediaCodecInfo> &list);
115 
116     static bool findCodecQuirks(const char *componentName, uint32_t *quirks);
117 
118 protected:
119     virtual ~OMXCodec();
120 
121 private:
122 
123     // Make sure mLock is accessible to OMXCodecObserver
124     friend class OMXCodecObserver;
125 
126     // Call this with mLock hold
127     void on_message(const omx_message &msg);
128 
129     enum State {
130         DEAD,
131         LOADED,
132         LOADED_TO_IDLE,
133         IDLE_TO_EXECUTING,
134         EXECUTING,
135         EXECUTING_TO_IDLE,
136         IDLE_TO_LOADED,
137         RECONFIGURING,
138         ERROR
139     };
140 
141     enum {
142         kPortIndexInput  = 0,
143         kPortIndexOutput = 1
144     };
145 
146     enum PortStatus {
147         ENABLED,
148         DISABLING,
149         DISABLED,
150         ENABLING,
151         SHUTTING_DOWN,
152     };
153 
154     enum BufferStatus {
155         OWNED_BY_US,
156         OWNED_BY_COMPONENT,
157         OWNED_BY_NATIVE_WINDOW,
158         OWNED_BY_CLIENT,
159     };
160 
161     struct BufferInfo {
162         IOMX::buffer_id mBuffer;
163         BufferStatus mStatus;
164         sp<IMemory> mMem;
165         size_t mSize;
166         void *mData;
167         MediaBuffer *mMediaBuffer;
168     };
169 
170     struct CodecSpecificData {
171         size_t mSize;
172         uint8_t mData[1];
173     };
174 
175     sp<IOMX> mOMX;
176     bool mOMXLivesLocally;
177     IOMX::node_id mNode;
178     uint32_t mQuirks;
179 
180     // Flags specified in the creation of the codec.
181     uint32_t mFlags;
182 
183     bool mIsEncoder;
184     bool mIsVideo;
185     char *mMIME;
186     char *mComponentName;
187     sp<MetaData> mOutputFormat;
188     sp<MediaSource> mSource;
189     Vector<CodecSpecificData *> mCodecSpecificData;
190     size_t mCodecSpecificDataIndex;
191 
192     sp<MemoryDealer> mDealer[2];
193 
194     State mState;
195     Vector<BufferInfo> mPortBuffers[2];
196     PortStatus mPortStatus[2];
197     bool mInitialBufferSubmit;
198     bool mSignalledEOS;
199     status_t mFinalStatus;
200     bool mNoMoreOutputData;
201     bool mOutputPortSettingsHaveChanged;
202     int64_t mSeekTimeUs;
203     ReadOptions::SeekMode mSeekMode;
204     int64_t mTargetTimeUs;
205     bool mOutputPortSettingsChangedPending;
206     sp<SkipCutBuffer> mSkipCutBuffer;
207 
208     MediaBuffer *mLeftOverBuffer;
209 
210     Mutex mLock;
211     Condition mAsyncCompletion;
212 
213     bool mPaused;
214 
215     sp<ANativeWindow> mNativeWindow;
216 
217     // The index in each of the mPortBuffers arrays of the buffer that will be
218     // submitted to OMX next.  This only applies when using buffers from a
219     // native window.
220     size_t mNextNativeBufferIndex[2];
221 
222     // A list of indices into mPortStatus[kPortIndexOutput] filled with data.
223     List<size_t> mFilledBuffers;
224     Condition mBufferFilled;
225 
226     // Used to record the decoding time for an output picture from
227     // a video encoder.
228     List<int64_t> mDecodingTimeList;
229 
230     OMXCodec(const sp<IOMX> &omx, IOMX::node_id node,
231              uint32_t quirks, uint32_t flags,
232              bool isEncoder, const char *mime, const char *componentName,
233              const sp<MediaSource> &source,
234              const sp<ANativeWindow> &nativeWindow);
235 
236     void addCodecSpecificData(const void *data, size_t size);
237     void clearCodecSpecificData();
238 
239     void setComponentRole();
240 
241     void setAMRFormat(bool isWAMR, int32_t bitRate);
242 
243     status_t setAACFormat(
244             int32_t numChannels, int32_t sampleRate, int32_t bitRate,
245             int32_t aacProfile, bool isADTS);
246 
247     status_t setAC3Format(int32_t numChannels, int32_t sampleRate);
248 
249     void setG711Format(int32_t sampleRate, int32_t numChannels);
250 
251     status_t setVideoPortFormatType(
252             OMX_U32 portIndex,
253             OMX_VIDEO_CODINGTYPE compressionFormat,
254             OMX_COLOR_FORMATTYPE colorFormat);
255 
256     void setVideoInputFormat(
257             const char *mime, const sp<MetaData>& meta);
258 
259     status_t setupBitRate(int32_t bitRate);
260     status_t setupErrorCorrectionParameters();
261     status_t setupH263EncoderParameters(const sp<MetaData>& meta);
262     status_t setupMPEG4EncoderParameters(const sp<MetaData>& meta);
263     status_t setupAVCEncoderParameters(const sp<MetaData>& meta);
264     status_t findTargetColorFormat(
265             const sp<MetaData>& meta, OMX_COLOR_FORMATTYPE *colorFormat);
266 
267     status_t isColorFormatSupported(
268             OMX_COLOR_FORMATTYPE colorFormat, int portIndex);
269 
270     // If profile/level is set in the meta data, its value in the meta
271     // data will be used; otherwise, the default value will be used.
272     status_t getVideoProfileLevel(const sp<MetaData>& meta,
273             const CodecProfileLevel& defaultProfileLevel,
274             CodecProfileLevel& profileLevel);
275 
276     status_t setVideoOutputFormat(
277             const char *mime, const sp<MetaData>& meta);
278 
279     void setImageOutputFormat(
280             OMX_COLOR_FORMATTYPE format, OMX_U32 width, OMX_U32 height);
281 
282     void setJPEGInputFormat(
283             OMX_U32 width, OMX_U32 height, OMX_U32 compressedSize);
284 
285     void setMinBufferSize(OMX_U32 portIndex, OMX_U32 size);
286 
287     void setRawAudioFormat(
288             OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels);
289 
290     status_t allocateBuffers();
291     status_t allocateBuffersOnPort(OMX_U32 portIndex);
292     status_t allocateOutputBuffersFromNativeWindow();
293 
294     status_t queueBufferToNativeWindow(BufferInfo *info);
295     status_t cancelBufferToNativeWindow(BufferInfo *info);
296     BufferInfo* dequeueBufferFromNativeWindow();
297 
298     status_t freeBuffersOnPort(
299             OMX_U32 portIndex, bool onlyThoseWeOwn = false);
300 
301     status_t freeBuffer(OMX_U32 portIndex, size_t bufIndex);
302 
303     bool drainInputBuffer(IOMX::buffer_id buffer);
304     void fillOutputBuffer(IOMX::buffer_id buffer);
305     bool drainInputBuffer(BufferInfo *info);
306     void fillOutputBuffer(BufferInfo *info);
307 
308     void drainInputBuffers();
309     void fillOutputBuffers();
310 
311     bool drainAnyInputBuffer();
312     BufferInfo *findInputBufferByDataPointer(void *ptr);
313     BufferInfo *findEmptyInputBuffer();
314 
315     // Returns true iff a flush was initiated and a completion event is
316     // upcoming, false otherwise (A flush was not necessary as we own all
317     // the buffers on that port).
318     // This method will ONLY ever return false for a component with quirk
319     // "kRequiresFlushCompleteEmulation".
320     bool flushPortAsync(OMX_U32 portIndex);
321 
322     void disablePortAsync(OMX_U32 portIndex);
323     status_t enablePortAsync(OMX_U32 portIndex);
324 
325     static size_t countBuffersWeOwn(const Vector<BufferInfo> &buffers);
326     static bool isIntermediateState(State state);
327 
328     void onEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2);
329     void onCmdComplete(OMX_COMMANDTYPE cmd, OMX_U32 data);
330     void onStateChange(OMX_STATETYPE newState);
331     void onPortSettingsChanged(OMX_U32 portIndex);
332 
333     void setState(State newState);
334 
335     status_t init();
336     void initOutputFormat(const sp<MetaData> &inputFormat);
337     status_t initNativeWindow();
338 
339     void initNativeWindowCrop();
340 
341     void dumpPortStatus(OMX_U32 portIndex);
342 
343     status_t configureCodec(const sp<MetaData> &meta);
344 
345     status_t waitForBufferFilled_l();
346 
347     int64_t getDecodingTimeUs();
348 
349     status_t parseHEVCCodecSpecificData(
350             const void *data, size_t size,
351             unsigned *profile, unsigned *level);
352     status_t parseAVCCodecSpecificData(
353             const void *data, size_t size,
354             unsigned *profile, unsigned *level);
355 
356     status_t stopOmxComponent_l();
357 
358     OMXCodec(const OMXCodec &);
359     OMXCodec &operator=(const OMXCodec &);
360 };
361 
362 struct CodecCapabilities {
363     enum {
364         kFlagSupportsAdaptivePlayback = 1 << 0,
365     };
366 
367     String8 mComponentName;
368     Vector<CodecProfileLevel> mProfileLevels;
369     Vector<OMX_U32> mColorFormats;
370     uint32_t mFlags;
371 };
372 
373 // Return a vector of componentNames with supported profile/level pairs
374 // supporting the given mime type, if queryDecoders==true, returns components
375 // that decode content of the given type, otherwise returns components
376 // that encode content of the given type.
377 // profile and level indications only make sense for h.263, mpeg4 and avc
378 // video.
379 // If hwCodecOnly==true, only returns hardware-based components, software and
380 // hardware otherwise.
381 // The profile/level values correspond to
382 // OMX_VIDEO_H263PROFILETYPE, OMX_VIDEO_MPEG4PROFILETYPE,
383 // OMX_VIDEO_AVCPROFILETYPE, OMX_VIDEO_H263LEVELTYPE, OMX_VIDEO_MPEG4LEVELTYPE
384 // and OMX_VIDEO_AVCLEVELTYPE respectively.
385 
386 status_t QueryCodecs(
387         const sp<IOMX> &omx,
388         const char *mimeType, bool queryDecoders, bool hwCodecOnly,
389         Vector<CodecCapabilities> *results);
390 
391 status_t QueryCodecs(
392         const sp<IOMX> &omx,
393         const char *mimeType, bool queryDecoders,
394         Vector<CodecCapabilities> *results);
395 
396 status_t QueryCodec(
397         const sp<IOMX> &omx,
398         const char *componentName, const char *mime,
399         bool isEncoder,
400         CodecCapabilities *caps);
401 
402 status_t getOMXChannelMapping(size_t numChannels, OMX_AUDIO_CHANNELTYPE map[]);
403 
404 }  // namespace android
405 
406 #endif  // OMX_CODEC_H_
407