ACodec.h revision b77d03b62cb743d0faf74b54aa466b4d220b5e61
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/hardware/MetadataBufferType.h> 24#include <media/IOMX.h> 25#include <media/stagefright/foundation/AHierarchicalStateMachine.h> 26#include <media/stagefright/CodecBase.h> 27#include <media/stagefright/FrameRenderTracker.h> 28#include <media/stagefright/SkipCutBuffer.h> 29#include <utils/NativeHandle.h> 30#include <OMX_Audio.h> 31 32#define TRACK_BUFFER_TIMING 0 33 34namespace android { 35 36struct ABuffer; 37struct MemoryDealer; 38struct DescribeColorFormat2Params; 39 40struct ACodec : public AHierarchicalStateMachine, public CodecBase { 41 ACodec(); 42 43 virtual void setNotificationMessage(const sp<AMessage> &msg); 44 45 void initiateSetup(const sp<AMessage> &msg); 46 47 virtual void initiateAllocateComponent(const sp<AMessage> &msg); 48 virtual void initiateConfigureComponent(const sp<AMessage> &msg); 49 virtual void initiateCreateInputSurface(); 50 virtual void initiateSetInputSurface(const sp<PersistentSurface> &surface); 51 virtual void initiateStart(); 52 virtual void initiateShutdown(bool keepComponentAllocated = false); 53 54 virtual status_t queryCapabilities( 55 const AString &name, const AString &mime, bool isEncoder, 56 sp<MediaCodecInfo::Capabilities> *caps); 57 58 virtual status_t setSurface(const sp<Surface> &surface); 59 60 virtual void signalFlush(); 61 virtual void signalResume(); 62 63 virtual void signalSetParameters(const sp<AMessage> &msg); 64 virtual void signalEndOfInputStream(); 65 virtual void signalRequestIDRFrame(); 66 67 // AHierarchicalStateMachine implements the message handling 68 virtual void onMessageReceived(const sp<AMessage> &msg) { 69 handleMessage(msg); 70 } 71 72 struct PortDescription : public CodecBase::PortDescription { 73 size_t countBuffers(); 74 IOMX::buffer_id bufferIDAt(size_t index) const; 75 sp<ABuffer> bufferAt(size_t index) const; 76 sp<NativeHandle> handleAt(size_t index) const; 77 sp<RefBase> memRefAt(size_t index) const; 78 79 private: 80 friend struct ACodec; 81 82 Vector<IOMX::buffer_id> mBufferIDs; 83 Vector<sp<ABuffer> > mBuffers; 84 Vector<sp<NativeHandle> > mHandles; 85 Vector<sp<RefBase> > mMemRefs; 86 87 PortDescription(); 88 void addBuffer( 89 IOMX::buffer_id id, const sp<ABuffer> &buffer, 90 const sp<NativeHandle> &handle, const sp<RefBase> &memRef); 91 92 DISALLOW_EVIL_CONSTRUCTORS(PortDescription); 93 }; 94 95 static bool isFlexibleColorFormat( 96 const sp<IOMX> &omx, IOMX::node_id node, 97 uint32_t colorFormat, bool usingNativeBuffers, OMX_U32 *flexibleEquivalent); 98 99 // Returns 0 if configuration is not supported. NOTE: this is treated by 100 // some OMX components as auto level, and by others as invalid level. 101 static int /* OMX_VIDEO_AVCLEVELTYPE */ getAVCLevelFor( 102 int width, int height, int rate, int bitrate, 103 OMX_VIDEO_AVCPROFILETYPE profile = OMX_VIDEO_AVCProfileBaseline); 104 105 // Quirk still supported, even though deprecated 106 enum Quirks { 107 kRequiresAllocateBufferOnInputPorts = 1, 108 kRequiresAllocateBufferOnOutputPorts = 2, 109 }; 110 111 static status_t getOMXChannelMapping(size_t numChannels, OMX_AUDIO_CHANNELTYPE map[]); 112 113protected: 114 virtual ~ACodec(); 115 116private: 117 struct BaseState; 118 struct UninitializedState; 119 struct LoadedState; 120 struct LoadedToIdleState; 121 struct IdleToExecutingState; 122 struct ExecutingState; 123 struct OutputPortSettingsChangedState; 124 struct ExecutingToIdleState; 125 struct IdleToLoadedState; 126 struct FlushingState; 127 struct DeathNotifier; 128 129 enum { 130 kWhatSetup = 'setu', 131 kWhatOMXMessage = 'omx ', 132 // same as kWhatOMXMessage - but only used with 133 // handleMessage during OMX message-list handling 134 kWhatOMXMessageItem = 'omxI', 135 kWhatOMXMessageList = 'omxL', 136 kWhatInputBufferFilled = 'inpF', 137 kWhatOutputBufferDrained = 'outD', 138 kWhatShutdown = 'shut', 139 kWhatFlush = 'flus', 140 kWhatResume = 'resm', 141 kWhatDrainDeferredMessages = 'drai', 142 kWhatAllocateComponent = 'allo', 143 kWhatConfigureComponent = 'conf', 144 kWhatSetSurface = 'setS', 145 kWhatCreateInputSurface = 'cisf', 146 kWhatSetInputSurface = 'sisf', 147 kWhatSignalEndOfInputStream = 'eois', 148 kWhatStart = 'star', 149 kWhatRequestIDRFrame = 'ridr', 150 kWhatSetParameters = 'setP', 151 kWhatSubmitOutputMetadataBufferIfEOS = 'subm', 152 kWhatOMXDied = 'OMXd', 153 kWhatReleaseCodecInstance = 'relC', 154 }; 155 156 enum { 157 kPortIndexInput = 0, 158 kPortIndexOutput = 1 159 }; 160 161 enum { 162 kFlagIsSecure = 1, 163 kFlagPushBlankBuffersToNativeWindowOnShutdown = 2, 164 kFlagIsGrallocUsageProtected = 4, 165 }; 166 167 enum { 168 kVideoGrallocUsage = (GRALLOC_USAGE_HW_TEXTURE 169 | GRALLOC_USAGE_HW_COMPOSER 170 | GRALLOC_USAGE_EXTERNAL_DISP), 171 }; 172 173 struct BufferInfo { 174 enum Status { 175 OWNED_BY_US, 176 OWNED_BY_COMPONENT, 177 OWNED_BY_UPSTREAM, 178 OWNED_BY_DOWNSTREAM, 179 OWNED_BY_NATIVE_WINDOW, 180 UNRECOGNIZED, // not a tracked buffer 181 }; 182 183 static inline Status getSafeStatus(BufferInfo *info) { 184 return info == NULL ? UNRECOGNIZED : info->mStatus; 185 } 186 187 IOMX::buffer_id mBufferID; 188 Status mStatus; 189 unsigned mDequeuedAt; 190 191 sp<ABuffer> mData; 192 sp<RefBase> mMemRef; 193 sp<GraphicBuffer> mGraphicBuffer; 194 sp<NativeHandle> mNativeHandle; 195 int mFenceFd; 196 FrameRenderTracker::Info *mRenderInfo; 197 198 // The following field and 4 methods are used for debugging only 199 bool mIsReadFence; 200 // Store |fenceFd| and set read/write flag. Log error, if there is already a fence stored. 201 void setReadFence(int fenceFd, const char *dbg); 202 void setWriteFence(int fenceFd, const char *dbg); 203 // Log error, if the current fence is not a read/write fence. 204 void checkReadFence(const char *dbg); 205 void checkWriteFence(const char *dbg); 206 }; 207 208 static const char *_asString(BufferInfo::Status s); 209 void dumpBuffers(OMX_U32 portIndex); 210 211 // If |fd| is non-negative, waits for fence with |fd| and logs an error if it fails. Returns 212 // the error code or OK on success. If |fd| is negative, it returns OK 213 status_t waitForFence(int fd, const char *dbg); 214 215#if TRACK_BUFFER_TIMING 216 struct BufferStats { 217 int64_t mEmptyBufferTimeUs; 218 int64_t mFillBufferDoneTimeUs; 219 }; 220 221 KeyedVector<int64_t, BufferStats> mBufferStats; 222#endif 223 224 sp<AMessage> mNotify; 225 226 sp<UninitializedState> mUninitializedState; 227 sp<LoadedState> mLoadedState; 228 sp<LoadedToIdleState> mLoadedToIdleState; 229 sp<IdleToExecutingState> mIdleToExecutingState; 230 sp<ExecutingState> mExecutingState; 231 sp<OutputPortSettingsChangedState> mOutputPortSettingsChangedState; 232 sp<ExecutingToIdleState> mExecutingToIdleState; 233 sp<IdleToLoadedState> mIdleToLoadedState; 234 sp<FlushingState> mFlushingState; 235 sp<SkipCutBuffer> mSkipCutBuffer; 236 237 AString mComponentName; 238 uint32_t mFlags; 239 uint32_t mQuirks; 240 sp<IOMX> mOMX; 241 sp<IBinder> mNodeBinder; 242 IOMX::node_id mNode; 243 sp<MemoryDealer> mDealer[2]; 244 245 bool mUsingNativeWindow; 246 sp<ANativeWindow> mNativeWindow; 247 int mNativeWindowUsageBits; 248 sp<AMessage> mConfigFormat; 249 sp<AMessage> mInputFormat; 250 sp<AMessage> mOutputFormat; 251 sp<AMessage> mBaseOutputFormat; 252 253 FrameRenderTracker mRenderTracker; // render information for buffers rendered by ACodec 254 Vector<BufferInfo> mBuffers[2]; 255 bool mPortEOS[2]; 256 status_t mInputEOSResult; 257 258 List<sp<AMessage> > mDeferredQueue; 259 260 sp<AMessage> mLastOutputFormat; 261 bool mIsVideo; 262 bool mIsEncoder; 263 bool mFatalError; 264 bool mShutdownInProgress; 265 bool mExplicitShutdown; 266 267 // If "mKeepComponentAllocated" we only transition back to Loaded state 268 // and do not release the component instance. 269 bool mKeepComponentAllocated; 270 271 int32_t mEncoderDelay; 272 int32_t mEncoderPadding; 273 int32_t mRotationDegrees; 274 275 bool mChannelMaskPresent; 276 int32_t mChannelMask; 277 unsigned mDequeueCounter; 278 MetadataBufferType mInputMetadataType; 279 MetadataBufferType mOutputMetadataType; 280 bool mLegacyAdaptiveExperiment; 281 int32_t mMetadataBuffersToSubmit; 282 size_t mNumUndequeuedBuffers; 283 284 int64_t mRepeatFrameDelayUs; 285 int64_t mMaxPtsGapUs; 286 float mMaxFps; 287 288 int64_t mTimePerFrameUs; 289 int64_t mTimePerCaptureUs; 290 291 bool mCreateInputBuffersSuspended; 292 293 bool mTunneled; 294 295 OMX_INDEXTYPE mDescribeColorAspectsIndex; 296 297 status_t setCyclicIntraMacroblockRefresh(const sp<AMessage> &msg, int32_t mode); 298 status_t allocateBuffersOnPort(OMX_U32 portIndex); 299 status_t freeBuffersOnPort(OMX_U32 portIndex); 300 status_t freeBuffer(OMX_U32 portIndex, size_t i); 301 302 status_t handleSetSurface(const sp<Surface> &surface); 303 status_t setupNativeWindowSizeFormatAndUsage( 304 ANativeWindow *nativeWindow /* nonnull */, int *finalUsage /* nonnull */); 305 306 status_t configureOutputBuffersFromNativeWindow( 307 OMX_U32 *nBufferCount, OMX_U32 *nBufferSize, 308 OMX_U32 *nMinUndequeuedBuffers); 309 status_t allocateOutputMetadataBuffers(); 310 status_t submitOutputMetadataBuffer(); 311 void signalSubmitOutputMetadataBufferIfEOS_workaround(); 312 status_t allocateOutputBuffersFromNativeWindow(); 313 status_t cancelBufferToNativeWindow(BufferInfo *info); 314 status_t freeOutputBuffersNotOwnedByComponent(); 315 BufferInfo *dequeueBufferFromNativeWindow(); 316 317 inline bool storingMetadataInDecodedBuffers() { 318 return mOutputMetadataType >= 0 && !mIsEncoder; 319 } 320 321 inline bool usingMetadataOnEncoderOutput() { 322 return mOutputMetadataType >= 0 && mIsEncoder; 323 } 324 325 BufferInfo *findBufferByID( 326 uint32_t portIndex, IOMX::buffer_id bufferID, 327 ssize_t *index = NULL); 328 329 status_t setComponentRole(bool isEncoder, const char *mime); 330 static const char *getComponentRole(bool isEncoder, const char *mime); 331 static status_t setComponentRole( 332 const sp<IOMX> &omx, IOMX::node_id node, const char *role); 333 334 status_t configureCodec(const char *mime, const sp<AMessage> &msg); 335 336 status_t configureTunneledVideoPlayback(int32_t audioHwSync, 337 const sp<ANativeWindow> &nativeWindow); 338 339 status_t setVideoPortFormatType( 340 OMX_U32 portIndex, 341 OMX_VIDEO_CODINGTYPE compressionFormat, 342 OMX_COLOR_FORMATTYPE colorFormat, 343 bool usingNativeBuffers = false); 344 345 status_t setSupportedOutputFormat(bool getLegacyFlexibleFormat); 346 347 status_t setupVideoDecoder( 348 const char *mime, const sp<AMessage> &msg, bool usingNativeBuffers, bool haveSwRenderer, 349 sp<AMessage> &outputformat); 350 351 status_t setupVideoEncoder( 352 const char *mime, const sp<AMessage> &msg, 353 sp<AMessage> &outputformat, sp<AMessage> &inputformat); 354 355 status_t setVideoFormatOnPort( 356 OMX_U32 portIndex, 357 int32_t width, int32_t height, 358 OMX_VIDEO_CODINGTYPE compressionFormat, float frameRate = -1.0); 359 360 // gets index or sets it to 0 on error. Returns error from codec. 361 status_t initDescribeColorAspectsIndex(); 362 363 // sets |params|. If |readBack| is true, it re-gets them afterwards if set succeeded. 364 // returns the codec error. 365 status_t setCodecColorAspects(DescribeColorAspectsParams ¶ms, bool readBack = false); 366 367 // gets |params|; returns the codec error. |param| should not change on error. 368 status_t getCodecColorAspects(DescribeColorAspectsParams ¶ms); 369 370 // gets dataspace guidance from codec and platform. |params| should be set up with the color 371 // aspects to use. If |tryCodec| is true, the codec is queried first. If it succeeds, we 372 // return OK. Otherwise, we fall back to the platform guidance and return the codec error; 373 // though, we return OK if the codec failed with UNSUPPORTED, as codec guidance is optional. 374 status_t getDataSpace( 375 DescribeColorAspectsParams ¶ms, android_dataspace *dataSpace /* nonnull */, 376 bool tryCodec); 377 378 // sets color aspects for the encoder for certain |width/height| based on |configFormat|, and 379 // set resulting color config into |outputFormat|. If |usingNativeWindow| is true, we use 380 // video defaults if config is unspecified. Returns error from the codec. 381 status_t setColorAspectsForVideoDecoder( 382 int32_t width, int32_t height, bool usingNativeWindow, 383 const sp<AMessage> &configFormat, sp<AMessage> &outputFormat); 384 385 // gets color aspects for the encoder for certain |width/height| based on |configFormat|, and 386 // set resulting color config into |outputFormat|. If |dataSpace| is non-null, it requests 387 // dataspace guidance from the codec and platform and sets it into |dataSpace|. Returns the 388 // error from the codec. 389 status_t getColorAspectsAndDataSpaceForVideoDecoder( 390 int32_t width, int32_t height, const sp<AMessage> &configFormat, 391 sp<AMessage> &outputFormat, android_dataspace *dataSpace); 392 393 // sets color aspects for the video encoder assuming bytebuffer mode for certain |configFormat| 394 // and sets resulting color config into |outputFormat|. For mediarecorder, also set dataspace 395 // into |inputFormat|. Returns the error from the codec. 396 status_t setColorAspectsForVideoEncoder( 397 const sp<AMessage> &configFormat, 398 sp<AMessage> &outputFormat, sp<AMessage> &inputFormat); 399 400 // sets color aspects for the video encoder in surface mode. This basically sets the default 401 // video values for unspecified aspects and sets the dataspace to use in the input format. 402 // Also sets the dataspace into |dataSpace|. 403 // Returns any codec errors during this configuration, except for optional steps. 404 status_t setInitialColorAspectsForVideoEncoderSurfaceAndGetDataSpace( 405 android_dataspace *dataSpace /* nonnull */); 406 407 // gets color aspects for the video encoder input port and sets them into the |format|. 408 // Returns any codec errors. 409 status_t getInputColorAspectsForVideoEncoder(sp<AMessage> &format); 410 411 // updates the encoder output format with |aspects| defaulting to |dataSpace| for 412 // unspecified values. 413 void onDataSpaceChanged(android_dataspace dataSpace, const ColorAspects &aspects); 414 415 typedef struct drcParams { 416 int32_t drcCut; 417 int32_t drcBoost; 418 int32_t heavyCompression; 419 int32_t targetRefLevel; 420 int32_t encodedTargetLevel; 421 } drcParams_t; 422 423 status_t setupAACCodec( 424 bool encoder, 425 int32_t numChannels, int32_t sampleRate, int32_t bitRate, 426 int32_t aacProfile, bool isADTS, int32_t sbrMode, 427 int32_t maxOutputChannelCount, const drcParams_t& drc, 428 int32_t pcmLimiterEnable); 429 430 status_t setupAC3Codec(bool encoder, int32_t numChannels, int32_t sampleRate); 431 432 status_t setupEAC3Codec(bool encoder, int32_t numChannels, int32_t sampleRate); 433 434 status_t selectAudioPortFormat( 435 OMX_U32 portIndex, OMX_AUDIO_CODINGTYPE desiredFormat); 436 437 status_t setupAMRCodec(bool encoder, bool isWAMR, int32_t bitRate); 438 status_t setupG711Codec(bool encoder, int32_t sampleRate, int32_t numChannels); 439 440 status_t setupFlacCodec( 441 bool encoder, int32_t numChannels, int32_t sampleRate, int32_t compressionLevel); 442 443 status_t setupRawAudioFormat( 444 OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels); 445 446 status_t setPriority(int32_t priority); 447 status_t setOperatingRate(float rateFloat, bool isVideo); 448 status_t getIntraRefreshPeriod(uint32_t *intraRefreshPeriod); 449 status_t setIntraRefreshPeriod(uint32_t intraRefreshPeriod, bool inConfigure); 450 451 status_t setMinBufferSize(OMX_U32 portIndex, size_t size); 452 453 status_t setupMPEG4EncoderParameters(const sp<AMessage> &msg); 454 status_t setupH263EncoderParameters(const sp<AMessage> &msg); 455 status_t setupAVCEncoderParameters(const sp<AMessage> &msg); 456 status_t setupHEVCEncoderParameters(const sp<AMessage> &msg); 457 status_t setupVPXEncoderParameters(const sp<AMessage> &msg); 458 459 status_t verifySupportForProfileAndLevel(int32_t profile, int32_t level); 460 461 status_t configureBitrate( 462 int32_t bitrate, OMX_VIDEO_CONTROLRATETYPE bitrateMode); 463 464 status_t setupErrorCorrectionParameters(); 465 466 status_t initNativeWindow(); 467 468 // Returns true iff all buffers on the given port have status 469 // OWNED_BY_US or OWNED_BY_NATIVE_WINDOW. 470 bool allYourBuffersAreBelongToUs(OMX_U32 portIndex); 471 472 bool allYourBuffersAreBelongToUs(); 473 474 void waitUntilAllPossibleNativeWindowBuffersAreReturnedToUs(); 475 476 size_t countBuffersOwnedByComponent(OMX_U32 portIndex) const; 477 size_t countBuffersOwnedByNativeWindow() const; 478 479 void deferMessage(const sp<AMessage> &msg); 480 void processDeferredMessages(); 481 482 void onFrameRendered(int64_t mediaTimeUs, nsecs_t systemNano); 483 // called when we have dequeued a buffer |buf| from the native window to track render info. 484 // |fenceFd| is the dequeue fence, and |info| points to the buffer info where this buffer is 485 // stored. 486 void updateRenderInfoForDequeuedBuffer( 487 ANativeWindowBuffer *buf, int fenceFd, BufferInfo *info); 488 489 // Checks to see if any frames have rendered up until |until|, and to notify client 490 // (MediaCodec) of rendered frames up-until the frame pointed to by |until| or the first 491 // unrendered frame. These frames are removed from the render queue. 492 // If |dropIncomplete| is true, unrendered frames up-until |until| will be dropped from the 493 // queue, allowing all rendered framed up till then to be notified of. 494 // (This will effectively clear the render queue up-until (and including) |until|.) 495 // If |until| is NULL, or is not in the rendered queue, this method will check all frames. 496 void notifyOfRenderedFrames( 497 bool dropIncomplete = false, FrameRenderTracker::Info *until = NULL); 498 499 void onOutputFormatChanged(); 500 void addKeyFormatChangesToRenderBufferNotification(sp<AMessage> ¬ify); 501 void sendFormatChange(); 502 503 status_t getPortFormat(OMX_U32 portIndex, sp<AMessage> ¬ify); 504 505 void signalError( 506 OMX_ERRORTYPE error = OMX_ErrorUndefined, 507 status_t internalError = UNKNOWN_ERROR); 508 509 static bool describeDefaultColorFormat(DescribeColorFormat2Params &describeParams); 510 static bool describeColorFormat( 511 const sp<IOMX> &omx, IOMX::node_id node, 512 DescribeColorFormat2Params &describeParams); 513 514 status_t requestIDRFrame(); 515 status_t setParameters(const sp<AMessage> ¶ms); 516 517 // Send EOS on input stream. 518 void onSignalEndOfInputStream(); 519 520 DISALLOW_EVIL_CONSTRUCTORS(ACodec); 521}; 522 523} // namespace android 524 525#endif // A_CODEC_H_ 526