00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <stdio.h>
00031 #include <limits.h>
00032 #include <math.h>
00033 #include <pthread.h>
00034
00035 #include "config.h"
00036 #include "global.h"
00037 #include "fastintfns.h"
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053 void ratectl_init_seq(ratectl_t *ratectl)
00054 {
00055 pthread_mutexattr_t mutex_attr;
00056 pthread_mutexattr_init(&mutex_attr);
00057 pthread_mutex_init(&(ratectl->ratectl_lock), &mutex_attr);
00058
00059 ratectl->avg_KI = 2.5;
00060 ratectl->avg_KB = 10.0;
00061 ratectl->avg_KP = 10.0;
00062
00063 ratectl->bits_per_mb = (double)bit_rate / (mb_per_pict);
00064
00065
00066
00067
00068
00069 if (ratectl->r == 0)
00070 ratectl->r = (int)floor(2.0 * bit_rate / frame_rate + 0.5);
00071
00072 ratectl->Ki = 1.2;
00073 ratectl->Kb = 1.4;
00074 ratectl->Kp = 1.1;
00075
00076
00077 if (ratectl->avg_act == 0.0) ratectl->avg_act = 400.0;
00078
00079
00080 ratectl->R = 0;
00081 ratectl->IR = 0;
00082
00083
00084
00085
00086
00087
00088
00089
00090 ratectl->CarryR = 0;
00091 ratectl->CarryRLim = video_buffer_size / 3;
00092
00093
00094
00095 ratectl->Xi = 1500*mb_per_pict;
00096 ratectl->Xp = 550*mb_per_pict;
00097 ratectl->Xb = 170*mb_per_pict;
00098 ratectl->d0i = -1;
00099 ratectl->d0pb = -1;
00100
00101 ratectl->current_quant = 1;
00102 }
00103
00104 void ratectl_init_GOP(ratectl_t *ratectl, int np, int nb)
00105 {
00106 double per_gop_bits =
00107 (double)(1 + np + nb) * (double)bit_rate / frame_rate;
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134 if( ratectl->R > 0 )
00135 {
00136
00137
00138
00139
00140 ratectl->gop_undershoot = intmin( video_buffer_size/2, (int)ratectl->R );
00141
00142 ratectl->R = ratectl->gop_undershoot + per_gop_bits;
00143 }
00144 else
00145 {
00146
00147 ratectl->R += per_gop_bits;
00148 ratectl->gop_undershoot = 0;
00149 }
00150 ratectl->IR = ratectl->R;
00151 ratectl->Np = fieldpic ? 2 * np + 1 : np;
00152 ratectl->Nb = fieldpic ? 2 * nb : nb;
00153 }
00154
00155 static int scale_quant(pict_data_s *picture, double quant )
00156 {
00157 int iquant;
00158
00159 if (picture->q_scale_type )
00160 {
00161 iquant = (int) floor(quant+0.5);
00162
00163
00164 if (iquant<1)
00165 iquant = 1;
00166 if (iquant>112)
00167 iquant = 112;
00168
00169 iquant =
00170 non_linear_mquant_table_hv[map_non_linear_mquant_hv[iquant]];
00171 }
00172 else
00173 {
00174
00175 iquant = (int)floor(quant+0.5);
00176 if (iquant<2)
00177 iquant = 2;
00178 if (iquant>62)
00179 iquant = 62;
00180 iquant = (iquant/2)*2;
00181 }
00182 return iquant;
00183 }
00184
00185
00186
00187
00188
00189 static double var_sblk(p, lx)
00190 unsigned char *p;
00191 int lx;
00192 {
00193 int j;
00194 register unsigned int v, s, s2;
00195
00196 s = s2 = 0;
00197
00198 for (j=0; j<8; j++)
00199 {
00200 v = p[0]; s += v; s2 += v * v;
00201 v = p[1]; s += v; s2 += v * v;
00202 v = p[2]; s += v; s2 += v * v;
00203 v = p[3]; s += v; s2 += v * v;
00204 v = p[4]; s += v; s2 += v * v;
00205 v = p[5]; s += v; s2 += v * v;
00206 v = p[6]; s += v; s2 += v * v;
00207 v = p[7]; s += v; s2 += v * v;
00208 p += lx;
00209 }
00210
00211 return (double)s2 / 64.0 - ((double)s / 64.0) * ((double)s / 64.0);
00212 }
00213
00214
00215 static double calc_actj(pict_data_s *picture)
00216 {
00217 int i,j,k,l;
00218 double actj,sum;
00219 uint16_t *i_q_mat;
00220 int actsum;
00221 sum = 0.0;
00222 k = 0;
00223
00224 for (j=0; j<height2; j+=16)
00225 for (i=0; i<width; i+=16)
00226 {
00227
00228
00229
00230
00231
00232
00233
00234
00235 if( picture->mbinfo[k].mb_type & MB_INTRA )
00236 {
00237 i_q_mat = i_intra_q;
00238
00239
00240
00241 actsum = -80*COEFFSUM_SCALE;
00242 }
00243 else
00244 {
00245 i_q_mat = i_inter_q;
00246 actsum = 0;
00247 }
00248
00249
00250
00251
00252
00253
00254
00255
00256 for( l = 0; l < 6; ++l )
00257 actsum +=
00258 (*pquant_weight_coeff_sum)
00259 ( cur_picture.mbinfo[k].dctblocks[l], i_q_mat ) ;
00260 actj = (double)actsum / (double)COEFFSUM_SCALE;
00261 if( actj < 12.0 )
00262 actj = 12.0;
00263
00264 picture->mbinfo[k].act = (double)actj;
00265 sum += (double)actj;
00266 ++k;
00267 }
00268 return sum;
00269 }
00270
00271
00272
00273
00274
00275 void ratectl_init_pict(ratectl_t *ratectl, pict_data_s *picture)
00276 {
00277 double avg_K;
00278 double target_Q;
00279 double current_Q;
00280 double Si, Sp, Sb;
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295 ratectl->actsum = calc_actj(picture );
00296 ratectl->avg_act = (double)ratectl->actsum/(double)(mb_per_pict);
00297 ratectl->sum_avg_act += ratectl->avg_act;
00298 ratectl->actcovered = 0.0;
00299
00300
00301
00302
00303
00304 ratectl->min_q = ratectl->min_d = INT_MAX;
00305 ratectl->max_q = ratectl->max_d = INT_MIN;
00306 switch (picture->pict_type)
00307 {
00308 case I_TYPE:
00309
00310
00311
00312
00313
00314
00315 ratectl->d = ratectl->d0i;
00316 avg_K = ratectl->avg_KI;
00317 Si = (ratectl->Xi + 3.0*avg_K*ratectl->actsum)/4.0;
00318 ratectl->T = ratectl->R/(1.0+ratectl->Np*ratectl->Xp*ratectl->Ki/(Si*ratectl->Kp)+ratectl->Nb*ratectl->Xb*ratectl->Ki/(Si*ratectl->Kb));
00319
00320 break;
00321 case P_TYPE:
00322 ratectl->d = ratectl->d0pb;
00323 avg_K = ratectl->avg_KP;
00324 Sp = (ratectl->Xp + avg_K*ratectl->actsum) / 2.0;
00325 ratectl->T = ratectl->R/(ratectl->Np+ratectl->Nb*ratectl->Kp*ratectl->Xb/(ratectl->Kb*Sp)) + 0.5;
00326 break;
00327 case B_TYPE:
00328 ratectl->d = ratectl->d0pb;
00329 avg_K = ratectl->avg_KB;
00330 Sb = ratectl->Xb ;
00331 ratectl->T = ratectl->R/(ratectl->Nb+ratectl->Np*ratectl->Kb*ratectl->Xp/(ratectl->Kp*Sb));
00332 break;
00333 }
00334
00335
00336 if( ratectl->d < 0 )
00337 ratectl->d = 0;
00338
00339
00340
00341
00342
00343 if( ratectl->T < 4000.0 )
00344 {
00345 ratectl->T = 4000.0;
00346 }
00347 target_Q = scale_quant(picture,
00348 avg_K * ratectl->avg_act *(mb_per_pict) / ratectl->T);
00349 current_Q = scale_quant(picture,62.0*ratectl->d / ratectl->r);
00350 #ifdef DEBUG
00351 if( !quiet )
00352 {
00353
00354 printf( "AA=%3.4f SA==%3.4f ",avg_act, sum_avg_act );
00355 }
00356 #endif
00357
00358 if ( current_Q < 3 && target_Q > 12 )
00359 {
00360
00361
00362
00363 ratectl->d = (int) (target_Q * ratectl->r / 62.0);
00364 }
00365
00366 ratectl->S = bitcount();
00367 ratectl->frame_start = bitcount();
00368
00369 if(ratectl->current_quant < 1) ratectl->current_quant = 1;
00370 if(ratectl->current_quant > 100) ratectl->current_quant = 100;
00371 }
00372
00373
00374 int ratectl_start_mb(ratectl_t *ratectl, pict_data_s *picture)
00375 {
00376 double Qj;
00377 int mquant;
00378
00379 if(fixed_mquant)
00380 Qj = fixed_mquant;
00381 else
00382 Qj = ratectl->current_quant;
00383
00384
00385 mquant = scale_quant( picture, Qj);
00386 mquant = intmax(mquant, quant_floor);
00387
00388 return mquant;
00389 }
00390
00391 void ratectl_update_pict(ratectl_t *ratectl, pict_data_s *picture)
00392 {
00393 double X;
00394 double K;
00395 int64_t AP,PP;
00396 int i;
00397 int Qsum;
00398 int frame_overshoot;
00399 double avg_bitrate;
00400 int last_size;
00401 double new_weight;
00402 double old_weight;
00403
00404 if(fixed_mquant) return;
00405
00406 AP = bitcount() - ratectl->S;
00407 frame_overshoot = (int)AP-(int)ratectl->T;
00408
00409
00410
00411
00412 ratectl->d += frame_overshoot;
00413
00414
00415
00416
00417
00418
00419
00420
00421 if( ratectl->gop_undershoot-frame_overshoot > video_buffer_size/2 )
00422 {
00423 int padding_bytes =
00424 ((ratectl->gop_undershoot - frame_overshoot) - video_buffer_size/2)/8;
00425 if( quant_floor != 0 )
00426 {
00427 PP = AP + padding_bytes;
00428 }
00429 else
00430 {
00431
00432
00433 for( i = 0; i < padding_bytes/2; ++i )
00434 {
00435
00436 }
00437 PP = bitcount() - ratectl->S;
00438 }
00439 frame_overshoot = (int)PP - (int)ratectl->T;
00440 }
00441 else
00442 PP = AP;
00443
00444
00445
00446
00447
00448
00449
00450 ratectl->gop_undershoot -= frame_overshoot;
00451 ratectl->gop_undershoot = ratectl->gop_undershoot > 0 ? ratectl->gop_undershoot : 0;
00452 ratectl->R -= PP;
00453
00454 Qsum = 0;
00455 for( i = 0; i < mb_per_pict; ++i )
00456 {
00457 Qsum += picture->mbinfo[i].mquant;
00458 }
00459
00460
00461 ratectl->AQ = (double)Qsum/(double)mb_per_pict;
00462
00463
00464
00465
00466
00467 ratectl->SQ += ratectl->AQ;
00468 X = (double)AP*(ratectl->AQ/2.0);
00469
00470 K = X / ratectl->actsum;
00471 #ifdef DEBUG
00472 if( !quiet )
00473 {
00474 printf( "AQ=%.1f SQ=%.2f", AQ,SQ);
00475 }
00476 #endif
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499 switch (picture->pict_type)
00500 {
00501 case I_TYPE:
00502 ratectl->avg_KI = (K + ratectl->avg_KI * K_AVG_WINDOW_I) / (K_AVG_WINDOW_I+1.0) ;
00503 ratectl->d0i = ratectl->d;
00504 ratectl->Xi = (X + 3.0 * ratectl->Xi) / 4.0;
00505 break;
00506 case P_TYPE:
00507 ratectl->avg_KP = (K + ratectl->avg_KP * K_AVG_WINDOW_P) / (K_AVG_WINDOW_P+1.0) ;
00508 ratectl->d0pb = ratectl->d;
00509 ratectl->Xp = (X + ratectl->Xp * 12.0) / 13.0;
00510 ratectl->Np--;
00511 break;
00512 case B_TYPE:
00513 ratectl->avg_KB = (K + ratectl->avg_KB * K_AVG_WINDOW_B) / (K_AVG_WINDOW_B + 1.0) ;
00514 ratectl->d0pb = ratectl->d;
00515 ratectl->Xb = (X + ratectl->Xb * 24.0) / 25.0;
00516 ratectl->Nb--;
00517 break;
00518 }
00519
00520 ratectl->frame_end = bitcount();
00521
00522 last_size = ratectl->frame_end - ratectl->frame_start;
00523 avg_bitrate = (double)last_size * frame_rate;
00524 switch(picture->pict_type)
00525 {
00526 case I_TYPE:
00527 new_weight = avg_bitrate / bit_rate * 1 / N;
00528 old_weight = (double)(N - 1) / N;
00529 break;
00530
00531 default:
00532 case P_TYPE:
00533 new_weight = avg_bitrate / bit_rate * (N - 1) / N;
00534 old_weight = (double)1 / N;
00535 break;
00536 }
00537 ratectl->current_quant *= (old_weight + new_weight);
00538
00539
00540
00541
00542
00543
00544
00545 }
00546
00547
00548 int ratectl_calc_mquant(ratectl_t *ratectl, pict_data_s *picture, int j)
00549 {
00550 int mquant;
00551 double dj, Qj, actj, N_actj;
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561 actj = picture->mbinfo[j].act;
00562
00563
00564
00565
00566
00567 dj = ((double)ratectl->d) +
00568 ((double)(bitcount() - ratectl->S) - ratectl->actcovered * ((double)ratectl->T) / ratectl->actsum);
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580 Qj = dj * 62.0 / ratectl->r;
00581
00582
00583 if(fixed_mquant)
00584 Qj = fixed_mquant;
00585 else
00586 Qj = ratectl->current_quant;
00587
00588 Qj = (Qj > quant_floor) ? Qj : quant_floor;
00589
00590
00591
00592
00593
00594 N_actj = actj < ratectl->avg_act ?
00595 1.0 :
00596 (actj + act_boost * ratectl->avg_act) /
00597 (act_boost * actj + ratectl->avg_act);
00598
00599 mquant = scale_quant(picture, Qj * N_actj);
00600
00601
00602
00603
00604 ratectl->actcovered += actj;
00605
00606
00607 return mquant;
00608 }
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622 void vbv_end_of_picture()
00623 {
00624 }
00625
00626
00627
00628
00629
00630
00631
00632 void calc_vbv_delay()
00633 {
00634 }
00635
00636 void stop_ratectl(ratectl_t *ratectl)
00637 {
00638 pthread_mutex_destroy(&(ratectl->ratectl_lock));
00639 }