UserPreferences.cpp

00001 /*********************************************************************** 00002 * * 00003 * ViTooKi * 00004 * * 00005 * title: UserPreferences.cpp * 00006 * * 00007 * * 00008 * * 00009 * ITEC institute of the University of Klagenfurt (Austria) * 00010 * http://www.itec.uni-klu.ac.at * 00011 * * 00012 * * 00013 * For more information visit the ViTooKi homepage: * 00014 * http://ViTooKi.sourceforge.net * 00015 * vitooki-user@lists.sourceforge.net * 00016 * vitooki-devel@lists.sourceforge.net * 00017 * * 00018 * This file is part of ViTooKi, a free video toolkit. * 00019 * ViTooKi is free software; you can redistribute it and/or * 00020 * modify it under the terms of the GNU General Public License * 00021 * as published by the Free Software Foundation; either version 2 * 00022 * of the License, or (at your option) any later version. * 00023 * * 00024 * This program is distributed in the hope that it will be useful, * 00025 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00026 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00027 * GNU General Public License for more details. * 00028 * * 00029 * You should have received a copy of the GNU General Public License * 00030 * along with this program; if not, write to the Free Software * 00031 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, * 00032 * MA 02111-1307, USA. * 00033 * * 00034 ***********************************************************************/ 00035 00036 /*********************************************************************** 00037 * * 00038 * REVISION HISTORY: * 00039 * * 00040 * * 00041 * * 00042 ***********************************************************************/ 00043 00044 #include "UserPreferences.hpp" 00045 #include "debug.hpp" 00046 #include "TerminalCapabilities.hpp" 00047 00048 UserPreferences::UserPreferences(const TerminalCapabilities* tc): 00049 dimX(0,0,0,0.25),bitrate(0,0,0,0.25),delayInMS(2000,0.0),color(1,0.25),frameRate(0.0,0.0,0.0,0.25) 00050 { 00051 printf("\033[32mXXXXXXXXXXXXXXXX UserPreferences THIS: %p XXXXXXXXXXX\n\033[0m", this ); 00052 //FIXME.. don't have reliable delay value, so 0 importance 00053 assert(tc); 00054 int col= (int)(tc->getColorDisplay()?1:0); 00055 color.point=col; 00056 int x=tc->getDisplayWidth(); 00057 dimX.best=x; 00058 dimX.max=x; 00059 int fr=tc->getDisplayRefreshRate(); 00060 frameRate.best=(double)fr; 00061 frameRate.max=(double)fr; 00062 uint br = tc->getNetworkCapacityInByte()*8; 00063 if (br > tc->getMaxDecoderBitRateInBit()) 00064 br=tc->getMaxDecoderBitRateInBit(); 00065 bitrate.best=br; 00066 bitrate.max=br; 00067 00068 //assume we have an incredible rich client 00069 // double pay=FLT_MAX; 00070 00071 }; 00072 00073 UserPreferences::UserPreferences(const UserPreferences* s):dimX(s->dimX),bitrate(s->bitrate), 00074 delayInMS(s->delayInMS),color(s->color),frameRate(s->frameRate) 00075 { 00076 pay=s->pay; 00077 } 00079 UserPreferences::UserPreferences(const FeatureRange& dX,const FeatureRange& br, 00080 const FeaturePoint& delay, const FeaturePoint& col, const dFeatureRange& fr,const double& pa): 00081 dimX(dX),bitrate(br),delayInMS(delay),color(col),frameRate(fr) 00082 { 00083 pay=pa; 00084 double total=dimX.importance+bitrate.importance+delayInMS.importance+color.importance+frameRate.importance; 00085 assert(total>0.999 && total<1.001); 00086 }; 00088 UserPreferences::UserPreferences(const FeatureRange* dX, const FeatureRange* br, 00089 const FeaturePoint* delay, const FeaturePoint* col, const dFeatureRange* fr,const double& pa): 00090 dimX(dX),bitrate(br),delayInMS(delay),color(col),frameRate(fr) 00091 { 00092 pay=pa; 00093 double total=dimX.importance+bitrate.importance+delayInMS.importance+color.importance+frameRate.importance; 00094 assert(total>0.999 && total<1.001); 00095 } 00096 double UserPreferences::calcQuality(const Feature* target, int delay) const 00097 { 00098 double result=0.0; 00099 double temp=0.0; 00100 if(dimX.importance>0.0) { 00101 temp=dimX.calcQuality(target->dimX); 00102 if(temp<=0.0) 00103 return 0.0; 00104 result+=temp; 00105 } 00106 if(color.importance>0.0) { 00107 temp=color.calcPointQuality(target->color); 00108 if(temp<=0.0) 00109 return 0.0; 00110 result+=temp; 00111 } 00112 if(bitrate.importance>0.0) { 00113 temp=bitrate.calcQuality(target->bitrate); 00114 if(temp<=0.0) 00115 return 0.0; 00116 result+=temp; 00117 } 00118 if(frameRate.importance>0.0) { 00119 temp=frameRate.calcQuality(target->frameRate); 00120 if(temp<=0.0) 00121 return 0.0; 00122 result+=temp; 00123 } 00124 if(delayInMS.importance>0.0) { 00125 temp=delayInMS.calcInverseQuality(delay); 00126 if(temp<=0.0) 00127 return 0.0; 00128 result+=temp; 00129 } 00130 return result; 00131 }; 00132 00133 UserPreferences* UserPreferences::create(FILE* fp) 00134 { 00135 if(!fp) 00136 return NULL; 00137 char *temp = new char[4960]; 00138 if(fp==stdin) { 00139 printf("\ndimx(min,medium,max)/importance bitrate(m,m,m)/i delay(m)/i color(i)/i framerate(double,d,d)/i pay double: "); 00140 fflush(stdout); 00141 } 00142 fgets(temp,4096,fp); 00143 00144 UserPreferences *up = UserPreferences::create(temp); 00145 delete [] temp; 00146 return up; 00147 }; 00148 char* UserPreferences::toString() const{ 00149 char* res=new char[1024]; 00150 char *t1=dimX.toString(); 00151 char *t2=bitrate.toString(); 00152 char *t3=delayInMS.toString(); 00153 char *t4=color.toString(); 00154 char *t5=frameRate.toString(); 00155 sprintf(res,"dimx%s bitrate%s delay%s color%s framerate%s pay%lf",t1,t2,t3,t4,t5,pay); 00156 delete t1; delete t2; delete t3; delete t4; delete t5; 00157 return res; 00158 }; 00159 00160 UserPreferences* UserPreferences::create(const char* buf) 00161 { 00162 const char* tmp=NULL; 00163 FeatureRange *dimX=NULL, *bitrate=NULL; 00164 dFeatureRange *frameRate=NULL; 00165 FeaturePoint *delayInMS=NULL, *color=NULL; 00166 double pay=0.0; 00167 double sum=0.0; 00168 bool err=false; 00169 UserPreferences* up=NULL; 00170 tmp=strstr(buf,"dimx"); 00171 if(tmp) { 00172 tmp+=strlen("dimx"); 00173 dimX=FeatureRange::create(tmp); 00174 sum+=dimX->importance; 00175 } 00176 tmp=strstr(buf,"bitrate"); 00177 if(tmp) { 00178 tmp+=strlen("bitrate"); 00179 bitrate=FeatureRange::create(tmp); 00180 sum+=bitrate->importance; 00181 } 00182 tmp=strstr(buf,"delay"); 00183 if(tmp) { 00184 tmp+=strlen("delay"); 00185 delayInMS=FeaturePoint::create(tmp); 00186 sum+=delayInMS->importance; 00187 } 00188 tmp=strstr(buf,"color"); 00189 if(tmp) { 00190 tmp+=strlen("color"); 00191 color=FeaturePoint::create(tmp); 00192 sum+=color->importance; 00193 } 00194 tmp=strstr(buf,"framerate"); 00195 if(tmp) { 00196 tmp+=strlen("framerate"); 00197 frameRate=dFeatureRange::create(tmp); 00198 sum+=frameRate->importance; 00199 } 00200 tmp=strstr(buf,"pay"); 00201 if(tmp) { 00202 tmp+=strlen("pay"); 00203 err=(1!=sscanf(tmp,"%lf",&pay)); 00204 } 00205 if(sum>1.001 || sum < 0.999) { 00206 dprintf_err("\nWeights in UserPreferences do not sum up to 1.0\n"); 00207 err=true; 00208 } 00209 if(dimX && bitrate && frameRate && delayInMS && color && !err) { 00210 up= new UserPreferences(dimX,bitrate,delayInMS,color,frameRate,pay); 00211 } 00212 if(dimX) 00213 delete dimX; 00214 if(bitrate) 00215 delete bitrate; 00216 if(delayInMS) 00217 delete delayInMS; 00218 if(frameRate) 00219 delete frameRate; 00220 if(color) 00221 delete color; 00222 return up; 00223 } 00224 00225 void UserPreferences::doTest() 00226 { 00227 FeatureRange dimX(0,176,352,0.2), bitrate(333,666,999,0.4); 00228 dFeatureRange frameRate(15.0,17.0,20.0,0.0); 00229 FeaturePoint delayInMS(1000,0.3), color(1,0.1); 00230 UserPreferences up1(dimX,bitrate,delayInMS,color,frameRate,99); 00231 char* up1s=up1.toString(); 00232 assert(up1s); 00233 UserPreferences* up2=UserPreferences::create(up1s); 00234 assert(up2); 00235 char* up2s=up2->toString(); 00236 assert(strcmp(up1s,up2s)==0); 00237 delete up1s; delete up2; delete up2s; 00238 } 00239 00240 00241 void UserPreferences::correctBitRate(int dimX,int dimY) 00242 { 00243 FeatureRange* br=&this->bitrate; 00244 static const int bitrates[]={1,2,4,8}; 00245 static const int numEntries=sizeof(bitrates)/sizeof(int); 00246 int keyval[numEntries]; 00247 int i; 00248 for(i=0;i<numEntries;i++) 00249 { 00250 keyval[i]=dimX*dimY*bitrates[i]; 00251 } 00252 dprintf_full("UserPreferences::correctBitRate %i,%i\n",dimX,dimY); 00253 // check if one of these bitrates is included in the range 00254 for(i=0;i<numEntries;i++) 00255 { 00256 dprintf_full("UserPreferences::correctBitRate Key %i min%i--max%i\n",keyval[i],br->min,br->max); 00257 if(keyval[i]>=br->min && keyval[i]<=br->max) 00258 return; 00259 } 00260 // we come that far --> invalid bitraterange 00261 // check if too small 00262 if(br->min>keyval[0]) 00263 br->min= ((keyval[0]-1000)>0?(keyval[0]-1000):0); 00264 // too large? 00265 if(br->max<keyval[3]) 00266 { 00267 br->max=keyval[3]+1000; 00268 } 00269 00270 } 00271 00272 void UserPreferences::correctDimension() 00273 { 00274 FeatureRange* dimX=&this->dimX; 00275 // min and max swapped? 00276 if(dimX->min>dimX->max) 00277 { 00278 // int t=dimX->min; 00279 dimX->min=dimX->max; 00280 dimX->max=dimX->min; 00281 } 00282 00283 int diff=dimX->max-dimX->min; 00284 if(diff<44) 00285 { 00286 diff=44-diff+1; // +1 for div 2 00287 diff/=2; 00288 dimX->max+=diff; 00289 dimX->min-=diff; 00290 } 00291 00292 }