ACodec.h revision c8efda9e9cd61dfe8e486c93fa8940b77cc3cceb
1/*
2 * Copyright (C) 2010 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 A_CODEC_H_
18
19#define A_CODEC_H_
20
21#include <stdint.h>
22#include <android/native_window.h>
23#include <media/IOMX.h>
24#include <media/stagefright/foundation/AHierarchicalStateMachine.h>
25#include <media/stagefright/SkipCutBuffer.h>
26#include <OMX_Audio.h>
27
28#define TRACK_BUFFER_TIMING     0
29
30namespace android {
31
32struct ABuffer;
33struct MemoryDealer;
34
35struct ACodec : public AHierarchicalStateMachine {
36    enum {
37        kWhatFillThisBuffer      = 'fill',
38        kWhatDrainThisBuffer     = 'drai',
39        kWhatEOS                 = 'eos ',
40        kWhatShutdownCompleted   = 'scom',
41        kWhatFlushCompleted      = 'fcom',
42        kWhatOutputFormatChanged = 'outC',
43        kWhatError               = 'erro',
44        kWhatComponentAllocated  = 'cAll',
45        kWhatComponentConfigured = 'cCon',
46        kWhatInputSurfaceCreated = 'isfc',
47        kWhatSignaledInputEOS    = 'seos',
48        kWhatBuffersAllocated    = 'allc',
49        kWhatOMXDied             = 'OMXd',
50    };
51
52    ACodec();
53
54    void setNotificationMessage(const sp<AMessage> &msg);
55    void initiateSetup(const sp<AMessage> &msg);
56    void signalFlush();
57    void signalResume();
58    void initiateShutdown(bool keepComponentAllocated = false);
59
60    void signalSetParameters(const sp<AMessage> &msg);
61    void signalEndOfInputStream();
62
63    void initiateAllocateComponent(const sp<AMessage> &msg);
64    void initiateConfigureComponent(const sp<AMessage> &msg);
65    void initiateCreateInputSurface();
66    void initiateStart();
67
68    void signalRequestIDRFrame();
69
70    struct PortDescription : public RefBase {
71        size_t countBuffers();
72        IOMX::buffer_id bufferIDAt(size_t index) const;
73        sp<ABuffer> bufferAt(size_t index) const;
74
75    private:
76        friend struct ACodec;
77
78        Vector<IOMX::buffer_id> mBufferIDs;
79        Vector<sp<ABuffer> > mBuffers;
80
81        PortDescription();
82        void addBuffer(IOMX::buffer_id id, const sp<ABuffer> &buffer);
83
84        DISALLOW_EVIL_CONSTRUCTORS(PortDescription);
85    };
86
87protected:
88    virtual ~ACodec();
89
90private:
91    struct BaseState;
92    struct UninitializedState;
93    struct LoadedState;
94    struct LoadedToIdleState;
95    struct IdleToExecutingState;
96    struct ExecutingState;
97    struct OutputPortSettingsChangedState;
98    struct ExecutingToIdleState;
99    struct IdleToLoadedState;
100    struct FlushingState;
101    struct DeathNotifier;
102
103    enum {
104        kWhatSetup                   = 'setu',
105        kWhatOMXMessage              = 'omx ',
106        kWhatInputBufferFilled       = 'inpF',
107        kWhatOutputBufferDrained     = 'outD',
108        kWhatShutdown                = 'shut',
109        kWhatFlush                   = 'flus',
110        kWhatResume                  = 'resm',
111        kWhatDrainDeferredMessages   = 'drai',
112        kWhatAllocateComponent       = 'allo',
113        kWhatConfigureComponent      = 'conf',
114        kWhatCreateInputSurface      = 'cisf',
115        kWhatSignalEndOfInputStream  = 'eois',
116        kWhatStart                   = 'star',
117        kWhatRequestIDRFrame         = 'ridr',
118        kWhatSetParameters           = 'setP',
119        kWhatSubmitOutputMetaDataBufferIfEOS = 'subm',
120    };
121
122    enum {
123        kPortIndexInput  = 0,
124        kPortIndexOutput = 1
125    };
126
127    enum {
128        kFlagIsSecure                                 = 1,
129        kFlagPushBlankBuffersToNativeWindowOnShutdown = 2,
130    };
131
132    struct BufferInfo {
133        enum Status {
134            OWNED_BY_US,
135            OWNED_BY_COMPONENT,
136            OWNED_BY_UPSTREAM,
137            OWNED_BY_DOWNSTREAM,
138            OWNED_BY_NATIVE_WINDOW,
139        };
140
141        IOMX::buffer_id mBufferID;
142        Status mStatus;
143        unsigned mDequeuedAt;
144
145        sp<ABuffer> mData;
146        sp<GraphicBuffer> mGraphicBuffer;
147    };
148
149#if TRACK_BUFFER_TIMING
150    struct BufferStats {
151        int64_t mEmptyBufferTimeUs;
152        int64_t mFillBufferDoneTimeUs;
153    };
154
155    KeyedVector<int64_t, BufferStats> mBufferStats;
156#endif
157
158    sp<AMessage> mNotify;
159
160    sp<UninitializedState> mUninitializedState;
161    sp<LoadedState> mLoadedState;
162    sp<LoadedToIdleState> mLoadedToIdleState;
163    sp<IdleToExecutingState> mIdleToExecutingState;
164    sp<ExecutingState> mExecutingState;
165    sp<OutputPortSettingsChangedState> mOutputPortSettingsChangedState;
166    sp<ExecutingToIdleState> mExecutingToIdleState;
167    sp<IdleToLoadedState> mIdleToLoadedState;
168    sp<FlushingState> mFlushingState;
169    sp<SkipCutBuffer> mSkipCutBuffer;
170
171    AString mComponentName;
172    uint32_t mFlags;
173    uint32_t mQuirks;
174    sp<IOMX> mOMX;
175    IOMX::node_id mNode;
176    sp<MemoryDealer> mDealer[2];
177
178    sp<ANativeWindow> mNativeWindow;
179    sp<AMessage> mInputFormat;
180    sp<AMessage> mOutputFormat;
181
182    Vector<BufferInfo> mBuffers[2];
183    bool mPortEOS[2];
184    status_t mInputEOSResult;
185
186    List<sp<AMessage> > mDeferredQueue;
187
188    bool mSentFormat;
189    bool mIsEncoder;
190    bool mUseMetadataOnEncoderOutput;
191    bool mShutdownInProgress;
192    bool mExplicitShutdown;
193
194    // If "mKeepComponentAllocated" we only transition back to Loaded state
195    // and do not release the component instance.
196    bool mKeepComponentAllocated;
197
198    int32_t mEncoderDelay;
199    int32_t mEncoderPadding;
200
201    bool mChannelMaskPresent;
202    int32_t mChannelMask;
203    unsigned mDequeueCounter;
204    bool mStoreMetaDataInOutputBuffers;
205    int32_t mMetaDataBuffersToSubmit;
206    size_t mNumUndequeuedBuffers;
207
208    int64_t mRepeatFrameDelayUs;
209    int64_t mMaxPtsGapUs;
210
211    int64_t mTimePerFrameUs;
212    int64_t mTimePerCaptureUs;
213
214    bool mCreateInputBuffersSuspended;
215
216    status_t setCyclicIntraMacroblockRefresh(const sp<AMessage> &msg, int32_t mode);
217    status_t allocateBuffersOnPort(OMX_U32 portIndex);
218    status_t freeBuffersOnPort(OMX_U32 portIndex);
219    status_t freeBuffer(OMX_U32 portIndex, size_t i);
220
221    status_t configureOutputBuffersFromNativeWindow(
222            OMX_U32 *nBufferCount, OMX_U32 *nBufferSize,
223            OMX_U32 *nMinUndequeuedBuffers);
224    status_t allocateOutputMetaDataBuffers();
225    status_t submitOutputMetaDataBuffer();
226    void signalSubmitOutputMetaDataBufferIfEOS_workaround();
227    status_t allocateOutputBuffersFromNativeWindow();
228    status_t cancelBufferToNativeWindow(BufferInfo *info);
229    status_t freeOutputBuffersNotOwnedByComponent();
230    BufferInfo *dequeueBufferFromNativeWindow();
231
232    BufferInfo *findBufferByID(
233            uint32_t portIndex, IOMX::buffer_id bufferID,
234            ssize_t *index = NULL);
235
236    status_t setComponentRole(bool isEncoder, const char *mime);
237    status_t configureCodec(const char *mime, const sp<AMessage> &msg);
238
239    status_t setVideoPortFormatType(
240            OMX_U32 portIndex,
241            OMX_VIDEO_CODINGTYPE compressionFormat,
242            OMX_COLOR_FORMATTYPE colorFormat);
243
244    status_t setSupportedOutputFormat();
245
246    status_t setupVideoDecoder(
247            const char *mime, int32_t width, int32_t height);
248
249    status_t setupVideoEncoder(
250            const char *mime, const sp<AMessage> &msg);
251
252    status_t setVideoFormatOnPort(
253            OMX_U32 portIndex,
254            int32_t width, int32_t height,
255            OMX_VIDEO_CODINGTYPE compressionFormat);
256
257    status_t setupAACCodec(
258            bool encoder,
259            int32_t numChannels, int32_t sampleRate, int32_t bitRate,
260            int32_t aacProfile, bool isADTS);
261
262    status_t setupAC3Codec(bool encoder, int32_t numChannels, int32_t sampleRate);
263
264    status_t selectAudioPortFormat(
265            OMX_U32 portIndex, OMX_AUDIO_CODINGTYPE desiredFormat);
266
267    status_t setupAMRCodec(bool encoder, bool isWAMR, int32_t bitRate);
268    status_t setupG711Codec(bool encoder, int32_t numChannels);
269
270    status_t setupFlacCodec(
271            bool encoder, int32_t numChannels, int32_t sampleRate, int32_t compressionLevel);
272
273    status_t setupRawAudioFormat(
274            OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels);
275
276    status_t setMinBufferSize(OMX_U32 portIndex, size_t size);
277
278    status_t setupMPEG4EncoderParameters(const sp<AMessage> &msg);
279    status_t setupH263EncoderParameters(const sp<AMessage> &msg);
280    status_t setupAVCEncoderParameters(const sp<AMessage> &msg);
281    status_t setupHEVCEncoderParameters(const sp<AMessage> &msg);
282    status_t setupVPXEncoderParameters(const sp<AMessage> &msg);
283
284    status_t verifySupportForProfileAndLevel(int32_t profile, int32_t level);
285
286    status_t configureBitrate(
287            int32_t bitrate, OMX_VIDEO_CONTROLRATETYPE bitrateMode);
288
289    status_t setupErrorCorrectionParameters();
290
291    status_t initNativeWindow();
292
293    status_t pushBlankBuffersToNativeWindow();
294
295    // Returns true iff all buffers on the given port have status
296    // OWNED_BY_US or OWNED_BY_NATIVE_WINDOW.
297    bool allYourBuffersAreBelongToUs(OMX_U32 portIndex);
298
299    bool allYourBuffersAreBelongToUs();
300
301    void waitUntilAllPossibleNativeWindowBuffersAreReturnedToUs();
302
303    size_t countBuffersOwnedByComponent(OMX_U32 portIndex) const;
304    size_t countBuffersOwnedByNativeWindow() const;
305
306    void deferMessage(const sp<AMessage> &msg);
307    void processDeferredMessages();
308
309    void sendFormatChange(const sp<AMessage> &reply);
310    status_t getPortFormat(OMX_U32 portIndex, sp<AMessage> &notify);
311
312    void signalError(
313            OMX_ERRORTYPE error = OMX_ErrorUndefined,
314            status_t internalError = UNKNOWN_ERROR);
315
316    status_t requestIDRFrame();
317    status_t setParameters(const sp<AMessage> &params);
318
319    // Send EOS on input stream.
320    void onSignalEndOfInputStream();
321
322    DISALLOW_EVIL_CONSTRUCTORS(ACodec);
323};
324
325}  // namespace android
326
327#endif  // A_CODEC_H_
328