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