checksum doesnt cover the forward_ptr anymore
[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
860a40c8
MN
55static int av_set_number(void *obj, const char *name, double num, int den, int64_t intnum){
56 AVOption *o= find_opt(obj, name);
57 void *dst;
58 if(!o || o->offset<=0)
59 return -1;
60
61 if(o->max*den < num*intnum || o->min*den > num*intnum)
62 return -1;
63
64 dst= ((uint8_t*)obj) + o->offset;
65
66 switch(o->type){
67 case FF_OPT_TYPE_INT:
68 *(int*)dst= lrintf(num/den)*intnum;
69 break;
70 case FF_OPT_TYPE_INT64:
71 *(int64_t*)dst= lrintf(num/den)*intnum;
72 break;
73 case FF_OPT_TYPE_FLOAT:
74 *(float*)dst= num*intnum/den;
75 break;
76 case FF_OPT_TYPE_DOUBLE:
77 *(double*)dst= num*intnum/den;
78 break;
79 case FF_OPT_TYPE_RATIONAL:
80 if((int)num == num)
81 *(AVRational*)dst= (AVRational){num*intnum, den};
82 else
83 *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
84 default:
85 return -1;
86 }
87 return 0;
88}
89
90//FIXME use eval.c maybe?
91int av_set_string(void *obj, const char *name, const char *val){
92 AVOption *o= find_opt(obj, name);
93 if(!o || !val || o->offset<=0)
94 return -1;
95 if(o->type != FF_OPT_TYPE_STRING){
96 double d=0, tmp_d;
97 for(;;){
98 int i;
99 char buf[256], *tail;
100
101 for(i=0; i<sizeof(buf)-1 && val[i] && val[i]!='+'; i++)
102 buf[i]= val[i];
103 buf[i]=0;
104 val+= i;
105
106 tmp_d= av_parse_num(buf, &tail);
107 if(tail > buf)
108 d+= tmp_d;
109 else{
110 AVOption *o_named= find_opt(obj, buf);
111 if(o_named && o_named->type == FF_OPT_TYPE_CONST)
112 d+= o_named->default_val;
113 else if(!strcmp(buf, "default")) d+= o->default_val;
114 else if(!strcmp(buf, "max" )) d+= o->max;
115 else if(!strcmp(buf, "min" )) d+= o->min;
116 else return -1;
117 }
118
119 if(*val == '+') val++;
120 if(!*val)
121 return av_set_number(obj, name, d, 1, 1);
122 }
123 return -1;
124 }
125
126 memcpy(((uint8_t*)obj) + o->offset, val, sizeof(val));
127 return 0;
128}
129
130int av_set_double(void *obj, const char *name, double n){
131 return av_set_number(obj, name, n, 1, 1);
132}
133
134int av_set_q(void *obj, const char *name, AVRational n){
135 return av_set_number(obj, name, n.num, n.den, 1);
136}
137
138int av_set_int(void *obj, const char *name, int64_t n){
139 return av_set_number(obj, name, 1, 1, n);
140}
141
142const char *av_get_string(void *obj, const char *name){
143 AVOption *o= find_opt(obj, name);
144 if(!o || o->offset<=0)
145 return NULL;
146 if(o->type != FF_OPT_TYPE_STRING) //FIXME convert to string? but what about free()?
147 return NULL;
148
149 return (const char*)(((uint8_t*)obj) + o->offset);
150}
151
152double av_get_double(void *obj, const char *name){
153 AVOption *o= find_opt(obj, name);
154 void *dst;
155 if(!o || o->offset<=0)
156 return NAN;
157
158 dst= ((uint8_t*)obj) + o->offset;
159
160 switch(o->type){
161 case FF_OPT_TYPE_INT: return *(int*)dst;
162 case FF_OPT_TYPE_INT64: return *(int64_t*)dst; //FIXME maybe write a av_get_int64() ?
163 case FF_OPT_TYPE_FLOAT: return *(float*)dst;
164 case FF_OPT_TYPE_DOUBLE: return *(double*)dst;
165 case FF_OPT_TYPE_RATIONAL: return av_q2d(*(AVRational*)dst); //FIXME maybe write a av_get_q() ?
166 default: return NAN;
167 }
168}