1 /* ratectl.c, bitrate control routines (linear quantization only currently) */
3 /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
6 * Disclaimer of Warranty
8 * These software programs are available to the user without any license fee or
9 * royalty on an "as is" basis. The MPEG Software Simulation Group disclaims
10 * any and all warranties, whether express, implied, or statuary, including any
11 * implied warranties or merchantability or of fitness for a particular
12 * purpose. In no event shall the copyright-holder be liable for any
13 * incidental, punitive, or consequential damages of any kind whatsoever
14 * arising from the use of these programs.
16 * This disclaimer of warranty extends to the user of these programs and user's
17 * customers, employees, agents, transferees, successors, and assigns.
19 * The MPEG Software Simulation Group does not represent or warrant that the
20 * programs furnished hereunder are free of infringement of any third-party
23 * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
24 * are subject to royalty fees to patent holders. Many of these patents are
25 * general enough such that they are unavoidable regardless of implementation
36 /* private prototypes */
37 static void calc_actj _ANSI_ARGS_((unsigned char *frame));
38 static double var_sblk _ANSI_ARGS_((unsigned char *p, int lx));
40 /* rate control variables */
41 int Xi, Xp, Xb, r, d0i, d0p, d0b;
45 static int Np, Nb, S, Q;
46 static int prev_mquant;
50 /* reaction parameter (constant) */
51 if (r==0) r = (int)floor(2.0*bit_rate/frame_rate + 0.5);
53 /* average activity */
54 if (avg_act==0.0) avg_act = 400.0;
56 /* remaining # of bits in GOP */
59 /* global complexity measure */
60 if (Xi==0) Xi = (int)floor(160.0*bit_rate/115.0 + 0.5);
61 if (Xp==0) Xp = (int)floor( 60.0*bit_rate/115.0 + 0.5);
62 if (Xb==0) Xb = (int)floor( 42.0*bit_rate/115.0 + 0.5);
64 /* virtual buffer fullness */
65 if (d0i==0) d0i = (int)floor(10.0*r/31.0 + 0.5);
66 if (d0p==0) d0p = (int)floor(10.0*r/31.0 + 0.5);
67 if (d0b==0) d0b = (int)floor(1.4*10.0*r/31.0 + 0.5);
69 if (d0i==0) d0i = (int)floor(10.0*r/(qscale_tab[0] ? 56.0 : 31.0) + 0.5);
70 if (d0p==0) d0p = (int)floor(10.0*r/(qscale_tab[1] ? 56.0 : 31.0) + 0.5);
71 if (d0b==0) d0b = (int)floor(1.4*10.0*r/(qscale_tab[2] ? 56.0 : 31.0) + 0.5);
74 fprintf(statfile,"\nrate control: sequence initialization\n");
76 " initial global complexity measures (I,P,B): Xi=%d, Xp=%d, Xb=%d\n",
78 fprintf(statfile," reaction parameter: r=%d\n", r);
80 " initial virtual buffer fullness (I,P,B): d0i=%d, d0p=%d, d0b=%d\n",
82 fprintf(statfile," initial average activity: avg_act=%.1f\n", avg_act);
85 void rc_init_GOP(np,nb)
88 R += (int) floor((1 + np + nb) * bit_rate / frame_rate + 0.5);
89 Np = fieldpic ? 2*np+1 : np;
90 Nb = fieldpic ? 2*nb : nb;
92 fprintf(statfile,"\nrate control: new group of pictures (GOP)\n");
93 fprintf(statfile," target number of bits for GOP: R=%d\n",R);
94 fprintf(statfile," number of P pictures in GOP: Np=%d\n",Np);
95 fprintf(statfile," number of B pictures in GOP: Nb=%d\n",Nb);
98 /* Note: we need to substitute K for the 1.4 and 1.0 constants -- this can
99 be modified to fit image content */
101 /* Step 1: compute target bits for current picture being coded */
102 void rc_init_pict(frame)
103 unsigned char *frame;
110 T = (int) floor(R/(1.0+Np*Xp/(Xi*1.0)+Nb*Xb/(Xi*1.4)) + 0.5);
114 T = (int) floor(R/(Np+Nb*1.0*Xb/(1.4*Xp)) + 0.5);
118 T = (int) floor(R/(Nb+Np*1.4*Xp/(1.0*Xb)) + 0.5);
123 Tmin = (int) floor(bit_rate/(8.0*frame_rate) + 0.5);
134 fprintf(statfile,"\nrate control: start of picture\n");
135 fprintf(statfile," target number of bits: T=%d\n",T);
138 static void calc_actj(frame)
139 unsigned char *frame;
147 for (j=0; j<height2; j+=16)
148 for (i=0; i<width; i+=16)
150 p = frame + ((pict_struct==BOTTOM_FIELD)?width:0) + i + width2*j;
152 /* take minimum spatial activity measure of luminance blocks */
154 actj = var_sblk(p,width2);
155 var = var_sblk(p+8,width2);
156 if (var<actj) actj = var;
157 var = var_sblk(p+8*width2,width2);
158 if (var<actj) actj = var;
159 var = var_sblk(p+8*width2+8,width2);
160 if (var<actj) actj = var;
162 if (!fieldpic && !prog_seq)
165 var = var_sblk(p,width<<1);
166 if (var<actj) actj = var;
167 var = var_sblk(p+8,width<<1);
168 if (var<actj) actj = var;
169 var = var_sblk(p+width,width<<1);
170 if (var<actj) actj = var;
171 var = var_sblk(p+width+8,width<<1);
172 if (var<actj) actj = var;
177 mbinfo[k++].act = actj;
181 void rc_update_pict()
185 S = bitcount() - S; /* total # of bits in picture */
186 R-= S; /* remaining # of bits in GOP */
187 X = (int) floor(S*((0.5*(double)Q)/(mb_width*mb_height2)) + 0.5);
189 avg_act = actsum/(mb_width*mb_height2);
209 fprintf(statfile,"\nrate control: end of picture\n");
210 fprintf(statfile," actual number of bits: S=%d\n",S);
211 fprintf(statfile," average quantization parameter Q=%.1f\n",
212 (double)Q/(mb_width*mb_height2));
213 fprintf(statfile," remaining number of bits in GOP: R=%d\n",R);
215 " global complexity measures (I,P,B): Xi=%d, Xp=%d, Xb=%d\n",
218 " virtual buffer fullness (I,P,B): d0i=%d, d0p=%d, d0b=%d\n",
220 fprintf(statfile," remaining number of P pictures in GOP: Np=%d\n",Np);
221 fprintf(statfile," remaining number of B pictures in GOP: Nb=%d\n",Nb);
222 fprintf(statfile," average activity: avg_act=%.1f\n", avg_act);
225 /* compute initial quantization stepsize (at the beginning of picture) */
232 mquant = (int) floor(2.0*d*31.0/r + 0.5);
234 /* clip mquant to legal (linear) range */
240 /* map to legal quantization level */
241 mquant = non_linear_mquant_table[map_non_linear_mquant[mquant]];
245 mquant = (int) floor(d*31.0/r + 0.5);
248 /* clip mquant to legal (linear) range */
254 prev_mquant = mquant;
258 fprintf(statfile,"rc_start_mb:\n");
259 fprintf(statfile,"mquant=%d\n",mquant);
265 /* Step 2: measure virtual buffer - estimated buffer discrepancy */
266 int rc_calc_mquant(j)
270 double dj, Qj, actj, N_actj;
272 /* measure virtual buffer discrepancy from uniform distribution model */
273 dj = d + (bitcount()-S) - j*(T/(mb_width*mb_height2));
275 /* scale against dynamic range of mquant and the bits/picture count */
277 /*Qj = dj*(q_scale_type ? 56.0 : 31.0)/r; */
279 actj = mbinfo[j].act;
282 /* compute normalized activity */
283 N_actj = (2.0*actj+avg_act)/(actj+2.0*avg_act);
287 /* modulate mquant with combined buffer and local activity measures */
288 mquant = (int) floor(2.0*Qj*N_actj + 0.5);
290 /* clip mquant to legal (linear) range */
296 /* map to legal quantization level */
297 mquant = non_linear_mquant_table[map_non_linear_mquant[mquant]];
301 /* modulate mquant with combined buffer and local activity measures */
302 mquant = (int) floor(Qj*N_actj + 0.5);
305 /* clip mquant to legal (linear) range */
311 /* ignore small changes in mquant */
312 if (mquant>=8 && (mquant-prev_mquant)>=-4 && (mquant-prev_mquant)<=4)
313 mquant = prev_mquant;
315 prev_mquant = mquant;
318 Q+= mquant; /* for calculation of average mquant */
321 fprintf(statfile,"rc_calc_mquant(%d): ",j);
322 fprintf(statfile,"bitcount=%d, dj=%f, Qj=%f, actj=%f, N_actj=%f, mquant=%d\n",
323 bitcount(),dj,Qj,actj,N_actj,mquant);
329 /* compute variance of 8x8 block */
330 static double var_sblk(p,lx)
335 unsigned int v, s, s2;
350 return s2/64.0 - (s/64.0)*(s/64.0);
355 * generates warnings if underflow or overflow occurs
358 /* vbv_end_of_picture
360 * - has to be called directly after writing picture_data()
361 * - needed for accurate VBV buffer overflow calculation
362 * - assumes there is no byte stuffing prior to the next start code
365 static int bitcnt_EOP;
367 void vbv_end_of_picture()
369 bitcnt_EOP = bitcount();
370 bitcnt_EOP = (bitcnt_EOP + 7) & ~7; /* account for bit stuffing */
375 * has to be called directly after writing the picture start code, the
376 * reference point for vbv_delay
379 void calc_vbv_delay()
381 double picture_delay;
382 static double next_ip_delay; /* due to frame reordering delay */
383 static double decoding_time;
385 /* number of 1/90000 s ticks until next picture is to be decoded */
386 if (pict_type == B_TYPE)
391 picture_delay = 90000.0/frame_rate; /* 1 frame */
395 picture_delay = 90000.0*2.0/frame_rate; /* 2 frames */
397 picture_delay = 90000.0*3.0/frame_rate; /* 3 frames */
404 picture_delay = 90000.0/(2.0*frame_rate); /* 1 field */
408 picture_delay = 90000.0*2.0/(2.0*frame_rate); /* 2 flds */
410 picture_delay = 90000.0*3.0/(2.0*frame_rate); /* 3 flds */
419 if(topfirst==(pict_struct==TOP_FIELD))
422 picture_delay = 90000.0/(2.0*frame_rate);
427 /* take frame reordering delay into account */
428 picture_delay = next_ip_delay - 90000.0/(2.0*frame_rate);
434 /* take frame reordering delay into account*/
435 picture_delay = next_ip_delay;
438 if (!fieldpic || topfirst!=(pict_struct==TOP_FIELD))
440 /* frame picture or second field */
444 next_ip_delay = 90000.0/frame_rate;
448 next_ip_delay = 90000.0*2.0/frame_rate;
450 next_ip_delay = 90000.0*3.0/frame_rate;
456 next_ip_delay = 90000.0/(2.0*frame_rate);
460 next_ip_delay = 90000.0*2.0/(2.0*frame_rate);
462 next_ip_delay = 90000.0*3.0/(2.0*frame_rate);
468 if (decoding_time==0.0)
470 /* first call of calc_vbv_delay */
471 /* we start with a 7/8 filled VBV buffer (12.5% back-off) */
472 picture_delay = ((vbv_buffer_size*16384*7)/8)*90000.0/bit_rate;
474 next_ip_delay = (int)(90000.0/frame_rate+0.5);
479 /* check for underflow (previous picture) */
480 if (!low_delay && (decoding_time < bitcnt_EOP*90000.0/bit_rate))
482 /* picture not completely in buffer at intended decoding time */
484 fprintf(stderr,"vbv_delay underflow! (decoding_time=%.1f, t_EOP=%.1f\n)",
485 decoding_time, bitcnt_EOP*90000.0/bit_rate);
488 /* when to decode current frame */
489 decoding_time += picture_delay;
491 /* warning: bitcount() may overflow (e.g. after 9 min. at 8 Mbit/s */
492 vbv_delay = (int)(decoding_time - bitcount()*90000.0/bit_rate);
494 /* check for overflow (current picture) */
495 if ((decoding_time - bitcnt_EOP*90000.0/bit_rate)
496 > (vbv_buffer_size*16384)*90000.0/bit_rate)
499 fprintf(stderr,"vbv_delay overflow!\n");
503 "\nvbv_delay=%d (bitcount=%d, decoding_time=%.2f, bitcnt_EOP=%d)\n",
504 vbv_delay,bitcount(),decoding_time,bitcnt_EOP);
509 fprintf(stderr,"vbv_delay underflow: %d\n",vbv_delay);
516 fprintf(stderr,"vbv_delay overflow: %d\n",vbv_delay);