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 #define A_CODEC_H_
19 
20 #include <stdint.h>
21 #include <android/native_window.h>
22 #include <media/hardware/MetadataBufferType.h>
23 #include <media/MediaCodecInfo.h>
24 #include <media/IOMX.h>
25 #include <media/stagefright/AHierarchicalStateMachine.h>
26 #include <media/stagefright/CodecBase.h>
27 #include <media/stagefright/FrameRenderTracker.h>
28 #include <media/stagefright/MediaDefs.h>
29 #include <media/stagefright/SkipCutBuffer.h>
30 #include <utils/NativeHandle.h>
31 #include <OMX_Audio.h>
32 #include <hardware/gralloc.h>
33 #include <nativebase/nativebase.h>
34 #include <android/hidl/allocator/1.0/IAllocator.h>
35 #include <android/hidl/memory/1.0/IMemory.h>
36 
37 #define TRACK_BUFFER_TIMING     0
38 
39 namespace android {
40 namespace hardware {
41 namespace media {
42 namespace omx {
43 namespace V1_0 {
44 struct IGraphicBufferSource;
45 }  // namespace V1_0
46 }  // namespace omx
47 }  // namespace media
48 }  // namespace hardware
49 
50 struct ABuffer;
51 class ACodecBufferChannel;
52 class MediaCodecBuffer;
53 class MemoryDealer;
54 struct DescribeColorFormat2Params;
55 struct DataConverter;
56 
57 typedef hidl::allocator::V1_0::IAllocator TAllocator;
58 typedef hidl::memory::V1_0::IMemory TMemory;
59 
60 struct ACodec : public AHierarchicalStateMachine, public CodecBase {
61     ACodec();
62 
63     void initiateSetup(const sp<AMessage> &msg);
64 
65     virtual std::shared_ptr<BufferChannelBase> getBufferChannel() override;
66     virtual void initiateAllocateComponent(const sp<AMessage> &msg);
67     virtual void initiateConfigureComponent(const sp<AMessage> &msg);
68     virtual void initiateCreateInputSurface();
69     virtual void initiateSetInputSurface(const sp<PersistentSurface> &surface);
70     virtual void initiateStart();
71     virtual void initiateShutdown(bool keepComponentAllocated = false);
72 
73     status_t queryCapabilities(
74             const char* owner, const char* name,
75             const char* mime, bool isEncoder,
76             MediaCodecInfo::CapabilitiesWriter* caps);
77 
78     virtual status_t setSurface(const sp<Surface> &surface);
79 
80     virtual void signalFlush();
81     virtual void signalResume();
82 
83     virtual void signalSetParameters(const sp<AMessage> &msg);
84     virtual void signalEndOfInputStream();
85     virtual void signalRequestIDRFrame();
86 
87     // AHierarchicalStateMachine implements the message handling
onMessageReceivedACodec88     virtual void onMessageReceived(const sp<AMessage> &msg) {
89         handleMessage(msg);
90     }
91 
92     // Returns 0 if configuration is not supported.  NOTE: this is treated by
93     // some OMX components as auto level, and by others as invalid level.
94     static int /* OMX_VIDEO_AVCLEVELTYPE */ getAVCLevelFor(
95             int width, int height, int rate, int bitrate,
96             OMX_VIDEO_AVCPROFILEEXTTYPE profile =
97                 (OMX_VIDEO_AVCPROFILEEXTTYPE)OMX_VIDEO_AVCProfileBaseline);
98 
99     // Quirk still supported, even though deprecated
100     enum Quirks {
101         kRequiresAllocateBufferOnInputPorts   = 1,
102         kRequiresAllocateBufferOnOutputPorts  = 2,
103     };
104 
105     static status_t getOMXChannelMapping(size_t numChannels, OMX_AUDIO_CHANNELTYPE map[]);
106 
107 protected:
108     virtual ~ACodec();
109 
110 private:
111     struct BaseState;
112     struct UninitializedState;
113     struct LoadedState;
114     struct LoadedToIdleState;
115     struct IdleToExecutingState;
116     struct ExecutingState;
117     struct OutputPortSettingsChangedState;
118     struct ExecutingToIdleState;
119     struct IdleToLoadedState;
120     struct FlushingState;
121     struct DeathNotifier;
122 
123     enum {
124         kWhatSetup                   = 'setu',
125         kWhatOMXMessage              = 'omx ',
126         // same as kWhatOMXMessage - but only used with
127         // handleMessage during OMX message-list handling
128         kWhatOMXMessageItem          = 'omxI',
129         kWhatOMXMessageList          = 'omxL',
130         kWhatInputBufferFilled       = 'inpF',
131         kWhatOutputBufferDrained     = 'outD',
132         kWhatShutdown                = 'shut',
133         kWhatFlush                   = 'flus',
134         kWhatResume                  = 'resm',
135         kWhatDrainDeferredMessages   = 'drai',
136         kWhatAllocateComponent       = 'allo',
137         kWhatConfigureComponent      = 'conf',
138         kWhatSetSurface              = 'setS',
139         kWhatCreateInputSurface      = 'cisf',
140         kWhatSetInputSurface         = 'sisf',
141         kWhatSignalEndOfInputStream  = 'eois',
142         kWhatStart                   = 'star',
143         kWhatRequestIDRFrame         = 'ridr',
144         kWhatSetParameters           = 'setP',
145         kWhatSubmitOutputMetadataBufferIfEOS = 'subm',
146         kWhatOMXDied                 = 'OMXd',
147         kWhatReleaseCodecInstance    = 'relC',
148         kWhatForceStateTransition    = 'fstt',
149         kWhatCheckIfStuck            = 'Cstk',
150     };
151 
152     enum {
153         kPortIndexInput  = 0,
154         kPortIndexOutput = 1
155     };
156 
157     enum {
158         kFlagIsSecure                                 = 1,
159         kFlagPushBlankBuffersToNativeWindowOnShutdown = 2,
160         kFlagIsGrallocUsageProtected                  = 4,
161         kFlagPreregisterMetadataBuffers               = 8,
162     };
163 
164     enum {
165         kVideoGrallocUsage = (GRALLOC_USAGE_HW_TEXTURE
166                             | GRALLOC_USAGE_HW_COMPOSER
167                             | GRALLOC_USAGE_EXTERNAL_DISP),
168     };
169 
170     struct BufferInfo {
171         enum Status {
172             OWNED_BY_US,
173             OWNED_BY_COMPONENT,
174             OWNED_BY_UPSTREAM,
175             OWNED_BY_DOWNSTREAM,
176             OWNED_BY_NATIVE_WINDOW,
177             UNRECOGNIZED,            // not a tracked buffer
178         };
179 
getSafeStatusACodec::BufferInfo180         static inline Status getSafeStatus(BufferInfo *info) {
181             return info == NULL ? UNRECOGNIZED : info->mStatus;
182         }
183 
184         IOMX::buffer_id mBufferID;
185         Status mStatus;
186         unsigned mDequeuedAt;
187 
188         sp<MediaCodecBuffer> mData;  // the client's buffer; if not using data conversion, this is
189                                      // the codec buffer; otherwise, it is allocated separately
190         sp<RefBase> mMemRef;         // and a reference to the IMemory, so it does not go away
191         sp<MediaCodecBuffer> mCodecData;  // the codec's buffer
192         sp<RefBase> mCodecRef;            // and a reference to the IMemory
193 
194         sp<GraphicBuffer> mGraphicBuffer;
195         bool mNewGraphicBuffer;
196         int mFenceFd;
197         FrameRenderTracker::Info *mRenderInfo;
198 
199         // The following field and 4 methods are used for debugging only
200         bool mIsReadFence;
201         // Store |fenceFd| and set read/write flag. Log error, if there is already a fence stored.
202         void setReadFence(int fenceFd, const char *dbg);
203         void setWriteFence(int fenceFd, const char *dbg);
204         // Log error, if the current fence is not a read/write fence.
205         void checkReadFence(const char *dbg);
206         void checkWriteFence(const char *dbg);
207     };
208 
209     static const char *_asString(BufferInfo::Status s);
210     void dumpBuffers(OMX_U32 portIndex);
211 
212     // If |fd| is non-negative, waits for fence with |fd| and logs an error if it fails. Returns
213     // the error code or OK on success. If |fd| is negative, it returns OK
214     status_t waitForFence(int fd, const char *dbg);
215 
216 #if TRACK_BUFFER_TIMING
217     struct BufferStats {
218         int64_t mEmptyBufferTimeUs;
219         int64_t mFillBufferDoneTimeUs;
220     };
221 
222     KeyedVector<int64_t, BufferStats> mBufferStats;
223 #endif
224 
225     sp<UninitializedState> mUninitializedState;
226     sp<LoadedState> mLoadedState;
227     sp<LoadedToIdleState> mLoadedToIdleState;
228     sp<IdleToExecutingState> mIdleToExecutingState;
229     sp<ExecutingState> mExecutingState;
230     sp<OutputPortSettingsChangedState> mOutputPortSettingsChangedState;
231     sp<ExecutingToIdleState> mExecutingToIdleState;
232     sp<IdleToLoadedState> mIdleToLoadedState;
233     sp<FlushingState> mFlushingState;
234     sp<SkipCutBuffer> mSkipCutBuffer;
235     int32_t mSampleRate;
236 
237     AString mComponentName;
238     uint32_t mFlags;
239     sp<IOMX> mOMX;
240     sp<IOMXNode> mOMXNode;
241     int32_t mNodeGeneration;
242     sp<TAllocator> mAllocator[2];
243 
244     bool mUsingNativeWindow;
245     sp<ANativeWindow> mNativeWindow;
246     int mNativeWindowUsageBits;
247     android_native_rect_t mLastNativeWindowCrop;
248     int32_t mLastNativeWindowDataSpace;
249     HDRStaticInfo mLastHDRStaticInfo;
250     sp<ABuffer> mHdr10PlusScratchBuffer;
251     sp<ABuffer> mLastHdr10PlusBuffer;
252     sp<AMessage> mConfigFormat;
253     sp<AMessage> mInputFormat;
254     sp<AMessage> mOutputFormat;
255 
256     // Initial output format + configuration params that is reused as the base for all subsequent
257     // format updates. This will equal to mOutputFormat until the first actual frame is received.
258     sp<AMessage> mBaseOutputFormat;
259 
260     FrameRenderTracker mRenderTracker; // render information for buffers rendered by ACodec
261     Vector<BufferInfo> mBuffers[2];
262     bool mPortEOS[2];
263     status_t mInputEOSResult;
264 
265     List<sp<AMessage> > mDeferredQueue;
266 
267     sp<AMessage> mLastOutputFormat;
268     bool mIsVideo;
269     bool mIsImage;
270     bool mIsEncoder;
271     bool mFatalError;
272     bool mShutdownInProgress;
273     bool mExplicitShutdown;
274     bool mIsLegacyVP9Decoder;
275 
276     // If "mKeepComponentAllocated" we only transition back to Loaded state
277     // and do not release the component instance.
278     bool mKeepComponentAllocated;
279 
280     int32_t mEncoderDelay;
281     int32_t mEncoderPadding;
282     int32_t mRotationDegrees;
283 
284     bool mChannelMaskPresent;
285     int32_t mChannelMask;
286     unsigned mDequeueCounter;
287     IOMX::PortMode mPortMode[2];
288     int32_t mMetadataBuffersToSubmit;
289     size_t mNumUndequeuedBuffers;
290     sp<DataConverter> mConverter[2];
291 
292     sp<hardware::media::omx::V1_0::IGraphicBufferSource> mGraphicBufferSource;
293     int64_t mRepeatFrameDelayUs;
294     int64_t mMaxPtsGapUs;
295     float mMaxFps;
296     double mFps;
297     double mCaptureFps;
298     bool mCreateInputBuffersSuspended;
299     std::optional<uint32_t> mLatency;
300 
301     bool mTunneled;
302 
303     OMX_INDEXTYPE mDescribeColorAspectsIndex;
304     OMX_INDEXTYPE mDescribeHDRStaticInfoIndex;
305     OMX_INDEXTYPE mDescribeHDR10PlusInfoIndex;
306 
307     std::shared_ptr<ACodecBufferChannel> mBufferChannel;
308 
309     int32_t mStateGeneration;
310 
311     enum {
312         kExtensionsUnchecked,
313         kExtensionsNone,
314         kExtensionsExist,
315     } mVendorExtensionsStatus;
316 
317     status_t setCyclicIntraMacroblockRefresh(const sp<AMessage> &msg, int32_t mode);
318     status_t allocateBuffersOnPort(OMX_U32 portIndex);
319     status_t freeBuffersOnPort(OMX_U32 portIndex);
320     status_t freeBuffer(OMX_U32 portIndex, size_t i);
321 
322     status_t handleSetSurface(const sp<Surface> &surface);
323     status_t setPortMode(int32_t portIndex, IOMX::PortMode mode);
324     status_t setupNativeWindowSizeFormatAndUsage(
325             ANativeWindow *nativeWindow /* nonnull */, int *finalUsage /* nonnull */,
326             bool reconnect);
327 
328     status_t configureOutputBuffersFromNativeWindow(
329             OMX_U32 *nBufferCount, OMX_U32 *nBufferSize,
330             OMX_U32 *nMinUndequeuedBuffers, bool preregister);
331     status_t allocateOutputMetadataBuffers();
332     status_t submitOutputMetadataBuffer();
333     void signalSubmitOutputMetadataBufferIfEOS_workaround();
334     status_t allocateOutputBuffersFromNativeWindow();
335     status_t cancelBufferToNativeWindow(BufferInfo *info);
336     status_t freeOutputBuffersNotOwnedByComponent();
337     BufferInfo *dequeueBufferFromNativeWindow();
338 
storingMetadataInDecodedBuffersACodec339     inline bool storingMetadataInDecodedBuffers() {
340         return (mPortMode[kPortIndexOutput] == IOMX::kPortModeDynamicANWBuffer) && !mIsEncoder;
341     }
342 
usingSecureBufferOnEncoderOutputACodec343     inline bool usingSecureBufferOnEncoderOutput() {
344         return (mPortMode[kPortIndexOutput] == IOMX::kPortModePresetSecureBuffer) && mIsEncoder;
345     }
346 
347     BufferInfo *findBufferByID(
348             uint32_t portIndex, IOMX::buffer_id bufferID,
349             ssize_t *index = NULL);
350 
351     status_t fillBuffer(BufferInfo *info);
352 
353     status_t setComponentRole(bool isEncoder, const char *mime);
354 
355     status_t configureCodec(const char *mime, const sp<AMessage> &msg);
356 
357     status_t configureTunneledVideoPlayback(int32_t audioHwSync,
358             const sp<ANativeWindow> &nativeWindow);
359 
360     status_t setVideoPortFormatType(
361             OMX_U32 portIndex,
362             OMX_VIDEO_CODINGTYPE compressionFormat,
363             OMX_COLOR_FORMATTYPE colorFormat,
364             bool usingNativeBuffers = false);
365 
366     status_t setSupportedOutputFormat(bool getLegacyFlexibleFormat);
367 
368     status_t setupVideoDecoder(
369             const char *mime, const sp<AMessage> &msg, bool usingNativeBuffers, bool haveSwRenderer,
370             sp<AMessage> &outputformat);
371 
372     status_t setupVideoEncoder(
373             const char *mime, const sp<AMessage> &msg,
374             sp<AMessage> &outputformat, sp<AMessage> &inputformat);
375 
376     status_t setVideoFormatOnPort(
377             OMX_U32 portIndex,
378             int32_t width, int32_t height,
379             OMX_VIDEO_CODINGTYPE compressionFormat, float frameRate = -1.0);
380 
381     // sets |portIndex| port buffer numbers to be |bufferNum|. NOTE: Component could reject
382     // this setting if the |bufferNum| is less than the minimum buffer num of the port.
383     status_t setPortBufferNum(OMX_U32 portIndex, int bufferNum);
384 
385     // gets index or sets it to 0 on error. Returns error from codec.
386     status_t initDescribeColorAspectsIndex();
387 
388     // sets |params|. If |readBack| is true, it re-gets them afterwards if set succeeded.
389     // returns the codec error.
390     status_t setCodecColorAspects(DescribeColorAspectsParams &params, bool readBack = false);
391 
392     // gets |params|; returns the codec error. |param| should not change on error.
393     status_t getCodecColorAspects(DescribeColorAspectsParams &params);
394 
395     // gets dataspace guidance from codec and platform. |params| should be set up with the color
396     // aspects to use. If |tryCodec| is true, the codec is queried first. If it succeeds, we
397     // return OK. Otherwise, we fall back to the platform guidance and return the codec error;
398     // though, we return OK if the codec failed with UNSUPPORTED, as codec guidance is optional.
399     status_t getDataSpace(
400             DescribeColorAspectsParams &params, android_dataspace *dataSpace /* nonnull */,
401             bool tryCodec);
402 
403     // sets color aspects for the encoder for certain |width/height| based on |configFormat|, and
404     // set resulting color config into |outputFormat|. If |usingNativeWindow| is true, we use
405     // video defaults if config is unspecified. Returns error from the codec.
406     status_t setColorAspectsForVideoDecoder(
407             int32_t width, int32_t height, bool usingNativeWindow,
408             const sp<AMessage> &configFormat, sp<AMessage> &outputFormat);
409 
410     // gets color aspects for the encoder for certain |width/height| based on |configFormat|, and
411     // set resulting color config into |outputFormat|. If |dataSpace| is non-null, it requests
412     // dataspace guidance from the codec and platform and sets it into |dataSpace|. Returns the
413     // error from the codec.
414     status_t getColorAspectsAndDataSpaceForVideoDecoder(
415             int32_t width, int32_t height, const sp<AMessage> &configFormat,
416             sp<AMessage> &outputFormat, android_dataspace *dataSpace);
417 
418     // sets color aspects for the video encoder assuming bytebuffer mode for certain |configFormat|
419     // and sets resulting color config into |outputFormat|. For mediarecorder, also set dataspace
420     // into |inputFormat|. Returns the error from the codec.
421     status_t setColorAspectsForVideoEncoder(
422             const sp<AMessage> &configFormat,
423             sp<AMessage> &outputFormat, sp<AMessage> &inputFormat);
424 
425     // sets color aspects for the video encoder in surface mode. This basically sets the default
426     // video values for unspecified aspects and sets the dataspace to use in the input format.
427     // Also sets the dataspace into |dataSpace|.
428     // Returns any codec errors during this configuration, except for optional steps.
429     status_t setInitialColorAspectsForVideoEncoderSurfaceAndGetDataSpace(
430             android_dataspace *dataSpace /* nonnull */);
431 
432     // gets color aspects for the video encoder input port and sets them into the |format|.
433     // Returns any codec errors.
434     status_t getInputColorAspectsForVideoEncoder(sp<AMessage> &format);
435 
436     // updates the encoder output format with |aspects| defaulting to |dataSpace| for
437     // unspecified values.
438     void onDataSpaceChanged(android_dataspace dataSpace, const ColorAspects &aspects);
439 
440     // notifies the codec that the config with |configIndex| has changed, the value
441     // can be queried by OMX getConfig, and the config should be applied to the next
442     // output buffer notified after this callback.
443     void onConfigUpdate(OMX_INDEXTYPE configIndex);
444 
445     // gets index or sets it to 0 on error. Returns error from codec.
446     status_t initDescribeHDRStaticInfoIndex();
447 
448     // sets HDR static metadata for the video encoder/decoder based on |configFormat|, and
449     // sets resulting HDRStaticInfo config into |outputFormat|. Returns error from the codec.
450     status_t setHDRStaticInfoForVideoCodec(
451             OMX_U32 portIndex, const sp<AMessage> &configFormat, sp<AMessage> &outputFormat);
452 
453     // sets |params|. Returns the codec error.
454     status_t setHDRStaticInfo(const DescribeHDRStaticInfoParams &params);
455 
456     // sets |hdr10PlusInfo|. Returns the codec error.
457     status_t setHdr10PlusInfo(const sp<ABuffer> &hdr10PlusInfo);
458 
459     // gets |params|. Returns the codec error.
460     status_t getHDRStaticInfo(DescribeHDRStaticInfoParams &params);
461 
462     // gets HDR static information for the video encoder/decoder port and sets them into |format|.
463     status_t getHDRStaticInfoForVideoCodec(OMX_U32 portIndex, sp<AMessage> &format);
464 
465     // gets DescribeHDR10PlusInfoParams params. If |paramSizeUsed| is zero, it's
466     // possible that the returned DescribeHDR10PlusInfoParams only has the
467     // nParamSizeUsed field updated, because the size of the storage is insufficient.
468     // In this case, getHDR10PlusInfo() should be called again with |paramSizeUsed|
469     // specified to the previous returned value.
470     DescribeHDR10PlusInfoParams* getHDR10PlusInfo(size_t paramSizeUsed = 0);
471 
472     typedef struct drcParams {
473         int32_t drcCut;
474         int32_t drcBoost;
475         int32_t heavyCompression;
476         int32_t targetRefLevel;
477         int32_t encodedTargetLevel;
478         int32_t effectType;
479         int32_t albumMode;
480         int32_t outputLoudness;
481     } drcParams_t;
482 
483     status_t setupAACCodec(
484             bool encoder,
485             int32_t numChannels, int32_t sampleRate, int32_t bitRate,
486             int32_t aacProfile, bool isADTS, int32_t sbrMode,
487             int32_t maxOutputChannelCount, const drcParams_t& drc,
488             int32_t pcmLimiterEnable);
489 
490     status_t setupAC3Codec(bool encoder, int32_t numChannels, int32_t sampleRate);
491 
492     status_t setupEAC3Codec(bool encoder, int32_t numChannels, int32_t sampleRate);
493 
494     status_t setupAC4Codec(bool encoder, int32_t numChannels, int32_t sampleRate);
495 
496     status_t selectAudioPortFormat(
497             OMX_U32 portIndex, OMX_AUDIO_CODINGTYPE desiredFormat);
498 
499     status_t setupAMRCodec(bool encoder, bool isWAMR, int32_t bitRate);
500     status_t setupG711Codec(bool encoder, int32_t sampleRate, int32_t numChannels);
501 
502     status_t setupFlacCodec(
503             bool encoder, int32_t numChannels, int32_t sampleRate, int32_t compressionLevel,
504             AudioEncoding encoding);
505 
506     status_t setupRawAudioFormat(
507             OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels,
508             AudioEncoding encoding = kAudioEncodingPcm16bit);
509 
510     status_t setPriority(int32_t priority);
511     status_t setLowLatency(int32_t lowLatency);
512     status_t setLatency(uint32_t latency);
513     status_t getLatency(uint32_t *latency);
514     status_t setAudioPresentation(int32_t presentationId, int32_t programId);
515     status_t setOperatingRate(float rateFloat, bool isVideo);
516     status_t getIntraRefreshPeriod(uint32_t *intraRefreshPeriod);
517     status_t setIntraRefreshPeriod(uint32_t intraRefreshPeriod, bool inConfigure);
518 
519     // Configures temporal layering based on |msg|. |inConfigure| shall be true iff this is called
520     // during configure() call. on success the configured layering is set in |outputFormat|. If
521     // |outputFormat| is mOutputFormat, it is copied to trigger an output format changed event.
522     status_t configureTemporalLayers(
523             const sp<AMessage> &msg, bool inConfigure, sp<AMessage> &outputFormat);
524 
525     status_t setMinBufferSize(OMX_U32 portIndex, size_t size);
526 
527     status_t setupMPEG4EncoderParameters(const sp<AMessage> &msg);
528     status_t setupH263EncoderParameters(const sp<AMessage> &msg);
529     status_t setupAVCEncoderParameters(const sp<AMessage> &msg);
530     status_t setupHEVCEncoderParameters(const sp<AMessage> &msg, sp<AMessage> &outputFormat);
531     status_t setupVPXEncoderParameters(const sp<AMessage> &msg, sp<AMessage> &outputFormat);
532 
533     status_t verifySupportForProfileAndLevel(
534             OMX_U32 portIndex, int32_t profile, int32_t level);
535 
536     status_t configureImageGrid(const sp<AMessage> &msg, sp<AMessage> &outputFormat);
537     status_t configureBitrate(
538             OMX_VIDEO_CONTROLRATETYPE bitrateMode, int32_t bitrate, int32_t quality = 0);
539     void configureEncoderLatency(const sp<AMessage> &msg);
540 
541     status_t setupErrorCorrectionParameters();
542 
543     // Returns true iff all buffers on the given port have status
544     // OWNED_BY_US or OWNED_BY_NATIVE_WINDOW.
545     bool allYourBuffersAreBelongToUs(OMX_U32 portIndex);
546 
547     bool allYourBuffersAreBelongToUs();
548 
549     void waitUntilAllPossibleNativeWindowBuffersAreReturnedToUs();
550 
551     size_t countBuffersOwnedByComponent(OMX_U32 portIndex) const;
552     size_t countBuffersOwnedByNativeWindow() const;
553 
554     void deferMessage(const sp<AMessage> &msg);
555     void processDeferredMessages();
556 
557     void onFrameRendered(int64_t mediaTimeUs, nsecs_t systemNano);
558     // called when we have dequeued a buffer |buf| from the native window to track render info.
559     // |fenceFd| is the dequeue fence, and |info| points to the buffer info where this buffer is
560     // stored.
561     void updateRenderInfoForDequeuedBuffer(
562             ANativeWindowBuffer *buf, int fenceFd, BufferInfo *info);
563 
564     // Checks to see if any frames have rendered up until |until|, and to notify client
565     // (MediaCodec) of rendered frames up-until the frame pointed to by |until| or the first
566     // unrendered frame. These frames are removed from the render queue.
567     // If |dropIncomplete| is true, unrendered frames up-until |until| will be dropped from the
568     // queue, allowing all rendered framed up till then to be notified of.
569     // (This will effectively clear the render queue up-until (and including) |until|.)
570     // If |until| is NULL, or is not in the rendered queue, this method will check all frames.
571     void notifyOfRenderedFrames(
572             bool dropIncomplete = false, FrameRenderTracker::Info *until = NULL);
573 
574     // Pass |expectedFormat| to print a warning if the format differs from it.
575     // Using sp<> instead of const sp<>& because expectedFormat is likely the current mOutputFormat
576     // which will get updated inside.
577     void onOutputFormatChanged(sp<const AMessage> expectedFormat = NULL);
578     void addKeyFormatChangesToRenderBufferNotification(sp<AMessage> &notify);
579     void sendFormatChange();
580 
581     status_t getPortFormat(OMX_U32 portIndex, sp<AMessage> &notify);
582 
583     void signalError(
584             OMX_ERRORTYPE error = OMX_ErrorUndefined,
585             status_t internalError = UNKNOWN_ERROR);
586 
587     status_t requestIDRFrame();
588     status_t setParameters(const sp<AMessage> &params);
589 
590     // set vendor extension parameters specified in params that are supported by the codec
591     status_t setVendorParameters(const sp<AMessage> &params);
592 
593     // get vendor extension parameters supported by the codec for a specific port and add it to
594     // |format|
595     status_t getVendorParameters(OMX_U32 portIndex, sp<AMessage> &format);
596 
597     // Send EOS on input stream.
598     void onSignalEndOfInputStream();
599 
600     // Force EXEC->IDLE->LOADED shutdown sequence if not stale.
601     void forceStateTransition(int generation);
602 
603     DISALLOW_EVIL_CONSTRUCTORS(ACodec);
604 };
605 
606 }  // namespace android
607 
608 #endif  // A_CODEC_H_
609