00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028 #include "avcodec.h"
00029 #include "opt.h"
00030 #include "eval.h"
00031
00032
00033 const AVOption *av_find_opt(void *v, const char *name, const char *unit, int mask, int flags){
00034 AVClass *c= *(AVClass**)v;
00035 const AVOption *o= c->option;
00036
00037 for(;o && o->name; o++){
00038 if(!strcmp(o->name, name) && (!unit || (o->unit && !strcmp(o->unit, unit))) && (o->flags & mask) == flags )
00039 return o;
00040 }
00041 return NULL;
00042 }
00043
00044 const AVOption *av_next_option(void *obj, const AVOption *last){
00045 if(last && last[1].name) return ++last;
00046 else if(last) return NULL;
00047 else return (*(AVClass**)obj)->option;
00048 }
00049
00050 static int av_set_number2(void *obj, const char *name, double num, int den, int64_t intnum, const AVOption **o_out){
00051 const AVOption *o= av_find_opt(obj, name, NULL, 0, 0);
00052 void *dst;
00053 if(o_out)
00054 *o_out= o;
00055 if(!o || o->offset<=0)
00056 return AVERROR(ENOENT);
00057
00058 if(o->max*den < num*intnum || o->min*den > num*intnum) {
00059 av_log(NULL, AV_LOG_ERROR, "Value %lf for parameter '%s' out of range\n", num, name);
00060 return AVERROR(ERANGE);
00061 }
00062
00063 dst= ((uint8_t*)obj) + o->offset;
00064
00065 switch(o->type){
00066 case FF_OPT_TYPE_FLAGS:
00067 case FF_OPT_TYPE_INT: *(int *)dst= llrint(num/den)*intnum; break;
00068 case FF_OPT_TYPE_INT64: *(int64_t *)dst= llrint(num/den)*intnum; break;
00069 case FF_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break;
00070 case FF_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break;
00071 case FF_OPT_TYPE_RATIONAL:
00072 if((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
00073 else *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
00074 break;
00075 default:
00076 return AVERROR(EINVAL);
00077 }
00078 return 0;
00079 }
00080
00081 static const AVOption *av_set_number(void *obj, const char *name, double num, int den, int64_t intnum){
00082 const AVOption *o = NULL;
00083 if (av_set_number2(obj, name, num, den, intnum, &o) < 0)
00084 return NULL;
00085 else
00086 return o;
00087 }
00088
00089 static const double const_values[]={
00090 M_PI,
00091 M_E,
00092 FF_QP2LAMBDA,
00093 0
00094 };
00095
00096 static const char * const const_names[]={
00097 "PI",
00098 "E",
00099 "QP2LAMBDA",
00100 0
00101 };
00102
00103 static int hexchar2int(char c) {
00104 if (c >= '0' && c <= '9') return c - '0';
00105 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
00106 if (c >= 'A' && c <= 'F') return c - 'A' + 10;
00107 return -1;
00108 }
00109
00110 int av_set_string3(void *obj, const char *name, const char *val, int alloc, const AVOption **o_out){
00111 int ret;
00112 const AVOption *o= av_find_opt(obj, name, NULL, 0, 0);
00113 if (o_out)
00114 *o_out = o;
00115 if(!o)
00116 return AVERROR(ENOENT);
00117 if(!val || o->offset<=0)
00118 return AVERROR(EINVAL);
00119
00120 if(o->type == FF_OPT_TYPE_BINARY){
00121 uint8_t **dst = (uint8_t **)(((uint8_t*)obj) + o->offset);
00122 int *lendst = (int *)(dst + 1);
00123 uint8_t *bin, *ptr;
00124 int len = strlen(val);
00125 av_freep(dst);
00126 *lendst = 0;
00127 if (len & 1) return AVERROR(EINVAL);
00128 len /= 2;
00129 ptr = bin = av_malloc(len);
00130 while (*val) {
00131 int a = hexchar2int(*val++);
00132 int b = hexchar2int(*val++);
00133 if (a < 0 || b < 0) {
00134 av_free(bin);
00135 return AVERROR(EINVAL);
00136 }
00137 *ptr++ = (a << 4) | b;
00138 }
00139 *dst = bin;
00140 *lendst = len;
00141 return 0;
00142 }
00143 if(o->type != FF_OPT_TYPE_STRING){
00144 int notfirst=0;
00145 for(;;){
00146 int i;
00147 char buf[256];
00148 int cmd=0;
00149 double d;
00150 const char *error = NULL;
00151
00152 if(*val == '+' || *val == '-')
00153 cmd= *(val++);
00154
00155 for(i=0; i<sizeof(buf)-1 && val[i] && val[i]!='+' && val[i]!='-'; i++)
00156 buf[i]= val[i];
00157 buf[i]=0;
00158
00159 d = ff_eval2(buf, const_values, const_names, NULL, NULL, NULL, NULL, NULL, &error);
00160 if(isnan(d)) {
00161 const AVOption *o_named= av_find_opt(obj, buf, o->unit, 0, 0);
00162 if(o_named && o_named->type == FF_OPT_TYPE_CONST)
00163 d= o_named->default_val;
00164 else if(!strcmp(buf, "default")) d= o->default_val;
00165 else if(!strcmp(buf, "max" )) d= o->max;
00166 else if(!strcmp(buf, "min" )) d= o->min;
00167 else if(!strcmp(buf, "none" )) d= 0;
00168 else if(!strcmp(buf, "all" )) d= ~0;
00169 else {
00170 if (error)
00171 av_log(NULL, AV_LOG_ERROR, "Unable to parse option value \"%s\": %s\n", val, error);
00172 return AVERROR(EINVAL);
00173 }
00174 }
00175 if(o->type == FF_OPT_TYPE_FLAGS){
00176 if (cmd=='+') d= av_get_int(obj, name, NULL) | (int64_t)d;
00177 else if(cmd=='-') d= av_get_int(obj, name, NULL) &~(int64_t)d;
00178 }else{
00179 if (cmd=='+') d= notfirst*av_get_double(obj, name, NULL) + d;
00180 else if(cmd=='-') d= notfirst*av_get_double(obj, name, NULL) - d;
00181 }
00182
00183 if ((ret = av_set_number2(obj, name, d, 1, 1, o_out)) < 0)
00184 return ret;
00185 val+= i;
00186 if(!*val)
00187 return 0;
00188 notfirst=1;
00189 }
00190 return AVERROR(EINVAL);
00191 }
00192
00193 if(alloc){
00194 av_free(*(void**)(((uint8_t*)obj) + o->offset));
00195 val= av_strdup(val);
00196 }
00197
00198 memcpy(((uint8_t*)obj) + o->offset, &val, sizeof(val));
00199 return 0;
00200 }
00201
00202 #if LIBAVCODEC_VERSION_MAJOR < 53
00203 const AVOption *av_set_string2(void *obj, const char *name, const char *val, int alloc){
00204 const AVOption *o;
00205 if (av_set_string3(obj, name, val, alloc, &o) < 0)
00206 return NULL;
00207 return o;
00208 }
00209
00210 const AVOption *av_set_string(void *obj, const char *name, const char *val){
00211 const AVOption *o;
00212 if (av_set_string3(obj, name, val, 0, &o) < 0)
00213 return NULL;
00214 return o;
00215 }
00216 #endif
00217
00218 const AVOption *av_set_double(void *obj, const char *name, double n){
00219 return av_set_number(obj, name, n, 1, 1);
00220 }
00221
00222 const AVOption *av_set_q(void *obj, const char *name, AVRational n){
00223 return av_set_number(obj, name, n.num, n.den, 1);
00224 }
00225
00226 const AVOption *av_set_int(void *obj, const char *name, int64_t n){
00227 return av_set_number(obj, name, 1, 1, n);
00228 }
00229
00235 const char *av_get_string(void *obj, const char *name, const AVOption **o_out, char *buf, int buf_len){
00236 const AVOption *o= av_find_opt(obj, name, NULL, 0, 0);
00237 void *dst;
00238 uint8_t *bin;
00239 int len, i;
00240 if(!o || o->offset<=0)
00241 return NULL;
00242 if(o->type != FF_OPT_TYPE_STRING && (!buf || !buf_len))
00243 return NULL;
00244
00245 dst= ((uint8_t*)obj) + o->offset;
00246 if(o_out) *o_out= o;
00247
00248 switch(o->type){
00249 case FF_OPT_TYPE_FLAGS: snprintf(buf, buf_len, "0x%08X",*(int *)dst);break;
00250 case FF_OPT_TYPE_INT: snprintf(buf, buf_len, "%d" , *(int *)dst);break;
00251 case FF_OPT_TYPE_INT64: snprintf(buf, buf_len, "%"PRId64, *(int64_t*)dst);break;
00252 case FF_OPT_TYPE_FLOAT: snprintf(buf, buf_len, "%f" , *(float *)dst);break;
00253 case FF_OPT_TYPE_DOUBLE: snprintf(buf, buf_len, "%f" , *(double *)dst);break;
00254 case FF_OPT_TYPE_RATIONAL: snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
00255 case FF_OPT_TYPE_STRING: return *(void**)dst;
00256 case FF_OPT_TYPE_BINARY:
00257 len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
00258 if(len >= (buf_len + 1)/2) return NULL;
00259 bin = *(uint8_t**)dst;
00260 for(i = 0; i < len; i++) snprintf(buf + i*2, 3, "%02X", bin[i]);
00261 break;
00262 default: return NULL;
00263 }
00264 return buf;
00265 }
00266
00267 static int av_get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum){
00268 const AVOption *o= av_find_opt(obj, name, NULL, 0, 0);
00269 void *dst;
00270 if(!o || o->offset<=0)
00271 goto error;
00272
00273 dst= ((uint8_t*)obj) + o->offset;
00274
00275 if(o_out) *o_out= o;
00276
00277 switch(o->type){
00278 case FF_OPT_TYPE_FLAGS: *intnum= *(unsigned int*)dst;return 0;
00279 case FF_OPT_TYPE_INT: *intnum= *(int *)dst;return 0;
00280 case FF_OPT_TYPE_INT64: *intnum= *(int64_t*)dst;return 0;
00281 case FF_OPT_TYPE_FLOAT: *num= *(float *)dst;return 0;
00282 case FF_OPT_TYPE_DOUBLE: *num= *(double *)dst;return 0;
00283 case FF_OPT_TYPE_RATIONAL: *intnum= ((AVRational*)dst)->num;
00284 *den = ((AVRational*)dst)->den;
00285 return 0;
00286 }
00287 error:
00288 *den=*intnum=0;
00289 return -1;
00290 }
00291
00292 double av_get_double(void *obj, const char *name, const AVOption **o_out){
00293 int64_t intnum=1;
00294 double num=1;
00295 int den=1;
00296
00297 av_get_number(obj, name, o_out, &num, &den, &intnum);
00298 return num*intnum/den;
00299 }
00300
00301 AVRational av_get_q(void *obj, const char *name, const AVOption **o_out){
00302 int64_t intnum=1;
00303 double num=1;
00304 int den=1;
00305
00306 av_get_number(obj, name, o_out, &num, &den, &intnum);
00307 if(num == 1.0 && (int)intnum == intnum)
00308 return (AVRational){intnum, den};
00309 else
00310 return av_d2q(num*intnum/den, 1<<24);
00311 }
00312
00313 int64_t av_get_int(void *obj, const char *name, const AVOption **o_out){
00314 int64_t intnum=1;
00315 double num=1;
00316 int den=1;
00317
00318 av_get_number(obj, name, o_out, &num, &den, &intnum);
00319 return num*intnum/den;
00320 }
00321
00322 static void opt_list(void *obj, void *av_log_obj, const char *unit)
00323 {
00324 const AVOption *opt=NULL;
00325
00326 while((opt= av_next_option(obj, opt))){
00327 if(!(opt->flags & (AV_OPT_FLAG_ENCODING_PARAM|AV_OPT_FLAG_DECODING_PARAM)))
00328 continue;
00329
00330
00331
00332
00333
00334 if (!unit && opt->type==FF_OPT_TYPE_CONST)
00335 continue;
00336 else if (unit && opt->type!=FF_OPT_TYPE_CONST)
00337 continue;
00338 else if (unit && opt->type==FF_OPT_TYPE_CONST && strcmp(unit, opt->unit))
00339 continue;
00340 else if (unit && opt->type == FF_OPT_TYPE_CONST)
00341 av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
00342 else
00343 av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name);
00344
00345 switch( opt->type )
00346 {
00347 case FF_OPT_TYPE_FLAGS:
00348 av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<flags>" );
00349 break;
00350 case FF_OPT_TYPE_INT:
00351 av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<int>" );
00352 break;
00353 case FF_OPT_TYPE_INT64:
00354 av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<int64>" );
00355 break;
00356 case FF_OPT_TYPE_DOUBLE:
00357 av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<double>" );
00358 break;
00359 case FF_OPT_TYPE_FLOAT:
00360 av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<float>" );
00361 break;
00362 case FF_OPT_TYPE_STRING:
00363 av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<string>" );
00364 break;
00365 case FF_OPT_TYPE_RATIONAL:
00366 av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<rational>" );
00367 break;
00368 case FF_OPT_TYPE_BINARY:
00369 av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<binary>" );
00370 break;
00371 case FF_OPT_TYPE_CONST:
00372 default:
00373 av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "" );
00374 break;
00375 }
00376 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
00377 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
00378 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
00379 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
00380 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
00381
00382 if(opt->help)
00383 av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
00384 av_log(av_log_obj, AV_LOG_INFO, "\n");
00385 if (opt->unit && opt->type != FF_OPT_TYPE_CONST) {
00386 opt_list(obj, av_log_obj, opt->unit);
00387 }
00388 }
00389 }
00390
00391 int av_opt_show(void *obj, void *av_log_obj){
00392 if(!obj)
00393 return -1;
00394
00395 av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
00396
00397 opt_list(obj, av_log_obj, NULL);
00398
00399 return 0;
00400 }
00401
00408 void av_opt_set_defaults2(void *s, int mask, int flags)
00409 {
00410 const AVOption *opt = NULL;
00411 while ((opt = av_next_option(s, opt)) != NULL) {
00412 if((opt->flags & mask) != flags)
00413 continue;
00414 switch(opt->type) {
00415 case FF_OPT_TYPE_CONST:
00416
00417 break;
00418 case FF_OPT_TYPE_FLAGS:
00419 case FF_OPT_TYPE_INT: {
00420 int val;
00421 val = opt->default_val;
00422 av_set_int(s, opt->name, val);
00423 }
00424 break;
00425 case FF_OPT_TYPE_INT64:
00426 if((double)(opt->default_val+0.6) == opt->default_val)
00427 av_log(s, AV_LOG_DEBUG, "loss of precission in default of %s\n", opt->name);
00428 av_set_int(s, opt->name, opt->default_val);
00429 break;
00430 case FF_OPT_TYPE_FLOAT: {
00431 double val;
00432 val = opt->default_val;
00433 av_set_double(s, opt->name, val);
00434 }
00435 break;
00436 case FF_OPT_TYPE_RATIONAL: {
00437 AVRational val;
00438 val = av_d2q(opt->default_val, INT_MAX);
00439 av_set_q(s, opt->name, val);
00440 }
00441 break;
00442 case FF_OPT_TYPE_STRING:
00443 case FF_OPT_TYPE_BINARY:
00444
00445 break;
00446 default:
00447 av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
00448 }
00449 }
00450 }
00451
00452 void av_opt_set_defaults(void *s){
00453 av_opt_set_defaults2(s, 0, 0);
00454 }
00455