Mercurial > libavcodec.hg
annotate opt.c @ 2875:1021498a5159 libavcodec
missing include noticed by g0th
| author | michael |
|---|---|
| date | Sun, 11 Sep 2005 14:39:33 +0000 |
| parents | b6def74f5811 |
| children | 8026edf6a349 |
| rev | line source |
|---|---|
| 2862 | 1 /* |
| 2 * AVOptions | |
| 3 * Copyright (c) 2005 Michael Niedermayer <michaelni@gmx.at> | |
| 4 * | |
| 5 * This library is free software; you can redistribute it and/or | |
| 6 * modify it under the terms of the GNU Lesser General Public | |
| 7 * License as published by the Free Software Foundation; either | |
| 8 * version 2 of the License, or (at your option) any later version. | |
| 9 * | |
| 10 * This library is distributed in the hope that it will be useful, | |
| 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 13 * Lesser General Public License for more details. | |
| 14 * | |
| 15 * You should have received a copy of the GNU Lesser General Public | |
| 16 * License along with this library; if not, write to the Free Software | |
| 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 18 * | |
| 19 */ | |
| 20 | |
| 21 /** | |
| 22 * @file opt.c | |
| 23 * AVOptions | |
| 24 * @author Michael Niedermayer <michaelni@gmx.at> | |
| 25 */ | |
| 26 | |
| 2875 | 27 #include <stdio.h> //for FILE * |
| 2862 | 28 #include "avcodec.h" |
| 29 | |
| 30 static double av_parse_num(const char *name, char **tail){ | |
| 31 double d; | |
| 32 d= strtod(name, tail); | |
| 33 if(*tail>name && (**tail=='/' || **tail==':')) | |
| 34 d/=strtod((*tail)+1, tail); | |
| 35 return d; | |
| 36 } | |
| 37 | |
| 38 //FIXME order them and do a bin search | |
| 39 static AVOption *find_opt(void *v, const char *name){ | |
| 40 AVClass *c= *(AVClass**)v; //FIXME silly way of storing AVClass | |
| 41 AVOption *o= c->option; | |
| 42 | |
| 43 for(;o && o->name; o++){ | |
| 44 if(!strcmp(o->name, name)) | |
| 45 return o; | |
| 46 } | |
| 47 return NULL; | |
| 48 } | |
| 49 | |
|
2865
3b999ce45b37
AVOption enumeration support and some flags to classify AVOptions
michael
parents:
2862
diff
changeset
|
50 AVOption *av_next_option(void *obj, AVOption *last){ |
|
3b999ce45b37
AVOption enumeration support and some flags to classify AVOptions
michael
parents:
2862
diff
changeset
|
51 if(last && last[1].name) return ++last; |
|
3b999ce45b37
AVOption enumeration support and some flags to classify AVOptions
michael
parents:
2862
diff
changeset
|
52 else if(last) return NULL; |
|
3b999ce45b37
AVOption enumeration support and some flags to classify AVOptions
michael
parents:
2862
diff
changeset
|
53 else return (*(AVClass**)obj)->option; |
|
3b999ce45b37
AVOption enumeration support and some flags to classify AVOptions
michael
parents:
2862
diff
changeset
|
54 } |
|
3b999ce45b37
AVOption enumeration support and some flags to classify AVOptions
michael
parents:
2862
diff
changeset
|
55 |
| 2873 | 56 static AVOption *av_set_number(void *obj, const char *name, double num, int den, int64_t intnum){ |
| 2862 | 57 AVOption *o= find_opt(obj, name); |
| 58 void *dst; | |
| 59 if(!o || o->offset<=0) | |
| 2873 | 60 return NULL; |
| 2862 | 61 |
| 62 if(o->max*den < num*intnum || o->min*den > num*intnum) | |
| 2873 | 63 return NULL; |
| 2862 | 64 |
| 65 dst= ((uint8_t*)obj) + o->offset; | |
| 66 | |
| 67 switch(o->type){ | |
|
2874
b6def74f5811
flags and named constants with type checking of course for AVOption
michael
parents:
2873
diff
changeset
|
68 case FF_OPT_TYPE_FLAGS: |
| 2873 | 69 case FF_OPT_TYPE_INT: *(int *)dst= lrintf(num/den)*intnum; break; |
| 70 case FF_OPT_TYPE_INT64: *(int64_t *)dst= lrintf(num/den)*intnum; break; | |
| 71 case FF_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break; | |
| 72 case FF_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break; | |
| 2862 | 73 case FF_OPT_TYPE_RATIONAL: |
| 2873 | 74 if((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den}; |
| 75 else *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24); | |
| 2862 | 76 default: |
| 2873 | 77 return NULL; |
| 2862 | 78 } |
| 2873 | 79 return o; |
| 2862 | 80 } |
| 81 | |
| 82 //FIXME use eval.c maybe? | |
| 2873 | 83 AVOption *av_set_string(void *obj, const char *name, const char *val){ |
| 2862 | 84 AVOption *o= find_opt(obj, name); |
| 85 if(!o || !val || o->offset<=0) | |
| 2873 | 86 return NULL; |
| 2862 | 87 if(o->type != FF_OPT_TYPE_STRING){ |
| 88 for(;;){ | |
| 89 int i; | |
| 90 char buf[256], *tail; | |
|
2874
b6def74f5811
flags and named constants with type checking of course for AVOption
michael
parents:
2873
diff
changeset
|
91 int cmd=0; |
|
b6def74f5811
flags and named constants with type checking of course for AVOption
michael
parents:
2873
diff
changeset
|
92 double d; |
| 2862 | 93 |
|
2874
b6def74f5811
flags and named constants with type checking of course for AVOption
michael
parents:
2873
diff
changeset
|
94 if(*val == '+' || *val == '-') |
|
b6def74f5811
flags and named constants with type checking of course for AVOption
michael
parents:
2873
diff
changeset
|
95 cmd= *(val++); |
|
b6def74f5811
flags and named constants with type checking of course for AVOption
michael
parents:
2873
diff
changeset
|
96 |
|
b6def74f5811
flags and named constants with type checking of course for AVOption
michael
parents:
2873
diff
changeset
|
97 for(i=0; i<sizeof(buf)-1 && val[i] && val[i]!='+' && val[i]!='-'; i++) |
| 2862 | 98 buf[i]= val[i]; |
| 99 buf[i]=0; | |
| 100 val+= i; | |
| 101 | |
|
2874
b6def74f5811
flags and named constants with type checking of course for AVOption
michael
parents:
2873
diff
changeset
|
102 d= av_parse_num(buf, &tail); |
|
b6def74f5811
flags and named constants with type checking of course for AVOption
michael
parents:
2873
diff
changeset
|
103 if(tail <= buf){ |
| 2862 | 104 AVOption *o_named= find_opt(obj, buf); |
|
2874
b6def74f5811
flags and named constants with type checking of course for AVOption
michael
parents:
2873
diff
changeset
|
105 if(o_named && o_named->type == FF_OPT_TYPE_CONST && !strcmp(o_named->unit, o->unit)) |
|
b6def74f5811
flags and named constants with type checking of course for AVOption
michael
parents:
2873
diff
changeset
|
106 d= o_named->default_val; |
|
b6def74f5811
flags and named constants with type checking of course for AVOption
michael
parents:
2873
diff
changeset
|
107 else if(!strcmp(buf, "default")) d= o->default_val; |
|
b6def74f5811
flags and named constants with type checking of course for AVOption
michael
parents:
2873
diff
changeset
|
108 else if(!strcmp(buf, "max" )) d= o->max; |
|
b6def74f5811
flags and named constants with type checking of course for AVOption
michael
parents:
2873
diff
changeset
|
109 else if(!strcmp(buf, "min" )) d= o->min; |
| 2873 | 110 else return NULL; |
| 2862 | 111 } |
|
2874
b6def74f5811
flags and named constants with type checking of course for AVOption
michael
parents:
2873
diff
changeset
|
112 if(o->type == FF_OPT_TYPE_FLAGS){ |
|
b6def74f5811
flags and named constants with type checking of course for AVOption
michael
parents:
2873
diff
changeset
|
113 if (cmd=='+') d= av_get_int(obj, name, NULL) | (int64_t)d; |
|
b6def74f5811
flags and named constants with type checking of course for AVOption
michael
parents:
2873
diff
changeset
|
114 else if(cmd=='-') d= av_get_int(obj, name, NULL) &~(int64_t)d; |
|
b6def74f5811
flags and named constants with type checking of course for AVOption
michael
parents:
2873
diff
changeset
|
115 }else if(cmd=='-') |
|
b6def74f5811
flags and named constants with type checking of course for AVOption
michael
parents:
2873
diff
changeset
|
116 d= -d; |
| 2862 | 117 |
|
2874
b6def74f5811
flags and named constants with type checking of course for AVOption
michael
parents:
2873
diff
changeset
|
118 av_set_number(obj, name, d, 1, 1); |
| 2862 | 119 if(!*val) |
|
2874
b6def74f5811
flags and named constants with type checking of course for AVOption
michael
parents:
2873
diff
changeset
|
120 return o; |
| 2862 | 121 } |
| 2873 | 122 return NULL; |
| 2862 | 123 } |
| 124 | |
| 125 memcpy(((uint8_t*)obj) + o->offset, val, sizeof(val)); | |
| 2873 | 126 return o; |
| 2862 | 127 } |
| 128 | |
| 2873 | 129 AVOption *av_set_double(void *obj, const char *name, double n){ |
| 2862 | 130 return av_set_number(obj, name, n, 1, 1); |
| 131 } | |
| 132 | |
| 2873 | 133 AVOption *av_set_q(void *obj, const char *name, AVRational n){ |
| 2862 | 134 return av_set_number(obj, name, n.num, n.den, 1); |
| 135 } | |
| 136 | |
| 2873 | 137 AVOption *av_set_int(void *obj, const char *name, int64_t n){ |
| 2862 | 138 return av_set_number(obj, name, 1, 1, n); |
| 139 } | |
| 140 | |
| 2873 | 141 /** |
| 142 * | |
| 143 * @param buf a buffer which is used for returning non string values as strings, can be NULL | |
| 144 * @param buf_len allocated length in bytes of buf | |
| 145 */ | |
| 146 const char *av_get_string(void *obj, const char *name, AVOption **o_out, char *buf, int buf_len){ | |
| 2862 | 147 AVOption *o= find_opt(obj, name); |
| 2873 | 148 void *dst; |
| 2862 | 149 if(!o || o->offset<=0) |
| 150 return NULL; | |
| 2873 | 151 if(o->type != FF_OPT_TYPE_STRING && (!buf || !buf_len)) |
| 2862 | 152 return NULL; |
| 153 | |
| 2873 | 154 dst= ((uint8_t*)obj) + o->offset; |
| 155 if(o_out) *o_out= o; | |
| 156 | |
| 157 if(o->type == FF_OPT_TYPE_STRING) | |
| 158 return dst; | |
| 159 | |
| 160 switch(o->type){ | |
|
2874
b6def74f5811
flags and named constants with type checking of course for AVOption
michael
parents:
2873
diff
changeset
|
161 case FF_OPT_TYPE_FLAGS: snprintf(buf, buf_len, "0x%08X",*(int *)dst);break; |
| 2873 | 162 case FF_OPT_TYPE_INT: snprintf(buf, buf_len, "%d" , *(int *)dst);break; |
| 163 case FF_OPT_TYPE_INT64: snprintf(buf, buf_len, "%Ld", *(int64_t*)dst);break; | |
| 164 case FF_OPT_TYPE_FLOAT: snprintf(buf, buf_len, "%f" , *(float *)dst);break; | |
| 165 case FF_OPT_TYPE_DOUBLE: snprintf(buf, buf_len, "%f" , *(double *)dst);break; | |
| 166 case FF_OPT_TYPE_RATIONAL: snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break; | |
| 167 default: return NULL; | |
| 168 } | |
| 169 return buf; | |
| 2862 | 170 } |
| 171 | |
| 2873 | 172 static int av_get_number(void *obj, const char *name, AVOption **o_out, double *num, int *den, int64_t *intnum){ |
| 2862 | 173 AVOption *o= find_opt(obj, name); |
| 174 void *dst; | |
| 175 if(!o || o->offset<=0) | |
| 2873 | 176 goto error; |
| 2862 | 177 |
| 178 dst= ((uint8_t*)obj) + o->offset; | |
| 179 | |
| 2873 | 180 if(o_out) *o_out= o; |
| 181 | |
| 2862 | 182 switch(o->type){ |
|
2874
b6def74f5811
flags and named constants with type checking of course for AVOption
michael
parents:
2873
diff
changeset
|
183 case FF_OPT_TYPE_FLAGS: |
| 2873 | 184 case FF_OPT_TYPE_INT: *intnum= *(int *)dst;return 0; |
| 185 case FF_OPT_TYPE_INT64: *intnum= *(int64_t*)dst;return 0; | |
| 186 case FF_OPT_TYPE_FLOAT: *num= *(float *)dst;return 0; | |
| 187 case FF_OPT_TYPE_DOUBLE: *num= *(double *)dst;return 0; | |
| 188 case FF_OPT_TYPE_RATIONAL: *intnum= ((AVRational*)dst)->num; | |
| 189 *den = ((AVRational*)dst)->den; | |
| 190 return 0; | |
| 2862 | 191 } |
| 2873 | 192 error: |
| 193 *den=*intnum=0; | |
| 194 return -1; | |
| 2862 | 195 } |
| 2873 | 196 |
| 197 double av_get_double(void *obj, const char *name, AVOption **o_out){ | |
| 198 int64_t intnum=1; | |
| 199 double num=1; | |
| 200 int den=1; | |
| 201 | |
| 202 av_get_number(obj, name, o_out, &num, &den, &intnum); | |
| 203 return num*intnum/den; | |
| 204 } | |
| 205 | |
| 206 AVRational av_get_q(void *obj, const char *name, AVOption **o_out){ | |
| 207 int64_t intnum=1; | |
| 208 double num=1; | |
| 209 int den=1; | |
| 210 | |
| 211 av_get_number(obj, name, o_out, &num, &den, &intnum); | |
| 212 if(num == 1.0 && (int)intnum == intnum) | |
| 213 return (AVRational){intnum, den}; | |
| 214 else | |
| 215 return av_d2q(num*intnum/den, 1<<24); | |
| 216 } | |
| 217 | |
| 218 int64_t av_get_int(void *obj, const char *name, AVOption **o_out){ | |
| 219 int64_t intnum=1; | |
| 220 double num=1; | |
| 221 int den=1; | |
| 222 | |
| 223 av_get_number(obj, name, o_out, &num, &den, &intnum); | |
| 224 return num*intnum/den; | |
| 225 } | |
| 226 | |
| 227 int av_opt_show(void *obj, FILE *f){ | |
| 228 AVOption *opt=NULL; | |
| 229 | |
| 230 if(!obj) | |
| 231 return -1; | |
| 232 #undef fprintf | |
| 233 fprintf(f, "%s AVOptions:\n", (*(AVClass**)obj)->class_name); | |
| 234 | |
| 235 while((opt= av_next_option(obj, opt))){ | |
| 236 if(!(opt->flags & (AV_OPT_FLAG_ENCODING_PARAM|AV_OPT_FLAG_DECODING_PARAM))) | |
| 237 continue; | |
| 238 | |
| 239 fprintf(f, "-%-17s ", opt->name); | |
| 240 fprintf(f, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.'); | |
| 241 fprintf(f, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.'); | |
| 242 fprintf(f, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.'); | |
| 243 fprintf(f, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.'); | |
| 244 fprintf(f, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.'); | |
| 245 | |
| 246 fprintf(f, " %s\n", opt->help); | |
| 247 } | |
| 248 return 0; | |
| 249 } |
