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 "libavutil/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(obj, 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
00151 if(*val == '+' || *val == '-')
00152 cmd= *(val++);
00153
00154 for(i=0; i<sizeof(buf)-1 && val[i] && val[i]!='+' && val[i]!='-'; i++)
00155 buf[i]= val[i];
00156 buf[i]=0;
00157
00158 {
00159 const AVOption *o_named= av_find_opt(obj, buf, o->unit, 0, 0);
00160 if(o_named && o_named->type == FF_OPT_TYPE_CONST)
00161 d= o_named->default_val;
00162 else if(!strcmp(buf, "default")) d= o->default_val;
00163 else if(!strcmp(buf, "max" )) d= o->max;
00164 else if(!strcmp(buf, "min" )) d= o->min;
00165 else if(!strcmp(buf, "none" )) d= 0;
00166 else if(!strcmp(buf, "all" )) d= ~0;
00167 else {
00168 int res = av_parse_and_eval_expr(&d, buf, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
00169 if (res < 0) {
00170 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
00171 return res;
00172 }
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 int req_flags, int rej_flags)
00324 {
00325 const AVOption *opt=NULL;
00326
00327 while((opt= av_next_option(obj, opt))){
00328 if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
00329 continue;
00330
00331
00332
00333
00334
00335 if (!unit && opt->type==FF_OPT_TYPE_CONST)
00336 continue;
00337 else if (unit && opt->type!=FF_OPT_TYPE_CONST)
00338 continue;
00339 else if (unit && opt->type==FF_OPT_TYPE_CONST && strcmp(unit, opt->unit))
00340 continue;
00341 else if (unit && opt->type == FF_OPT_TYPE_CONST)
00342 av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
00343 else
00344 av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name);
00345
00346 switch( opt->type )
00347 {
00348 case FF_OPT_TYPE_FLAGS:
00349 av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<flags>" );
00350 break;
00351 case FF_OPT_TYPE_INT:
00352 av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<int>" );
00353 break;
00354 case FF_OPT_TYPE_INT64:
00355 av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<int64>" );
00356 break;
00357 case FF_OPT_TYPE_DOUBLE:
00358 av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<double>" );
00359 break;
00360 case FF_OPT_TYPE_FLOAT:
00361 av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<float>" );
00362 break;
00363 case FF_OPT_TYPE_STRING:
00364 av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<string>" );
00365 break;
00366 case FF_OPT_TYPE_RATIONAL:
00367 av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<rational>" );
00368 break;
00369 case FF_OPT_TYPE_BINARY:
00370 av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<binary>" );
00371 break;
00372 case FF_OPT_TYPE_CONST:
00373 default:
00374 av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "" );
00375 break;
00376 }
00377 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
00378 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
00379 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
00380 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
00381 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
00382
00383 if(opt->help)
00384 av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
00385 av_log(av_log_obj, AV_LOG_INFO, "\n");
00386 if (opt->unit && opt->type != FF_OPT_TYPE_CONST) {
00387 opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
00388 }
00389 }
00390 }
00391
00392 int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
00393 {
00394 if(!obj)
00395 return -1;
00396
00397 av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
00398
00399 opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
00400
00401 return 0;
00402 }
00403
00404 #if FF_API_OPT_SHOW
00405 int av_opt_show(void *obj, void *av_log_obj){
00406 return av_opt_show2(obj, av_log_obj,
00407 AV_OPT_FLAG_ENCODING_PARAM|AV_OPT_FLAG_DECODING_PARAM, 0);
00408 }
00409 #endif
00410
00417 void av_opt_set_defaults2(void *s, int mask, int flags)
00418 {
00419 const AVOption *opt = NULL;
00420 while ((opt = av_next_option(s, opt)) != NULL) {
00421 if((opt->flags & mask) != flags)
00422 continue;
00423 switch(opt->type) {
00424 case FF_OPT_TYPE_CONST:
00425
00426 break;
00427 case FF_OPT_TYPE_FLAGS:
00428 case FF_OPT_TYPE_INT: {
00429 int val;
00430 val = opt->default_val;
00431 av_set_int(s, opt->name, val);
00432 }
00433 break;
00434 case FF_OPT_TYPE_INT64:
00435 if((double)(opt->default_val+0.6) == opt->default_val)
00436 av_log(s, AV_LOG_DEBUG, "loss of precision in default of %s\n", opt->name);
00437 av_set_int(s, opt->name, opt->default_val);
00438 break;
00439 case FF_OPT_TYPE_FLOAT: {
00440 double val;
00441 val = opt->default_val;
00442 av_set_double(s, opt->name, val);
00443 }
00444 break;
00445 case FF_OPT_TYPE_RATIONAL: {
00446 AVRational val;
00447 val = av_d2q(opt->default_val, INT_MAX);
00448 av_set_q(s, opt->name, val);
00449 }
00450 break;
00451 case FF_OPT_TYPE_STRING:
00452 case FF_OPT_TYPE_BINARY:
00453
00454 break;
00455 default:
00456 av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
00457 }
00458 }
00459 }
00460
00461 void av_opt_set_defaults(void *s){
00462 av_opt_set_defaults2(s, 0, 0);
00463 }
00464