You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
4.9KB

  1. /*
  2. * AVOptions
  3. * Copyright (c) 2005 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. /**
  21. * @file opt.c
  22. * AVOptions
  23. * @author Michael Niedermayer <michaelni@gmx.at>
  24. */
  25. #include "avcodec.h"
  26. static double av_parse_num(const char *name, char **tail){
  27. double d;
  28. d= strtod(name, tail);
  29. if(*tail>name && (**tail=='/' || **tail==':'))
  30. d/=strtod((*tail)+1, tail);
  31. return d;
  32. }
  33. //FIXME order them and do a bin search
  34. static AVOption *find_opt(void *v, const char *name){
  35. AVClass *c= *(AVClass**)v; //FIXME silly way of storing AVClass
  36. AVOption *o= c->option;
  37. for(;o && o->name; o++){
  38. if(!strcmp(o->name, name))
  39. return o;
  40. }
  41. return NULL;
  42. }
  43. AVOption *av_next_option(void *obj, AVOption *last){
  44. if(last && last[1].name) return ++last;
  45. else if(last) return NULL;
  46. else return (*(AVClass**)obj)->option;
  47. }
  48. static int av_set_number(void *obj, const char *name, double num, int den, int64_t intnum){
  49. AVOption *o= find_opt(obj, name);
  50. void *dst;
  51. if(!o || o->offset<=0)
  52. return -1;
  53. if(o->max*den < num*intnum || o->min*den > num*intnum)
  54. return -1;
  55. dst= ((uint8_t*)obj) + o->offset;
  56. switch(o->type){
  57. case FF_OPT_TYPE_INT:
  58. *(int*)dst= lrintf(num/den)*intnum;
  59. break;
  60. case FF_OPT_TYPE_INT64:
  61. *(int64_t*)dst= lrintf(num/den)*intnum;
  62. break;
  63. case FF_OPT_TYPE_FLOAT:
  64. *(float*)dst= num*intnum/den;
  65. break;
  66. case FF_OPT_TYPE_DOUBLE:
  67. *(double*)dst= num*intnum/den;
  68. break;
  69. case FF_OPT_TYPE_RATIONAL:
  70. if((int)num == num)
  71. *(AVRational*)dst= (AVRational){num*intnum, den};
  72. else
  73. *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
  74. default:
  75. return -1;
  76. }
  77. return 0;
  78. }
  79. //FIXME use eval.c maybe?
  80. int av_set_string(void *obj, const char *name, const char *val){
  81. AVOption *o= find_opt(obj, name);
  82. if(!o || !val || o->offset<=0)
  83. return -1;
  84. if(o->type != FF_OPT_TYPE_STRING){
  85. double d=0, tmp_d;
  86. for(;;){
  87. int i;
  88. char buf[256], *tail;
  89. for(i=0; i<sizeof(buf)-1 && val[i] && val[i]!='+'; i++)
  90. buf[i]= val[i];
  91. buf[i]=0;
  92. val+= i;
  93. tmp_d= av_parse_num(buf, &tail);
  94. if(tail > buf)
  95. d+= tmp_d;
  96. else{
  97. AVOption *o_named= find_opt(obj, buf);
  98. if(o_named && o_named->type == FF_OPT_TYPE_CONST)
  99. d+= o_named->default_val;
  100. else if(!strcmp(buf, "default")) d+= o->default_val;
  101. else if(!strcmp(buf, "max" )) d+= o->max;
  102. else if(!strcmp(buf, "min" )) d+= o->min;
  103. else return -1;
  104. }
  105. if(*val == '+') val++;
  106. if(!*val)
  107. return av_set_number(obj, name, d, 1, 1);
  108. }
  109. return -1;
  110. }
  111. memcpy(((uint8_t*)obj) + o->offset, val, sizeof(val));
  112. return 0;
  113. }
  114. int av_set_double(void *obj, const char *name, double n){
  115. return av_set_number(obj, name, n, 1, 1);
  116. }
  117. int av_set_q(void *obj, const char *name, AVRational n){
  118. return av_set_number(obj, name, n.num, n.den, 1);
  119. }
  120. int av_set_int(void *obj, const char *name, int64_t n){
  121. return av_set_number(obj, name, 1, 1, n);
  122. }
  123. const char *av_get_string(void *obj, const char *name){
  124. AVOption *o= find_opt(obj, name);
  125. if(!o || o->offset<=0)
  126. return NULL;
  127. if(o->type != FF_OPT_TYPE_STRING) //FIXME convert to string? but what about free()?
  128. return NULL;
  129. return (const char*)(((uint8_t*)obj) + o->offset);
  130. }
  131. double av_get_double(void *obj, const char *name){
  132. AVOption *o= find_opt(obj, name);
  133. void *dst;
  134. if(!o || o->offset<=0)
  135. return NAN;
  136. dst= ((uint8_t*)obj) + o->offset;
  137. switch(o->type){
  138. case FF_OPT_TYPE_INT: return *(int*)dst;
  139. case FF_OPT_TYPE_INT64: return *(int64_t*)dst; //FIXME maybe write a av_get_int64() ?
  140. case FF_OPT_TYPE_FLOAT: return *(float*)dst;
  141. case FF_OPT_TYPE_DOUBLE: return *(double*)dst;
  142. case FF_OPT_TYPE_RATIONAL: return av_q2d(*(AVRational*)dst); //FIXME maybe write a av_get_q() ?
  143. default: return NAN;
  144. }
  145. }