Mercurial > libavcodec.hg
annotate opt.c @ 2873:55809f38eb63 libavcodec
AVOption API improvments
support AVOptions for encoding in ffmpeg.c
author | michael |
---|---|
date | Sun, 11 Sep 2005 11:10:25 +0000 |
parents | 3b999ce45b37 |
children | b6def74f5811 |
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 | |
27 #include "avcodec.h" | |
28 | |
29 static 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 | |
38 static 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 | |
2865
3b999ce45b37
AVOption enumeration support and some flags to classify AVOptions
michael
parents:
2862
diff
changeset
|
49 AVOption *av_next_option(void *obj, AVOption *last){ |
3b999ce45b37
AVOption enumeration support and some flags to classify AVOptions
michael
parents:
2862
diff
changeset
|
50 if(last && last[1].name) return ++last; |
3b999ce45b37
AVOption enumeration support and some flags to classify AVOptions
michael
parents:
2862
diff
changeset
|
51 else if(last) return NULL; |
3b999ce45b37
AVOption enumeration support and some flags to classify AVOptions
michael
parents:
2862
diff
changeset
|
52 else return (*(AVClass**)obj)->option; |
3b999ce45b37
AVOption enumeration support and some flags to classify AVOptions
michael
parents:
2862
diff
changeset
|
53 } |
3b999ce45b37
AVOption enumeration support and some flags to classify AVOptions
michael
parents:
2862
diff
changeset
|
54 |
2873 | 55 static AVOption *av_set_number(void *obj, const char *name, double num, int den, int64_t intnum){ |
2862 | 56 AVOption *o= find_opt(obj, name); |
57 void *dst; | |
58 if(!o || o->offset<=0) | |
2873 | 59 return NULL; |
2862 | 60 |
61 if(o->max*den < num*intnum || o->min*den > num*intnum) | |
2873 | 62 return NULL; |
2862 | 63 |
64 dst= ((uint8_t*)obj) + o->offset; | |
65 | |
66 switch(o->type){ | |
2873 | 67 case FF_OPT_TYPE_INT: *(int *)dst= lrintf(num/den)*intnum; break; |
68 case FF_OPT_TYPE_INT64: *(int64_t *)dst= lrintf(num/den)*intnum; break; | |
69 case FF_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break; | |
70 case FF_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break; | |
2862 | 71 case FF_OPT_TYPE_RATIONAL: |
2873 | 72 if((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den}; |
73 else *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24); | |
2862 | 74 default: |
2873 | 75 return NULL; |
2862 | 76 } |
2873 | 77 return o; |
2862 | 78 } |
79 | |
80 //FIXME use eval.c maybe? | |
2873 | 81 AVOption *av_set_string(void *obj, const char *name, const char *val){ |
2862 | 82 AVOption *o= find_opt(obj, name); |
83 if(!o || !val || o->offset<=0) | |
2873 | 84 return NULL; |
2862 | 85 if(o->type != FF_OPT_TYPE_STRING){ |
86 double d=0, tmp_d; | |
87 for(;;){ | |
88 int i; | |
89 char buf[256], *tail; | |
90 | |
91 for(i=0; i<sizeof(buf)-1 && val[i] && val[i]!='+'; i++) | |
92 buf[i]= val[i]; | |
93 buf[i]=0; | |
94 val+= i; | |
95 | |
96 tmp_d= av_parse_num(buf, &tail); | |
97 if(tail > buf) | |
98 d+= tmp_d; | |
99 else{ | |
100 AVOption *o_named= find_opt(obj, buf); | |
101 if(o_named && o_named->type == FF_OPT_TYPE_CONST) | |
102 d+= o_named->default_val; | |
103 else if(!strcmp(buf, "default")) d+= o->default_val; | |
104 else if(!strcmp(buf, "max" )) d+= o->max; | |
105 else if(!strcmp(buf, "min" )) d+= o->min; | |
2873 | 106 else return NULL; |
2862 | 107 } |
108 | |
109 if(*val == '+') val++; | |
110 if(!*val) | |
111 return av_set_number(obj, name, d, 1, 1); | |
112 } | |
2873 | 113 return NULL; |
2862 | 114 } |
115 | |
116 memcpy(((uint8_t*)obj) + o->offset, val, sizeof(val)); | |
2873 | 117 return o; |
2862 | 118 } |
119 | |
2873 | 120 AVOption *av_set_double(void *obj, const char *name, double n){ |
2862 | 121 return av_set_number(obj, name, n, 1, 1); |
122 } | |
123 | |
2873 | 124 AVOption *av_set_q(void *obj, const char *name, AVRational n){ |
2862 | 125 return av_set_number(obj, name, n.num, n.den, 1); |
126 } | |
127 | |
2873 | 128 AVOption *av_set_int(void *obj, const char *name, int64_t n){ |
2862 | 129 return av_set_number(obj, name, 1, 1, n); |
130 } | |
131 | |
2873 | 132 /** |
133 * | |
134 * @param buf a buffer which is used for returning non string values as strings, can be NULL | |
135 * @param buf_len allocated length in bytes of buf | |
136 */ | |
137 const char *av_get_string(void *obj, const char *name, AVOption **o_out, char *buf, int buf_len){ | |
2862 | 138 AVOption *o= find_opt(obj, name); |
2873 | 139 void *dst; |
2862 | 140 if(!o || o->offset<=0) |
141 return NULL; | |
2873 | 142 if(o->type != FF_OPT_TYPE_STRING && (!buf || !buf_len)) |
2862 | 143 return NULL; |
144 | |
2873 | 145 dst= ((uint8_t*)obj) + o->offset; |
146 if(o_out) *o_out= o; | |
147 | |
148 if(o->type == FF_OPT_TYPE_STRING) | |
149 return dst; | |
150 | |
151 switch(o->type){ | |
152 case FF_OPT_TYPE_INT: snprintf(buf, buf_len, "%d" , *(int *)dst);break; | |
153 case FF_OPT_TYPE_INT64: snprintf(buf, buf_len, "%Ld", *(int64_t*)dst);break; | |
154 case FF_OPT_TYPE_FLOAT: snprintf(buf, buf_len, "%f" , *(float *)dst);break; | |
155 case FF_OPT_TYPE_DOUBLE: snprintf(buf, buf_len, "%f" , *(double *)dst);break; | |
156 case FF_OPT_TYPE_RATIONAL: snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break; | |
157 default: return NULL; | |
158 } | |
159 return buf; | |
2862 | 160 } |
161 | |
2873 | 162 static int av_get_number(void *obj, const char *name, AVOption **o_out, double *num, int *den, int64_t *intnum){ |
2862 | 163 AVOption *o= find_opt(obj, name); |
164 void *dst; | |
165 if(!o || o->offset<=0) | |
2873 | 166 goto error; |
2862 | 167 |
168 dst= ((uint8_t*)obj) + o->offset; | |
169 | |
2873 | 170 if(o_out) *o_out= o; |
171 | |
2862 | 172 switch(o->type){ |
2873 | 173 case FF_OPT_TYPE_INT: *intnum= *(int *)dst;return 0; |
174 case FF_OPT_TYPE_INT64: *intnum= *(int64_t*)dst;return 0; | |
175 case FF_OPT_TYPE_FLOAT: *num= *(float *)dst;return 0; | |
176 case FF_OPT_TYPE_DOUBLE: *num= *(double *)dst;return 0; | |
177 case FF_OPT_TYPE_RATIONAL: *intnum= ((AVRational*)dst)->num; | |
178 *den = ((AVRational*)dst)->den; | |
179 return 0; | |
2862 | 180 } |
2873 | 181 error: |
182 *den=*intnum=0; | |
183 return -1; | |
2862 | 184 } |
2873 | 185 |
186 double av_get_double(void *obj, const char *name, AVOption **o_out){ | |
187 int64_t intnum=1; | |
188 double num=1; | |
189 int den=1; | |
190 | |
191 av_get_number(obj, name, o_out, &num, &den, &intnum); | |
192 return num*intnum/den; | |
193 } | |
194 | |
195 AVRational av_get_q(void *obj, const char *name, AVOption **o_out){ | |
196 int64_t intnum=1; | |
197 double num=1; | |
198 int den=1; | |
199 | |
200 av_get_number(obj, name, o_out, &num, &den, &intnum); | |
201 if(num == 1.0 && (int)intnum == intnum) | |
202 return (AVRational){intnum, den}; | |
203 else | |
204 return av_d2q(num*intnum/den, 1<<24); | |
205 } | |
206 | |
207 int64_t av_get_int(void *obj, const char *name, AVOption **o_out){ | |
208 int64_t intnum=1; | |
209 double num=1; | |
210 int den=1; | |
211 | |
212 av_get_number(obj, name, o_out, &num, &den, &intnum); | |
213 return num*intnum/den; | |
214 } | |
215 | |
216 int av_opt_show(void *obj, FILE *f){ | |
217 AVOption *opt=NULL; | |
218 | |
219 if(!obj) | |
220 return -1; | |
221 #undef fprintf | |
222 fprintf(f, "%s AVOptions:\n", (*(AVClass**)obj)->class_name); | |
223 | |
224 while((opt= av_next_option(obj, opt))){ | |
225 if(!(opt->flags & (AV_OPT_FLAG_ENCODING_PARAM|AV_OPT_FLAG_DECODING_PARAM))) | |
226 continue; | |
227 | |
228 fprintf(f, "-%-17s ", opt->name); | |
229 fprintf(f, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.'); | |
230 fprintf(f, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.'); | |
231 fprintf(f, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.'); | |
232 fprintf(f, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.'); | |
233 fprintf(f, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.'); | |
234 | |
235 fprintf(f, " %s\n", opt->help); | |
236 } | |
237 return 0; | |
238 } |