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.

163 lines
4.7KB

  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. static int av_set_number(void *obj, const char *name, double num, int den, int64_t intnum){
  44. AVOption *o= find_opt(obj, name);
  45. void *dst;
  46. if(!o || o->offset<=0)
  47. return -1;
  48. if(o->max*den < num*intnum || o->min*den > num*intnum)
  49. return -1;
  50. dst= ((uint8_t*)obj) + o->offset;
  51. switch(o->type){
  52. case FF_OPT_TYPE_INT:
  53. *(int*)dst= lrintf(num/den)*intnum;
  54. break;
  55. case FF_OPT_TYPE_INT64:
  56. *(int64_t*)dst= lrintf(num/den)*intnum;
  57. break;
  58. case FF_OPT_TYPE_FLOAT:
  59. *(float*)dst= num*intnum/den;
  60. break;
  61. case FF_OPT_TYPE_DOUBLE:
  62. *(double*)dst= num*intnum/den;
  63. break;
  64. case FF_OPT_TYPE_RATIONAL:
  65. if((int)num == num)
  66. *(AVRational*)dst= (AVRational){num*intnum, den};
  67. else
  68. *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
  69. default:
  70. return -1;
  71. }
  72. return 0;
  73. }
  74. //FIXME use eval.c maybe?
  75. int av_set_string(void *obj, const char *name, const char *val){
  76. AVOption *o= find_opt(obj, name);
  77. if(!o || !val || o->offset<=0)
  78. return -1;
  79. if(o->type != FF_OPT_TYPE_STRING){
  80. double d=0, tmp_d;
  81. for(;;){
  82. int i;
  83. char buf[256], *tail;
  84. for(i=0; i<sizeof(buf)-1 && val[i] && val[i]!='+'; i++)
  85. buf[i]= val[i];
  86. buf[i]=0;
  87. val+= i;
  88. tmp_d= av_parse_num(buf, &tail);
  89. if(tail > buf)
  90. d+= tmp_d;
  91. else{
  92. AVOption *o_named= find_opt(obj, buf);
  93. if(o_named && o_named->type == FF_OPT_TYPE_CONST)
  94. d+= o_named->default_val;
  95. else if(!strcmp(buf, "default")) d+= o->default_val;
  96. else if(!strcmp(buf, "max" )) d+= o->max;
  97. else if(!strcmp(buf, "min" )) d+= o->min;
  98. else return -1;
  99. }
  100. if(*val == '+') val++;
  101. if(!*val)
  102. return av_set_number(obj, name, d, 1, 1);
  103. }
  104. return -1;
  105. }
  106. memcpy(((uint8_t*)obj) + o->offset, val, sizeof(val));
  107. return 0;
  108. }
  109. int av_set_double(void *obj, const char *name, double n){
  110. return av_set_number(obj, name, n, 1, 1);
  111. }
  112. int av_set_q(void *obj, const char *name, AVRational n){
  113. return av_set_number(obj, name, n.num, n.den, 1);
  114. }
  115. int av_set_int(void *obj, const char *name, int64_t n){
  116. return av_set_number(obj, name, 1, 1, n);
  117. }
  118. const char *av_get_string(void *obj, const char *name){
  119. AVOption *o= find_opt(obj, name);
  120. if(!o || o->offset<=0)
  121. return NULL;
  122. if(o->type != FF_OPT_TYPE_STRING) //FIXME convert to string? but what about free()?
  123. return NULL;
  124. return (const char*)(((uint8_t*)obj) + o->offset);
  125. }
  126. double av_get_double(void *obj, const char *name){
  127. AVOption *o= find_opt(obj, name);
  128. void *dst;
  129. if(!o || o->offset<=0)
  130. return NAN;
  131. dst= ((uint8_t*)obj) + o->offset;
  132. switch(o->type){
  133. case FF_OPT_TYPE_INT: return *(int*)dst;
  134. case FF_OPT_TYPE_INT64: return *(int64_t*)dst; //FIXME maybe write a av_get_int64() ?
  135. case FF_OPT_TYPE_FLOAT: return *(float*)dst;
  136. case FF_OPT_TYPE_DOUBLE: return *(double*)dst;
  137. case FF_OPT_TYPE_RATIONAL: return av_q2d(*(AVRational*)dst); //FIXME maybe write a av_get_q() ?
  138. default: return NAN;
  139. }
  140. }