3 * Copyright (c) 2005 Michael Niedermayer <michaelni@gmx.at>
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.
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.
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
24 * @author Michael Niedermayer <michaelni@gmx.at>
27 #include <stdio.h> //for FILE *
30 static double av_parse_num(const char *name, char **tail){
32 d= strtod(name, tail);
33 if(*tail>name && (**tail=='/' || **tail==':'))
34 d/=strtod((*tail)+1, tail);
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;
43 for(;o && o->name; o++){
44 if(!strcmp(o->name, name))
50 AVOption *av_next_option(void *obj, AVOption *last){
51 if(last && last[1].name) return ++last;
52 else if(last) return NULL;
53 else return (*(AVClass**)obj)->option;
56 static AVOption *av_set_number(void *obj, const char *name, double num, int den, int64_t intnum){
57 AVOption *o= find_opt(obj, name);
59 if(!o || o->offset<=0)
62 if(o->max*den < num*intnum || o->min*den > num*intnum)
65 dst= ((uint8_t*)obj) + o->offset;
68 case FF_OPT_TYPE_FLAGS:
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;
73 case FF_OPT_TYPE_RATIONAL:
74 if((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
75 else *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
82 //FIXME use eval.c maybe?
83 AVOption *av_set_string(void *obj, const char *name, const char *val){
84 AVOption *o= find_opt(obj, name);
85 if(!o || !val || o->offset<=0)
87 if(o->type != FF_OPT_TYPE_STRING){
94 if(*val == '+' || *val == '-')
97 for(i=0; i<sizeof(buf)-1 && val[i] && val[i]!='+' && val[i]!='-'; i++)
102 d= av_parse_num(buf, &tail);
104 AVOption *o_named= find_opt(obj, buf);
105 if(o_named && o_named->type == FF_OPT_TYPE_CONST && !strcmp(o_named->unit, o->unit))
106 d= o_named->default_val;
107 else if(!strcmp(buf, "default")) d= o->default_val;
108 else if(!strcmp(buf, "max" )) d= o->max;
109 else if(!strcmp(buf, "min" )) d= o->min;
112 if(o->type == FF_OPT_TYPE_FLAGS){
113 if (cmd=='+') d= av_get_int(obj, name, NULL) | (int64_t)d;
114 else if(cmd=='-') d= av_get_int(obj, name, NULL) &~(int64_t)d;
118 av_set_number(obj, name, d, 1, 1);
125 memcpy(((uint8_t*)obj) + o->offset, val, sizeof(val));
129 AVOption *av_set_double(void *obj, const char *name, double n){
130 return av_set_number(obj, name, n, 1, 1);
133 AVOption *av_set_q(void *obj, const char *name, AVRational n){
134 return av_set_number(obj, name, n.num, n.den, 1);
137 AVOption *av_set_int(void *obj, const char *name, int64_t n){
138 return av_set_number(obj, name, 1, 1, n);
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
146 const char *av_get_string(void *obj, const char *name, AVOption **o_out, char *buf, int buf_len){
147 AVOption *o= find_opt(obj, name);
149 if(!o || o->offset<=0)
151 if(o->type != FF_OPT_TYPE_STRING && (!buf || !buf_len))
154 dst= ((uint8_t*)obj) + o->offset;
157 if(o->type == FF_OPT_TYPE_STRING)
161 case FF_OPT_TYPE_FLAGS: snprintf(buf, buf_len, "0x%08X",*(int *)dst);break;
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;
172 static int av_get_number(void *obj, const char *name, AVOption **o_out, double *num, int *den, int64_t *intnum){
173 AVOption *o= find_opt(obj, name);
175 if(!o || o->offset<=0)
178 dst= ((uint8_t*)obj) + o->offset;
183 case FF_OPT_TYPE_FLAGS:
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;
197 double av_get_double(void *obj, const char *name, AVOption **o_out){
202 av_get_number(obj, name, o_out, &num, &den, &intnum);
203 return num*intnum/den;
206 AVRational av_get_q(void *obj, const char *name, AVOption **o_out){
211 av_get_number(obj, name, o_out, &num, &den, &intnum);
212 if(num == 1.0 && (int)intnum == intnum)
213 return (AVRational){intnum, den};
215 return av_d2q(num*intnum/den, 1<<24);
218 int64_t av_get_int(void *obj, const char *name, AVOption **o_out){
223 av_get_number(obj, name, o_out, &num, &den, &intnum);
224 return num*intnum/den;
227 int av_opt_show(void *obj, FILE *f){
233 fprintf(f, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
235 while((opt= av_next_option(obj, opt))){
236 if(!(opt->flags & (AV_OPT_FLAG_ENCODING_PARAM|AV_OPT_FLAG_DECODING_PARAM)))
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' : '.');
246 fprintf(f, " %s\n", opt->help);