Home | History | Annotate | Download | only in enc
      1 /*
      2  * Copyright (C) 2012 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 SOFT_AVC_ENCODER_H_
     18 #define SOFT_AVC_ENCODER_H_
     19 
     20 #include <media/stagefright/MediaBuffer.h>
     21 #include <media/stagefright/foundation/ABase.h>
     22 #include <utils/Vector.h>
     23 
     24 #include "avcenc_api.h"
     25 #include "SimpleSoftOMXComponent.h"
     26 
     27 namespace android {
     28 
     29 struct MediaBuffer;
     30 
     31 struct SoftAVCEncoder : public MediaBufferObserver,
     32                         public SimpleSoftOMXComponent {
     33     SoftAVCEncoder(
     34             const char *name,
     35             const OMX_CALLBACKTYPE *callbacks,
     36             OMX_PTR appData,
     37             OMX_COMPONENTTYPE **component);
     38 
     39     // Override SimpleSoftOMXComponent methods
     40     virtual OMX_ERRORTYPE internalGetParameter(
     41             OMX_INDEXTYPE index, OMX_PTR params);
     42 
     43     virtual OMX_ERRORTYPE internalSetParameter(
     44             OMX_INDEXTYPE index, const OMX_PTR params);
     45 
     46     virtual void onQueueFilled(OMX_U32 portIndex);
     47 
     48     // Override SoftOMXComponent methods
     49 
     50     virtual OMX_ERRORTYPE getExtensionIndex(
     51             const char *name, OMX_INDEXTYPE *index);
     52 
     53     // Implement MediaBufferObserver
     54     virtual void signalBufferReturned(MediaBuffer *buffer);
     55 
     56 
     57     // Callbacks required by PV's encoder
     58     int32_t allocOutputBuffers(unsigned int sizeInMbs, unsigned int numBuffers);
     59     void    unbindOutputBuffer(int32_t index);
     60     int32_t bindOutputBuffer(int32_t index, uint8_t **yuv);
     61 
     62 protected:
     63     virtual ~SoftAVCEncoder();
     64 
     65 private:
     66     enum {
     67         kNumBuffers = 2,
     68     };
     69 
     70     enum {
     71         kStoreMetaDataExtensionIndex = OMX_IndexVendorStartUnused + 1
     72     };
     73 
     74     // OMX input buffer's timestamp and flags
     75     typedef struct {
     76         int64_t mTimeUs;
     77         int32_t mFlags;
     78     } InputBufferInfo;
     79 
     80     int32_t  mVideoWidth;
     81     int32_t  mVideoHeight;
     82     int32_t  mVideoFrameRate;
     83     int32_t  mVideoBitRate;
     84     int32_t  mVideoColorFormat;
     85     bool     mStoreMetaDataInBuffers;
     86     int32_t  mIDRFrameRefreshIntervalInSec;
     87     AVCProfile mAVCEncProfile;
     88     AVCLevel   mAVCEncLevel;
     89 
     90     int64_t  mNumInputFrames;
     91     int64_t  mPrevTimestampUs;
     92     bool     mStarted;
     93     bool     mSpsPpsHeaderReceived;
     94     bool     mReadyForNextFrame;
     95     bool     mSawInputEOS;
     96     bool     mSignalledError;
     97     bool     mIsIDRFrame;
     98 
     99     tagAVCHandle          *mHandle;
    100     tagAVCEncParam        *mEncParams;
    101     uint8_t               *mInputFrameData;
    102     uint32_t              *mSliceGroup;
    103     Vector<MediaBuffer *> mOutputBuffers;
    104     Vector<InputBufferInfo> mInputBufferInfoVec;
    105 
    106     void initPorts();
    107     OMX_ERRORTYPE initEncParams();
    108     OMX_ERRORTYPE initEncoder();
    109     OMX_ERRORTYPE releaseEncoder();
    110     void releaseOutputBuffers();
    111 
    112     uint8_t* extractGrallocData(void *data, buffer_handle_t *buffer);
    113     void releaseGrallocData(buffer_handle_t buffer);
    114 
    115     DISALLOW_EVIL_CONSTRUCTORS(SoftAVCEncoder);
    116 };
    117 
    118 }  // namespace android
    119 
    120 #endif  // SOFT_AVC_ENCODER_H_
    121