~medibuntu-maintainers/mplayer/medibuntu.precise

« back to all changes in this revision

Viewing changes to ffmpeg/libavutil/opt.c

  • Committer: Package Import Robot
  • Author(s): Reinhard Tartler
  • Date: 2012-01-12 22:23:28 UTC
  • mfrom: (0.4.7 sid)
  • mto: This revision was merged to the branch mainline in revision 76.
  • Revision ID: package-import@ubuntu.com-20120112222328-8jqdyodym3p84ygu
Tags: 2:1.0~rc4.dfsg1+svn34540-1
* New upstream snapshot
* upload to unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
#include "opt.h"
31
31
#include "eval.h"
32
32
#include "dict.h"
 
33
#include "log.h"
33
34
 
34
35
#if FF_API_FIND_OPT
35
36
//FIXME order them and do a bin search
46
47
}
47
48
#endif
48
49
 
 
50
#if FF_API_OLD_AVOPTIONS
49
51
const AVOption *av_next_option(void *obj, const AVOption *last)
50
52
{
51
 
    if (last && last[1].name) return ++last;
52
 
    else if (last)            return NULL;
53
 
    else                      return (*(AVClass**)obj)->option;
54
 
}
55
 
 
56
 
static int av_set_number2(void *obj, const char *name, double num, int den, int64_t intnum, const AVOption **o_out)
57
 
{
58
 
    const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
59
 
    void *dst;
60
 
    if (o_out)
61
 
        *o_out= o;
62
 
    if (!o || o->offset<=0)
63
 
        return AVERROR_OPTION_NOT_FOUND;
64
 
 
 
53
    return av_opt_next(obj, last);
 
54
}
 
55
#endif
 
56
 
 
57
const AVOption *av_opt_next(void *obj, const AVOption *last)
 
58
{
 
59
    AVClass *class = *(AVClass**)obj;
 
60
    if (!last && class->option[0].name) return class->option;
 
61
    if (last && last[1].name)           return ++last;
 
62
    return NULL;
 
63
}
 
64
 
 
65
static int read_number(const AVOption *o, void *dst, double *num, int *den, int64_t *intnum)
 
66
{
 
67
    switch (o->type) {
 
68
    case AV_OPT_TYPE_FLAGS:     *intnum = *(unsigned int*)dst;return 0;
 
69
    case AV_OPT_TYPE_INT:       *intnum = *(int         *)dst;return 0;
 
70
    case AV_OPT_TYPE_INT64:     *intnum = *(int64_t     *)dst;return 0;
 
71
    case AV_OPT_TYPE_FLOAT:     *num    = *(float       *)dst;return 0;
 
72
    case AV_OPT_TYPE_DOUBLE:    *num    = *(double      *)dst;return 0;
 
73
    case AV_OPT_TYPE_RATIONAL:  *intnum = ((AVRational*)dst)->num;
 
74
                                *den    = ((AVRational*)dst)->den;
 
75
                                                        return 0;
 
76
    }
 
77
    return AVERROR(EINVAL);
 
78
}
 
79
 
 
80
static int write_number(void *obj, const AVOption *o, void *dst, double num, int den, int64_t intnum)
 
81
{
65
82
    if (o->max*den < num*intnum || o->min*den > num*intnum) {
66
 
        av_log(obj, AV_LOG_ERROR, "Value %lf for parameter '%s' out of range\n", num, name);
 
83
        av_log(obj, AV_LOG_ERROR, "Value %lf for parameter '%s' out of range\n", num, o->name);
67
84
        return AVERROR(ERANGE);
68
85
    }
69
86
 
70
 
    dst= ((uint8_t*)obj) + o->offset;
71
 
 
72
87
    switch (o->type) {
73
 
    case FF_OPT_TYPE_FLAGS:
74
 
    case FF_OPT_TYPE_INT:   *(int       *)dst= llrint(num/den)*intnum; break;
75
 
    case FF_OPT_TYPE_INT64: *(int64_t   *)dst= llrint(num/den)*intnum; break;
76
 
    case FF_OPT_TYPE_FLOAT: *(float     *)dst= num*intnum/den;         break;
77
 
    case FF_OPT_TYPE_DOUBLE:*(double    *)dst= num*intnum/den;         break;
78
 
    case FF_OPT_TYPE_RATIONAL:
 
88
    case AV_OPT_TYPE_FLAGS:
 
89
    case AV_OPT_TYPE_INT:   *(int       *)dst= llrint(num/den)*intnum; break;
 
90
    case AV_OPT_TYPE_INT64: *(int64_t   *)dst= llrint(num/den)*intnum; break;
 
91
    case AV_OPT_TYPE_FLOAT: *(float     *)dst= num*intnum/den;         break;
 
92
    case AV_OPT_TYPE_DOUBLE:*(double    *)dst= num*intnum/den;         break;
 
93
    case AV_OPT_TYPE_RATIONAL:
79
94
        if ((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
80
95
        else                 *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
81
96
        break;
85
100
    return 0;
86
101
}
87
102
 
88
 
static const AVOption *av_set_number(void *obj, const char *name, double num, int den, int64_t intnum)
89
 
{
90
 
    const AVOption *o = NULL;
91
 
    if (av_set_number2(obj, name, num, den, intnum, &o) < 0)
92
 
        return NULL;
93
 
    else
94
 
        return o;
95
 
}
96
 
 
97
103
static const double const_values[] = {
98
104
    M_PI,
99
105
    M_E,
115
121
    return -1;
116
122
}
117
123
 
 
124
static int set_string_binary(void *obj, const AVOption *o, const char *val, uint8_t **dst)
 
125
{
 
126
    int *lendst = (int *)(dst + 1);
 
127
    uint8_t *bin, *ptr;
 
128
    int len = strlen(val);
 
129
 
 
130
    av_freep(dst);
 
131
    *lendst = 0;
 
132
 
 
133
    if (len & 1)
 
134
        return AVERROR(EINVAL);
 
135
    len /= 2;
 
136
 
 
137
    ptr = bin = av_malloc(len);
 
138
    while (*val) {
 
139
        int a = hexchar2int(*val++);
 
140
        int b = hexchar2int(*val++);
 
141
        if (a < 0 || b < 0) {
 
142
            av_free(bin);
 
143
            return AVERROR(EINVAL);
 
144
        }
 
145
        *ptr++ = (a << 4) | b;
 
146
    }
 
147
    *dst = bin;
 
148
    *lendst = len;
 
149
 
 
150
    return 0;
 
151
}
 
152
 
 
153
static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **dst)
 
154
{
 
155
    av_freep(dst);
 
156
    *dst = av_strdup(val);
 
157
    return 0;
 
158
}
 
159
 
 
160
static int set_string_number(void *obj, const AVOption *o, const char *val, void *dst)
 
161
{
 
162
    int ret = 0, notfirst = 0;
 
163
    for (;;) {
 
164
        int i, den = 1;
 
165
        char buf[256];
 
166
        int cmd = 0;
 
167
        double d, num = 1;
 
168
        int64_t intnum = 1;
 
169
 
 
170
        if (*val == '+' || *val == '-')
 
171
            cmd = *(val++);
 
172
 
 
173
        for (i = 0; i < sizeof(buf) - 1 && val[i] && val[i] != '+' && val[i] != '-'; i++)
 
174
            buf[i] = val[i];
 
175
        buf[i] = 0;
 
176
 
 
177
        {
 
178
            const AVOption *o_named = av_opt_find(obj, buf, o->unit, 0, 0);
 
179
            if (o_named && o_named->type == AV_OPT_TYPE_CONST)
 
180
                d = o_named->default_val.dbl;
 
181
            else if (!strcmp(buf, "default")) d = o->default_val.dbl;
 
182
            else if (!strcmp(buf, "max"    )) d = o->max;
 
183
            else if (!strcmp(buf, "min"    )) d = o->min;
 
184
            else if (!strcmp(buf, "none"   )) d = 0;
 
185
            else if (!strcmp(buf, "all"    )) d = ~0;
 
186
            else {
 
187
                int res = av_expr_parse_and_eval(&d, buf, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
 
188
                if (res < 0) {
 
189
                    av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
 
190
                    return res;
 
191
                }
 
192
            }
 
193
        }
 
194
        if (o->type == AV_OPT_TYPE_FLAGS) {
 
195
            read_number(o, dst, NULL, NULL, &intnum);
 
196
            if      (cmd == '+') d = intnum | (int64_t)d;
 
197
            else if (cmd == '-') d = intnum &~(int64_t)d;
 
198
        } else {
 
199
            read_number(o, dst, &num, &den, &intnum);
 
200
            if      (cmd == '+') d = notfirst*num*intnum/den + d;
 
201
            else if (cmd == '-') d = notfirst*num*intnum/den - d;
 
202
        }
 
203
 
 
204
        if ((ret = write_number(obj, o, dst, d, 1, 1)) < 0)
 
205
            return ret;
 
206
        val += i;
 
207
        if (!*val)
 
208
            return 0;
 
209
        notfirst = 1;
 
210
    }
 
211
 
 
212
    return 0;
 
213
}
 
214
 
 
215
#if FF_API_OLD_AVOPTIONS
118
216
int av_set_string3(void *obj, const char *name, const char *val, int alloc, const AVOption **o_out)
119
217
{
120
 
    int ret;
121
218
    const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
122
219
    if (o_out)
123
220
        *o_out = o;
124
 
    if (!o)
125
 
        return AVERROR_OPTION_NOT_FOUND;
126
 
    if (!val || o->offset<=0)
127
 
        return AVERROR(EINVAL);
128
 
 
129
 
    if (o->type == FF_OPT_TYPE_BINARY) {
130
 
        uint8_t **dst = (uint8_t **)(((uint8_t*)obj) + o->offset);
131
 
        int *lendst = (int *)(dst + 1);
132
 
        uint8_t *bin, *ptr;
133
 
        int len = strlen(val);
134
 
        av_freep(dst);
135
 
        *lendst = 0;
136
 
        if (len & 1) return AVERROR(EINVAL);
137
 
        len /= 2;
138
 
        ptr = bin = av_malloc(len);
139
 
        while (*val) {
140
 
            int a = hexchar2int(*val++);
141
 
            int b = hexchar2int(*val++);
142
 
            if (a < 0 || b < 0) {
143
 
                av_free(bin);
144
 
                return AVERROR(EINVAL);
145
 
            }
146
 
            *ptr++ = (a << 4) | b;
147
 
        }
148
 
        *dst = bin;
149
 
        *lendst = len;
150
 
        return 0;
151
 
    }
152
 
    if (o->type != FF_OPT_TYPE_STRING) {
153
 
        int notfirst=0;
154
 
        for (;;) {
155
 
            int i;
156
 
            char buf[256];
157
 
            int cmd=0;
158
 
            double d;
159
 
 
160
 
            if (*val == '+' || *val == '-')
161
 
                cmd= *(val++);
162
 
 
163
 
            for (i=0; i<sizeof(buf)-1 && val[i] && val[i]!='+' && val[i]!='-'; i++)
164
 
                buf[i]= val[i];
165
 
            buf[i]=0;
166
 
 
167
 
            {
168
 
                const AVOption *o_named = av_opt_find(obj, buf, o->unit, 0, 0);
169
 
                if (o_named && o_named->type == FF_OPT_TYPE_CONST)
170
 
                    d= o_named->default_val.dbl;
171
 
                else if (!strcmp(buf, "default")) d= o->default_val.dbl;
172
 
                else if (!strcmp(buf, "max"    )) d= o->max;
173
 
                else if (!strcmp(buf, "min"    )) d= o->min;
174
 
                else if (!strcmp(buf, "none"   )) d= 0;
175
 
                else if (!strcmp(buf, "all"    )) d= ~0;
176
 
                else {
177
 
                    int res = av_expr_parse_and_eval(&d, buf, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
178
 
                    if (res < 0) {
179
 
                        av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
180
 
                        return res;
181
 
                    }
182
 
                }
183
 
            }
184
 
            if (o->type == FF_OPT_TYPE_FLAGS) {
185
 
                if      (cmd=='+') d= av_get_int(obj, name, NULL) | (int64_t)d;
186
 
                else if (cmd=='-') d= av_get_int(obj, name, NULL) &~(int64_t)d;
187
 
            } else {
188
 
                if      (cmd=='+') d= notfirst*av_get_double(obj, name, NULL) + d;
189
 
                else if (cmd=='-') d= notfirst*av_get_double(obj, name, NULL) - d;
190
 
            }
191
 
 
192
 
            if ((ret = av_set_number2(obj, name, d, 1, 1, o_out)) < 0)
193
 
                return ret;
194
 
            val+= i;
195
 
            if (!*val)
196
 
                return 0;
197
 
            notfirst=1;
198
 
        }
199
 
        return AVERROR(EINVAL);
200
 
    }
201
 
 
202
 
    if (alloc) {
203
 
        av_free(*(void**)(((uint8_t*)obj) + o->offset));
204
 
        val= av_strdup(val);
205
 
    }
206
 
 
207
 
    memcpy(((uint8_t*)obj) + o->offset, &val, sizeof(val));
208
 
    return 0;
209
 
}
210
 
 
 
221
    return av_opt_set(obj, name, val, 0);
 
222
}
 
223
#endif
 
224
 
 
225
int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
 
226
{
 
227
    void *dst, *target_obj;
 
228
    const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
 
229
    if (!o || !target_obj)
 
230
        return AVERROR_OPTION_NOT_FOUND;
 
231
    if (!val)
 
232
        return AVERROR(EINVAL);
 
233
 
 
234
    dst = ((uint8_t*)target_obj) + o->offset;
 
235
    switch (o->type) {
 
236
    case AV_OPT_TYPE_STRING:   return set_string(obj, o, val, dst);
 
237
    case AV_OPT_TYPE_BINARY:   return set_string_binary(obj, o, val, dst);
 
238
    case AV_OPT_TYPE_FLAGS:
 
239
    case AV_OPT_TYPE_INT:
 
240
    case AV_OPT_TYPE_INT64:
 
241
    case AV_OPT_TYPE_FLOAT:
 
242
    case AV_OPT_TYPE_DOUBLE:
 
243
    case AV_OPT_TYPE_RATIONAL: return set_string_number(obj, o, val, dst);
 
244
    }
 
245
 
 
246
    av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
 
247
    return AVERROR(EINVAL);
 
248
}
 
249
 
 
250
#define OPT_EVAL_NUMBER(name, opttype, vartype)\
 
251
    int av_opt_eval_ ## name(void *obj, const AVOption *o, const char *val, vartype *name ## _out)\
 
252
    {\
 
253
        if (!o || o->type != opttype)\
 
254
            return AVERROR(EINVAL);\
 
255
        return set_string_number(obj, o, val, name ## _out);\
 
256
    }
 
257
 
 
258
OPT_EVAL_NUMBER(flags,  AV_OPT_TYPE_FLAGS,    int)
 
259
OPT_EVAL_NUMBER(int,    AV_OPT_TYPE_INT,      int)
 
260
OPT_EVAL_NUMBER(int64,  AV_OPT_TYPE_INT64,    int64_t)
 
261
OPT_EVAL_NUMBER(float,  AV_OPT_TYPE_FLOAT,    float)
 
262
OPT_EVAL_NUMBER(double, AV_OPT_TYPE_DOUBLE,   double)
 
263
OPT_EVAL_NUMBER(q,      AV_OPT_TYPE_RATIONAL, AVRational)
 
264
 
 
265
static int set_number(void *obj, const char *name, double num, int den, int64_t intnum,
 
266
                                  int search_flags)
 
267
{
 
268
    void *dst, *target_obj;
 
269
    const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
 
270
 
 
271
    if (!o || !target_obj)
 
272
        return AVERROR_OPTION_NOT_FOUND;
 
273
 
 
274
    dst = ((uint8_t*)target_obj) + o->offset;
 
275
    return write_number(obj, o, dst, num, den, intnum);
 
276
}
 
277
 
 
278
#if FF_API_OLD_AVOPTIONS
211
279
const AVOption *av_set_double(void *obj, const char *name, double n)
212
280
{
213
 
    return av_set_number(obj, name, n, 1, 1);
 
281
    const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
 
282
    if (set_number(obj, name, n, 1, 1, 0) < 0)
 
283
        return NULL;
 
284
    return o;
214
285
}
215
286
 
216
287
const AVOption *av_set_q(void *obj, const char *name, AVRational n)
217
288
{
218
 
    return av_set_number(obj, name, n.num, n.den, 1);
 
289
    const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
 
290
    if (set_number(obj, name, n.num, n.den, 1, 0) < 0)
 
291
        return NULL;
 
292
    return o;
219
293
}
220
294
 
221
295
const AVOption *av_set_int(void *obj, const char *name, int64_t n)
222
296
{
223
 
    return av_set_number(obj, name, 1, 1, n);
224
 
}
225
 
 
 
297
    const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
 
298
    if (set_number(obj, name, 1, 1, n, 0) < 0)
 
299
        return NULL;
 
300
    return o;
 
301
}
 
302
#endif
 
303
 
 
304
int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
 
305
{
 
306
    return set_number(obj, name, 1, 1, val, search_flags);
 
307
}
 
308
 
 
309
int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
 
310
{
 
311
    return set_number(obj, name, val, 1, 1, search_flags);
 
312
}
 
313
 
 
314
int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
 
315
{
 
316
    return set_number(obj, name, val.num, val.den, 1, search_flags);
 
317
}
 
318
 
 
319
#if FF_API_OLD_AVOPTIONS
226
320
/**
227
321
 *
228
322
 * @param buf a buffer which is used for returning non string values as strings, can be NULL
234
328
    void *dst;
235
329
    uint8_t *bin;
236
330
    int len, i;
237
 
    if (!o || o->offset<=0)
 
331
    if (!o)
238
332
        return NULL;
239
 
    if (o->type != FF_OPT_TYPE_STRING && (!buf || !buf_len))
 
333
    if (o->type != AV_OPT_TYPE_STRING && (!buf || !buf_len))
240
334
        return NULL;
241
335
 
242
336
    dst= ((uint8_t*)obj) + o->offset;
243
337
    if (o_out) *o_out= o;
244
338
 
245
339
    switch (o->type) {
246
 
    case FF_OPT_TYPE_FLAGS:     snprintf(buf, buf_len, "0x%08X",*(int    *)dst);break;
247
 
    case FF_OPT_TYPE_INT:       snprintf(buf, buf_len, "%d" , *(int    *)dst);break;
248
 
    case FF_OPT_TYPE_INT64:     snprintf(buf, buf_len, "%"PRId64, *(int64_t*)dst);break;
249
 
    case FF_OPT_TYPE_FLOAT:     snprintf(buf, buf_len, "%f" , *(float  *)dst);break;
250
 
    case FF_OPT_TYPE_DOUBLE:    snprintf(buf, buf_len, "%f" , *(double *)dst);break;
251
 
    case FF_OPT_TYPE_RATIONAL:  snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
252
 
    case FF_OPT_TYPE_STRING:    return *(void**)dst;
253
 
    case FF_OPT_TYPE_BINARY:
 
340
    case AV_OPT_TYPE_FLAGS:     snprintf(buf, buf_len, "0x%08X",*(int    *)dst);break;
 
341
    case AV_OPT_TYPE_INT:       snprintf(buf, buf_len, "%d" , *(int    *)dst);break;
 
342
    case AV_OPT_TYPE_INT64:     snprintf(buf, buf_len, "%"PRId64, *(int64_t*)dst);break;
 
343
    case AV_OPT_TYPE_FLOAT:     snprintf(buf, buf_len, "%f" , *(float  *)dst);break;
 
344
    case AV_OPT_TYPE_DOUBLE:    snprintf(buf, buf_len, "%f" , *(double *)dst);break;
 
345
    case AV_OPT_TYPE_RATIONAL:  snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
 
346
    case AV_OPT_TYPE_STRING:    return *(void**)dst;
 
347
    case AV_OPT_TYPE_BINARY:
254
348
        len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
255
349
        if (len >= (buf_len + 1)/2) return NULL;
256
350
        bin = *(uint8_t**)dst;
260
354
    }
261
355
    return buf;
262
356
}
263
 
 
264
 
static int av_get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum)
265
 
{
266
 
    const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
267
 
    void *dst;
268
 
    if (!o || o->offset<=0)
 
357
#endif
 
358
 
 
359
int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
 
360
{
 
361
    void *dst, *target_obj;
 
362
    const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
 
363
    uint8_t *bin, buf[128];
 
364
    int len, i, ret;
 
365
 
 
366
    if (!o || !target_obj)
 
367
        return AVERROR_OPTION_NOT_FOUND;
 
368
 
 
369
    dst = (uint8_t*)target_obj + o->offset;
 
370
 
 
371
    buf[0] = 0;
 
372
    switch (o->type) {
 
373
    case AV_OPT_TYPE_FLAGS:     ret = snprintf(buf, sizeof(buf), "0x%08X",  *(int    *)dst);break;
 
374
    case AV_OPT_TYPE_INT:       ret = snprintf(buf, sizeof(buf), "%d" ,     *(int    *)dst);break;
 
375
    case AV_OPT_TYPE_INT64:     ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t*)dst);break;
 
376
    case AV_OPT_TYPE_FLOAT:     ret = snprintf(buf, sizeof(buf), "%f" ,     *(float  *)dst);break;
 
377
    case AV_OPT_TYPE_DOUBLE:    ret = snprintf(buf, sizeof(buf), "%f" ,     *(double *)dst);break;
 
378
    case AV_OPT_TYPE_RATIONAL:  ret = snprintf(buf, sizeof(buf), "%d/%d",   ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
 
379
    case AV_OPT_TYPE_STRING:
 
380
        if (*(uint8_t**)dst)
 
381
            *out_val = av_strdup(*(uint8_t**)dst);
 
382
        else
 
383
            *out_val = av_strdup("");
 
384
        return 0;
 
385
    case AV_OPT_TYPE_BINARY:
 
386
        len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
 
387
        if ((uint64_t)len*2 + 1 > INT_MAX)
 
388
            return AVERROR(EINVAL);
 
389
        if (!(*out_val = av_malloc(len*2 + 1)))
 
390
            return AVERROR(ENOMEM);
 
391
        bin = *(uint8_t**)dst;
 
392
        for (i = 0; i < len; i++)
 
393
            snprintf(*out_val + i*2, 3, "%02X", bin[i]);
 
394
        return 0;
 
395
    default:
 
396
        return AVERROR(EINVAL);
 
397
    }
 
398
 
 
399
    if (ret >= sizeof(buf))
 
400
        return AVERROR(EINVAL);
 
401
    *out_val = av_strdup(buf);
 
402
    return 0;
 
403
}
 
404
 
 
405
static int get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum,
 
406
                      int search_flags)
 
407
{
 
408
    void *dst, *target_obj;
 
409
    const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
 
410
    if (!o || !target_obj)
269
411
        goto error;
270
412
 
271
 
    dst= ((uint8_t*)obj) + o->offset;
 
413
    dst = ((uint8_t*)target_obj) + o->offset;
272
414
 
273
415
    if (o_out) *o_out= o;
274
416
 
275
 
    switch (o->type) {
276
 
    case FF_OPT_TYPE_FLAGS:     *intnum= *(unsigned int*)dst;return 0;
277
 
    case FF_OPT_TYPE_INT:       *intnum= *(int    *)dst;return 0;
278
 
    case FF_OPT_TYPE_INT64:     *intnum= *(int64_t*)dst;return 0;
279
 
    case FF_OPT_TYPE_FLOAT:     *num=    *(float  *)dst;return 0;
280
 
    case FF_OPT_TYPE_DOUBLE:    *num=    *(double *)dst;return 0;
281
 
    case FF_OPT_TYPE_RATIONAL:  *intnum= ((AVRational*)dst)->num;
282
 
                                *den   = ((AVRational*)dst)->den;
283
 
                                                        return 0;
284
 
    }
 
417
    return read_number(o, dst, num, den, intnum);
 
418
 
285
419
error:
286
420
    *den=*intnum=0;
287
421
    return -1;
288
422
}
289
423
 
 
424
#if FF_API_OLD_AVOPTIONS
290
425
double av_get_double(void *obj, const char *name, const AVOption **o_out)
291
426
{
292
427
    int64_t intnum=1;
293
428
    double num=1;
294
429
    int den=1;
295
430
 
296
 
    if (av_get_number(obj, name, o_out, &num, &den, &intnum) < 0)
 
431
    if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
297
432
        return NAN;
298
433
    return num*intnum/den;
299
434
}
304
439
    double num=1;
305
440
    int den=1;
306
441
 
307
 
    if (av_get_number(obj, name, o_out, &num, &den, &intnum) < 0)
 
442
    if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
308
443
        return (AVRational){0, 0};
309
444
    if (num == 1.0 && (int)intnum == intnum)
310
445
        return (AVRational){intnum, den};
318
453
    double num=1;
319
454
    int den=1;
320
455
 
321
 
    if (av_get_number(obj, name, o_out, &num, &den, &intnum) < 0)
 
456
    if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
322
457
        return -1;
323
458
    return num*intnum/den;
324
459
}
 
460
#endif
 
461
 
 
462
int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
 
463
{
 
464
    int64_t intnum = 1;
 
465
    double     num = 1;
 
466
    int   ret, den = 1;
 
467
 
 
468
    if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
 
469
        return ret;
 
470
    *out_val = num*intnum/den;
 
471
    return 0;
 
472
}
 
473
 
 
474
int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
 
475
{
 
476
    int64_t intnum = 1;
 
477
    double     num = 1;
 
478
    int   ret, den = 1;
 
479
 
 
480
    if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
 
481
        return ret;
 
482
    *out_val = num*intnum/den;
 
483
    return 0;
 
484
}
 
485
 
 
486
int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
 
487
{
 
488
    int64_t intnum = 1;
 
489
    double     num = 1;
 
490
    int   ret, den = 1;
 
491
 
 
492
    if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
 
493
        return ret;
 
494
 
 
495
    if (num == 1.0 && (int)intnum == intnum)
 
496
        *out_val = (AVRational){intnum, den};
 
497
    else
 
498
        *out_val = av_d2q(num*intnum/den, 1<<24);
 
499
    return 0;
 
500
}
325
501
 
326
502
int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
327
503
{
328
 
    const AVOption *field = av_find_opt(obj, field_name, NULL, 0, 0);
329
 
    const AVOption *flag  = av_find_opt(obj, flag_name,  NULL, 0, 0);
 
504
    const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
 
505
    const AVOption *flag  = av_opt_find(obj, flag_name,
 
506
                                        field ? field->unit : NULL, 0, 0);
 
507
    int64_t res;
330
508
 
331
 
    if (!field || !flag || flag->type != FF_OPT_TYPE_CONST)
 
509
    if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
 
510
        av_opt_get_int(obj, field_name, 0, &res) < 0)
332
511
        return 0;
333
 
    return av_get_int(obj, field_name, NULL) & (int) flag->default_val.dbl;
 
512
    return res & (int) flag->default_val.dbl;
334
513
}
335
514
 
336
515
static void opt_list(void *obj, void *av_log_obj, const char *unit,
338
517
{
339
518
    const AVOption *opt=NULL;
340
519
 
341
 
    while ((opt= av_next_option(obj, opt))) {
 
520
    while ((opt = av_opt_next(obj, opt))) {
342
521
        if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
343
522
            continue;
344
523
 
346
525
         * Don't print anything but CONST's on level two.
347
526
         * Only print items from the requested unit.
348
527
         */
349
 
        if (!unit && opt->type==FF_OPT_TYPE_CONST)
350
 
            continue;
351
 
        else if (unit && opt->type!=FF_OPT_TYPE_CONST)
352
 
            continue;
353
 
        else if (unit && opt->type==FF_OPT_TYPE_CONST && strcmp(unit, opt->unit))
354
 
            continue;
355
 
        else if (unit && opt->type == FF_OPT_TYPE_CONST)
 
528
        if (!unit && opt->type==AV_OPT_TYPE_CONST)
 
529
            continue;
 
530
        else if (unit && opt->type!=AV_OPT_TYPE_CONST)
 
531
            continue;
 
532
        else if (unit && opt->type==AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
 
533
            continue;
 
534
        else if (unit && opt->type == AV_OPT_TYPE_CONST)
356
535
            av_log(av_log_obj, AV_LOG_INFO, "   %-15s ", opt->name);
357
536
        else
358
537
            av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name);
359
538
 
360
539
        switch (opt->type) {
361
 
            case FF_OPT_TYPE_FLAGS:
 
540
            case AV_OPT_TYPE_FLAGS:
362
541
                av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<flags>");
363
542
                break;
364
 
            case FF_OPT_TYPE_INT:
 
543
            case AV_OPT_TYPE_INT:
365
544
                av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int>");
366
545
                break;
367
 
            case FF_OPT_TYPE_INT64:
 
546
            case AV_OPT_TYPE_INT64:
368
547
                av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int64>");
369
548
                break;
370
 
            case FF_OPT_TYPE_DOUBLE:
 
549
            case AV_OPT_TYPE_DOUBLE:
371
550
                av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<double>");
372
551
                break;
373
 
            case FF_OPT_TYPE_FLOAT:
 
552
            case AV_OPT_TYPE_FLOAT:
374
553
                av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<float>");
375
554
                break;
376
 
            case FF_OPT_TYPE_STRING:
 
555
            case AV_OPT_TYPE_STRING:
377
556
                av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<string>");
378
557
                break;
379
 
            case FF_OPT_TYPE_RATIONAL:
 
558
            case AV_OPT_TYPE_RATIONAL:
380
559
                av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<rational>");
381
560
                break;
382
 
            case FF_OPT_TYPE_BINARY:
 
561
            case AV_OPT_TYPE_BINARY:
383
562
                av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<binary>");
384
563
                break;
385
 
            case FF_OPT_TYPE_CONST:
 
564
            case AV_OPT_TYPE_CONST:
386
565
            default:
387
566
                av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "");
388
567
                break;
396
575
        if (opt->help)
397
576
            av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
398
577
        av_log(av_log_obj, AV_LOG_INFO, "\n");
399
 
        if (opt->unit && opt->type != FF_OPT_TYPE_CONST) {
 
578
        if (opt->unit && opt->type != AV_OPT_TYPE_CONST) {
400
579
            opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
401
580
        }
402
581
    }
414
593
    return 0;
415
594
}
416
595
 
417
 
/** Set the values of the AVCodecContext or AVFormatContext structure.
418
 
 * They are set to the defaults specified in the according AVOption options
419
 
 * array default_val field.
420
 
 *
421
 
 * @param s AVCodecContext or AVFormatContext for which the defaults will be set
422
 
 */
 
596
void av_opt_set_defaults(void *s)
 
597
{
 
598
#if FF_API_OLD_AVOPTIONS
 
599
    av_opt_set_defaults2(s, 0, 0);
 
600
}
 
601
 
423
602
void av_opt_set_defaults2(void *s, int mask, int flags)
424
603
{
 
604
#endif
425
605
    const AVOption *opt = NULL;
426
 
    while ((opt = av_next_option(s, opt)) != NULL) {
 
606
    while ((opt = av_opt_next(s, opt)) != NULL) {
 
607
#if FF_API_OLD_AVOPTIONS
427
608
        if ((opt->flags & mask) != flags)
428
609
            continue;
 
610
#endif
429
611
        switch (opt->type) {
430
 
            case FF_OPT_TYPE_CONST:
 
612
            case AV_OPT_TYPE_CONST:
431
613
                /* Nothing to be done here */
432
614
            break;
433
 
            case FF_OPT_TYPE_FLAGS:
434
 
            case FF_OPT_TYPE_INT: {
 
615
            case AV_OPT_TYPE_FLAGS:
 
616
            case AV_OPT_TYPE_INT: {
435
617
                int val;
436
618
                val = opt->default_val.dbl;
437
 
                av_set_int(s, opt->name, val);
 
619
                av_opt_set_int(s, opt->name, val, 0);
438
620
            }
439
621
            break;
440
 
            case FF_OPT_TYPE_INT64:
 
622
            case AV_OPT_TYPE_INT64:
441
623
                if ((double)(opt->default_val.dbl+0.6) == opt->default_val.dbl)
442
624
                    av_log(s, AV_LOG_DEBUG, "loss of precision in default of %s\n", opt->name);
443
 
                av_set_int(s, opt->name, opt->default_val.dbl);
 
625
                av_opt_set_int(s, opt->name, opt->default_val.dbl, 0);
444
626
            break;
445
 
            case FF_OPT_TYPE_DOUBLE:
446
 
            case FF_OPT_TYPE_FLOAT: {
 
627
            case AV_OPT_TYPE_DOUBLE:
 
628
            case AV_OPT_TYPE_FLOAT: {
447
629
                double val;
448
630
                val = opt->default_val.dbl;
449
 
                av_set_double(s, opt->name, val);
 
631
                av_opt_set_double(s, opt->name, val, 0);
450
632
            }
451
633
            break;
452
 
            case FF_OPT_TYPE_RATIONAL: {
 
634
            case AV_OPT_TYPE_RATIONAL: {
453
635
                AVRational val;
454
636
                val = av_d2q(opt->default_val.dbl, INT_MAX);
455
 
                av_set_q(s, opt->name, val);
 
637
                av_opt_set_q(s, opt->name, val, 0);
456
638
            }
457
639
            break;
458
 
            case FF_OPT_TYPE_STRING:
459
 
                av_set_string3(s, opt->name, opt->default_val.str, 1, NULL);
 
640
            case AV_OPT_TYPE_STRING:
 
641
                av_opt_set(s, opt->name, opt->default_val.str, 0);
460
642
                break;
461
 
            case FF_OPT_TYPE_BINARY:
 
643
            case AV_OPT_TYPE_BINARY:
462
644
                /* Cannot set default for binary */
463
645
            break;
464
646
            default:
467
649
    }
468
650
}
469
651
 
470
 
void av_opt_set_defaults(void *s)
471
 
{
472
 
    av_opt_set_defaults2(s, 0, 0);
473
 
}
474
 
 
475
652
/**
476
653
 * Store the value in the field in ctx that is named like key.
477
654
 * ctx must be an AVClass context, storing is done using AVOptions.
486
663
 * set, or a negative value corresponding to an AVERROR code in case
487
664
 * of error:
488
665
 * AVERROR(EINVAL) if the key/value pair cannot be parsed,
489
 
 * the error code issued by av_set_string3() if the key/value pair
 
666
 * the error code issued by av_opt_set() if the key/value pair
490
667
 * cannot be set
491
668
 */
492
669
static int parse_key_value_pair(void *ctx, const char **buf,
507
684
 
508
685
    av_log(ctx, AV_LOG_DEBUG, "Setting value '%s' for key '%s'\n", val, key);
509
686
 
510
 
    ret = av_set_string3(ctx, key, val, 1, NULL);
 
687
    ret = av_opt_set(ctx, key, val, 0);
511
688
    if (ret == AVERROR_OPTION_NOT_FOUND)
512
689
        av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
513
690
 
521
698
{
522
699
    int ret, count = 0;
523
700
 
 
701
    if (!opts)
 
702
        return 0;
 
703
 
524
704
    while (*opts) {
525
705
        if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
526
706
            return ret;
536
716
void av_opt_free(void *obj)
537
717
{
538
718
    const AVOption *o = NULL;
539
 
    while ((o = av_next_option(obj, o)))
540
 
        if (o->type == FF_OPT_TYPE_STRING || o->type == FF_OPT_TYPE_BINARY)
 
719
    while ((o = av_opt_next(obj, o)))
 
720
        if (o->type == AV_OPT_TYPE_STRING || o->type == AV_OPT_TYPE_BINARY)
541
721
            av_freep((uint8_t *)obj + o->offset);
542
722
}
543
723
 
548
728
    int ret = 0;
549
729
 
550
730
    while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
551
 
        ret = av_set_string3(obj, t->key, t->value, 1, NULL);
 
731
        ret = av_opt_set(obj, t->key, t->value, 0);
552
732
        if (ret == AVERROR_OPTION_NOT_FOUND)
553
733
            av_dict_set(&tmp, t->key, t->value, 0);
554
734
        else if (ret < 0) {
565
745
const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
566
746
                            int opt_flags, int search_flags)
567
747
{
568
 
    AVClass *c = *(AVClass**)obj;
 
748
    return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
 
749
}
 
750
 
 
751
const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
 
752
                             int opt_flags, int search_flags, void **target_obj)
 
753
{
 
754
    const AVClass  *c = *(AVClass**)obj;
569
755
    const AVOption *o = NULL;
570
756
 
571
 
    if (c->opt_find && search_flags & AV_OPT_SEARCH_CHILDREN &&
572
 
        (o = c->opt_find(obj, name, unit, opt_flags, search_flags)))
573
 
        return o;
 
757
    if (search_flags & AV_OPT_SEARCH_CHILDREN) {
 
758
        if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
 
759
            const AVClass *child = NULL;
 
760
            while (child = av_opt_child_class_next(c, child))
 
761
                if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
 
762
                    return o;
 
763
        } else {
 
764
            void *child = NULL;
 
765
            while (child = av_opt_child_next(obj, child))
 
766
                if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
 
767
                    return o;
 
768
        }
 
769
    }
574
770
 
575
 
    while (o = av_next_option(obj, o)) {
576
 
        if (!strcmp(o->name, name) && (!unit || (o->unit && !strcmp(o->unit, unit))) &&
577
 
            (o->flags & opt_flags) == opt_flags)
 
771
    while (o = av_opt_next(obj, o)) {
 
772
        if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
 
773
            ((!unit && o->type != AV_OPT_TYPE_CONST) ||
 
774
             (unit  && o->unit && !strcmp(o->unit, unit)))) {
 
775
            if (target_obj) {
 
776
                if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
 
777
                    *target_obj = obj;
 
778
                else
 
779
                    *target_obj = NULL;
 
780
            }
578
781
            return o;
 
782
        }
579
783
    }
580
784
    return NULL;
581
785
}
582
786
 
 
787
void *av_opt_child_next(void *obj, void *prev)
 
788
{
 
789
    const AVClass *c = *(AVClass**)obj;
 
790
    if (c->child_next)
 
791
        return c->child_next(obj, prev);
 
792
    return NULL;
 
793
}
 
794
 
 
795
const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
 
796
{
 
797
    if (parent->child_class_next)
 
798
        return parent->child_class_next(prev);
 
799
    return NULL;
 
800
}
 
801
 
583
802
#ifdef TEST
584
803
 
585
804
#undef printf
601
820
#define TEST_FLAG_MU   04
602
821
 
603
822
static const AVOption test_options[]= {
604
 
{"num",      "set num",        OFFSET(num),      FF_OPT_TYPE_INT,      0,              0,        100                 },
605
 
{"toggle",   "set toggle",     OFFSET(toggle),   FF_OPT_TYPE_INT,      0,              0,        1                   },
606
 
{"rational", "set rational",   OFFSET(rational), FF_OPT_TYPE_RATIONAL, 0,              0,        10                  },
607
 
{"string",   "set string",     OFFSET(string),   FF_OPT_TYPE_STRING,   0,              CHAR_MIN, CHAR_MAX            },
608
 
{"flags",    "set flags",      OFFSET(flags),    FF_OPT_TYPE_FLAGS,    0,              0,        INT_MAX, 0, "flags" },
609
 
{"cool",     "set cool flag ", 0,                FF_OPT_TYPE_CONST,    TEST_FLAG_COOL, INT_MIN,  INT_MAX, 0, "flags" },
610
 
{"lame",     "set lame flag ", 0,                FF_OPT_TYPE_CONST,    TEST_FLAG_LAME, INT_MIN,  INT_MAX, 0, "flags" },
611
 
{"mu",       "set mu flag ",   0,                FF_OPT_TYPE_CONST,    TEST_FLAG_MU,   INT_MIN,  INT_MAX, 0, "flags" },
 
823
{"num",      "set num",        OFFSET(num),      AV_OPT_TYPE_INT,      {0},              0,        100                 },
 
824
{"toggle",   "set toggle",     OFFSET(toggle),   AV_OPT_TYPE_INT,      {0},              0,        1                   },
 
825
{"rational", "set rational",   OFFSET(rational), AV_OPT_TYPE_RATIONAL, {0},              0,        10                  },
 
826
{"string",   "set string",     OFFSET(string),   AV_OPT_TYPE_STRING,   {0},              CHAR_MIN, CHAR_MAX            },
 
827
{"flags",    "set flags",      OFFSET(flags),    AV_OPT_TYPE_FLAGS,    {0},              0,        INT_MAX, 0, "flags" },
 
828
{"cool",     "set cool flag ", 0,                AV_OPT_TYPE_CONST,    {TEST_FLAG_COOL}, INT_MIN,  INT_MAX, 0, "flags" },
 
829
{"lame",     "set lame flag ", 0,                AV_OPT_TYPE_CONST,    {TEST_FLAG_LAME}, INT_MIN,  INT_MAX, 0, "flags" },
 
830
{"mu",       "set mu flag ",   0,                AV_OPT_TYPE_CONST,    {TEST_FLAG_MU},   INT_MIN,  INT_MAX, 0, "flags" },
612
831
{NULL},
613
832
};
614
833
 
653
872
        };
654
873
 
655
874
        test_ctx.class = &test_class;
656
 
        av_opt_set_defaults2(&test_ctx, 0, 0);
 
875
        av_opt_set_defaults(&test_ctx);
657
876
        test_ctx.string = av_strdup("default");
658
877
 
659
878
        av_log_set_level(AV_LOG_DEBUG);