avoid stdio.h
[libav.git] / libavcodec / opt.c
CommitLineData
860a40c8
MN
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
27#include "avcodec.h"
28
29static double av_parse_num(const char *name, char **tail){
30 double d;
31 d= strtod(name, tail);
32 if(*tail>name && (**tail=='/' || **tail==':'))
33 d/=strtod((*tail)+1, tail);
34 return d;
35}
36
37//FIXME order them and do a bin search
38static AVOption *find_opt(void *v, const char *name){
39 AVClass *c= *(AVClass**)v; //FIXME silly way of storing AVClass
40 AVOption *o= c->option;
41
42 for(;o && o->name; o++){
43 if(!strcmp(o->name, name))
44 return o;
45 }
46 return NULL;
47}
48
3735e82f
MN
49AVOption *av_next_option(void *obj, AVOption *last){
50 if(last && last[1].name) return ++last;
51 else if(last) return NULL;
52 else return (*(AVClass**)obj)->option;
53}
54
8bbf6db9 55static AVOption *av_set_number(void *obj, const char *name, double num, int den, int64_t intnum){
860a40c8
MN
56 AVOption *o= find_opt(obj, name);
57 void *dst;
58 if(!o || o->offset<=0)
8bbf6db9 59 return NULL;
860a40c8
MN
60
61 if(o->max*den < num*intnum || o->min*den > num*intnum)
8bbf6db9 62 return NULL;
860a40c8
MN
63
64 dst= ((uint8_t*)obj) + o->offset;
65
66 switch(o->type){
233f6f88 67 case FF_OPT_TYPE_FLAGS:
8bbf6db9
MN
68 case FF_OPT_TYPE_INT: *(int *)dst= lrintf(num/den)*intnum; break;
69 case FF_OPT_TYPE_INT64: *(int64_t *)dst= lrintf(num/den)*intnum; break;
70 case FF_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break;
71 case FF_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break;
860a40c8 72 case FF_OPT_TYPE_RATIONAL:
8bbf6db9
MN
73 if((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
74 else *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
860a40c8 75 default:
8bbf6db9 76 return NULL;
860a40c8 77 }
8bbf6db9 78 return o;
860a40c8
MN
79}
80
81//FIXME use eval.c maybe?
8bbf6db9 82AVOption *av_set_string(void *obj, const char *name, const char *val){
860a40c8
MN
83 AVOption *o= find_opt(obj, name);
84 if(!o || !val || o->offset<=0)
8bbf6db9 85 return NULL;
860a40c8 86 if(o->type != FF_OPT_TYPE_STRING){
860a40c8
MN
87 for(;;){
88 int i;
89 char buf[256], *tail;
233f6f88
MN
90 int cmd=0;
91 double d;
860a40c8 92
233f6f88
MN
93 if(*val == '+' || *val == '-')
94 cmd= *(val++);
95
96 for(i=0; i<sizeof(buf)-1 && val[i] && val[i]!='+' && val[i]!='-'; i++)
860a40c8
MN
97 buf[i]= val[i];
98 buf[i]=0;
99 val+= i;
100
233f6f88
MN
101 d= av_parse_num(buf, &tail);
102 if(tail <= buf){
860a40c8 103 AVOption *o_named= find_opt(obj, buf);
233f6f88
MN
104 if(o_named && o_named->type == FF_OPT_TYPE_CONST && !strcmp(o_named->unit, o->unit))
105 d= o_named->default_val;
106 else if(!strcmp(buf, "default")) d= o->default_val;
107 else if(!strcmp(buf, "max" )) d= o->max;
108 else if(!strcmp(buf, "min" )) d= o->min;
8bbf6db9 109 else return NULL;
860a40c8 110 }
233f6f88
MN
111 if(o->type == FF_OPT_TYPE_FLAGS){
112 if (cmd=='+') d= av_get_int(obj, name, NULL) | (int64_t)d;
113 else if(cmd=='-') d= av_get_int(obj, name, NULL) &~(int64_t)d;
114 }else if(cmd=='-')
115 d= -d;
860a40c8 116
233f6f88 117 av_set_number(obj, name, d, 1, 1);
860a40c8 118 if(!*val)
233f6f88 119 return o;
860a40c8 120 }
8bbf6db9 121 return NULL;
860a40c8
MN
122 }
123
124 memcpy(((uint8_t*)obj) + o->offset, val, sizeof(val));
8bbf6db9 125 return o;
860a40c8
MN
126}
127
8bbf6db9 128AVOption *av_set_double(void *obj, const char *name, double n){
860a40c8
MN
129 return av_set_number(obj, name, n, 1, 1);
130}
131
8bbf6db9 132AVOption *av_set_q(void *obj, const char *name, AVRational n){
860a40c8
MN
133 return av_set_number(obj, name, n.num, n.den, 1);
134}
135
8bbf6db9 136AVOption *av_set_int(void *obj, const char *name, int64_t n){
860a40c8
MN
137 return av_set_number(obj, name, 1, 1, n);
138}
139
8bbf6db9
MN
140/**
141 *
142 * @param buf a buffer which is used for returning non string values as strings, can be NULL
143 * @param buf_len allocated length in bytes of buf
144 */
145const char *av_get_string(void *obj, const char *name, AVOption **o_out, char *buf, int buf_len){
860a40c8 146 AVOption *o= find_opt(obj, name);
8bbf6db9 147 void *dst;
860a40c8
MN
148 if(!o || o->offset<=0)
149 return NULL;
8bbf6db9 150 if(o->type != FF_OPT_TYPE_STRING && (!buf || !buf_len))
860a40c8
MN
151 return NULL;
152
8bbf6db9
MN
153 dst= ((uint8_t*)obj) + o->offset;
154 if(o_out) *o_out= o;
155
156 if(o->type == FF_OPT_TYPE_STRING)
157 return dst;
158
159 switch(o->type){
233f6f88 160 case FF_OPT_TYPE_FLAGS: snprintf(buf, buf_len, "0x%08X",*(int *)dst);break;
8bbf6db9
MN
161 case FF_OPT_TYPE_INT: snprintf(buf, buf_len, "%d" , *(int *)dst);break;
162 case FF_OPT_TYPE_INT64: snprintf(buf, buf_len, "%Ld", *(int64_t*)dst);break;
163 case FF_OPT_TYPE_FLOAT: snprintf(buf, buf_len, "%f" , *(float *)dst);break;
164 case FF_OPT_TYPE_DOUBLE: snprintf(buf, buf_len, "%f" , *(double *)dst);break;
165 case FF_OPT_TYPE_RATIONAL: snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
166 default: return NULL;
167 }
168 return buf;
860a40c8
MN
169}
170
8bbf6db9 171static int av_get_number(void *obj, const char *name, AVOption **o_out, double *num, int *den, int64_t *intnum){
860a40c8
MN
172 AVOption *o= find_opt(obj, name);
173 void *dst;
174 if(!o || o->offset<=0)
8bbf6db9 175 goto error;
860a40c8
MN
176
177 dst= ((uint8_t*)obj) + o->offset;
178
8bbf6db9
MN
179 if(o_out) *o_out= o;
180
860a40c8 181 switch(o->type){
233f6f88 182 case FF_OPT_TYPE_FLAGS:
8bbf6db9
MN
183 case FF_OPT_TYPE_INT: *intnum= *(int *)dst;return 0;
184 case FF_OPT_TYPE_INT64: *intnum= *(int64_t*)dst;return 0;
185 case FF_OPT_TYPE_FLOAT: *num= *(float *)dst;return 0;
186 case FF_OPT_TYPE_DOUBLE: *num= *(double *)dst;return 0;
187 case FF_OPT_TYPE_RATIONAL: *intnum= ((AVRational*)dst)->num;
188 *den = ((AVRational*)dst)->den;
189 return 0;
860a40c8 190 }
8bbf6db9
MN
191error:
192 *den=*intnum=0;
193 return -1;
194}
195
196double av_get_double(void *obj, const char *name, AVOption **o_out){
197 int64_t intnum=1;
198 double num=1;
199 int den=1;
200
201 av_get_number(obj, name, o_out, &num, &den, &intnum);
202 return num*intnum/den;
203}
204
205AVRational av_get_q(void *obj, const char *name, AVOption **o_out){
206 int64_t intnum=1;
207 double num=1;
208 int den=1;
209
210 av_get_number(obj, name, o_out, &num, &den, &intnum);
211 if(num == 1.0 && (int)intnum == intnum)
212 return (AVRational){intnum, den};
213 else
214 return av_d2q(num*intnum/den, 1<<24);
215}
216
217int64_t av_get_int(void *obj, const char *name, AVOption **o_out){
218 int64_t intnum=1;
219 double num=1;
220 int den=1;
221
222 av_get_number(obj, name, o_out, &num, &den, &intnum);
223 return num*intnum/den;
224}
225
a1689e2f 226int av_opt_show(void *obj, void *av_log_obj){
8bbf6db9
MN
227 AVOption *opt=NULL;
228
229 if(!obj)
230 return -1;
a1689e2f
MN
231
232 av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
8bbf6db9
MN
233
234 while((opt= av_next_option(obj, opt))){
235 if(!(opt->flags & (AV_OPT_FLAG_ENCODING_PARAM|AV_OPT_FLAG_DECODING_PARAM)))
236 continue;
237
a1689e2f
MN
238 av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name);
239 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
240 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
241 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
242 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
243 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
8bbf6db9 244
a1689e2f 245 av_log(av_log_obj, AV_LOG_INFO, " %s\n", opt->help);
8bbf6db9
MN
246 }
247 return 0;
860a40c8 248}