]> git.sesse.net Git - ffmpeg/blob - libavcodec/opt.c
missing include noticed by g0th
[ffmpeg] / libavcodec / opt.c
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 <stdio.h> //for FILE *
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
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;
54 }
55
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);
58     void *dst;
59     if(!o || o->offset<=0) 
60         return NULL;
61     
62     if(o->max*den < num*intnum || o->min*den > num*intnum)
63         return NULL;
64         
65     dst= ((uint8_t*)obj) + o->offset;
66
67     switch(o->type){
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);
76     default:
77         return NULL;
78     }
79     return o;
80 }
81
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) 
86         return NULL;
87     if(o->type != FF_OPT_TYPE_STRING){
88         for(;;){
89             int i;
90             char buf[256], *tail;
91             int cmd=0;
92             double d;
93
94             if(*val == '+' || *val == '-')
95                 cmd= *(val++);
96             
97             for(i=0; i<sizeof(buf)-1 && val[i] && val[i]!='+' && val[i]!='-'; i++)
98                 buf[i]= val[i];
99             buf[i]=0;
100             val+= i;
101             
102             d= av_parse_num(buf, &tail);
103             if(tail <= buf){
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;
110                 else return NULL;
111             }
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;
115             }else if(cmd=='-')
116                 d= -d;
117
118             av_set_number(obj, name, d, 1, 1);
119             if(!*val)
120                 return o;
121         }
122         return NULL;
123     }
124     
125     memcpy(((uint8_t*)obj) + o->offset, val, sizeof(val));
126     return o;
127 }
128
129 AVOption *av_set_double(void *obj, const char *name, double n){
130     return av_set_number(obj, name, n, 1, 1);
131 }
132
133 AVOption *av_set_q(void *obj, const char *name, AVRational n){
134     return av_set_number(obj, name, n.num, n.den, 1);
135 }
136
137 AVOption *av_set_int(void *obj, const char *name, int64_t n){
138     return av_set_number(obj, name, 1, 1, n);
139 }
140
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){
147     AVOption *o= find_opt(obj, name);
148     void *dst;
149     if(!o || o->offset<=0)
150         return NULL;
151     if(o->type != FF_OPT_TYPE_STRING && (!buf || !buf_len))
152         return NULL;
153
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){
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;
168     }
169     return buf;
170 }
171
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);
174     void *dst;
175     if(!o || o->offset<=0)
176         goto error;
177
178     dst= ((uint8_t*)obj) + o->offset;
179
180     if(o_out) *o_out= o;
181
182     switch(o->type){
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;
190                                                         return 0;
191     }
192 error:
193     *den=*intnum=0;
194     return -1;
195 }
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 }