Home | History | Annotate | Download | only in src
      1 /* ------------------------------------------------------------------
      2  * Copyright (C) 1998-2009 PacketVideo
      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
     13  * express or implied.
     14  * See the License for the specific language governing permissions
     15  * and limitations under the License.
     16  * -------------------------------------------------------------------
     17  */
     18 /**
     19 This file contains application function interfaces to the AVC encoder library
     20 and necessary type defitionitions and enumerations.
     21 @publishedAll
     22 */
     23 
     24 #ifndef AVCENC_INT_H_INCLUDED
     25 #define AVCENC_INT_H_INCLUDED
     26 
     27 #ifndef AVCINT_COMMON_H_INCLUDED
     28 #include "avcint_common.h"
     29 #endif
     30 #ifndef AVCENC_API_H_INCLUDED
     31 #include "avcenc_api.h"
     32 #endif
     33 
     34 /* Definition for the structures below */
     35 #define DEFAULT_ATTR    0 /* default memory attribute */
     36 #define MAX_INPUT_FRAME 30 /* some arbitrary number, it can be much higher than this. */
     37 #define MAX_REF_FRAME  16 /* max size of the RefPicList0 and RefPicList1 */
     38 #define MAX_REF_PIC_LIST 33
     39 
     40 #define MIN_QP          0
     41 #define MAX_QP          51
     42 #define SHIFT_QP        12
     43 #define  LAMBDA_ACCURACY_BITS         16
     44 #define  LAMBDA_FACTOR(lambda)        ((int)((double)(1<<LAMBDA_ACCURACY_BITS)*lambda+0.5))
     45 
     46 
     47 #define DISABLE_THRESHOLDING  0
     48 // for better R-D performance
     49 #define _LUMA_COEFF_COST_       4 //!< threshold for luma coeffs
     50 #define _CHROMA_COEFF_COST_     4 //!< threshold for chroma coeffs, used to be 7
     51 #define _LUMA_MB_COEFF_COST_    5 //!< threshold for luma coeffs of inter Macroblocks
     52 #define _LUMA_8x8_COEFF_COST_   5 //!< threshold for luma coeffs of 8x8 Inter Partition
     53 #define MAX_VALUE       999999   //!< used for start value for some variables
     54 
     55 #define  WEIGHTED_COST(factor,bits)   (((factor)*(bits))>>LAMBDA_ACCURACY_BITS)
     56 #define  MV_COST(f,s,cx,cy,px,py)     (WEIGHTED_COST(f,mvbits[((cx)<<(s))-px]+mvbits[((cy)<<(s))-py]))
     57 #define  MV_COST_S(f,cx,cy,px,py)     (WEIGHTED_COST(f,mvbits[cx-px]+mvbits[cy-py]))
     58 
     59 /* for sub-pel search and interpolation */
     60 #define SUBPEL_PRED_BLK_SIZE 576 // 24x24
     61 #define REF_CENTER 75
     62 #define V2Q_H0Q 1
     63 #define V0Q_H2Q 2
     64 #define V2Q_H2Q 3
     65 
     66 /*
     67 #define V3Q_H0Q 1
     68 #define V3Q_H1Q 2
     69 #define V0Q_H1Q 3
     70 #define V1Q_H1Q 4
     71 #define V1Q_H0Q 5
     72 #define V1Q_H3Q 6
     73 #define V0Q_H3Q 7
     74 #define V3Q_H3Q 8
     75 #define V2Q_H3Q 9
     76 #define V2Q_H0Q 10
     77 #define V2Q_H1Q 11
     78 #define V2Q_H2Q 12
     79 #define V3Q_H2Q 13
     80 #define V0Q_H2Q 14
     81 #define V1Q_H2Q 15
     82 */
     83 
     84 
     85 #define DEFAULT_OVERRUN_BUFFER_SIZE 1000
     86 
     87 // associated with the above cost model
     88 const uint8 COEFF_COST[2][16] =
     89 {
     90     {3, 2, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
     91     {9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9}
     92 };
     93 
     94 
     95 
     96 //! convert from H.263 QP to H.264 quant given by: quant=pow(2,QP/6)
     97 const int QP2QUANT[40] =
     98 {
     99     1, 1, 1, 1, 2, 2, 2, 2,
    100     3, 3, 3, 4, 4, 4, 5, 6,
    101     6, 7, 8, 9, 10, 11, 13, 14,
    102     16, 18, 20, 23, 25, 29, 32, 36,
    103     40, 45, 51, 57, 64, 72, 81, 91
    104 };
    105 
    106 
    107 /**
    108 This enumeration keeps track of the internal status of the encoder whether it is doing
    109 something. The encoding flow follows the order in which these states are.
    110 @publishedAll
    111 */
    112 typedef enum
    113 {
    114     AVCEnc_Initializing = 0,
    115     AVCEnc_Encoding_SPS,
    116     AVCEnc_Encoding_PPS,
    117     AVCEnc_Analyzing_Frame,
    118     AVCEnc_WaitingForBuffer,  // pending state
    119     AVCEnc_Encoding_Frame,
    120 } AVCEnc_State ;
    121 
    122 /**
    123 Bitstream structure contains bitstream related parameters such as the pointer
    124 to the buffer, the current byte position and bit position. The content of the
    125 bitstreamBuffer will be in EBSP format as the emulation prevention codes are
    126 automatically inserted as the RBSP is recorded.
    127 @publishedAll
    128 */
    129 typedef struct tagEncBitstream
    130 {
    131     uint8 *bitstreamBuffer; /* pointer to buffer memory   */
    132     int buf_size;       /* size of the buffer memory */
    133     int write_pos;      /* next position to write to bitstreamBuffer  */
    134     int count_zeros;   /* count number of consecutive zero */
    135     uint current_word;  /* byte-swapped (MSB left) current word to write to buffer */
    136     int bit_left;      /* number of bit left in current_word */
    137     uint8   *overrunBuffer;  /* extra output buffer to prevent current skip due to output buffer overrun*/
    138     int     oBSize;     /* size of allocated overrun buffer */
    139     void   *encvid; /* pointer to the main object */
    140 
    141 } AVCEncBitstream;
    142 
    143 /**
    144 This structure is used for rate control purpose and other performance related control
    145 variables such as, RD cost, statistics, motion search stuffs, etc.
    146 should be in this structure.
    147 @publishedAll
    148 */
    149 
    150 
    151 typedef struct tagRDInfo
    152 {
    153     int QP;
    154     int actual_bits;
    155     OsclFloat mad;
    156     OsclFloat R_D;
    157 } RDInfo;
    158 
    159 typedef struct tagMultiPass
    160 {
    161     /* multipass rate control data */
    162     int target_bits;    /* target bits for current frame, = rc->T */
    163     int actual_bits;    /* actual bits for current frame obtained after encoding, = rc->Rc*/
    164     int QP;             /* quantization level for current frame, = rc->Qc*/
    165     int prev_QP;        /* quantization level for previous frame */
    166     int prev_prev_QP;   /* quantization level for previous frame before last*/
    167     OsclFloat mad;          /* mad for current frame, = video->avgMAD*/
    168     int bitrate;        /* bitrate for current frame */
    169     OsclFloat framerate;    /* framerate for current frame*/
    170 
    171     int nRe_Quantized;  /* control variable for multipass encoding, */
    172     /* 0 : first pass */
    173     /* 1 : intermediate pass(quantization and VLC loop only) */
    174     /* 2 : final pass(de-quantization, idct, etc) */
    175     /* 3 : macroblock level rate control */
    176 
    177     int encoded_frames;     /* counter for all encoded frames */
    178     int re_encoded_frames;  /* counter for all multipass encoded frames*/
    179     int re_encoded_times;   /* counter for all times of multipass frame encoding */
    180 
    181     /* Multiple frame prediction*/
    182     RDInfo **pRDSamples;        /* pRDSamples[30][32], 30->30fps, 32 -> 5 bit quantizer, 32 candidates*/
    183     int framePos;               /* specific position in previous multiple frames*/
    184     int frameRange;             /* number of overall previous multiple frames */
    185     int samplesPerFrame[30];    /* number of samples per frame, 30->30fps */
    186 
    187     /* Bit allocation for scene change frames and high motion frames */
    188     OsclFloat sum_mad;
    189     int counter_BTsrc;  /* BT = Bit Transfer, bit transfer from low motion frames or less complicatedly compressed frames */
    190     int counter_BTdst;  /* BT = Bit Transfer, bit transfer to scene change frames or high motion frames or more complicatedly compressed frames */
    191     OsclFloat sum_QP;
    192     int diff_counter;   /* diff_counter = -diff_counter_BTdst, or diff_counter_BTsrc */
    193 
    194     /* For target bitrate or framerate update */
    195     OsclFloat target_bits_per_frame;        /* = C = bitrate/framerate */
    196     OsclFloat target_bits_per_frame_prev;   /* previous C */
    197     OsclFloat aver_mad;                     /* so-far average mad could replace sum_mad */
    198     OsclFloat aver_mad_prev;                /* previous average mad */
    199     int   overlapped_win_size;          /* transition period of time */
    200     int   encoded_frames_prev;          /* previous encoded_frames */
    201 } MultiPass;
    202 
    203 
    204 typedef struct tagdataPointArray
    205 {
    206     int Qp;
    207     int Rp;
    208     OsclFloat Mp;   /* for MB-based RC */
    209     struct tagdataPointArray *next;
    210     struct tagdataPointArray *prev;
    211 } dataPointArray;
    212 
    213 typedef struct tagAVCRateControl
    214 {
    215 
    216     /* these parameters are initialized by the users AVCEncParams */
    217     /* bitrate-robustness tradeoff */
    218     uint scdEnable; /* enable scene change detection */
    219     int idrPeriod;  /* IDR period in number of frames */
    220     int intraMBRate;   /* intra MB refresh rate per frame */
    221     uint dpEnable;  /* enable data partitioning */
    222 
    223     /* quality-complexity tradeoff */
    224     uint subPelEnable;  /* enable quarter pel search */
    225     int mvRange;    /* motion vector search range in +/- pixel */
    226     uint subMBEnable;  /* enable sub MB prediction mode (4x4, 4x8, 8x4) */
    227     uint rdOptEnable;  /* enable RD-opt mode selection */
    228     uint twoPass; /* flag for 2 pass encoding ( for future )*/
    229     uint bidirPred; /* bi-directional prediction for B-frame. */
    230 
    231     uint rcEnable;  /* enable rate control, '1' on, '0' const QP */
    232     int initQP; /* initial QP */
    233 
    234     /* note the following 3 params are for HRD, these triplets can be a series
    235     of triplets as the generalized HRD allows. SEI message must be generated in this case. */
    236     /* We no longer have to differentiate between CBR and VBR. The users to the
    237     AVC encoder lib will do the mapping from CBR/VBR to these parameters. */
    238     int32 bitRate;  /* target bit rate for the overall clip in bits/second*/
    239     int32 cpbSize;  /* coded picture buffer size in bytes */
    240     int32 initDelayOffset; /* initial CBP removal delay in bits */
    241 
    242     OsclFloat frame_rate; /* frame rate */
    243     int srcInterval; /* source frame rate in msec */
    244     int basicUnit;  /* number of macroblocks per BU */
    245 
    246     /* Then internal parameters for the operation */
    247     uint first_frame; /* a flag for the first frame */
    248     int lambda_mf; /* for example */
    249     int totalSAD;    /* SAD of current frame */
    250 
    251     /*******************************************/
    252     /* this part comes from MPEG4 rate control */
    253     int alpha;  /* weight for I frame */
    254     int Rs;     /*bit rate for the sequence (or segment) e.g., 24000 bits/sec */
    255     int Rc;     /*bits used for the current frame. It is the bit count obtained after encoding. */
    256     int Rp;     /*bits to be removed from the buffer per picture. */
    257     /*? is this the average one, or just the bits coded for the previous frame */
    258     int Rps;    /*bit to be removed from buffer per src frame */
    259     OsclFloat Ts;   /*number of seconds for the sequence  (or segment). e.g., 10 sec */
    260     OsclFloat Ep;
    261     OsclFloat Ec;   /*mean absolute difference for the current frame after motion compensation.*/
    262     /*If the macroblock is intra coded, the original spatial pixel values are summed.*/
    263     int Qc;     /*quantization level used for the current frame. */
    264     int Nr;     /*number of P frames remaining for encoding.*/
    265     int Rr; /*number of bits remaining for encoding this sequence (or segment).*/
    266     int Rr_Old;
    267     int T;      /*target bit to be used for the current frame.*/
    268     int S;      /*number of bits used for encoding the previous frame.*/
    269     int Hc; /*header and motion vector bits used in the current frame. It includes all the  information except to the residual information.*/
    270     int Hp; /*header and motion vector bits used in the previous frame. It includes all the     information except to the residual information.*/
    271     int Ql; /*quantization level used in the previous frame */
    272     int Bs; /*buffer size e.g., R/2 */
    273     int B;      /*current buffer level e.g., R/4 - start from the middle of the buffer */
    274     OsclFloat X1;
    275     OsclFloat X2;
    276     OsclFloat X11;
    277     OsclFloat M;            /*safe margin for the buffer */
    278     OsclFloat smTick;    /*ratio of src versus enc frame rate */
    279     double remnant;  /*remainder frame of src/enc frame for fine frame skipping */
    280     int timeIncRes; /* vol->timeIncrementResolution */
    281 
    282     dataPointArray   *end; /*quantization levels for the past (20) frames */
    283 
    284     int     frameNumber; /* ranging from 0 to 20 nodes*/
    285     int     w;
    286     int     Nr_Original;
    287     int     Nr_Old, Nr_Old2;
    288     int     skip_next_frame;
    289     int     Qdep;       /* smooth Q adjustment */
    290     int     VBR_Enabled;
    291 
    292     int totalFrameNumber; /* total coded frames, for debugging!!*/
    293 
    294     char    oFirstTime;
    295 
    296     int numFrameBits; /* keep track of number of bits of the current frame */
    297     int NumberofHeaderBits;
    298     int NumberofTextureBits;
    299     int numMBHeaderBits;
    300     int numMBTextureBits;
    301     double *MADofMB;
    302     int32 bitsPerFrame;
    303 
    304     /* BX rate control, something like TMN8 rate control*/
    305 
    306     MultiPass *pMP;
    307 
    308     int     TMN_W;
    309     int     TMN_TH;
    310     int     VBV_fullness;
    311     int     max_BitVariance_num; /* the number of the maximum bit variance within the given buffer with the unit of 10% of bitrate/framerate*/
    312     int     encoded_frames; /* counter for all encoded frames */
    313     int     low_bound;              /* bound for underflow detection, usually low_bound=-Bs/2, but could be changed in H.263 mode */
    314     int     VBV_fullness_offset;    /* offset of VBV_fullness, usually is zero, but can be changed in H.263 mode*/
    315     /* End BX */
    316 
    317 } AVCRateControl;
    318 
    319 
    320 /**
    321 This structure is for the motion vector information. */
    322 typedef struct tagMV
    323 {
    324     int x;
    325     int y;
    326     uint sad;
    327 } AVCMV;
    328 
    329 /**
    330 This structure contains function pointers for different platform dependent implementation of
    331 functions. */
    332 typedef struct tagAVCEncFuncPtr
    333 {
    334 
    335     int (*SAD_MB_HalfPel[4])(uint8*, uint8*, int, void *);
    336     int (*SAD_Macroblock)(uint8 *ref, uint8 *blk, int dmin_lx, void *extra_info);
    337 
    338 } AVCEncFuncPtr;
    339 
    340 /**
    341 This structure contains information necessary for correct padding.
    342 */
    343 typedef struct tagPadInfo
    344 {
    345     int i;
    346     int width;
    347     int j;
    348     int height;
    349 } AVCPadInfo;
    350 
    351 
    352 #ifdef HTFM
    353 typedef struct tagHTFM_Stat
    354 {
    355     int abs_dif_mad_avg;
    356     uint countbreak;
    357     int offsetArray[16];
    358     int offsetRef[16];
    359 } HTFM_Stat;
    360 #endif
    361 
    362 
    363 /**
    364 This structure is the main object for AVC encoder library providing access to all
    365 global variables. It is allocated at PVAVCInitEncoder and freed at PVAVCCleanUpEncoder.
    366 @publishedAll
    367 */
    368 typedef struct tagEncObject
    369 {
    370 
    371     AVCCommonObj *common;
    372 
    373     AVCEncBitstream     *bitstream; /* for current NAL */
    374     uint8   *overrunBuffer;  /* extra output buffer to prevent current skip due to output buffer overrun*/
    375     int     oBSize;     /* size of allocated overrun buffer */
    376 
    377     /* rate control */
    378     AVCRateControl      *rateCtrl; /* pointer to the rate control structure */
    379 
    380     /* encoding operation */
    381     AVCEnc_State        enc_state; /* encoding state */
    382 
    383     AVCFrameIO          *currInput; /* pointer to the current input frame */
    384 
    385     int                 currSliceGroup; /* currently encoded slice group id */
    386 
    387     int     level[24][16], run[24][16]; /* scratch memory */
    388     int     leveldc[16], rundc[16]; /* for DC component */
    389     int     levelcdc[16], runcdc[16]; /* for chroma DC component */
    390     int     numcoefcdc[2]; /* number of coefficient for chroma DC */
    391     int     numcoefdc;      /* number of coefficients for DC component */
    392 
    393     int     qp_const;
    394     int     qp_const_c;
    395     /********* intra prediction scratch memory **********************/
    396     uint8   pred_i16[AVCNumI16PredMode][256]; /* save prediction for MB */
    397     uint8   pred_i4[AVCNumI4PredMode][16];  /* save prediction for blk */
    398     uint8   pred_ic[AVCNumIChromaMode][128];  /* for 2 chroma */
    399 
    400     int     mostProbableI4Mode[16]; /* in raster scan order */
    401     /********* motion compensation related variables ****************/
    402     AVCMV   *mot16x16;          /* Saved motion vectors for 16x16 block*/
    403     AVCMV(*mot16x8)[2];     /* Saved motion vectors for 16x8 block*/
    404     AVCMV(*mot8x16)[2];     /* Saved motion vectors for 8x16 block*/
    405     AVCMV(*mot8x8)[4];      /* Saved motion vectors for 8x8 block*/
    406 
    407     /********* subpel position **************************************/
    408     uint32  subpel_pred[SUBPEL_PRED_BLK_SIZE/*<<2*/]; /* all 16 sub-pel positions  */
    409     uint8   *hpel_cand[9];      /* pointer to half-pel position */
    410     int     best_hpel_pos;          /* best position */
    411     uint8   qpel_cand[8][24*16];        /* pointer to quarter-pel position */
    412     int     best_qpel_pos;
    413     uint8   *bilin_base[9][4];    /* pointer to 4 position at top left of bilinear quarter-pel */
    414 
    415     /* need for intra refresh rate */
    416     uint8   *intraSearch;       /* Intra Array for MBs to be intra searched */
    417     uint    firstIntraRefreshMBIndx; /* keep track for intra refresh */
    418 
    419     int     i4_sad;             /* temporary for i4 mode SAD */
    420     int     *min_cost;          /* Minimum cost for the all MBs */
    421     int     lambda_mode;        /* Lagrange parameter for mode selection */
    422     int     lambda_motion;      /* Lagrange parameter for MV selection */
    423 
    424     uint8   *mvbits_array;      /* Table for bits spent in the cost funciton */
    425     uint8   *mvbits;            /* An offset to the above array. */
    426 
    427     /* to speedup the SAD calculation */
    428     void *sad_extra_info;
    429     uint8 currYMB[256];     /* interleaved current macroblock in HTFM order */
    430 
    431 #ifdef HTFM
    432     int nrmlz_th[48];       /* Threshold for fast SAD calculation using HTFM */
    433     HTFM_Stat htfm_stat;    /* For statistics collection */
    434 #endif
    435 
    436     /* statistics */
    437     int numIntraMB;         /* keep track of number of intra MB */
    438 
    439     /* encoding complexity control */
    440     uint fullsearch_enable; /* flag to enable full-pel full-search */
    441 
    442     /* misc.*/
    443     bool outOfBandParamSet; /* flag to enable out-of-band param set */
    444 
    445     AVCSeqParamSet extSPS; /* for external SPS */
    446     AVCPicParamSet extPPS; /* for external PPS */
    447 
    448     /* time control */
    449     uint32  prevFrameNum;   /* previous frame number starting from modTimeRef */
    450     uint32  modTimeRef;     /* Reference modTime update every I-Vop*/
    451     uint32  wrapModTime;    /* Offset to modTime Ref, rarely used */
    452 
    453     uint    prevProcFrameNum;  /* previously processed frame number, could be skipped */
    454     uint    prevCodedFrameNum;  /* previously encoded frame number */
    455     /* POC related variables */
    456     uint32  dispOrdPOCRef;      /* reference POC is displayer order unit. */
    457 
    458     /* Function pointers */
    459     AVCEncFuncPtr *functionPointer; /* store pointers to platform specific functions */
    460 
    461     /* Application control data */
    462     AVCHandle *avcHandle;
    463 
    464 
    465 } AVCEncObject;
    466 
    467 
    468 #endif /*AVCENC_INT_H_INCLUDED*/
    469 
    470