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