annotate opts.c @ 1077:296c7fc8358f libavcodec

32 vs. 64bit bugfix by (Dave Huang (dahanc))
author michaelni
date Sat, 22 Feb 2003 13:44:16 +0000
parents 3c3da6edc9a1
children 1e39f273ecd6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
962
c5aef83c6a3f * first shot for generaly usable option parser for codecs
kabi
parents:
diff changeset
1 /*
c5aef83c6a3f * first shot for generaly usable option parser for codecs
kabi
parents:
diff changeset
2 * LGPL
c5aef83c6a3f * first shot for generaly usable option parser for codecs
kabi
parents:
diff changeset
3 */
c5aef83c6a3f * first shot for generaly usable option parser for codecs
kabi
parents:
diff changeset
4
c5aef83c6a3f * first shot for generaly usable option parser for codecs
kabi
parents:
diff changeset
5 /*
c5aef83c6a3f * first shot for generaly usable option parser for codecs
kabi
parents:
diff changeset
6 * typical parsed command line:
c5aef83c6a3f * first shot for generaly usable option parser for codecs
kabi
parents:
diff changeset
7 * msmpeg4:bitrate=720000:qmax=16
c5aef83c6a3f * first shot for generaly usable option parser for codecs
kabi
parents:
diff changeset
8 *
c5aef83c6a3f * first shot for generaly usable option parser for codecs
kabi
parents:
diff changeset
9 */
c5aef83c6a3f * first shot for generaly usable option parser for codecs
kabi
parents:
diff changeset
10
c5aef83c6a3f * first shot for generaly usable option parser for codecs
kabi
parents:
diff changeset
11 #include "avcodec.h"
1058
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
12
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
13 extern const AVOption common_options[2];
962
c5aef83c6a3f * first shot for generaly usable option parser for codecs
kabi
parents:
diff changeset
14
1058
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
15 const AVOption common_options[2] = {
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
16 AVOPTION_CODEC_INT("common", "test", bit_rate, 0, 10, 0),
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
17 AVOPTION_END()
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
18 };
962
c5aef83c6a3f * first shot for generaly usable option parser for codecs
kabi
parents:
diff changeset
19
1058
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
20 static int parse_bool(const AVOption *c, char *s, int *var)
1019
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
21 {
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
22 int b = 1; /* by default -on- when present */
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
23 if (s) {
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
24 if (!strcasecmp(s, "off") || !strcasecmp(s, "false")
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
25 || !strcmp(s, "0"))
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
26 b = 0;
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
27 else if (!strcasecmp(s, "on") || !strcasecmp(s, "true")
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
28 || !strcmp(s, "1"))
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
29 b = 1;
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
30 else
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
31 return -1;
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
32 }
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
33
1058
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
34 *var = b;
1019
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
35 return 0;
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
36 }
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
37
1058
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
38 static int parse_double(const AVOption *c, char *s, double *var)
1019
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
39 {
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
40 double d;
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
41 if (!s)
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
42 return -1;
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
43 d = atof(s);
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
44 if (c->min != c->max) {
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
45 if (d < c->min || d > c->max) {
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
46 fprintf(stderr, "Option: %s double value: %f out of range <%f, %f>\n",
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
47 c->name, d, c->min, c->max);
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
48 return -1;
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
49 }
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
50 }
1058
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
51 *var = d;
1019
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
52 return 0;
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
53 }
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
54
1058
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
55 static int parse_int(const AVOption* c, char* s, int* var)
1019
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
56 {
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
57 int i;
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
58 if (!s)
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
59 return -1;
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
60 i = atoi(s);
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
61 if (c->min != c->max) {
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
62 if (i < (int)c->min || i > (int)c->max) {
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
63 fprintf(stderr, "Option: %s integer value: %d out of range <%d, %d>\n",
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
64 c->name, i, (int)c->min, (int)c->max);
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
65 return -1;
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
66 }
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
67 }
1058
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
68 *var = i;
1019
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
69 return 0;
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
70 }
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
71
1058
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
72 static int parse_string(const AVOption *c, char *s, AVCodecContext *avctx, char **var)
1019
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
73 {
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
74 if (!s)
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
75 return -1;
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
76
1058
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
77 if (c->type == FF_OPT_TYPE_RCOVERRIDE) {
1019
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
78 int sf, ef, qs;
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
79 float qf;
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
80 if (sscanf(s, "%d,%d,%d,%f", &sf, &ef, &qs, &qf) == 4 && sf < ef) {
1058
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
81 RcOverride *o;
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
82 avctx->rc_override = av_realloc(avctx->rc_override,
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
83 sizeof(RcOverride) * (avctx->rc_override_count + 1));
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
84 o = avctx->rc_override + avctx->rc_override_count++;
1019
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
85 o->start_frame = sf;
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
86 o->end_frame = ef;
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
87 o->qscale = qs;
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
88 o->quality_factor = qf;
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
89
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
90 //printf("parsed Rc: %d,%d,%d,%f (%d)\n", sf,ef,qs,qf, avctx->rc_override_count);
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
91 } else {
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
92 printf("incorrect/unparsable Rc: \"%s\"\n", s);
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
93 }
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
94 } else
1058
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
95 *var = av_strdup(s);
1019
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
96 return 0;
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
97 }
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
98
962
c5aef83c6a3f * first shot for generaly usable option parser for codecs
kabi
parents:
diff changeset
99 /**
1019
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
100 *
1058
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
101 * \param codec codec for option parsing
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
102 * \param opts string with options for parsing
962
c5aef83c6a3f * first shot for generaly usable option parser for codecs
kabi
parents:
diff changeset
103 * \param avctx where to store parsed results
1058
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
104 */
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
105 int avcodec_parse(const AVCodec *codec, const char *opts, AVCodecContext *avctx)
962
c5aef83c6a3f * first shot for generaly usable option parser for codecs
kabi
parents:
diff changeset
106 {
1058
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
107 int r = 0;
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
108 char* dopts = av_strdup(opts);
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
109 if (dopts) {
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
110 char *str = dopts;
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
111
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
112 while (str && *str && r == 0) {
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
113 const AVOption *stack[FF_OPT_MAX_DEPTH];
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
114 int depth = 0;
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
115 const AVOption *c = codec->options;
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
116 char* e = strchr(str, ':');
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
117 char* p;
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
118 if (e)
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
119 *e++ = 0;
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
120
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
121 p = strchr(str, '=');
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
122 if (p)
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
123 *p++ = 0;
962
c5aef83c6a3f * first shot for generaly usable option parser for codecs
kabi
parents:
diff changeset
124
1058
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
125 // going through option structures
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
126 for (;;) {
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
127 if (!c->name) {
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
128 if (c->sub) {
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
129 stack[depth++] = c;
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
130 c = c->sub;
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
131 assert(depth > FF_OPT_MAX_DEPTH);
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
132 } else {
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
133 if (depth == 0)
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
134 break; // finished
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
135 c = stack[--depth];
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
136 c++;
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
137 }
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
138 } else {
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
139 if (!strcmp(c->name, str)) {
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
140 void* ptr = (char*)avctx + c->offset;
962
c5aef83c6a3f * first shot for generaly usable option parser for codecs
kabi
parents:
diff changeset
141
1058
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
142 switch (c->type & FF_OPT_TYPE_MASK) {
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
143 case FF_OPT_TYPE_BOOL:
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
144 r = parse_bool(c, p, (int*)ptr);
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
145 break;
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
146 case FF_OPT_TYPE_DOUBLE:
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
147 r = parse_double(c, p, (double*)ptr);
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
148 break;
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
149 case FF_OPT_TYPE_INT:
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
150 r = parse_int(c, p, (int*)ptr);
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
151 break;
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
152 case FF_OPT_TYPE_STRING:
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
153 r = parse_string(c, p, avctx, (char**)ptr);
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
154 break;
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
155 default:
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
156 assert(0 == 1);
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
157 }
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
158 }
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
159 c++;
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
160 }
1019
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
161 }
1058
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
162 str = e;
1019
ef905ded19fe * code for parsing options
kabi
parents: 962
diff changeset
163 }
1058
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
164 av_free(dopts);
962
c5aef83c6a3f * first shot for generaly usable option parser for codecs
kabi
parents:
diff changeset
165 }
1058
3c3da6edc9a1 * still unfinished code for Options
kabi
parents: 1019
diff changeset
166 return r;
962
c5aef83c6a3f * first shot for generaly usable option parser for codecs
kabi
parents:
diff changeset
167 }