00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "contributors.h"
00016 #include "global.h"
00017
00018 #ifdef _LEAKYBUCKET_
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 int get_LeakyBucketRate(InputParameters *p_Inp, unsigned long NumberLeakyBuckets, unsigned long *Rmin)
00043 {
00044 FILE *f;
00045 unsigned long i, buf;
00046
00047 if((f = fopen(p_Inp->LeakyBucketRateFile, "r")) == NULL)
00048 {
00049 printf(" LeakyBucketRate File does not exist. Using rate calculated from avg. rate \n");
00050 return 0;
00051 }
00052
00053 for(i=0; i<NumberLeakyBuckets; i++)
00054 {
00055 if(1 != fscanf(f, "%lu", &buf))
00056 {
00057 printf(" Leaky BucketRateFile does not have valid entries.\n Using rate calculated from avg. rate \n");
00058 fclose (f);
00059 return 0;
00060 }
00061 Rmin[i] = buf;
00062 }
00063 fclose (f);
00064 return 1;
00065 }
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085 void PutBigDoubleWord(unsigned long dw, FILE *fp)
00086 {
00087 fputc((dw >> 0x18) & 0xFF, fp);
00088 fputc((dw >> 0x10) & 0xFF, fp);
00089 fputc((dw >> 0x08) & 0xFF, fp);
00090 fputc(dw & 0xFF, fp);
00091 }
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119 void write_buffer(InputParameters *p_Inp, unsigned long NumberLeakyBuckets, unsigned long Rmin[], unsigned long Bmin[], unsigned long Fmin[])
00120 {
00121 FILE *outf;
00122 unsigned long iBucket;
00123
00124 if ((outf = fopen(p_Inp->LeakyBucketParamFile,"wb"))==NULL)
00125 {
00126 snprintf(errortext, ET_SIZE, "Error open file lk %s \n",p_Inp->LeakyBucketParamFile);
00127 error(errortext,1);
00128 }
00129
00130 PutBigDoubleWord(NumberLeakyBuckets, outf);
00131 if (p_Inp->Verbose != 0)
00132 printf(" Number Leaky Buckets: %ld \n Rmin Bmin Fmin \n", NumberLeakyBuckets);
00133 for(iBucket =0; iBucket < NumberLeakyBuckets; iBucket++)
00134 {
00135
00136
00137
00138 PutBigDoubleWord(Rmin[iBucket], outf);
00139 PutBigDoubleWord(Bmin[iBucket], outf);
00140 PutBigDoubleWord(Fmin[iBucket], outf);
00141 if (p_Inp->Verbose != 0)
00142 printf(" %8ld %8ld %8ld \n", Rmin[iBucket], Bmin[iBucket], Fmin[iBucket]);
00143 }
00144 fclose(outf);
00145 }
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165 void Sort(unsigned long NumberLeakyBuckets, unsigned long *Rmin)
00166 {
00167 unsigned long i, j;
00168 unsigned long temp;
00169 for(i=0; i< NumberLeakyBuckets-1; i++)
00170 {
00171 for(j=i+1; j<NumberLeakyBuckets; j++)
00172 {
00173 if(Rmin[i] > Rmin[j])
00174 {
00175 temp = Rmin[i];
00176 Rmin[i] = Rmin[j];
00177 Rmin[j] = temp;
00178 }
00179 }
00180 }
00181 }
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198 void calc_buffer(ImageParameters *p_Img, InputParameters *p_Inp)
00199 {
00200 unsigned long AvgRate, TotalRate, NumberLeakyBuckets;
00201 long *buffer_frame, minB;
00202 unsigned long iBucket, iFrame, FrameIndex = 0;
00203 long maxBuffer, actualBuffer, InitFullness, iChannelRate;
00204 unsigned long *Rmin, *Bmin, *Fmin;
00205
00206 switch (p_Inp->Verbose)
00207 {
00208 case 1:
00209 fprintf(stdout,"-------------------------------------------------------------------------------\n");
00210 break;
00211 case 2:
00212 fprintf(stdout,"------------------------------------------------------------------------------------------------\n");
00213 break;
00214 case 3:
00215 fprintf(stdout,"-------------------------------------------------------------------------------------------------------\n");
00216 break;
00217 case 0:
00218 default:
00219 fprintf(stdout,"\n-------------------------------------------------------------------------------\n");
00220 break;
00221 }
00222 printf(" Total Frames: %ld (%d) \n", p_Img->total_frame_buffer, p_Inp->no_frm_base);
00223 NumberLeakyBuckets = (unsigned long) p_Inp->NumberLeakyBuckets;
00224 buffer_frame = calloc(p_Img->total_frame_buffer + 1, sizeof(long));
00225 if(!buffer_frame)
00226 no_mem_exit("init_buffer: buffer_frame");
00227 Rmin = calloc(NumberLeakyBuckets, sizeof(unsigned long));
00228 if(!Rmin)
00229 no_mem_exit("init_buffer: Rmin");
00230 Bmin = calloc(NumberLeakyBuckets, sizeof(unsigned long));
00231 if(!Bmin)
00232 no_mem_exit("init_buffer: Bmin");
00233 Fmin = calloc(NumberLeakyBuckets, sizeof(unsigned long));
00234 if(!Fmin)
00235 no_mem_exit("init_buffer: Fmin");
00236
00237 TotalRate = 0;
00238 for(iFrame=0; iFrame < p_Img->total_frame_buffer; iFrame++)
00239 {
00240 TotalRate += (unsigned long) p_Img->Bit_Buffer[iFrame];
00241 }
00242 AvgRate = (unsigned long) ((float) TotalRate/ p_Img->total_frame_buffer);
00243
00244 if(1 != get_LeakyBucketRate(p_Inp, NumberLeakyBuckets, Rmin))
00245 {
00246 for(iBucket=0; iBucket < NumberLeakyBuckets; iBucket++)
00247 {
00248 if(iBucket == 0)
00249 Rmin[iBucket] = (unsigned long)((float) AvgRate * p_Img->framerate);
00250 else
00251 Rmin[iBucket] = (unsigned long) ((float) Rmin[iBucket-1] + (AvgRate/4) * (p_Img->framerate));
00252 }
00253 }
00254 Sort(NumberLeakyBuckets, Rmin);
00255
00256 maxBuffer = AvgRate * 20;
00257 for(iBucket=0; iBucket< NumberLeakyBuckets; iBucket++)
00258 {
00259 iChannelRate = (long) (Rmin[iBucket] / (p_Img->framerate));
00260
00261 InitFullness = maxBuffer;
00262 buffer_frame[0] = InitFullness;
00263 minB = maxBuffer;
00264
00265 for(iFrame=0; iFrame < p_Img->total_frame_buffer ; iFrame++)
00266 {
00267 buffer_frame[iFrame] = buffer_frame[iFrame] - p_Img->Bit_Buffer[iFrame];
00268 if(buffer_frame[iFrame] < minB)
00269 {
00270 minB = buffer_frame[iFrame];
00271 FrameIndex = iFrame;
00272 }
00273
00274 if ( iFrame < p_Img->total_frame_buffer )
00275 {
00276 buffer_frame[iFrame+1] = buffer_frame[iFrame] + iChannelRate;
00277 if(buffer_frame[iFrame+1] > maxBuffer)
00278 buffer_frame[iFrame+1] = maxBuffer;
00279 }
00280 }
00281 actualBuffer = (maxBuffer - minB);
00282
00283
00284 InitFullness = p_Img->Bit_Buffer[0];
00285 buffer_frame[0] = InitFullness;
00286 for(iFrame=0; iFrame < FrameIndex+1; iFrame++)
00287 {
00288 buffer_frame[iFrame] = buffer_frame[iFrame] - p_Img->Bit_Buffer[iFrame];
00289 if(buffer_frame[iFrame] < 0)
00290 {
00291 InitFullness -= buffer_frame[iFrame];
00292 buffer_frame[iFrame] = 0;
00293 }
00294 if ( iFrame < p_Img->total_frame_buffer )
00295 {
00296 buffer_frame[iFrame+1] = buffer_frame[iFrame] + iChannelRate;
00297 if(buffer_frame[iFrame+1] > actualBuffer)
00298 break;
00299 }
00300 }
00301 Bmin[iBucket] = (unsigned long) actualBuffer;
00302 Fmin[iBucket] = (unsigned long) InitFullness;
00303 }
00304
00305 write_buffer(p_Inp, NumberLeakyBuckets, Rmin, Bmin, Fmin);
00306
00307 free(buffer_frame);
00308 free(Rmin);
00309 free(Bmin);
00310 free(Fmin);
00311 return;
00312 }
00313 #endif
00314