H264RateControl.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*------------------------------------------------------------------------------
  2. -- --
  3. -- This software is confidential and proprietary and may be used --
  4. -- only as expressly authorized by a licensing agreement from --
  5. -- --
  6. -- Hantro Products Oy. --
  7. -- --
  8. -- (C) COPYRIGHT 2006 HANTRO PRODUCTS OY --
  9. -- ALL RIGHTS RESERVED --
  10. -- --
  11. -- The entire notice above must be reproduced --
  12. -- on all copies and should not be removed. --
  13. -- --
  14. --------------------------------------------------------------------------------
  15. --
  16. -- Description : Rate control structures and function prototypes
  17. --
  18. ------------------------------------------------------------------------------*/
  19. #ifndef H264_RATE_CONTROL_H
  20. #define H264_RATE_CONTROL_H
  21. #include "enccommon.h"
  22. #include "H264Sei.h"
  23. enum
  24. { H264RC_OVERFLOW = -1 };
  25. #define RC_CBR_HRD 0 /* 1 = Constant bit rate model. Must use filler
  26. * data to conform */
  27. #define CTRL_LEVELS 7 /* DO NOT CHANGE THIS */
  28. #define CHECK_POINTS_MAX 10 /* DO NOT CHANGE THIS */
  29. #define RC_TABLE_LENGTH 10 /* DO NOT CHANGE THIS */
  30. typedef struct {
  31. i32 a1; /* model parameter */
  32. i32 a2; /* model parameter */
  33. i32 qp_prev; /* previous QP */
  34. i32 qs[RC_TABLE_LENGTH+1]; /* quantization step size */
  35. i32 bits[RC_TABLE_LENGTH+1];/* Number of bits needed to code residual */
  36. i32 pos; /* current position */
  37. i32 len; /* current lenght */
  38. i32 zero_div; /* a1 divisor is 0 */
  39. } linReg_s;
  40. typedef struct
  41. {
  42. i32 wordError[CTRL_LEVELS]; /* Check point error bit */
  43. i32 qpChange[CTRL_LEVELS]; /* Check point qp difference */
  44. i32 wordCntTarget[CHECK_POINTS_MAX]; /* Required bit count */
  45. i32 wordCntPrev[CHECK_POINTS_MAX]; /* Real bit count */
  46. i32 checkPointDistance;
  47. i32 checkPoints;
  48. } h264QpCtrl_s;
  49. /* Virtual buffer */
  50. typedef struct
  51. {
  52. i32 bufferSize; /* size of the virtual buffer */
  53. i32 bitRate; /* input bit rate per second */
  54. i32 bitPerPic; /* average number of bits per picture */
  55. i32 picTimeInc; /* timeInc since last coded picture */
  56. i32 timeScale; /* input frame rate numerator */
  57. i32 unitsInTic; /* input frame rate denominator */
  58. i32 virtualBitCnt; /* virtual (channel) bit count */
  59. i32 realBitCnt; /* real bit count */
  60. i32 bufferOccupancy; /* number of bits in the buffer */
  61. i32 skipFrameTarget; /* how many frames should be skipped in a row */
  62. i32 skippedFrames; /* how many frames have been skipped in a row */
  63. i32 nonZeroTarget;
  64. i32 bucketFullness; /* Leaky Bucket fullness */
  65. /* new rate control */
  66. i32 windowRem;
  67. } h264VirtualBuffer_s;
  68. typedef struct
  69. {
  70. true_e picRc;
  71. true_e mbRc; /* Mb header qp can vary, check point rc */
  72. true_e picSkip; /* Frame Skip enable */
  73. true_e hrd; /* HRD restrictions followed or not */
  74. u32 fillerIdx;
  75. i32 mbPerPic; /* Number of macroblock per picture */
  76. i32 mbRows; /* MB rows in picture */
  77. i32 coeffCntMax; /* Number of coeff per picture */
  78. i32 nonZeroCnt;
  79. i32 srcPrm; /* Source parameter */
  80. i32 qpSum; /* Qp sum counter */
  81. u32 sliceTypeCur;
  82. u32 sliceTypePrev;
  83. true_e frameCoded; /* Pic coded information */
  84. i32 fixedQp; /* Pic header qp when fixed */
  85. i32 qpHdr; /* Pic header qp of current voded picture */
  86. i32 qpMin; /* Pic header minimum qp, user set */
  87. i32 qpMax; /* Pic header maximum qp, user set */
  88. i32 qpHdrPrev; /* Pic header qp of previous coded picture */
  89. i32 qpLastCoded; /* Quantization parameter of last coded mb */
  90. i32 qpTarget; /* Target quantrization parameter */
  91. u32 estTimeInc;
  92. i32 outRateNum;
  93. i32 outRateDenom;
  94. i32 gDelaySum;
  95. i32 gInitialDelay;
  96. i32 gInitialDoffs;
  97. h264QpCtrl_s qpCtrl;
  98. h264VirtualBuffer_s virtualBuffer;
  99. sei_s sei;
  100. i32 gBufferMin, gBufferMax;
  101. /* new rate control */
  102. linReg_s linReg; /* Data for R-Q model for inter frames */
  103. linReg_s rError; /* Rate prediction error for inter frames(bits) */
  104. linReg_s intra; /* Data for intra frames */
  105. linReg_s intraError; /* Prediction error for intra frames */
  106. linReg_s gop; /* Data for GOP */
  107. i32 targetPicSize;
  108. i32 frameBitCnt;
  109. /* for gop rate control */
  110. i32 gopQpSum; /* Sum of current GOP inter frame QPs */
  111. i32 gopQpDiv;
  112. i32 gopBitCnt; /* Current GOP bit count so far */
  113. i32 gopAvgBitCnt; /* Previous GOP average bit count */
  114. u32 frameCnt;
  115. i32 gopLen;
  116. i32 windowLen; /* Bitrate window which tries to match target */
  117. i32 intraInterval; /* Distance between two previous I-frames */
  118. i32 intraIntervalCtr;
  119. i32 intraQpDelta;
  120. u32 fixedIntraQp;
  121. i32 mbQpAdjustment; /* QP delta for MAD macroblock QP adjustment */
  122. } h264RateControl_s;
  123. /*------------------------------------------------------------------------------
  124. Function prototypes
  125. ------------------------------------------------------------------------------*/
  126. bool_e H264InitRc(h264RateControl_s * rc);
  127. void H264BeforePicRc(h264RateControl_s * rc, u32 timeInc, u32 sliceType);
  128. i32 H264AfterPicRc(h264RateControl_s * rc, u32 nonZeroCnt, u32 byteCnt,
  129. u32 qpSum);
  130. u32 H264FillerRc(h264RateControl_s * rc, u32 frameCnt);
  131. i32 H264Calculate(i32 a, i32 b, i32 c);
  132. #endif /* H264_RATE_CONTROL_H */