00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <math.h>
00020 #include <limits.h>
00021
00022 #include "global.h"
00023 #include "ratectl.h"
00024
00025
00026
00027
00028
00029
00030
00031
00032 void rc_store_mad(Macroblock *currMB)
00033 {
00034 ImageParameters *p_Img = currMB->p_Img;
00035 InputParameters *p_Inp = currMB->p_Inp;
00036 RCGeneric *p_gen = p_Img->p_rc_gen;
00037
00038 p_gen->MADofMB[currMB->mbAddrX] = ComputeMBMAD(currMB->p_slice->diffy);
00039
00040 if(p_Inp->basicunit < p_Img->FrameSizeInMbs)
00041 {
00042 p_gen->TotalMADBasicUnit += p_gen->MADofMB[currMB->mbAddrX];
00043 }
00044 }
00045
00046
00047
00048
00049
00050
00051
00052
00053 double QP2Qstep( int QP )
00054 {
00055 int i;
00056 double Qstep;
00057 static const double QP2QSTEP[6] = { 0.625, 0.6875, 0.8125, 0.875, 1.0, 1.125 };
00058
00059 Qstep = QP2QSTEP[QP % 6];
00060 for( i=0; i<(QP/6); i++)
00061 Qstep *= 2;
00062
00063 return Qstep;
00064 }
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074 int Qstep2QP( double Qstep, int qp_offset )
00075 {
00076 int q_per = 0, q_rem = 0;
00077
00078 if( Qstep < QP2Qstep(MIN_QP))
00079 return MIN_QP;
00080 else if (Qstep > QP2Qstep(MAX_QP + qp_offset) )
00081 return (MAX_QP + qp_offset);
00082
00083 while( Qstep > QP2Qstep(5) )
00084 {
00085 Qstep /= 2.0;
00086 q_per++;
00087 }
00088
00089 if (Qstep <= 0.65625)
00090 {
00091
00092 q_rem = 0;
00093 }
00094 else if (Qstep <= 0.75)
00095 {
00096
00097 q_rem = 1;
00098 }
00099 else if (Qstep <= 0.84375)
00100 {
00101
00102 q_rem = 2;
00103 }
00104 else if (Qstep <= 0.9375)
00105 {
00106
00107 q_rem = 3;
00108 }
00109 else if (Qstep <= 1.0625)
00110 {
00111
00112 q_rem = 4;
00113 }
00114 else
00115 {
00116
00117 q_rem = 5;
00118 }
00119
00120 return (q_per * 6 + q_rem);
00121 }
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133 int ComputeMBMAD(int diffy[16][16])
00134 {
00135 int k, l, sum = 0;
00136
00137 for (k = 0; k < 16; k++)
00138 for (l = 0; l < 16; l++)
00139 sum += iabs(diffy[k][l]);
00140
00141 return sum;
00142 }
00143
00144
00145
00146
00147
00148
00149
00150
00151 double ComputeFrameMAD(ImageParameters *p_Img)
00152 {
00153 int64 TotalMAD = 0;
00154 unsigned int i;
00155 for(i = 0; i < p_Img->FrameSizeInMbs; i++)
00156 TotalMAD += p_Img->p_rc_gen->MADofMB[i];
00157 return (double)TotalMAD / (256.0 * (double)p_Img->FrameSizeInMbs);
00158 }
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168 void rc_copy_generic( ImageParameters *p_Img, RCGeneric *dst, RCGeneric *src )
00169 {
00170
00171 int *tmpMADofMB = dst->MADofMB;
00172
00173
00174
00175
00176 memcpy( (void *)dst, (void *)src, sizeof(RCGeneric) );
00177
00178
00179 dst->MADofMB = tmpMADofMB;
00180
00181
00182 memcpy( (void *)dst->MADofMB, (void *)src->MADofMB, p_Img->FrameSizeInMbs * sizeof (int) );
00183 }
00184
00185
00186
00187
00188
00189
00190
00191
00192 void rc_alloc_generic( ImageParameters *p_Img, RCGeneric **p_quad )
00193 {
00194 *p_quad = (RCGeneric *) malloc ( sizeof( RCGeneric ) );
00195 if (NULL == *p_quad)
00196 {
00197 no_mem_exit("rc_alloc_generic: rc_alloc_generic");
00198 }
00199 (*p_quad)->MADofMB = (int *) calloc (p_Img->FrameSizeInMbs, sizeof (int));
00200 if (NULL == (*p_quad)->MADofMB)
00201 {
00202 no_mem_exit("rc_alloc_generic: (*p_quad)->MADofMB");
00203 }
00204 (*p_quad)->FieldFrame = 1;
00205 }
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215 void rc_free_generic(RCGeneric **p_quad)
00216 {
00217 if (NULL!=(*p_quad)->MADofMB)
00218 {
00219 free ((*p_quad)->MADofMB);
00220 (*p_quad)->MADofMB = NULL;
00221 }
00222 if (NULL!=(*p_quad))
00223 {
00224 free ((*p_quad));
00225 (*p_quad) = NULL;
00226 }
00227 }
00228
00229
00230
00231
00232
00233
00234
00235
00236 void rc_init_gop_params(ImageParameters *p_Img, InputParameters *p_Inp)
00237 {
00238 int np, nb;
00239 RCQuadratic *p_quad = p_Img->p_rc_quad;
00240 RCGeneric *p_gen = p_Img->p_rc_gen;
00241
00242 switch( p_Inp->RCUpdateMode )
00243 {
00244 case RC_MODE_1: case RC_MODE_3:
00245 if ( !(p_Img->number) )
00246 {
00247
00248 np = p_Inp->no_frm_base - 1;
00249
00250 nb = np * p_Inp->NumberBFrames;
00251
00252 rc_init_GOP(p_Img, p_Inp, p_quad, p_gen, np, nb);
00253 }
00254 break;
00255 case RC_MODE_0: case RC_MODE_2:
00256 if (p_Inp->idr_period == 0)
00257 {
00258 if ( !(p_Img->number) )
00259 {
00260
00261 np = p_Inp->no_frm_base - 1;
00262
00263 nb = np * p_Inp->NumberBFrames;
00264 rc_init_GOP(p_Img, p_Inp, p_quad, p_gen, np, nb);
00265 }
00266 }
00267 else if ( (!p_Inp->adaptive_idr_period && ( p_Img->frm_number - p_Img->lastIDRnumber ) % p_Inp->idr_period == 0)
00268 || (p_Inp->adaptive_idr_period == 1 && ( p_Img->frm_number - imax(p_Img->lastIntraNumber, p_Img->lastIDRnumber) ) % p_Inp->idr_period == 0) )
00269 {
00270 int M = p_Inp->NumberBFrames + 1;
00271 int N = M * p_Inp->idr_period;
00272 int n = (p_Img->number == 0) ? N - ( M - 1) : N;
00273
00274
00275 if ((p_Img->number / p_Inp->idr_period) >= (p_Inp->no_frm_base / p_Inp->idr_period))
00276 {
00277 if (p_Img->number != 0)
00278 n = (p_Inp->no_frm_base - p_Img->number) * (p_Inp->NumberBFrames + 1);
00279 else
00280 n = p_Inp->no_frm_base + (p_Inp->no_frm_base - 1) * p_Inp->NumberBFrames;
00281 }
00282
00283
00284 np = (p_Img->number == 0) ? 1 + ((n - 2) / M) : (n - 1) / M;
00285
00286 nb = n - np - 1;
00287 rc_init_GOP(p_Img, p_Inp, p_quad, p_gen, np, nb);
00288 }
00289 break;
00290 default:
00291 break;
00292 }
00293 }
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303 void rc_init_frame(ImageParameters *p_Img, InputParameters *p_Inp)
00304 {
00305 switch( p_Inp->RCUpdateMode )
00306 {
00307 case RC_MODE_0: case RC_MODE_1: case RC_MODE_2: case RC_MODE_3:
00308
00309
00310 if( (p_Inp->MbInterlace) && (p_Inp->basicunit < p_Img->FrameSizeInMbs) && (p_Img->type == P_SLICE || (p_Inp->RCUpdateMode == RC_MODE_1 && p_Img->number) ) )
00311 p_Img->BasicUnit = p_Inp->basicunit << 1;
00312 else
00313 p_Img->BasicUnit = p_Inp->basicunit;
00314
00315 if ( p_Inp->RDPictureDecision )
00316 {
00317 rc_copy_quadratic( p_Img, p_Inp, p_Img->p_rc_quad_init, p_Img->p_rc_quad );
00318 rc_copy_generic( p_Img, p_Img->p_rc_gen_init, p_Img->p_rc_gen );
00319 }
00320 p_Img->rc_init_pict_ptr(p_Img, p_Inp, p_Img->p_rc_quad, p_Img->p_rc_gen, 1,0,1, 1.0F);
00321
00322 if( p_Img->active_sps->frame_mbs_only_flag)
00323 p_Img->p_rc_gen->TopFieldFlag=0;
00324
00325 p_Img->qp = p_Img->updateQP(p_Img, p_Inp, p_Img->p_rc_quad, p_Img->p_rc_gen, 0) - p_Img->p_rc_quad->bitdepth_qp_scale;
00326 break;
00327 default:
00328 break;
00329 }
00330 }
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340 void rc_init_sequence(ImageParameters *p_Img, InputParameters *p_Inp)
00341 {
00342 switch( p_Inp->RCUpdateMode )
00343 {
00344 case RC_MODE_0: case RC_MODE_1: case RC_MODE_2: case RC_MODE_3:
00345 rc_init_seq(p_Img, p_Inp, p_Img->p_rc_quad, p_Img->p_rc_gen);
00346 break;
00347 default:
00348 break;
00349 }
00350 }
00351
00352 void rc_store_slice_header_bits( ImageParameters *p_Img, InputParameters *p_Inp, int len )
00353 {
00354 switch (p_Inp->RCUpdateMode)
00355 {
00356 case RC_MODE_0: case RC_MODE_1: case RC_MODE_2: case RC_MODE_3:
00357 p_Img->p_rc_gen->NumberofHeaderBits +=len;
00358
00359
00360 if(p_Img->BasicUnit < p_Img->FrameSizeInMbs)
00361 p_Img->p_rc_gen->NumberofBasicUnitHeaderBits +=len;
00362 break;
00363 default:
00364 break;
00365 }
00366 }
00367
00368
00369
00370
00371
00372
00373
00374
00375 void rc_store_diff(int diffy[16][16], imgpel **p_curImg, int cpix_x,imgpel **prediction)
00376 {
00377 int i, j;
00378 int *iDst;
00379 imgpel *Src1, *Src2;
00380
00381 for(j = 0; j < MB_BLOCK_SIZE; j++)
00382 {
00383 iDst = diffy[j];
00384 Src1 = &p_curImg[j][cpix_x];
00385 Src2 = prediction[j];
00386 for (i = 0; i < MB_BLOCK_SIZE; i++)
00387 {
00388 iDst[i] = Src1[i] - Src2[i];
00389 }
00390 }
00391 }
00392