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