ACodec.h revision 0dbe5a9321b24b6883fbb2fe97cd9d525128b0b5
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        kWhatBuffersAllocated    = 'allc',
47    };
48
49    ACodec();
50
51    void setNotificationMessage(const sp<AMessage> &msg);
52    void initiateSetup(const sp<AMessage> &msg);
53    void signalFlush();
54    void signalResume();
55    void initiateShutdown(bool keepComponentAllocated = false);
56
57    void initiateAllocateComponent(const sp<AMessage> &msg);
58    void initiateConfigureComponent(const sp<AMessage> &msg);
59    void initiateStart();
60
61    void signalRequestIDRFrame();
62
63    struct PortDescription : public RefBase {
64        size_t countBuffers();
65        IOMX::buffer_id bufferIDAt(size_t index) const;
66        sp<ABuffer> bufferAt(size_t index) const;
67
68    private:
69        friend struct ACodec;
70
71        Vector<IOMX::buffer_id> mBufferIDs;
72        Vector<sp<ABuffer> > mBuffers;
73
74        PortDescription();
75        void addBuffer(IOMX::buffer_id id, const sp<ABuffer> &buffer);
76
77        DISALLOW_EVIL_CONSTRUCTORS(PortDescription);
78    };
79
80protected:
81    virtual ~ACodec();
82
83private:
84    struct BaseState;
85    struct UninitializedState;
86    struct LoadedState;
87    struct LoadedToIdleState;
88    struct IdleToExecutingState;
89    struct ExecutingState;
90    struct OutputPortSettingsChangedState;
91    struct ExecutingToIdleState;
92    struct IdleToLoadedState;
93    struct FlushingState;
94
95    enum {
96        kWhatSetup                   = 'setu',
97        kWhatOMXMessage              = 'omx ',
98        kWhatInputBufferFilled       = 'inpF',
99        kWhatOutputBufferDrained     = 'outD',
100        kWhatShutdown                = 'shut',
101        kWhatFlush                   = 'flus',
102        kWhatResume                  = 'resm',
103        kWhatDrainDeferredMessages   = 'drai',
104        kWhatAllocateComponent       = 'allo',
105        kWhatConfigureComponent      = 'conf',
106        kWhatStart                   = 'star',
107        kWhatRequestIDRFrame         = 'ridr',
108    };
109
110    enum {
111        kPortIndexInput  = 0,
112        kPortIndexOutput = 1
113    };
114
115    enum {
116        kFlagIsSecure   = 1,
117    };
118
119    struct BufferInfo {
120        enum Status {
121            OWNED_BY_US,
122            OWNED_BY_COMPONENT,
123            OWNED_BY_UPSTREAM,
124            OWNED_BY_DOWNSTREAM,
125            OWNED_BY_NATIVE_WINDOW,
126        };
127
128        IOMX::buffer_id mBufferID;
129        Status mStatus;
130
131        sp<ABuffer> mData;
132        sp<GraphicBuffer> mGraphicBuffer;
133    };
134
135#if TRACK_BUFFER_TIMING
136    struct BufferStats {
137        int64_t mEmptyBufferTimeUs;
138        int64_t mFillBufferDoneTimeUs;
139    };
140
141    KeyedVector<int64_t, BufferStats> mBufferStats;
142#endif
143
144    sp<AMessage> mNotify;
145
146    sp<UninitializedState> mUninitializedState;
147    sp<LoadedState> mLoadedState;
148    sp<LoadedToIdleState> mLoadedToIdleState;
149    sp<IdleToExecutingState> mIdleToExecutingState;
150    sp<ExecutingState> mExecutingState;
151    sp<OutputPortSettingsChangedState> mOutputPortSettingsChangedState;
152    sp<ExecutingToIdleState> mExecutingToIdleState;
153    sp<IdleToLoadedState> mIdleToLoadedState;
154    sp<FlushingState> mFlushingState;
155    sp<SkipCutBuffer> mSkipCutBuffer;
156
157    AString mComponentName;
158    uint32_t mFlags;
159    uint32_t mQuirks;
160    sp<IOMX> mOMX;
161    IOMX::node_id mNode;
162    sp<MemoryDealer> mDealer[2];
163
164    sp<ANativeWindow> mNativeWindow;
165
166    Vector<BufferInfo> mBuffers[2];
167    bool mPortEOS[2];
168    status_t mInputEOSResult;
169
170    List<sp<AMessage> > mDeferredQueue;
171
172    bool mSentFormat;
173    bool mIsEncoder;
174
175    bool mShutdownInProgress;
176
177    // If "mKeepComponentAllocated" we only transition back to Loaded state
178    // and do not release the component instance.
179    bool mKeepComponentAllocated;
180
181    int32_t mEncoderDelay;
182    int32_t mEncoderPadding;
183
184    bool mChannelMaskPresent;
185    int32_t mChannelMask;
186
187    status_t setCyclicIntraMacroblockRefresh(const sp<AMessage> &msg, int32_t mode);
188    status_t allocateBuffersOnPort(OMX_U32 portIndex);
189    status_t freeBuffersOnPort(OMX_U32 portIndex);
190    status_t freeBuffer(OMX_U32 portIndex, size_t i);
191
192    status_t allocateOutputBuffersFromNativeWindow();
193    status_t cancelBufferToNativeWindow(BufferInfo *info);
194    status_t freeOutputBuffersNotOwnedByComponent();
195    BufferInfo *dequeueBufferFromNativeWindow();
196
197    BufferInfo *findBufferByID(
198            uint32_t portIndex, IOMX::buffer_id bufferID,
199            ssize_t *index = NULL);
200
201    status_t setComponentRole(bool isEncoder, const char *mime);
202    status_t configureCodec(const char *mime, const sp<AMessage> &msg);
203
204    status_t setVideoPortFormatType(
205            OMX_U32 portIndex,
206            OMX_VIDEO_CODINGTYPE compressionFormat,
207            OMX_COLOR_FORMATTYPE colorFormat);
208
209    status_t setSupportedOutputFormat();
210
211    status_t setupVideoDecoder(
212            const char *mime, int32_t width, int32_t height);
213
214    status_t setupVideoEncoder(
215            const char *mime, const sp<AMessage> &msg);
216
217    status_t setVideoFormatOnPort(
218            OMX_U32 portIndex,
219            int32_t width, int32_t height,
220            OMX_VIDEO_CODINGTYPE compressionFormat);
221
222    status_t setupAACCodec(
223            bool encoder,
224            int32_t numChannels, int32_t sampleRate, int32_t bitRate,
225            int32_t aacProfile, bool isADTS);
226
227    status_t selectAudioPortFormat(
228            OMX_U32 portIndex, OMX_AUDIO_CODINGTYPE desiredFormat);
229
230    status_t setupAMRCodec(bool encoder, bool isWAMR, int32_t bitRate);
231    status_t setupG711Codec(bool encoder, int32_t numChannels);
232
233    status_t setupFlacCodec(
234            bool encoder, int32_t numChannels, int32_t sampleRate, int32_t compressionLevel);
235
236    status_t setupRawAudioFormat(
237            OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels);
238
239    status_t setMinBufferSize(OMX_U32 portIndex, size_t size);
240
241    status_t setupMPEG4EncoderParameters(const sp<AMessage> &msg);
242    status_t setupH263EncoderParameters(const sp<AMessage> &msg);
243    status_t setupAVCEncoderParameters(const sp<AMessage> &msg);
244
245    status_t verifySupportForProfileAndLevel(int32_t profile, int32_t level);
246
247    status_t configureBitrate(
248            int32_t bitrate, OMX_VIDEO_CONTROLRATETYPE bitrateMode);
249
250    status_t setupErrorCorrectionParameters();
251
252    status_t initNativeWindow();
253
254    status_t pushBlankBuffersToNativeWindow();
255
256    // Returns true iff all buffers on the given port have status OWNED_BY_US.
257    bool allYourBuffersAreBelongToUs(OMX_U32 portIndex);
258
259    bool allYourBuffersAreBelongToUs();
260
261    size_t countBuffersOwnedByComponent(OMX_U32 portIndex) const;
262
263    void deferMessage(const sp<AMessage> &msg);
264    void processDeferredMessages();
265
266    void sendFormatChange();
267
268    void signalError(
269            OMX_ERRORTYPE error = OMX_ErrorUndefined,
270            status_t internalError = UNKNOWN_ERROR);
271
272    status_t requestIDRFrame();
273
274    DISALLOW_EVIL_CONSTRUCTORS(ACodec);
275};
276
277}  // namespace android
278
279#endif  // A_CODEC_H_
280