Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

236 lines
8.7 KiB

  1. /* File: sv_h263_ratectl.c */
  2. /*****************************************************************************
  3. ** Copyright (c) Digital Equipment Corporation, 1995, 1997 **
  4. ** **
  5. ** All Rights Reserved. Unpublished rights reserved under the copyright **
  6. ** laws of the United States. **
  7. ** **
  8. ** The software contained on this media is proprietary to and embodies **
  9. ** the confidential technology of Digital Equipment Corporation. **
  10. ** Possession, use, duplication or dissemination of the software and **
  11. ** media is authorized only pursuant to a valid written license from **
  12. ** Digital Equipment Corporation. **
  13. ** **
  14. ** RESTRICTED RIGHTS LEGEND Use, duplication, or disclosure by the U.S. **
  15. ** Government is subject to restrictions as set forth in Subparagraph **
  16. ** (c)(1)(ii) of DFARS 252.227-7013, or in FAR 52.227-19, as applicable. **
  17. ******************************************************************************/
  18. /* ABOUT THE NEW RATE CONTROL:
  19. ratectrl.c now contains the new simplified rate control we used to
  20. generate the MPEG-4 anchors, instead of the TMN5 rate control. This
  21. simplified scheme works fine to get a specified mean bitrate for
  22. the whole sequence for easy comparison with other coding schemes,
  23. but it is too simple to guarantee a minimum delay in real
  24. videophone applications. It does not skip any extra pictures after
  25. the first frame, and it uses a fixed frame rate. Its purpose is to
  26. achieve the target bitrate as a mean bitrate for the whole
  27. sequence. If the number of pictures encoded is very small, this
  28. will not always be possible because of the high number of bits
  29. spent on the first frame.
  30. The reason we have removed the TMN5 rate control is that we did not
  31. think it worked as well as it should, especially when PB-frames
  32. were used. Any real H.263 product would have had to improve it
  33. anyway.
  34. When grabbing sequences from a framegrabber card, you will not
  35. always get the full reference frame rate, and the original sequence
  36. will have skipped frames. This was much easier to support with a
  37. fixed frame rate scheme.
  38. If you would like to include code for a rate control scheme which
  39. satisfies the HRD requirements in the H.263 standard as well as
  40. works for all types of sequences with and without PB-frames (for
  41. instance with the adaptive PB-frames as included in this version),
  42. please feel free to do so.
  43. If you think the TMN5 scheme worked well enough for you, and the
  44. simplified scheme is too simple, you can add the TMN5 code
  45. without too much work. However, this will not work with the
  46. adaptive PB-frames without a lot of changes, and also coding
  47. sequences which has a lower frame rate than the reference frame
  48. rate will not be possible without additional changes. */
  49. /*
  50. #define _SLIBDEBUG_
  51. */
  52. #include <math.h>
  53. #include "sv_h263.h"
  54. #include "proto.h"
  55. #ifdef _SLIBDEBUG_
  56. #include "sc_debug.h"
  57. #define _DEBUG_ 0 /* detailed debuging statements */
  58. #define _VERBOSE_ 1 /* show progress */
  59. #define _VERIFY_ 0 /* verify correct operation */
  60. #define _WARN_ 1 /* warnings about strange behavior */
  61. #endif
  62. /**********************************************************************
  63. *
  64. * Name: FrameUpdateQP
  65. * Description: updates quantizer once per frame for
  66. * simplified rate control
  67. *
  68. * Returns: new quantizer
  69. * Side effects:
  70. *
  71. ***********************************************************************/
  72. int sv_H263FrameUpdateQP(int buf, int bits, int frames_left, int QP, int B,
  73. float seconds)
  74. {
  75. int newQP, dQP;
  76. float buf_rest, buf_rest_pic;
  77. buf_rest = seconds * B - (float)buf;
  78. newQP = QP;
  79. if (frames_left > 0) {
  80. buf_rest_pic = buf_rest / (float)frames_left;
  81. _SlibDebug(_VERBOSE_,
  82. ScDebugPrintf(NULL, " Simplified rate control for %d remaining pictures:\n", frames_left);
  83. ScDebugPrintf(NULL, " Bits spent / left : %8d / %d (%d per picture)\n",
  84. buf, mnint(buf_rest), mnint(buf_rest_pic));
  85. ScDebugPrintf(NULL, " Limits : %8.0f / %.0f\n",
  86. buf_rest_pic / 1.15, buf_rest_pic * 1.15);
  87. ScDebugPrintf(NULL, " Bits spent on last frame: %8d\n", bits)
  88. );
  89. dQP = (int) mmax(1,QP*0.1);
  90. if (bits > buf_rest_pic * 1.15) {
  91. newQP = mmin(31,QP+dQP);
  92. _SlibDebug(_VERBOSE_, ScDebugPrintf(NULL, " QP -> new QP : %2d -> %2d\n", QP, newQP) );
  93. }
  94. else if (bits < buf_rest_pic / 1.15) {
  95. newQP = mmax(1,QP-dQP);
  96. _SlibDebug(_VERBOSE_, ScDebugPrintf(NULL, " QP -> new QP : %2d -> %2d\n", QP, newQP) );
  97. }
  98. else {
  99. _SlibDebug(_VERBOSE_, ScDebugPrintf(NULL, " QP not changed\n", QP, newQP) );
  100. }
  101. }
  102. return newQP;
  103. }
  104. /* rate control static variables */
  105. static float B_prev; /* number of bits spent for the previous frame */
  106. static float B_target; /* target number of bits/picture */
  107. static float global_adj; /* due to bits spent for the previous frame */
  108. void sv_H263GOBInitRateCntrl()
  109. {
  110. B_prev = (float)0.0;
  111. }
  112. void sv_H263GOBUpdateRateCntrl(int bits)
  113. {
  114. B_prev = (float)bits;
  115. }
  116. int sv_H263GOBInitQP(float bit_rate, float target_frame_rate, float QP_mean)
  117. /* QP_mean = mean quantizer parameter for the previous picture */
  118. /* bitcount = current total bit count */
  119. /* To calculate bitcount in coder.c, do something like this : */
  120. /* int bitcount; */
  121. /* AddBitsPicture(bits); */
  122. /* bitcount = bits->total; */
  123. {
  124. int newQP;
  125. B_target = bit_rate / target_frame_rate;
  126. /* compute picture buffer descrepency as of the previous picture */
  127. if (B_prev != 0.0) {
  128. global_adj = (B_prev - B_target) / (2*B_target);
  129. }
  130. else {
  131. global_adj = (float)0.0;
  132. }
  133. newQP = (int)(QP_mean + QP_mean * global_adj + (float)0.5);
  134. newQP = mmax(1,mmin(31,newQP));
  135. return newQP;
  136. }
  137. /*********************************************************************
  138. * Name: UpdateQuantizer
  139. *
  140. *
  141. * Description: This function generates a new quantizer step size based
  142. * on bits spent up until current macroblock and bits
  143. * spent from the previous picture. Note: this
  144. * routine should be called at the beginning of each
  145. * macroblock line as specified by TMN4. However, this
  146. * can be done at any macroblock if so desired.
  147. *
  148. * Input: current macroblock number (raster scan), mean quantizer
  149. * paramter for previous picture, bit rate, source frame rate,
  150. * hor. number of macroblocks, vertical number of macroblocks, total #
  151. * of bits used until now in the current picture.
  152. *
  153. * Returns: Returns a new quantizer step size for the use of current
  154. * macroblock Note: adjustment to fit with 2-bit DQUANT should be done
  155. * in the calling program.
  156. *
  157. * Side Effects:
  158. *
  159. * Date: 1/5/95 Author: Anurag Bist
  160. *
  161. **********************************************************************/
  162. int sv_H263GOBUpdateQP(int mb, float QP_mean, float bit_rate,
  163. int mb_width, int mb_height, int bitcount,
  164. int NOgob, int *VARgob, int pb_frame)
  165. /* mb = macroblock index number */
  166. /* QP_mean = mean quantizer parameter for the previous picture */
  167. /* bitcount = total # of bits used until now in the current picture */
  168. {
  169. int newQP=16, i, VARavg=0;
  170. float local_adj, descrepency, projection;
  171. double VARratio=0.0;
  172. if(NOgob) {
  173. for(i=0;i<NOgob;i++) VARavg += VARgob[i];
  174. VARavg /= NOgob;
  175. }
  176. /* compute expected buffer fullness */
  177. projection = mb * (B_target / (mb_width*mb_height));
  178. /* measure descrepency between current fullness and projection */
  179. descrepency= ((float)bitcount - projection);
  180. /* scale */
  181. local_adj = 12 * descrepency / bit_rate;
  182. if(NOgob) {
  183. VARratio = (double)VARgob[NOgob] / (double)VARavg ;
  184. VARratio = log(VARratio) / 0.693147 ;
  185. if(pb_frame) local_adj += (float) (VARratio / 4.0);
  186. else local_adj += (float) (VARratio / 2.0);
  187. }
  188. newQP = (int)(QP_mean + QP_mean * (global_adj + local_adj) + (float)0.5);
  189. newQP = mmax(1,mmin(31,newQP));
  190. return newQP;
  191. }