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