H264Mad.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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
  17. --
  18. ------------------------------------------------------------------------------*/
  19. #include "H264Mad.h"
  20. /*------------------------------------------------------------------------------
  21. Init MAD structure
  22. ------------------------------------------------------------------------------*/
  23. void H264MadInit(madTable_s *mad, u32 mbPerFrame)
  24. {
  25. i32 i;
  26. mad->mbPerFrame = mbPerFrame;
  27. mad->threshold = 256*6;
  28. for (i = 0; i < MAD_TABLE_LEN; i++)
  29. {
  30. mad->th[i] = 0;
  31. mad->count[i] = 0;
  32. }
  33. mad->pos = 0;
  34. mad->len = 0;
  35. }
  36. /*------------------------------------------------------------------------------
  37. update_tables()
  38. ------------------------------------------------------------------------------*/
  39. static void update_tables(madTable_s *p, i32 th, i32 count)
  40. {
  41. const i32 clen = 3;
  42. i32 tmp = p->pos;
  43. p->th[tmp] = th;
  44. p->count[tmp] = count;
  45. if (++p->pos >= clen) {
  46. p->pos = 0;
  47. }
  48. if (p->len < clen) {
  49. p->len++;
  50. }
  51. }
  52. /*------------------------------------------------------------------------------
  53. lin_sx() calculate value of Sx for n points.
  54. ------------------------------------------------------------------------------*/
  55. static i32 lin_sx(i32 *x, i32 n)
  56. {
  57. i32 sum = 0;
  58. while (n--) {
  59. sum += x[n];
  60. if (sum < 0) {
  61. return I32_MAX;
  62. }
  63. }
  64. return sum;
  65. }
  66. /*------------------------------------------------------------------------------
  67. lin_sxy() calculate value of Sxy for n points.
  68. ------------------------------------------------------------------------------*/
  69. static i32 lin_sxy(i32 *qp, i32 *r, i32 n)
  70. {
  71. i32 sum = 0;
  72. while (n--) {
  73. sum += qp[n] * r[n];
  74. if (sum < 0) {
  75. return I32_MAX;
  76. }
  77. }
  78. return sum;
  79. }
  80. /*------------------------------------------------------------------------------
  81. lin_nsxx() calculate value of n * Sxx for n points.
  82. ------------------------------------------------------------------------------*/
  83. static i32 lin_nsxx(i32 *qp, i32 n)
  84. {
  85. i32 sum = 0, d = n;
  86. while (n--) {
  87. sum += d * qp[n] * qp[n];
  88. if (sum < 0) {
  89. return I32_MAX;
  90. }
  91. }
  92. return sum;
  93. }
  94. /*------------------------------------------------------------------------------
  95. update_model() Update model parameter by Linear Regression.
  96. ------------------------------------------------------------------------------*/
  97. static void update_model(madTable_s *p)
  98. {
  99. i32 *count = p->count, *th = p->th, n = p->len;
  100. i32 sx = lin_sx(p->count, n);
  101. i32 sy = lin_sx(p->th, n);
  102. i32 a1 = 0, a2 = 0;
  103. /*i32 i;
  104. for (i = 0; i < n; i++) {
  105. printf("model: cnt %i th %i\n", count[i], th[i]);
  106. }*/
  107. ASSERT(sx >= 0);
  108. ASSERT(sy >= 0);
  109. if (n > 1)
  110. {
  111. a1 = lin_sxy(count, th, n);
  112. a1 = a1 < (I32_MAX / n) ? a1 * n : I32_MAX;
  113. if (sy) {
  114. a1 -= sx < (I32_MAX / sy) ? sx * sy : I32_MAX;
  115. }
  116. a2 = (lin_nsxx(count, n) - (sx * sx));
  117. if (a2) {
  118. a1 = DIV(a1 * DSCY, a2);
  119. } else {
  120. a1 = 0;
  121. }
  122. /* Value of a1 shouldn't be excessive */
  123. a1 = MAX(a1, 0);
  124. a1 = MIN(a1, 1024*DSCY);
  125. if (n)
  126. a2 = DIV(sy, n) - DIV(a1 * sx, n*DSCY);
  127. else
  128. a2 = 0;
  129. }
  130. p->a1 = a1;
  131. p->a2 = a2;
  132. /*printf("model: a2:%9d a1:%8d\n", p->a2, p->a1);*/
  133. }
  134. /*------------------------------------------------------------------------------
  135. Update MAD threshold based on previous frame count of macroblocks with MAD
  136. under threshold. Trying to adjust threshold so that madCount == targetCount.
  137. ------------------------------------------------------------------------------*/
  138. void H264MadThreshold(madTable_s *mad, u32 madCount)
  139. {
  140. /* Target to improve quality for 30% of macroblocks */
  141. i32 targetCount = 30*mad->mbPerFrame/100;
  142. i32 threshold = mad->threshold;
  143. i32 lowLimit, highLimit;
  144. update_tables(mad, mad->threshold, madCount);
  145. update_model(mad);
  146. /* Calculate new threshold for next frame using either linear regression
  147. * model or adjustment based on current setting */
  148. if (mad->a1)
  149. threshold = mad->a1 * targetCount / DSCY + mad->a2;
  150. else if (madCount < (u32)targetCount)
  151. threshold = MAX(mad->threshold * 5/4, mad->threshold + 256);
  152. else
  153. threshold = MIN(mad->threshold * 3/4, mad->threshold - 256);
  154. /* For small count, ensure that we increase the threshold minimum 1 step */
  155. if (madCount < (u32)targetCount/2)
  156. threshold = MAX(threshold, mad->threshold + 256);
  157. /* If previous frame had zero count, ensure that we increase threshold */
  158. if (!madCount)
  159. threshold = MAX(threshold, mad->threshold + 256*4);
  160. /* Limit how much the threshold can change between two frames */
  161. lowLimit = mad->threshold/2;
  162. highLimit = MAX(mad->threshold*2, 256*4);
  163. mad->threshold = MIN(highLimit, MAX(lowLimit, threshold));
  164. /* threshold_div256 has 6-bits range [0,63] */
  165. mad->threshold = ((mad->threshold+128)/256)*256;
  166. mad->threshold = MAX(0, MIN(63*256, mad->threshold));
  167. }