RateControl.cpp
上传用户:szklck
上传日期:2007-01-22
资源大小:925k
文件大小:4k
源码类别:

图形图像处理

开发平台:

Visual C++

  1. #define OFFLINE_RATE_CONTROL//jwp
  2. #include"stdafx.h"
  3. #include "Global.h"
  4. #ifdef OFFLINE_RATE_CONTROL
  5. #include <stdio.h>
  6. #include <math.h>
  7. extern int FrameUpdateQP(int buf, int bits, int frames_left, int QP, int B,float seconds) 
  8. {
  9.   int newQP, dQP;
  10.   float buf_rest, buf_rest_pic;
  11.   buf_rest = seconds * B - (float)buf;
  12.   newQP = QP;
  13.   if (frames_left > 0) {
  14.     buf_rest_pic = buf_rest / (float)frames_left;
  15.     dQP = mmax(1,(int)(QP*0.1));
  16.     if (bits > buf_rest_pic * 1.15) {
  17.       newQP = mmin(31,QP+dQP);
  18.     }
  19.     else if (bits < buf_rest_pic / 1.15) {
  20.       newQP = mmax(1,QP-dQP);
  21.     }
  22.     else {
  23.     }
  24.   }
  25.   return newQP;
  26. }
  27. #else
  28. /* 
  29.    These routines are needed for the low-delay , variable frame rate,
  30.    rate control specified in the TMN5 document
  31. */
  32. #include <math.h>
  33. /* rate control static variables */
  34. static float B_prev;     /* number of bits spent for the previous frame */
  35. static float B_target;   /* target number of bits/picture               */
  36. static float global_adj; /* due to bits spent for the previous frame    */
  37. void InitializeRateControl()
  38. {
  39.   B_prev = (float)0.0;
  40. }
  41. void UpdateRateControl(int bits)
  42. {
  43.   B_prev = (float)bits;
  44. }
  45. int InitializeQuantizer(int pict_type, float bit_rate, 
  46.         float target_frame_rate, float QP_mean) 
  47. /* QP_mean = mean quantizer parameter for the previous picture */
  48. /* bitcount = current total bit count                          */
  49. /* To calculate bitcount in coder.c, do something like this :  */
  50. /* int bitcount;                                               */
  51. /* AddBitsPicture(bits);                                       */
  52. /* bitcount = bits->total;                                     */
  53. {
  54.   int newQP;
  55.   if (pict_type == PCT_INTER) {
  56.     B_target = bit_rate / target_frame_rate;
  57.     /* compute picture buffer descrepency as of the previous picture */
  58.     if (B_prev != 0.0) {
  59.       global_adj = (B_prev - B_target) / (2*B_target);
  60.     }
  61.     else {
  62.       global_adj = (float)0.0;
  63.     }
  64.     newQP = (int)(QP_mean * (1 + global_adj) + (float)0.5);
  65.     newQP = mmax(1,mmin(31,newQP));  
  66.   }
  67.   else if (pict_type == PCT_INTRA) {
  68.     newQP=4;
  69.   }
  70.   else  {
  71.     newQP=4;
  72.   }  
  73.   return newQP;
  74. }
  75. /*********************************************************************
  76. *   Name:          UpdateQuantizer
  77. *
  78. *
  79. * Description: This function generates a new quantizer step size based
  80. *                  on bits spent up until current macroblock and bits
  81. *                  spent from the previous picture.  Note: this
  82. *                  routine should be called at the beginning of each
  83. *                  macroblock line as specified by TMN4. However, this
  84. *                  can be done at any macroblock if so desired.
  85. *
  86. *  Input: current macroblock number (raster scan), mean quantizer
  87. *  paramter for previous picture, bit rate, source frame rate,
  88. *  hor. number of macroblocks, vertical number of macroblocks, total #
  89. *  of bits used until now in the current picture.
  90. *
  91. *  Returns: Returns a new quantizer step size for the use of current
  92. *  macroblock Note: adjustment to fit with 2-bit DQUANT should be done
  93. *  in the calling program.
  94. *
  95. *  Side Effects:  
  96. *
  97. *  Date: 1/5/95    Author: Anurag Bist
  98. *
  99. **********************************************************************/
  100. int UpdateQuantizer(int mb, float QP_mean, int pict_type, float bit_rate, 
  101.                     int mb_width, int mb_height, int bitcount) 
  102. /* mb = macroblock index number */
  103. /* mb_width = macroblock numbers in width */
  104. /* QP_mean = mean quantizer parameter for the previous picture */
  105. /* bitcount = total # of bits used until now in the current picture */
  106. {
  107.   int newQP=4;
  108.   float local_adj, descrepency, projection;
  109.   
  110.   if (pict_type == PCT_INTRA) {
  111.     newQP = 4;
  112.   }
  113.   else if (pict_type == PCT_INTER) {
  114.     /* compute expected buffer fullness */
  115.     
  116.     projection = mb * (B_target / (mb_width*mb_height));
  117.     
  118.     /* measure descrepency between current fullness and projection */
  119.     descrepency= (bitcount - projection);
  120.     
  121.     /* scale */
  122.     
  123.     local_adj = 12 * descrepency / bit_rate;
  124.     
  125.     
  126.     newQP = (int)(QP_mean * (1 + global_adj + local_adj) + 0.5);
  127.     
  128.   /* the update equation for newQP in TMN4 document section 3.7 */
  129.   }
  130.   else  {
  131.    AfxMessageBox("Error (UpdateQuantizer): picture type unkown.n");
  132.   }
  133.   
  134.   newQP = mmax(1,mmin(31,newQP));  
  135.   return newQP;
  136. }
  137. #endif