00001 /* 00002 * NOTE: 00003 * 00004 * I removed the OFFLINE_RATE_CONTROL stuff because I'm not using it. 00005 * 00006 * Brendan Dowling 8/27/96 00007 */ 00008 00009 /************************************************************************ 00010 * 00011 * vid_ratectrl.c, part of tmn (TMN encoder) 00012 * Copyright (C) 1995, 1996 Telenor R&D, Norway 00013 * Karl Olav Lillevold <Karl.Lillevold@nta.no> 00014 * 00015 * Contacts: 00016 * Karl Olav Lillevold <Karl.Lillevold@nta.no>, or 00017 * Robert Danielsen <Robert.Danielsen@nta.no> 00018 * 00019 * Telenor Research and Development http://www.nta.no/brukere/DVC/ 00020 * P.O.Box 83 tel.: +47 63 84 84 00 00021 * N-2007 Kjeller, Norway fax.: +47 63 81 00 76 00022 * 00023 ************************************************************************/ 00024 00025 /* 00026 * Disclaimer of Warranty 00027 * 00028 * These software programs are available to the user without any 00029 * license fee or royalty on an "as is" basis. Telenor Research and 00030 * Development disclaims any and all warranties, whether express, 00031 * implied, or statuary, including any implied warranties or 00032 * merchantability or of fitness for a particular purpose. In no 00033 * event shall the copyright-holder be liable for any incidental, 00034 * punitive, or consequential damages of any kind whatsoever arising 00035 * from the use of these programs. 00036 * 00037 * This disclaimer of warranty extends to the user of these programs 00038 * and user's customers, employees, agents, transferees, successors, 00039 * and assigns. 00040 * 00041 * Telenor Research and Development does not represent or warrant that 00042 * the programs furnished hereunder are free of infringement of any 00043 * third-party patents. 00044 * 00045 * Commercial implementations of H.263, including shareware, are 00046 * subject to royalty fees to patent holders. Many of these patents 00047 * are general enough such that they are unavoidable regardless of 00048 * implementation design. 00049 * */ 00050 00051 00052 /* IMPORTANT NOTE: 00053 00054 The H.263 standard does not specify a rate control method. Each 00055 H.263 encoder has to implement a rate control suited for what the 00056 encoder is going to be used for. This software now includes two 00057 rate control methods: (i) the rate control from the TMN5 document, 00058 and (ii) the rate control method used to encode the H.263 00059 bitstreams submitted to the MPEG-4 tests. The default rate control 00060 is (i) which is suitable for low-delay teleconferencing. If the 00061 software is compiled with the OFFLINE_RATE_CONTROL flag, (ii) will 00062 be used. Read more about (ii) below. 00063 00064 */ 00065 00066 #include "vid_sim.h" 00067 #include "video_codec.h" 00068 00069 extern video_codec *VidSt; 00070 00071 /* 00072 * These routines are needed for the low-delay , variable frame rate, 00073 * rate control specified in the TMN5 document 00074 */ 00075 00076 #include <math.h> 00077 00078 #if(0) 00079 /* rate control static variables */ 00080 static float B_prev; /* number of bits spent for the previous frame */ 00081 static float B_target; /* target number of bits/picture */ 00082 static float global_adj; /* due to bits spent for the previous frame */ 00083 #endif 00084 00085 void InitializeRateControl() 00086 { 00087 (VidSt->B_prev) = (float)0.0; 00088 } 00089 00090 void UpdateRateControl(int bits) 00091 { 00092 (VidSt->B_prev) = (float)bits; 00093 } 00094 00095 int InitializeQuantizer(int pict_type, float bit_rate, 00096 float target_frame_rate, float QP_mean) 00097 00098 /* QP_mean = mean quantizer parameter for the previous picture */ 00099 /* bitcount = current total bit count */ 00100 /* To calculate bitcount in vid_coder.c, do something like this : */ 00101 /* int bitcount; */ 00102 /* AddBitsPicture(bits); */ 00103 /* bitcount = bits->total; */ 00104 00105 { 00106 int newQP; 00107 00108 if (pict_type == PCT_INTER) { 00109 00110 (VidSt->B_target) = bit_rate / target_frame_rate; 00111 00112 /* compute picture buffer descrepency as of the previous picture */ 00113 00114 if ((VidSt->B_prev) != 0.0) { 00115 (VidSt->global_adj) = ((VidSt->B_prev) - (VidSt->B_target)) / (2*(VidSt->B_target)); 00116 } 00117 else { 00118 (VidSt->global_adj) = (float)0.0; 00119 } 00120 newQP = (int)(QP_mean * (1 + (VidSt->global_adj)) + (float)0.5); 00121 newQP = mmax(1,mmin(31,newQP)); 00122 } 00123 else if (pict_type == PCT_INTRA) { 00124 fprintf(stderr,"No need to call InititializeQuantizer() for Intra picture\n"); 00125 exit(-1); 00126 } 00127 else { 00128 fprintf(stderr,"Error (InitializePictureRate): picture type unkown.\n"); 00129 exit(-1); 00130 } 00131 #if 1 00132 printf("Global adj = %.2f\n", (VidSt->global_adj)); 00133 printf("meanQP = %.2f newQP = %d\n", QP_mean, newQP); 00134 #endif 00135 fprintf(stdout,"Target no. of bits: %.2f\n", (VidSt->B_target)); 00136 00137 return newQP; 00138 } 00139 00140 00141 /********************************************************************* 00142 * Name: UpdateQuantizer 00143 * 00144 * 00145 * Description: This function generates a new quantizer step size based 00146 * on bits spent up until current macroblock and bits 00147 * spent from the previous picture. Note: this 00148 * routine should be called at the beginning of each 00149 * macroblock line as specified by TMN4. However, this 00150 * can be done at any macroblock if so desired. 00151 * 00152 * Input: current macroblock number (raster scan), mean quantizer 00153 * paramter for previous picture, bit rate, source frame rate, 00154 * hor. number of macroblocks, vertical number of macroblocks, total # 00155 * of bits used until now in the current picture. 00156 * 00157 * Returns: Returns a new quantizer step size for the use of current 00158 * macroblock Note: adjustment to fit with 2-bit DQUANT should be done 00159 * in the calling program. 00160 * 00161 * Side Effects: 00162 * 00163 * Date: 1/5/95 Author: Anurag Bist 00164 * 00165 **********************************************************************/ 00166 00167 00168 int UpdateQuantizer(int mb, float QP_mean, int pict_type, float bit_rate, 00169 int mb_width, int mb_height, int bitcount) 00170 00171 /* mb = macroblock index number */ 00172 /* QP_mean = mean quantizer parameter for the previous picture */ 00173 /* bitcount = total # of bits used until now in the current picture */ 00174 00175 { 00176 int newQP=16; 00177 float local_adj, descrepency, projection; 00178 00179 if (pict_type == PCT_INTRA) { 00180 newQP = 16; /* Changed from newQP = 16;, BD 8/27/96 */ 00181 } 00182 else if (pict_type == PCT_INTER) { 00183 /* compute expected buffer fullness */ 00184 00185 projection = mb * ((VidSt->B_target) / (mb_width*mb_height)); 00186 00187 /* measure descrepency between current fullness and projection */ 00188 descrepency= (bitcount - projection); 00189 00190 /* scale */ 00191 00192 local_adj = 12 * descrepency / bit_rate; 00193 00194 #if(0) 00195 printf("mb = %d\n",mb); 00196 printf("bit_count = %d projection = %.2f \n",bitcount,projection); 00197 printf("B_target = %.2f local_adj = %.2f \n",(VidSt->B_target),local_adj); 00198 #endif 00199 00200 newQP = (int)(QP_mean * (1 + (VidSt->global_adj) + local_adj) + 0.5); 00201 00202 /* the update equation for newQP in TMN4 document section 3.7 */ 00203 00204 } 00205 else { 00206 fprintf(stderr,"Error (UpdateQuantizer): picture type unkown.\n"); 00207 } 00208 00209 #if(0) 00210 printf("mb = %d newQP = %d \n",mb,newQP); 00211 #endif 00212 00213 newQP = mmax(1,mmin(31,newQP)); 00214 return newQP; 00215 } 00216 00217