Libav 0.7.1
libavutil/opt.c
Go to the documentation of this file.
00001 /*
00002  * AVOptions
00003  * Copyright (c) 2005 Michael Niedermayer <michaelni@gmx.at>
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00028 #include "avutil.h"
00029 #include "avstring.h"
00030 #include "opt.h"
00031 #include "eval.h"
00032 #include "dict.h"
00033 
00034 #if FF_API_FIND_OPT
00035 //FIXME order them and do a bin search
00036 const AVOption *av_find_opt(void *v, const char *name, const char *unit, int mask, int flags)
00037 {
00038     AVClass *c= *(AVClass**)v; //FIXME silly way of storing AVClass
00039     const AVOption *o= c->option;
00040 
00041     for (; o && o->name; o++) {
00042         if (!strcmp(o->name, name) && (!unit || (o->unit && !strcmp(o->unit, unit))) && (o->flags & mask) == flags)
00043             return o;
00044     }
00045     return NULL;
00046 }
00047 #endif
00048 
00049 const AVOption *av_next_option(void *obj, const AVOption *last)
00050 {
00051     if (last && last[1].name) return ++last;
00052     else if (last)            return NULL;
00053     else                      return (*(AVClass**)obj)->option;
00054 }
00055 
00056 static int av_set_number2(void *obj, const char *name, double num, int den, int64_t intnum, const AVOption **o_out)
00057 {
00058     const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
00059     void *dst;
00060     if (o_out)
00061         *o_out= o;
00062     if (!o || o->offset<=0)
00063         return AVERROR_OPTION_NOT_FOUND;
00064 
00065     if (o->max*den < num*intnum || o->min*den > num*intnum) {
00066         av_log(obj, AV_LOG_ERROR, "Value %lf for parameter '%s' out of range\n", num, name);
00067         return AVERROR(ERANGE);
00068     }
00069 
00070     dst= ((uint8_t*)obj) + o->offset;
00071 
00072     switch (o->type) {
00073     case FF_OPT_TYPE_FLAGS:
00074     case FF_OPT_TYPE_INT:   *(int       *)dst= llrint(num/den)*intnum; break;
00075     case FF_OPT_TYPE_INT64: *(int64_t   *)dst= llrint(num/den)*intnum; break;
00076     case FF_OPT_TYPE_FLOAT: *(float     *)dst= num*intnum/den;         break;
00077     case FF_OPT_TYPE_DOUBLE:*(double    *)dst= num*intnum/den;         break;
00078     case FF_OPT_TYPE_RATIONAL:
00079         if ((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
00080         else                 *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
00081         break;
00082     default:
00083         return AVERROR(EINVAL);
00084     }
00085     return 0;
00086 }
00087 
00088 static const AVOption *av_set_number(void *obj, const char *name, double num, int den, int64_t intnum)
00089 {
00090     const AVOption *o = NULL;
00091     if (av_set_number2(obj, name, num, den, intnum, &o) < 0)
00092         return NULL;
00093     else
00094         return o;
00095 }
00096 
00097 static const double const_values[] = {
00098     M_PI,
00099     M_E,
00100     FF_QP2LAMBDA,
00101     0
00102 };
00103 
00104 static const char * const const_names[] = {
00105     "PI",
00106     "E",
00107     "QP2LAMBDA",
00108     0
00109 };
00110 
00111 static int hexchar2int(char c) {
00112     if (c >= '0' && c <= '9') return c - '0';
00113     if (c >= 'a' && c <= 'f') return c - 'a' + 10;
00114     if (c >= 'A' && c <= 'F') return c - 'A' + 10;
00115     return -1;
00116 }
00117 
00118 int av_set_string3(void *obj, const char *name, const char *val, int alloc, const AVOption **o_out)
00119 {
00120     int ret;
00121     const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
00122     if (o_out)
00123         *o_out = o;
00124     if (!o)
00125         return AVERROR_OPTION_NOT_FOUND;
00126     if (!val || o->offset<=0)
00127         return AVERROR(EINVAL);
00128 
00129     if (o->type == FF_OPT_TYPE_BINARY) {
00130         uint8_t **dst = (uint8_t **)(((uint8_t*)obj) + o->offset);
00131         int *lendst = (int *)(dst + 1);
00132         uint8_t *bin, *ptr;
00133         int len = strlen(val);
00134         av_freep(dst);
00135         *lendst = 0;
00136         if (len & 1) return AVERROR(EINVAL);
00137         len /= 2;
00138         ptr = bin = av_malloc(len);
00139         while (*val) {
00140             int a = hexchar2int(*val++);
00141             int b = hexchar2int(*val++);
00142             if (a < 0 || b < 0) {
00143                 av_free(bin);
00144                 return AVERROR(EINVAL);
00145             }
00146             *ptr++ = (a << 4) | b;
00147         }
00148         *dst = bin;
00149         *lendst = len;
00150         return 0;
00151     }
00152     if (o->type != FF_OPT_TYPE_STRING) {
00153         int notfirst=0;
00154         for (;;) {
00155             int i;
00156             char buf[256];
00157             int cmd=0;
00158             double d;
00159 
00160             if (*val == '+' || *val == '-')
00161                 cmd= *(val++);
00162 
00163             for (i=0; i<sizeof(buf)-1 && val[i] && val[i]!='+' && val[i]!='-'; i++)
00164                 buf[i]= val[i];
00165             buf[i]=0;
00166 
00167             {
00168                 const AVOption *o_named = av_opt_find(obj, buf, o->unit, 0, 0);
00169                 if (o_named && o_named->type == FF_OPT_TYPE_CONST)
00170                     d= o_named->default_val.dbl;
00171                 else if (!strcmp(buf, "default")) d= o->default_val.dbl;
00172                 else if (!strcmp(buf, "max"    )) d= o->max;
00173                 else if (!strcmp(buf, "min"    )) d= o->min;
00174                 else if (!strcmp(buf, "none"   )) d= 0;
00175                 else if (!strcmp(buf, "all"    )) d= ~0;
00176                 else {
00177                     int res = av_expr_parse_and_eval(&d, buf, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
00178                     if (res < 0) {
00179                         av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
00180                         return res;
00181                     }
00182                 }
00183             }
00184             if (o->type == FF_OPT_TYPE_FLAGS) {
00185                 if      (cmd=='+') d= av_get_int(obj, name, NULL) | (int64_t)d;
00186                 else if (cmd=='-') d= av_get_int(obj, name, NULL) &~(int64_t)d;
00187             } else {
00188                 if      (cmd=='+') d= notfirst*av_get_double(obj, name, NULL) + d;
00189                 else if (cmd=='-') d= notfirst*av_get_double(obj, name, NULL) - d;
00190             }
00191 
00192             if ((ret = av_set_number2(obj, name, d, 1, 1, o_out)) < 0)
00193                 return ret;
00194             val+= i;
00195             if (!*val)
00196                 return 0;
00197             notfirst=1;
00198         }
00199         return AVERROR(EINVAL);
00200     }
00201 
00202     if (alloc) {
00203         av_free(*(void**)(((uint8_t*)obj) + o->offset));
00204         val= av_strdup(val);
00205     }
00206 
00207     memcpy(((uint8_t*)obj) + o->offset, &val, sizeof(val));
00208     return 0;
00209 }
00210 
00211 const AVOption *av_set_double(void *obj, const char *name, double n)
00212 {
00213     return av_set_number(obj, name, n, 1, 1);
00214 }
00215 
00216 const AVOption *av_set_q(void *obj, const char *name, AVRational n)
00217 {
00218     return av_set_number(obj, name, n.num, n.den, 1);
00219 }
00220 
00221 const AVOption *av_set_int(void *obj, const char *name, int64_t n)
00222 {
00223     return av_set_number(obj, name, 1, 1, n);
00224 }
00225 
00231 const char *av_get_string(void *obj, const char *name, const AVOption **o_out, char *buf, int buf_len)
00232 {
00233     const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
00234     void *dst;
00235     uint8_t *bin;
00236     int len, i;
00237     if (!o || o->offset<=0)
00238         return NULL;
00239     if (o->type != FF_OPT_TYPE_STRING && (!buf || !buf_len))
00240         return NULL;
00241 
00242     dst= ((uint8_t*)obj) + o->offset;
00243     if (o_out) *o_out= o;
00244 
00245     switch (o->type) {
00246     case FF_OPT_TYPE_FLAGS:     snprintf(buf, buf_len, "0x%08X",*(int    *)dst);break;
00247     case FF_OPT_TYPE_INT:       snprintf(buf, buf_len, "%d" , *(int    *)dst);break;
00248     case FF_OPT_TYPE_INT64:     snprintf(buf, buf_len, "%"PRId64, *(int64_t*)dst);break;
00249     case FF_OPT_TYPE_FLOAT:     snprintf(buf, buf_len, "%f" , *(float  *)dst);break;
00250     case FF_OPT_TYPE_DOUBLE:    snprintf(buf, buf_len, "%f" , *(double *)dst);break;
00251     case FF_OPT_TYPE_RATIONAL:  snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
00252     case FF_OPT_TYPE_STRING:    return *(void**)dst;
00253     case FF_OPT_TYPE_BINARY:
00254         len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
00255         if (len >= (buf_len + 1)/2) return NULL;
00256         bin = *(uint8_t**)dst;
00257         for (i = 0; i < len; i++) snprintf(buf + i*2, 3, "%02X", bin[i]);
00258         break;
00259     default: return NULL;
00260     }
00261     return buf;
00262 }
00263 
00264 static int av_get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum)
00265 {
00266     const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
00267     void *dst;
00268     if (!o || o->offset<=0)
00269         goto error;
00270 
00271     dst= ((uint8_t*)obj) + o->offset;
00272 
00273     if (o_out) *o_out= o;
00274 
00275     switch (o->type) {
00276     case FF_OPT_TYPE_FLAGS:     *intnum= *(unsigned int*)dst;return 0;
00277     case FF_OPT_TYPE_INT:       *intnum= *(int    *)dst;return 0;
00278     case FF_OPT_TYPE_INT64:     *intnum= *(int64_t*)dst;return 0;
00279     case FF_OPT_TYPE_FLOAT:     *num=    *(float  *)dst;return 0;
00280     case FF_OPT_TYPE_DOUBLE:    *num=    *(double *)dst;return 0;
00281     case FF_OPT_TYPE_RATIONAL:  *intnum= ((AVRational*)dst)->num;
00282                                 *den   = ((AVRational*)dst)->den;
00283                                                         return 0;
00284     }
00285 error:
00286     *den=*intnum=0;
00287     return -1;
00288 }
00289 
00290 double av_get_double(void *obj, const char *name, const AVOption **o_out)
00291 {
00292     int64_t intnum=1;
00293     double num=1;
00294     int den=1;
00295 
00296     if (av_get_number(obj, name, o_out, &num, &den, &intnum) < 0)
00297         return NAN;
00298     return num*intnum/den;
00299 }
00300 
00301 AVRational av_get_q(void *obj, const char *name, const AVOption **o_out)
00302 {
00303     int64_t intnum=1;
00304     double num=1;
00305     int den=1;
00306 
00307     if (av_get_number(obj, name, o_out, &num, &den, &intnum) < 0)
00308         return (AVRational){0, 0};
00309     if (num == 1.0 && (int)intnum == intnum)
00310         return (AVRational){intnum, den};
00311     else
00312         return av_d2q(num*intnum/den, 1<<24);
00313 }
00314 
00315 int64_t av_get_int(void *obj, const char *name, const AVOption **o_out)
00316 {
00317     int64_t intnum=1;
00318     double num=1;
00319     int den=1;
00320 
00321     if (av_get_number(obj, name, o_out, &num, &den, &intnum) < 0)
00322         return -1;
00323     return num*intnum/den;
00324 }
00325 
00326 int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
00327 {
00328     const AVOption *field = av_find_opt(obj, field_name, NULL, 0, 0);
00329     const AVOption *flag  = av_find_opt(obj, flag_name,  NULL, 0, 0);
00330 
00331     if (!field || !flag || flag->type != FF_OPT_TYPE_CONST)
00332         return 0;
00333     return av_get_int(obj, field_name, NULL) & (int) flag->default_val.dbl;
00334 }
00335 
00336 static void opt_list(void *obj, void *av_log_obj, const char *unit,
00337                      int req_flags, int rej_flags)
00338 {
00339     const AVOption *opt=NULL;
00340 
00341     while ((opt= av_next_option(obj, opt))) {
00342         if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
00343             continue;
00344 
00345         /* Don't print CONST's on level one.
00346          * Don't print anything but CONST's on level two.
00347          * Only print items from the requested unit.
00348          */
00349         if (!unit && opt->type==FF_OPT_TYPE_CONST)
00350             continue;
00351         else if (unit && opt->type!=FF_OPT_TYPE_CONST)
00352             continue;
00353         else if (unit && opt->type==FF_OPT_TYPE_CONST && strcmp(unit, opt->unit))
00354             continue;
00355         else if (unit && opt->type == FF_OPT_TYPE_CONST)
00356             av_log(av_log_obj, AV_LOG_INFO, "   %-15s ", opt->name);
00357         else
00358             av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name);
00359 
00360         switch (opt->type) {
00361             case FF_OPT_TYPE_FLAGS:
00362                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<flags>");
00363                 break;
00364             case FF_OPT_TYPE_INT:
00365                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int>");
00366                 break;
00367             case FF_OPT_TYPE_INT64:
00368                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int64>");
00369                 break;
00370             case FF_OPT_TYPE_DOUBLE:
00371                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<double>");
00372                 break;
00373             case FF_OPT_TYPE_FLOAT:
00374                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<float>");
00375                 break;
00376             case FF_OPT_TYPE_STRING:
00377                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<string>");
00378                 break;
00379             case FF_OPT_TYPE_RATIONAL:
00380                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<rational>");
00381                 break;
00382             case FF_OPT_TYPE_BINARY:
00383                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<binary>");
00384                 break;
00385             case FF_OPT_TYPE_CONST:
00386             default:
00387                 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "");
00388                 break;
00389         }
00390         av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
00391         av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
00392         av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM   ) ? 'V' : '.');
00393         av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM   ) ? 'A' : '.');
00394         av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
00395 
00396         if (opt->help)
00397             av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
00398         av_log(av_log_obj, AV_LOG_INFO, "\n");
00399         if (opt->unit && opt->type != FF_OPT_TYPE_CONST) {
00400             opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
00401         }
00402     }
00403 }
00404 
00405 int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
00406 {
00407     if (!obj)
00408         return -1;
00409 
00410     av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
00411 
00412     opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
00413 
00414     return 0;
00415 }
00416 
00423 void av_opt_set_defaults2(void *s, int mask, int flags)
00424 {
00425     const AVOption *opt = NULL;
00426     while ((opt = av_next_option(s, opt)) != NULL) {
00427         if ((opt->flags & mask) != flags)
00428             continue;
00429         switch (opt->type) {
00430             case FF_OPT_TYPE_CONST:
00431                 /* Nothing to be done here */
00432             break;
00433             case FF_OPT_TYPE_FLAGS:
00434             case FF_OPT_TYPE_INT: {
00435                 int val;
00436                 val = opt->default_val.dbl;
00437                 av_set_int(s, opt->name, val);
00438             }
00439             break;
00440             case FF_OPT_TYPE_INT64:
00441                 if ((double)(opt->default_val.dbl+0.6) == opt->default_val.dbl)
00442                     av_log(s, AV_LOG_DEBUG, "loss of precision in default of %s\n", opt->name);
00443                 av_set_int(s, opt->name, opt->default_val.dbl);
00444             break;
00445             case FF_OPT_TYPE_DOUBLE:
00446             case FF_OPT_TYPE_FLOAT: {
00447                 double val;
00448                 val = opt->default_val.dbl;
00449                 av_set_double(s, opt->name, val);
00450             }
00451             break;
00452             case FF_OPT_TYPE_RATIONAL: {
00453                 AVRational val;
00454                 val = av_d2q(opt->default_val.dbl, INT_MAX);
00455                 av_set_q(s, opt->name, val);
00456             }
00457             break;
00458             case FF_OPT_TYPE_STRING:
00459                 av_set_string3(s, opt->name, opt->default_val.str, 1, NULL);
00460                 break;
00461             case FF_OPT_TYPE_BINARY:
00462                 /* Cannot set default for binary */
00463             break;
00464             default:
00465                 av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
00466         }
00467     }
00468 }
00469 
00470 void av_opt_set_defaults(void *s)
00471 {
00472     av_opt_set_defaults2(s, 0, 0);
00473 }
00474 
00492 static int parse_key_value_pair(void *ctx, const char **buf,
00493                                 const char *key_val_sep, const char *pairs_sep)
00494 {
00495     char *key = av_get_token(buf, key_val_sep);
00496     char *val;
00497     int ret;
00498 
00499     if (*key && strspn(*buf, key_val_sep)) {
00500         (*buf)++;
00501         val = av_get_token(buf, pairs_sep);
00502     } else {
00503         av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
00504         av_free(key);
00505         return AVERROR(EINVAL);
00506     }
00507 
00508     av_log(ctx, AV_LOG_DEBUG, "Setting value '%s' for key '%s'\n", val, key);
00509 
00510     ret = av_set_string3(ctx, key, val, 1, NULL);
00511     if (ret == AVERROR_OPTION_NOT_FOUND)
00512         av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
00513 
00514     av_free(key);
00515     av_free(val);
00516     return ret;
00517 }
00518 
00519 int av_set_options_string(void *ctx, const char *opts,
00520                           const char *key_val_sep, const char *pairs_sep)
00521 {
00522     int ret, count = 0;
00523 
00524     while (*opts) {
00525         if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
00526             return ret;
00527         count++;
00528 
00529         if (*opts)
00530             opts++;
00531     }
00532 
00533     return count;
00534 }
00535 
00536 void av_opt_free(void *obj)
00537 {
00538     const AVOption *o = NULL;
00539     while ((o = av_next_option(obj, o)))
00540         if (o->type == FF_OPT_TYPE_STRING || o->type == FF_OPT_TYPE_BINARY)
00541             av_freep((uint8_t *)obj + o->offset);
00542 }
00543 
00544 int av_opt_set_dict(void *obj, AVDictionary **options)
00545 {
00546     AVDictionaryEntry *t = NULL;
00547     AVDictionary    *tmp = NULL;
00548     int ret = 0;
00549 
00550     while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
00551         ret = av_set_string3(obj, t->key, t->value, 1, NULL);
00552         if (ret == AVERROR_OPTION_NOT_FOUND)
00553             av_dict_set(&tmp, t->key, t->value, 0);
00554         else if (ret < 0) {
00555             av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
00556             break;
00557         }
00558         ret = 0;
00559     }
00560     av_dict_free(options);
00561     *options = tmp;
00562     return ret;
00563 }
00564 
00565 const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
00566                             int opt_flags, int search_flags)
00567 {
00568     AVClass *c = *(AVClass**)obj;
00569     const AVOption *o = NULL;
00570 
00571     if (c->opt_find && search_flags & AV_OPT_SEARCH_CHILDREN &&
00572         (o = c->opt_find(obj, name, unit, opt_flags, search_flags)))
00573         return o;
00574 
00575     while (o = av_next_option(obj, o)) {
00576         if (!strcmp(o->name, name) && (!unit || (o->unit && !strcmp(o->unit, unit))) &&
00577             (o->flags & opt_flags) == opt_flags)
00578             return o;
00579     }
00580     return NULL;
00581 }
00582 
00583 #ifdef TEST
00584 
00585 #undef printf
00586 
00587 typedef struct TestContext
00588 {
00589     const AVClass *class;
00590     int num;
00591     int toggle;
00592     char *string;
00593     int flags;
00594     AVRational rational;
00595 } TestContext;
00596 
00597 #define OFFSET(x) offsetof(TestContext, x)
00598 
00599 #define TEST_FLAG_COOL 01
00600 #define TEST_FLAG_LAME 02
00601 #define TEST_FLAG_MU   04
00602 
00603 static const AVOption test_options[]= {
00604 {"num",      "set num",        OFFSET(num),      FF_OPT_TYPE_INT,      0,              0,        100                 },
00605 {"toggle",   "set toggle",     OFFSET(toggle),   FF_OPT_TYPE_INT,      0,              0,        1                   },
00606 {"rational", "set rational",   OFFSET(rational), FF_OPT_TYPE_RATIONAL, 0,              0,        10                  },
00607 {"string",   "set string",     OFFSET(string),   FF_OPT_TYPE_STRING,   0,              CHAR_MIN, CHAR_MAX            },
00608 {"flags",    "set flags",      OFFSET(flags),    FF_OPT_TYPE_FLAGS,    0,              0,        INT_MAX, 0, "flags" },
00609 {"cool",     "set cool flag ", 0,                FF_OPT_TYPE_CONST,    TEST_FLAG_COOL, INT_MIN,  INT_MAX, 0, "flags" },
00610 {"lame",     "set lame flag ", 0,                FF_OPT_TYPE_CONST,    TEST_FLAG_LAME, INT_MIN,  INT_MAX, 0, "flags" },
00611 {"mu",       "set mu flag ",   0,                FF_OPT_TYPE_CONST,    TEST_FLAG_MU,   INT_MIN,  INT_MAX, 0, "flags" },
00612 {NULL},
00613 };
00614 
00615 static const char *test_get_name(void *ctx)
00616 {
00617     return "test";
00618 }
00619 
00620 static const AVClass test_class = {
00621     "TestContext",
00622     test_get_name,
00623     test_options
00624 };
00625 
00626 int main(void)
00627 {
00628     int i;
00629 
00630     printf("\nTesting av_set_options_string()\n");
00631     {
00632         TestContext test_ctx;
00633         const char *options[] = {
00634             "",
00635             ":",
00636             "=",
00637             "foo=:",
00638             ":=foo",
00639             "=foo",
00640             "foo=",
00641             "foo",
00642             "foo=val",
00643             "foo==val",
00644             "toggle=:",
00645             "string=:",
00646             "toggle=1 : foo",
00647             "toggle=100",
00648             "toggle==1",
00649             "flags=+mu-lame : num=42: toggle=0",
00650             "num=42 : string=blahblah",
00651             "rational=0 : rational=1/2 : rational=1/-1",
00652             "rational=-1/0",
00653         };
00654 
00655         test_ctx.class = &test_class;
00656         av_opt_set_defaults2(&test_ctx, 0, 0);
00657         test_ctx.string = av_strdup("default");
00658 
00659         av_log_set_level(AV_LOG_DEBUG);
00660 
00661         for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
00662             av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
00663             if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
00664                 av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
00665             printf("\n");
00666         }
00667     }
00668 
00669     return 0;
00670 }
00671 
00672 #endif