Home | History | Annotate | Download | only in avcenc
      1 /*
      2  * Copyright 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_ENC_H__
     18 #define __SOFT_AVC_ENC_H__
     19 
     20 
     21 #include <media/stagefright/foundation/ABase.h>
     22 #include <utils/Vector.h>
     23 
     24 #include "SoftVideoEncoderOMXComponent.h"
     25 
     26 namespace android {
     27 
     28 #define MAX_INPUT_BUFFER_HEADERS 4
     29 #define MAX_CONVERSION_BUFFERS   4
     30 #define CODEC_MAX_CORES          4
     31 #define LEN_STATUS_BUFFER        (10  * 1024)
     32 #define MAX_VBV_BUFF_SIZE        (120 * 16384)
     33 #define MAX_NUM_IO_BUFS           3
     34 
     35 #define DEFAULT_MAX_REF_FRM         2
     36 #define DEFAULT_MAX_REORDER_FRM     0
     37 #define DEFAULT_QP_MIN              10
     38 #define DEFAULT_QP_MAX              40
     39 #define DEFAULT_MAX_BITRATE         20000000
     40 #define DEFAULT_MAX_SRCH_RANGE_X    256
     41 #define DEFAULT_MAX_SRCH_RANGE_Y    256
     42 #define DEFAULT_MAX_FRAMERATE       120000
     43 #define DEFAULT_NUM_CORES           1
     44 #define DEFAULT_NUM_CORES_PRE_ENC   0
     45 #define DEFAULT_FPS                 30
     46 #define DEFAULT_ENC_SPEED           IVE_NORMAL
     47 
     48 #define DEFAULT_MEM_REC_CNT         0
     49 #define DEFAULT_RECON_ENABLE        0
     50 #define DEFAULT_CHKSUM_ENABLE       0
     51 #define DEFAULT_START_FRM           0
     52 #define DEFAULT_NUM_FRMS            0xFFFFFFFF
     53 #define DEFAULT_INP_COLOR_FORMAT       IV_YUV_420SP_VU
     54 #define DEFAULT_RECON_COLOR_FORMAT     IV_YUV_420P
     55 #define DEFAULT_LOOPBACK            0
     56 #define DEFAULT_SRC_FRAME_RATE      30
     57 #define DEFAULT_TGT_FRAME_RATE      30
     58 #define DEFAULT_MAX_WD              1920
     59 #define DEFAULT_MAX_HT              1920
     60 #define DEFAULT_MAX_LEVEL           41
     61 #define DEFAULT_STRIDE              0
     62 #define DEFAULT_WD                  1280
     63 #define DEFAULT_HT                  720
     64 #define DEFAULT_PSNR_ENABLE         0
     65 #define DEFAULT_ME_SPEED            100
     66 #define DEFAULT_ENABLE_FAST_SAD     0
     67 #define DEFAULT_ENABLE_ALT_REF      0
     68 #define DEFAULT_RC_MODE             IVE_RC_STORAGE
     69 #define DEFAULT_BITRATE             6000000
     70 #define DEFAULT_I_QP                22
     71 #define DEFAULT_I_QP_MAX            DEFAULT_QP_MAX
     72 #define DEFAULT_I_QP_MIN            DEFAULT_QP_MIN
     73 #define DEFAULT_P_QP                28
     74 #define DEFAULT_P_QP_MAX            DEFAULT_QP_MAX
     75 #define DEFAULT_P_QP_MIN            DEFAULT_QP_MIN
     76 #define DEFAULT_B_QP                22
     77 #define DEFAULT_B_QP_MAX            DEFAULT_QP_MAX
     78 #define DEFAULT_B_QP_MIN            DEFAULT_QP_MIN
     79 #define DEFAULT_AIR                 IVE_AIR_MODE_NONE
     80 #define DEFAULT_AIR_REFRESH_PERIOD  30
     81 #define DEFAULT_SRCH_RNG_X          64
     82 #define DEFAULT_SRCH_RNG_Y          48
     83 #define DEFAULT_I_INTERVAL          30
     84 #define DEFAULT_IDR_INTERVAL        1000
     85 #define DEFAULT_B_FRAMES            0
     86 #define DEFAULT_DISABLE_DEBLK_LEVEL 0
     87 #define DEFAULT_HPEL                1
     88 #define DEFAULT_QPEL                1
     89 #define DEFAULT_I4                  1
     90 #define DEFAULT_EPROFILE            IV_PROFILE_BASE
     91 #define DEFAULT_ENTROPY_MODE        0
     92 #define DEFAULT_SLICE_MODE          IVE_SLICE_MODE_NONE
     93 #define DEFAULT_SLICE_PARAM         256
     94 #define DEFAULT_ARCH                ARCH_ARM_A9Q
     95 #define DEFAULT_SOC                 SOC_GENERIC
     96 #define DEFAULT_INTRA4x4            0
     97 #define STRLENGTH                   500
     98 #define DEFAULT_CONSTRAINED_INTRA   0
     99 
    100 #define MIN(a, b) ((a) < (b))? (a) : (b)
    101 #define MAX(a, b) ((a) > (b))? (a) : (b)
    102 #define ALIGN16(x) ((((x) + 15) >> 4) << 4)
    103 #define ALIGN128(x) ((((x) + 127) >> 7) << 7)
    104 #define ALIGN4096(x) ((((x) + 4095) >> 12) << 12)
    105 
    106 /** Used to remove warnings about unused parameters */
    107 #define UNUSED(x) ((void)(x))
    108 
    109 /** Get time */
    110 #define GETTIME(a, b) gettimeofday(a, b);
    111 
    112 /** Compute difference between start and end */
    113 #define TIME_DIFF(start, end, diff) \
    114     diff = ((end.tv_sec - start.tv_sec) * 1000000) + \
    115             (end.tv_usec - start.tv_usec);
    116 
    117 #define ive_aligned_malloc(alignment, size) memalign(alignment, size)
    118 #define ive_aligned_free(buf) free(buf)
    119 
    120 struct SoftAVC : public SoftVideoEncoderOMXComponent {
    121     SoftAVC(
    122             const char *name,
    123             const OMX_CALLBACKTYPE *callbacks,
    124             OMX_PTR appData,
    125             OMX_COMPONENTTYPE **component);
    126 
    127     // Override SimpleSoftOMXComponent methods
    128     virtual OMX_ERRORTYPE internalGetParameter(
    129             OMX_INDEXTYPE index, OMX_PTR params);
    130 
    131     virtual OMX_ERRORTYPE internalSetParameter(
    132             OMX_INDEXTYPE index, const OMX_PTR params);
    133 
    134     virtual void onQueueFilled(OMX_U32 portIndex);
    135 
    136 protected:
    137     virtual ~SoftAVC();
    138 
    139 private:
    140     enum {
    141         kNumBuffers = 2,
    142     };
    143 
    144     enum {
    145         kUpdateBitrate            = 1 << 0,
    146         kRequestKeyFrame          = 1 << 1,
    147         kUpdateAIRMode            = 1 << 2,
    148     };
    149 
    150     // OMX input buffer's timestamp and flags
    151     typedef struct {
    152         int64_t mTimeUs;
    153         int32_t mFlags;
    154     } InputBufferInfo;
    155 
    156     int32_t  mStride;
    157 
    158     struct timeval mTimeStart;   // Time at the start of decode()
    159     struct timeval mTimeEnd;     // Time at the end of decode()
    160 
    161     int mUpdateFlag;
    162 
    163 #ifdef FILE_DUMP_ENABLE
    164     char mInFile[200];
    165     char mOutFile[200];
    166 #endif /* FILE_DUMP_ENABLE */
    167 
    168     IV_COLOR_FORMAT_T mIvVideoColorFormat;
    169 
    170     IV_PROFILE_T mAVCEncProfile;
    171     WORD32   mAVCEncLevel;
    172     bool     mStarted;
    173     bool     mSpsPpsHeaderReceived;
    174 
    175     bool     mSawInputEOS;
    176     bool     mSawOutputEOS;
    177     bool     mSignalledError;
    178     bool     mIntra4x4;
    179     bool     mEnableFastSad;
    180     bool     mEnableAltRef;
    181     bool     mReconEnable;
    182     bool     mPSNREnable;
    183     bool     mEntropyMode;
    184     bool     mConstrainedIntraFlag;
    185     IVE_SPEED_CONFIG     mEncSpeed;
    186 
    187     uint8_t *mConversionBuffers[MAX_CONVERSION_BUFFERS];
    188     bool     mConversionBuffersFree[MAX_CONVERSION_BUFFERS];
    189     BufferInfo *mInputBufferInfo[MAX_INPUT_BUFFER_HEADERS];
    190     iv_obj_t *mCodecCtx;         // Codec context
    191     iv_mem_rec_t *mMemRecords;   // Memory records requested by the codec
    192     size_t mNumMemRecords;       // Number of memory records requested by codec
    193     size_t mNumCores;            // Number of cores used by the codec
    194 
    195     UWORD32 mHeaderGenerated;
    196     UWORD32 mBframes;
    197     IV_ARCH_T mArch;
    198     IVE_SLICE_MODE_T mSliceMode;
    199     UWORD32 mSliceParam;
    200     bool mHalfPelEnable;
    201     UWORD32 mIInterval;
    202     UWORD32 mIDRInterval;
    203     UWORD32 mDisableDeblkLevel;
    204     IVE_AIR_MODE_T mAIRMode;
    205     UWORD32 mAIRRefreshPeriod;
    206 
    207     void initEncParams();
    208     OMX_ERRORTYPE initEncoder();
    209     OMX_ERRORTYPE releaseEncoder();
    210 
    211     // Verifies the component role tried to be set to this OMX component is
    212     // strictly video_encoder.avc
    213     OMX_ERRORTYPE internalSetRoleParams(
    214         const OMX_PARAM_COMPONENTROLETYPE *role);
    215 
    216     // Updates bitrate to reflect port settings.
    217     OMX_ERRORTYPE internalSetBitrateParams(
    218         const OMX_VIDEO_PARAM_BITRATETYPE *bitrate);
    219 
    220     OMX_ERRORTYPE setConfig(
    221         OMX_INDEXTYPE index, const OMX_PTR _params);
    222 
    223     OMX_ERRORTYPE getConfig(
    224         OMX_INDEXTYPE index, const OMX_PTR _params);
    225 
    226     // Handles port definition changes.
    227     OMX_ERRORTYPE internalSetPortParams(
    228         const OMX_PARAM_PORTDEFINITIONTYPE *port);
    229 
    230     OMX_ERRORTYPE internalSetFormatParams(
    231         const OMX_VIDEO_PARAM_PORTFORMATTYPE *format);
    232 
    233     OMX_ERRORTYPE setFrameType(IV_PICTURE_CODING_TYPE_T  e_frame_type);
    234     OMX_ERRORTYPE setQp();
    235     OMX_ERRORTYPE setEncMode(IVE_ENC_MODE_T e_enc_mode);
    236     OMX_ERRORTYPE setDimensions();
    237     OMX_ERRORTYPE setNumCores();
    238     OMX_ERRORTYPE setFrameRate();
    239     OMX_ERRORTYPE setIpeParams();
    240     OMX_ERRORTYPE setBitRate();
    241     OMX_ERRORTYPE setAirParams();
    242     OMX_ERRORTYPE setMeParams();
    243     OMX_ERRORTYPE setGopParams();
    244     OMX_ERRORTYPE setProfileParams();
    245     OMX_ERRORTYPE setDeblockParams();
    246     OMX_ERRORTYPE setVbvParams();
    247     void logVersion();
    248     OMX_ERRORTYPE setEncodeArgs(
    249         ive_video_encode_ip_t *ps_encode_ip,
    250         ive_video_encode_op_t *ps_encode_op,
    251         OMX_BUFFERHEADERTYPE *inputBufferHeader,
    252         OMX_BUFFERHEADERTYPE *outputBufferHeader);
    253 
    254     DISALLOW_EVIL_CONSTRUCTORS(SoftAVC);
    255 };
    256 
    257 #ifdef FILE_DUMP_ENABLE
    258 
    259 #define INPUT_DUMP_PATH     "/sdcard/media/avce_input"
    260 #define INPUT_DUMP_EXT      "yuv"
    261 #define OUTPUT_DUMP_PATH    "/sdcard/media/avce_output"
    262 #define OUTPUT_DUMP_EXT     "h264"
    263 
    264 #define GENERATE_FILE_NAMES() {                         \
    265     GETTIME(&mTimeStart, NULL);                         \
    266     strcpy(mInFile, "");                                \
    267     sprintf(mInFile, "%s_%ld.%ld.%s", INPUT_DUMP_PATH,  \
    268             mTimeStart.tv_sec, mTimeStart.tv_usec,      \
    269             INPUT_DUMP_EXT);                            \
    270     strcpy(mOutFile, "");                               \
    271     sprintf(mOutFile, "%s_%ld.%ld.%s", OUTPUT_DUMP_PATH,\
    272             mTimeStart.tv_sec, mTimeStart.tv_usec,      \
    273             OUTPUT_DUMP_EXT);                           \
    274 }
    275 
    276 #define CREATE_DUMP_FILE(m_filename) {                  \
    277     FILE *fp = fopen(m_filename, "wb");                 \
    278     if (fp != NULL) {                                   \
    279         ALOGD("Opened file %s", m_filename);            \
    280         fclose(fp);                                     \
    281     } else {                                            \
    282         ALOGD("Could not open file %s", m_filename);    \
    283     }                                                   \
    284 }
    285 #define DUMP_TO_FILE(m_filename, m_buf, m_size)         \
    286 {                                                       \
    287     FILE *fp = fopen(m_filename, "ab");                 \
    288     if (fp != NULL && m_buf != NULL) {                  \
    289         int i;                                          \
    290         i = fwrite(m_buf, 1, m_size, fp);               \
    291         ALOGD("fwrite ret %d to write %d", i, m_size);  \
    292         if (i != (int)m_size) {                         \
    293             ALOGD("Error in fwrite, returned %d", i);   \
    294             perror("Error in write to file");           \
    295         }                                               \
    296         fclose(fp);                                     \
    297     } else {                                            \
    298         ALOGD("Could not write to file %s", m_filename);\
    299         if (fp != NULL)                                 \
    300             fclose(fp);                                 \
    301     }                                                   \
    302 }
    303 #else /* FILE_DUMP_ENABLE */
    304 #define INPUT_DUMP_PATH
    305 #define INPUT_DUMP_EXT
    306 #define OUTPUT_DUMP_PATH
    307 #define OUTPUT_DUMP_EXT
    308 #define GENERATE_FILE_NAMES()
    309 #define CREATE_DUMP_FILE(m_filename)
    310 #define DUMP_TO_FILE(m_filename, m_buf, m_size)
    311 #endif /* FILE_DUMP_ENABLE */
    312 
    313 }  // namespace android
    314 
    315 #endif  // __SOFT_AVC_ENC_H__
    316