comparison opt.c @ 8356:0030146fc2ba libavcodec

Implement av_set_string3().
author stefano
date Tue, 16 Dec 2008 21:35:35 +0000
parents 844463c05193
children ea27308fa023
comparison
equal deleted inserted replaced
8355:179f8e1327ce 8356:0030146fc2ba
105 if (c >= 'a' && c <= 'f') return c - 'a' + 10; 105 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
106 if (c >= 'A' && c <= 'F') return c - 'A' + 10; 106 if (c >= 'A' && c <= 'F') return c - 'A' + 10;
107 return -1; 107 return -1;
108 } 108 }
109 109
110 const AVOption *av_set_string2(void *obj, const char *name, const char *val, int alloc){ 110 int av_set_string3(void *obj, const char *name, const char *val, int alloc, const AVOption **o_out){
111 int ret;
111 const AVOption *o= av_find_opt(obj, name, NULL, 0, 0); 112 const AVOption *o= av_find_opt(obj, name, NULL, 0, 0);
112 if(!o || !val || o->offset<=0) 113 if (o_out)
113 return NULL; 114 *o_out = o;
115 if(!o)
116 return AVERROR(ENOENT);
117 if(!val || o->offset<=0)
118 return AVERROR(EINVAL);
119
114 if(o->type == FF_OPT_TYPE_BINARY){ 120 if(o->type == FF_OPT_TYPE_BINARY){
115 uint8_t **dst = (uint8_t **)(((uint8_t*)obj) + o->offset); 121 uint8_t **dst = (uint8_t **)(((uint8_t*)obj) + o->offset);
116 int *lendst = (int *)(dst + 1); 122 int *lendst = (int *)(dst + 1);
117 uint8_t *bin, *ptr; 123 uint8_t *bin, *ptr;
118 int len = strlen(val); 124 int len = strlen(val);
119 av_freep(dst); 125 av_freep(dst);
120 *lendst = 0; 126 *lendst = 0;
121 if (len & 1) return NULL; 127 if (len & 1) return AVERROR(EINVAL);
122 len /= 2; 128 len /= 2;
123 ptr = bin = av_malloc(len); 129 ptr = bin = av_malloc(len);
124 while (*val) { 130 while (*val) {
125 int a = hexchar2int(*val++); 131 int a = hexchar2int(*val++);
126 int b = hexchar2int(*val++); 132 int b = hexchar2int(*val++);
127 if (a < 0 || b < 0) { 133 if (a < 0 || b < 0) {
128 av_free(bin); 134 av_free(bin);
129 return NULL; 135 return AVERROR(EINVAL);
130 } 136 }
131 *ptr++ = (a << 4) | b; 137 *ptr++ = (a << 4) | b;
132 } 138 }
133 *dst = bin; 139 *dst = bin;
134 *lendst = len; 140 *lendst = len;
135 return o; 141 return 0;
136 } 142 }
137 if(o->type != FF_OPT_TYPE_STRING){ 143 if(o->type != FF_OPT_TYPE_STRING){
138 int notfirst=0; 144 int notfirst=0;
139 for(;;){ 145 for(;;){
140 int i; 146 int i;
161 else if(!strcmp(buf, "none" )) d= 0; 167 else if(!strcmp(buf, "none" )) d= 0;
162 else if(!strcmp(buf, "all" )) d= ~0; 168 else if(!strcmp(buf, "all" )) d= ~0;
163 else { 169 else {
164 if (error) 170 if (error)
165 av_log(NULL, AV_LOG_ERROR, "Unable to parse option value \"%s\": %s\n", val, error); 171 av_log(NULL, AV_LOG_ERROR, "Unable to parse option value \"%s\": %s\n", val, error);
166 return NULL; 172 return AVERROR(EINVAL);
167 } 173 }
168 } 174 }
169 if(o->type == FF_OPT_TYPE_FLAGS){ 175 if(o->type == FF_OPT_TYPE_FLAGS){
170 if (cmd=='+') d= av_get_int(obj, name, NULL) | (int64_t)d; 176 if (cmd=='+') d= av_get_int(obj, name, NULL) | (int64_t)d;
171 else if(cmd=='-') d= av_get_int(obj, name, NULL) &~(int64_t)d; 177 else if(cmd=='-') d= av_get_int(obj, name, NULL) &~(int64_t)d;
172 }else{ 178 }else{
173 if (cmd=='+') d= notfirst*av_get_double(obj, name, NULL) + d; 179 if (cmd=='+') d= notfirst*av_get_double(obj, name, NULL) + d;
174 else if(cmd=='-') d= notfirst*av_get_double(obj, name, NULL) - d; 180 else if(cmd=='-') d= notfirst*av_get_double(obj, name, NULL) - d;
175 } 181 }
176 182
177 if (!av_set_number(obj, name, d, 1, 1)) 183 if ((ret = av_set_number2(obj, name, d, 1, 1, o_out)) < 0)
178 return NULL; 184 return ret;
179 val+= i; 185 val+= i;
180 if(!*val) 186 if(!*val)
181 return o; 187 return 0;
182 notfirst=1; 188 notfirst=1;
183 } 189 }
184 return NULL; 190 return AVERROR(EINVAL);
185 } 191 }
186 192
187 if(alloc){ 193 if(alloc){
188 av_free(*(void**)(((uint8_t*)obj) + o->offset)); 194 av_free(*(void**)(((uint8_t*)obj) + o->offset));
189 val= av_strdup(val); 195 val= av_strdup(val);
190 } 196 }
191 197
192 memcpy(((uint8_t*)obj) + o->offset, &val, sizeof(val)); 198 memcpy(((uint8_t*)obj) + o->offset, &val, sizeof(val));
199 return 0;
200 }
201
202 const AVOption *av_set_string2(void *obj, const char *name, const char *val, int alloc){
203 const AVOption *o;
204 if (av_set_string3(obj, name, val, alloc, &o) < 0)
205 return NULL;
193 return o; 206 return o;
194 } 207 }
195 208
196 const AVOption *av_set_string(void *obj, const char *name, const char *val){ 209 const AVOption *av_set_string(void *obj, const char *name, const char *val){
197 return av_set_string2(obj, name, val, 0); 210 return av_set_string2(obj, name, val, 0);