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 #ifndef _MP4LIB_INT_H_
     20 #define _MP4LIB_INT_H_
     21 
     22 #include "mp4def.h"
     23 #include "mp4enc_api.h"
     24 #include "rate_control.h"
     25 
     26 /* BitstreamEncVideo will be modified */
     27 typedef struct tagBitstream
     28 {
     29     Int(*writeVideoPacket)(UChar *buf, Int nbytes_required);   /*write video packet out */
     30     UChar *bitstreamBuffer; /*buffer to hold one video packet*/
     31     Int bufferSize; /*total bitstream buffer size in bytes */
     32     Int byteCount;  /*how many bytes already encoded*/
     33     UInt word;      /*hold one word temporarily */
     34     Int bitLeft;    /*number of bits left in "word" */
     35     UChar* overrunBuffer;  /* pointer to overrun buffer */
     36     Int oBSize;     /* length of overrun buffer */
     37     struct tagVideoEncData *video;
     38 } BitstreamEncVideo;
     39 
     40 typedef struct tagVOP
     41 {
     42     PIXEL   *allChan;           /* [yuv]Chan point into this buffer */
     43     PIXEL   *yChan;             /* The Y component */
     44     PIXEL   *uChan;             /* The U component */
     45     PIXEL   *vChan;             /* The V component */
     46     Int     frame;              /* frame number */
     47     Int     volID;              /* Layer number */
     48     //Int       timeStamp;          /* Vop TimeStamp in msec */
     49 
     50     /* Syntax elements copied from VOL (standard) */
     51     Int     width;              /* Width (multiple of 16) */
     52     Int     height;             /* Height (multiple of 16) */
     53     Int     pitch;              /* Pitch (differs from width for UMV case) */
     54     Int     padded;     /* flag whether this frame has been padded */
     55 
     56     /* Actual syntax elements for VOP (standard) */
     57     Int     predictionType;     /* VOP prediction type */
     58     Int     timeInc;            /* VOP time increment (relative to last mtb) */
     59     Int     vopCoded;
     60     Int     roundingType;
     61     Int     intraDCVlcThr;
     62     Int     quantizer;          /* VOP quantizer */
     63     Int     fcodeForward;       /* VOP dynamic range of motion vectors */
     64     Int     fcodeBackward;      /* VOP dynamic range of motion vectors */
     65     Int     refSelectCode;      /* enhancement layer reference select code */
     66 
     67     /* H.263 parameters */
     68     Int     gobNumber;
     69     Int     gobFrameID;
     70     Int     temporalRef;        /* temporal reference, roll over at 256 */
     71     Int     temporalInterval;   /* increase every 256 temporalRef */
     72 
     73 } Vop;
     74 
     75 typedef struct tagVol
     76 {
     77     Int     volID;              /* VOL identifier (for tracking) */
     78     Int     shortVideoHeader;   /* shortVideoHeader mode */
     79     Int     GOVStart;           /* Insert GOV Header */
     80     Int     timeIncrementResolution;    /* VOL time increment */
     81     Int     nbitsTimeIncRes;    /* number of bits for time increment */
     82     Int     timeIncrement;      /* time increment */
     83     Int     moduloTimeBase;     /* internal decoder clock */
     84     Int     prevModuloTimeBase; /* in case of pre-frameskip */
     85 
     86     Int     fixedVopRate;
     87     BitstreamEncVideo  *stream; /* library bitstream buffer (input buffer) */
     88 
     89     /* VOL Dimensions */
     90     Int     width;              /* Width */
     91     Int     height;             /* Height */
     92 
     93     /* Error Resilience Flags */
     94     Int     ResyncMarkerDisable; /* VOL Disable Resynch Markers */
     95     Int     useReverseVLC;      /* VOL reversible VLCs */
     96     Int     dataPartitioning;   /* VOL data partitioning */
     97 
     98     /* Quantization related parameters */
     99     Int     quantPrecision;     /* Quantizer precision */
    100     Int     quantType;          /* MPEG-4 or H.263 Quantization Type */
    101 
    102     /* Added loaded quant mat, 05/22/2000 */
    103     Int     loadIntraQuantMat;      /* Load intra quantization matrix */
    104     Int     loadNonIntraQuantMat;   /* Load nonintra quantization matrix */
    105     Int     iqmat[64];          /* Intra quant.matrix */
    106     Int     niqmat[64];         /* Non-intra quant.matrix */
    107 
    108 
    109     /* Parameters used for scalability */
    110     Int     scalability;        /* VOL scalability (flag) */
    111     Int     scalType;           /* temporal = 0, spatial = 1, both = 2 */
    112 
    113     Int     refVolID;           /* VOL id of reference VOL */
    114     Int     refSampDir;         /* VOL resol. of ref. VOL */
    115     Int     horSamp_n;          /* VOL hor. resampling of ref. VOL given by */
    116     Int     horSamp_m;          /* sampfac = hor_samp_n/hor_samp_m      */
    117     Int     verSamp_n;          /* VOL ver. resampling of ref. VOL given by */
    118     Int     verSamp_m;          /* sampfac = ver_samp_n/ver_samp_m      */
    119     Int     enhancementType;    /* VOL type of enhancement layer */
    120 
    121     /* These variables were added since they are used a lot. */
    122     Int     nMBPerRow, nMBPerCol;   /* number of MBs in each row & column    */
    123     Int     nTotalMB;
    124     Int     nBitsForMBID;           /* how many bits required for MB number? */
    125 
    126     /* for short video header */
    127     Int     nMBinGOB;           /* number of MBs in GOB, 05/22/00 */
    128     Int     nGOBinVop;          /* number of GOB in Vop  05/22/00 */
    129 } Vol;
    130 
    131 typedef struct tagMacroBlock
    132 {
    133     Int     mb_x;               /* X coordinate */
    134     Int     mb_y;               /* Y coordinate */
    135     Short   block[9][64];       /* 4-Y, U and V blocks , and AAN Scale*/
    136 } MacroBlock;
    137 
    138 typedef struct tagRunLevelBlock
    139 {
    140     Int run[64];        /* Runlength */
    141     Int level[64];      /* Abs(level) */
    142     Int s[64];          /* sign level */
    143 } RunLevelBlock;
    144 
    145 typedef struct tagHeaderInfoDecVideo
    146 {
    147     UChar       *Mode;              /* Modes INTRA/INTER/etc. */
    148     UChar       *CBP;               /* MCBPC/CBPY stuff */
    149 } HeaderInfoEncVideo;
    150 
    151 typedef Short typeDCStore[6];   /* ACDC */
    152 typedef Short typeDCACStore[4][8];
    153 
    154 typedef struct tagMOT
    155 {
    156     Int x;  /* half-pel resolution x component */
    157     Int y;      /* half-pel resolution y component */
    158     Int sad;  /* SAD */
    159 } MOT;
    160 
    161 typedef struct tagHintTrackInfo
    162 {
    163     UChar MTB;
    164     UChar LayerID;
    165     UChar CodeType;
    166     UChar RefSelCode;
    167 
    168 } HintTrackInfo;
    169 
    170 
    171 typedef struct tagVideoEncParams
    172 {
    173     //Int       Width;                  /* Input Width */
    174     //Int       Height;                 /* Input Height */
    175     //float FrameRate;              /* Input Frame Rate */
    176     UInt    TimeIncrementRes;       /* timeIncrementRes */
    177 
    178     /*VOL Parameters */
    179     Int     nLayers;
    180     Int     LayerWidth[4];          /* Encoded Width */
    181     Int     LayerHeight[4];         /* Encoded Height */
    182     float   LayerFrameRate[4];      /* Encoded Frame Rate */
    183     Int     LayerBitRate[4];        /* Encoded BitRate */
    184     Int     LayerMaxBitRate[4];     /* Maximum Encoded BitRate */
    185     float   LayerMaxFrameRate[4];   /* Maximum Encoded Frame Rate */
    186     Int     LayerMaxMbsPerSec[4];   /* Maximum mbs per second, according to the specified profile and level */
    187     Int     LayerMaxBufferSize[4];  /* Maximum buffer size, according to the specified profile and level */
    188 
    189     Bool    ResyncMarkerDisable;    /* Disable Resync Marker */
    190     Bool    DataPartitioning;       /* Base Layer Data Partitioning */
    191     Bool    ReversibleVLC;          /* RVLC when Data Partitioning */
    192     Bool    ACDCPrediction;         /* AC/DC Prediction    */
    193     Int     QuantType[4];           /* H263, MPEG2 */
    194     Int     InitQuantBvop[4];
    195     Int     InitQuantPvop[4];
    196     Int     InitQuantIvop[4];
    197     Int     ResyncPacketsize;
    198 
    199     Int     RoundingType;
    200     Int     IntraDCVlcThr;
    201 
    202     /* Rate Control Parameters */
    203     MP4RateControlType  RC_Type;        /*Constant Q, M4 constantRate, VM5+, M4RC,MPEG2TM5 */
    204 
    205     /* Intra Refresh Parameters */
    206     Int     IntraPeriod;            /* Intra update period */
    207     Int     Refresh;                /* Number of MBs refresh in each frame */
    208     /* Other Parameters */
    209     Bool    SceneChange_Det;        /* scene change detection */
    210     Bool    FineFrameSkip_Enabled;  /* src rate resolution frame skipping */
    211     Bool    VBR_Enabled;            /* VBR rate control */
    212     Bool    NoFrameSkip_Enabled;    /* do not allow frame skip */
    213     Bool    NoPreSkip_Enabled;      /* do not allow pre-skip */
    214 
    215     Bool    H263_Enabled;           /* H263 Short Header */
    216     Bool    GOV_Enabled;            /* GOV Header Enabled */
    217     Bool    SequenceStartCode;      /* This probably should be removed */
    218     Bool    FullSearch_Enabled;     /* full-pel exhaustive search motion estimation */
    219     Bool    HalfPel_Enabled;        /* Turn Halfpel ME on or off */
    220     Bool    MV8x8_Enabled;          /* Enable 8x8 motion vectors */
    221     Bool    RD_opt_Enabled;         /* Enable operational R-D optimization */
    222     Int     GOB_Header_Interval;        /* Enable encoding GOB header in H263_WITH_ERR_RES and SHORT_HERDER_WITH_ERR_RES */
    223     Int     SearchRange;            /* Search range for 16x16 motion vector */
    224     Int     MemoryUsage;            /* Amount of memory allocated */
    225     Int     GetVolHeader[2];        /* Flag to check if Vol Header has been retrieved */
    226     Int     BufferSize[2];          /* Buffer Size for Base and Enhance Layers */
    227     Int     ProfileLevel[2];        /* Profile and Level for encoding purposes */
    228     float   VBV_delay;              /* VBV buffer size in the form of delay */
    229     Int     maxFrameSize;           /* maximum frame size(bits) for H263/Short header mode, k*16384 */
    230     Int     profile_table_index;    /* index for profile and level tables given the specified profile and level */
    231 
    232 } VideoEncParams;
    233 
    234 /* platform dependent functions */
    235 typedef struct tagFuncPtr
    236 {
    237 //  Int (*SAD_MB_HalfPel)(UChar *ref,UChar *blk,Int dmin_lx,Int xh,Int yh,void *extra_info);
    238     Int(*SAD_MB_HalfPel[4])(UChar*, UChar*, Int, void *);
    239     Int(*SAD_Blk_HalfPel)(UChar *ref, UChar *blk, Int dmin, Int lx, Int rx, Int xh, Int yh, void *extra_info);
    240     Int(*SAD_Macroblock)(UChar *ref, UChar *blk, Int dmin_lx, void *extra_info);
    241     Int(*SAD_Block)(UChar *ref, UChar *blk, Int dmin, Int lx, void *extra_info);
    242     Int(*SAD_MB_PADDING)(UChar *ref, UChar *blk, Int dmin, Int lx, void *extra_info); /*, 4/21/01 */
    243     void (*ComputeMBSum)(UChar *cur, Int lx, MOT *mot_mb);
    244     void (*ChooseMode)(UChar *Mode, UChar *cur, Int lx, Int min_SAD);
    245     void (*GetHalfPelMBRegion)(UChar *cand, UChar *hmem, Int lx);
    246     void (*blockIdct)(Int *block);
    247 
    248 
    249 } FuncPtr;
    250 
    251 /* 04/09/01, for multipass rate control */
    252 
    253 typedef struct tagRDInfo
    254 {
    255     Int QP;
    256     Int actual_bits;
    257     float mad;
    258     float R_D;
    259 } RDInfo;
    260 
    261 typedef struct tagMultiPass
    262 {
    263     /* multipass rate control data */
    264     Int target_bits;    /* target bits for current frame, = rc->T */
    265     Int actual_bits;    /* actual bits for current frame obtained after encoding, = rc->Rc*/
    266     Int QP;             /* quantization level for current frame, = rc->Qc*/
    267     Int prev_QP;        /* quantization level for previous frame */
    268     Int prev_prev_QP;   /* quantization level for previous frame before last*/
    269     float mad;          /* mad for current frame, = video->avgMAD*/
    270     Int bitrate;        /* bitrate for current frame */
    271     float framerate;    /* framerate for current frame*/
    272 
    273     Int nRe_Quantized;  /* control variable for multipass encoding, */
    274     /* 0 : first pass */
    275     /* 1 : intermediate pass(quantization and VLC loop only) */
    276     /* 2 : final pass(de-quantization, idct, etc) */
    277     /* 3 : macroblock level rate control */
    278 
    279     Int encoded_frames;     /* counter for all encoded frames */
    280     Int re_encoded_frames;  /* counter for all multipass encoded frames*/
    281     Int re_encoded_times;   /* counter for all times of multipass frame encoding */
    282 
    283     /* Multiple frame prediction*/
    284     RDInfo **pRDSamples;        /* pRDSamples[30][32], 30->30fps, 32 -> 5 bit quantizer, 32 candidates*/
    285     Int framePos;               /* specific position in previous multiple frames*/
    286     Int frameRange;             /* number of overall previous multiple frames */
    287     Int samplesPerFrame[30];    /* number of samples per frame, 30->30fps */
    288 
    289     /* Bit allocation for scene change frames and high motion frames */
    290     float sum_mad;
    291     Int counter_BTsrc;  /* BT = Bit Transfer, bit transfer from low motion frames or less complicatedly compressed frames */
    292     Int counter_BTdst;  /* BT = Bit Transfer, bit transfer to scene change frames or high motion frames or more complicatedly compressed frames */
    293     float sum_QP;
    294     Int diff_counter;   /* diff_counter = -diff_counter_BTdst, or diff_counter_BTsrc */
    295 
    296     /* For target bitrate or framerate update */
    297     float target_bits_per_frame;        /* = C = bitrate/framerate */
    298     float target_bits_per_frame_prev;   /* previous C */
    299     float aver_mad;                     /* so-far average mad could replace sum_mad */
    300     float aver_mad_prev;                /* previous average mad */
    301     Int   overlapped_win_size;          /* transition period of time */
    302     Int   encoded_frames_prev;          /* previous encoded_frames */
    303 } MultiPass;
    304 
    305 /* End */
    306 
    307 #ifdef HTFM
    308 typedef struct tagHTFM_Stat
    309 {
    310     Int abs_dif_mad_avg;
    311     UInt countbreak;
    312     Int offsetArray[16];
    313     Int offsetRef[16];
    314 } HTFM_Stat;
    315 #endif
    316 
    317 /* Global structure that can be passed around */
    318 typedef struct tagVideoEncData
    319 {
    320     /* VOL Header Initialization */
    321     UChar   volInitialize[4];       /* Used to Write VOL Headers */
    322     /* Data For Layers (Scalability) */
    323     Int     numberOfLayers;     /* Number of Layers */
    324     Vol     **vol;              /* Data stored for each VOL */
    325 
    326     /* Data used for encoding frames */
    327     VideoEncFrameIO *input;     /* original input frame */
    328     Vop     *currVop;           /* Current reconstructed VOP */
    329     Vop     *prevBaseVop;       /* Previous reference Base Vop */
    330     Vop     *nextBaseVop;       /* Next reference Base Vop */
    331     Vop     *prevEnhanceVop;/* Previous Enhancement Layer Vop */
    332     Vop     *forwardRefVop;     /* Forward Reference VOP */
    333     Vop     *backwardRefVop;    /* Backward Reference VOP */
    334 
    335     /* scratch memory */
    336     BitstreamEncVideo  *bitstream1; /* Used for data partitioning */
    337     BitstreamEncVideo  *bitstream2; /* and combined modes as      */
    338     BitstreamEncVideo  *bitstream3; /* intermediate storages      */
    339 
    340     UChar   *overrunBuffer;  /* extra output buffer to prevent current skip due to output buffer overrun*/
    341     Int     oBSize;     /* size of allocated overrun buffer */
    342 
    343     Int dc_scalar_1;            /*dc scalar for Y block */
    344     Int dc_scalar_2;            /*dc scalar for U, V block*/
    345 
    346     /* Annex L Rate Control */
    347     rateControl     *rc[4];         /* Pointer to Rate Control structure*/
    348     /* 12/25/00, each R.C. for each layer */
    349 
    350     /********* motion compensation related variables ****************/
    351     MOT     **mot;              /* Motion vectors */
    352     /*  where [mbnum][0] = 1MV.
    353         [mbnum][1...4] = 4MVs
    354         [mbnum][5] = backward MV.
    355         [mbnum][6] = delta MV for direct mode.
    356         [mbnum][7] = nothing yet. */
    357     UChar   *intraArray;            /* Intra Update Arrary */
    358     float   sumMAD;             /* SAD/MAD for frame */
    359 
    360     /* to speedup the SAD calculation */
    361     void *sad_extra_info;
    362 #ifdef HTFM
    363     Int nrmlz_th[48];       /* Threshold for fast SAD calculation using HTFM */
    364     HTFM_Stat htfm_stat;    /* For statistics collection */
    365 #endif
    366 
    367     /*Tao 04/09/00  For DCT routine */
    368     UChar currYMB[256];     /* interleaved current macroblock in HTFM order */
    369     MacroBlock  *outputMB;          /* Output MB to VLC encode */
    370     UChar   predictedMB[384];   /* scrath memory for predicted value */
    371     RunLevelBlock RLB[6];       /* Run and Level of coefficients! */
    372     Short   dataBlock[128];     /* DCT block data before and after quant/dequant*/
    373 
    374     UChar   bitmaprow[8];       /* Need to keep it for ACDCPrediction, 8 bytes for alignment, need only 6 */
    375     UChar   bitmapcol[6][8];
    376     UInt    bitmapzz[6][2]; /* for zigzag bitmap */
    377     Int     zeroMV;         /* flag for zero MV */
    378 
    379     Int     usePrevQP;      /* flag for intraDCVlcThreshold switch decision */
    380     Int     QP_prev;            /* use for DQUANT calculation */
    381     Int     *acPredFlag;        /* */
    382     typeDCStore     *predDC;        /* The DC coeffs for each MB */
    383     typeDCACStore   *predDCAC_row;
    384     typeDCACStore   *predDCAC_col;
    385 
    386 
    387     UChar   *sliceNo;           /* Slice Number for each MB */
    388 
    389     Int     header_bits;        /* header bits in frmae */
    390     HeaderInfoEncVideo  headerInfo; /* MB Header information */
    391     UChar   zz_direction;       /* direction of zigzag scan */
    392     UChar   *QPMB;              /* Quantizer value for each MB */
    393 
    394     /* Miscellaneous data points to be passed */
    395     float   FrameRate;          /* Src frame Rate */
    396 
    397     ULong   nextModTime;        /* expected next frame time */
    398     UInt    prevFrameNum[4];    /* previous frame number starting from modTimeRef */
    399     UInt    modTimeRef;     /* Reference modTime update every I-Vop*/
    400     UInt    refTick[4];         /* second aligned referenc tick */
    401     Int     relLayerCodeTime[4];/* Next coding time for each Layer relative to highest layer */
    402 
    403     ULong   modTime;            /* Input frame modTime */
    404     Int     currLayer;          /* Current frame layer  */
    405     Int     mbnum;              /*  Macroblock number */
    406 
    407     /* slice coding, state variables */
    408     Vop     *tempForwRefVop;
    409     Int     tempRefSelCode;
    410     Int     end_of_buf;         /* end of bitstream buffer flag */
    411     Int     slice_coding;       /* flag for slice based coding */
    412     Int     totalSAD;           /* So far total SAD for a frame */
    413     Int     numIntra;           /* So far number of Intra MB */
    414     Int     offset;             /* So far MB offset */
    415     Int     ind_x, ind_y;       /* So far MB coordinate */
    416     Int     collect;
    417     Int     hp_guess;
    418     /*********************************/
    419 
    420     HintTrackInfo hintTrackInfo;    /* hintTrackInfo */
    421     /* IntraPeriod, Timestamp, etc. */
    422     float       nextEncIVop;    /* counter til the next I-Vop */
    423     float       numVopsInGOP;   /* value at the beginning of nextEncIVop */
    424 
    425     /* platform dependent functions */
    426     FuncPtr     *functionPointer;   /* structure containing platform dependent functions */
    427 
    428     /* Application controls */
    429     VideoEncControls    *videoEncControls;
    430     VideoEncParams      *encParams;
    431 
    432     MultiPass *pMP[4]; /* for multipass encoding, 4 represents 4 layer encoding */
    433 
    434 } VideoEncData;
    435 
    436 /*************************************************************/
    437 /*                  VLC structures                           */
    438 /*************************************************************/
    439 
    440 typedef struct tagVLCtable
    441 {
    442     unsigned int code; /* right justified */
    443     int len;
    444 } VLCtable, *LPVLCtable;
    445 
    446 
    447 /*************************************************************/
    448 /*                  Approx DCT                               */
    449 /*************************************************************/
    450 typedef struct struct_approxDCT  approxDCT;
    451 struct struct_approxDCT
    452 {
    453     Void(*BlockDCT8x8)(Int *, Int *, UChar *, UChar *, Int, Int);
    454     Void(*BlockDCT8x8Intra)(Int *, Int *, UChar *, UChar *, Int, Int);
    455     Void(*BlockDCT8x8wSub)(Int *, Int *, UChar *, UChar *, Int, Int);
    456 };
    457 
    458 /*************************************************************/
    459 /*                  QP structure                             */
    460 /*************************************************************/
    461 
    462 struct QPstruct
    463 {
    464     Int QPx2 ;
    465     Int QP;
    466     Int QPdiv2;
    467     Int QPx2plus;
    468     Int Addition;
    469 };
    470 
    471 
    472 #endif /* _MP4LIB_INT_H_ */
    473 
    474