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.

270 lines
8.8KB

  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, const char *unit){
  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) && (!unit || !strcmp(o->unit, unit)) )
  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 AVOption *av_set_number(void *obj, const char *name, double num, int den, int64_t intnum){
  49. AVOption *o= find_opt(obj, name, NULL);
  50. void *dst;
  51. if(!o || o->offset<=0)
  52. return NULL;
  53. if(o->max*den < num*intnum || o->min*den > num*intnum)
  54. return NULL;
  55. dst= ((uint8_t*)obj) + o->offset;
  56. switch(o->type){
  57. case FF_OPT_TYPE_FLAGS:
  58. case FF_OPT_TYPE_INT: *(int *)dst= lrintf(num/den)*intnum; break;
  59. case FF_OPT_TYPE_INT64: *(int64_t *)dst= lrintf(num/den)*intnum; break;
  60. case FF_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break;
  61. case FF_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break;
  62. case FF_OPT_TYPE_RATIONAL:
  63. if((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
  64. else *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
  65. default:
  66. return NULL;
  67. }
  68. return o;
  69. }
  70. static AVOption *set_all_opt(void *v, const char *unit, double d){
  71. AVClass *c= *(AVClass**)v; //FIXME silly way of storing AVClass
  72. AVOption *o= c->option;
  73. AVOption *ret=NULL;
  74. for(;o && o->name; o++){
  75. if(o->type != FF_OPT_TYPE_CONST && o->unit && !strcmp(o->unit, unit)){
  76. double tmp= d;
  77. if(o->type == FF_OPT_TYPE_FLAGS)
  78. tmp= av_get_int(v, o->name, NULL) | (int64_t)d;
  79. av_set_number(v, o->name, tmp, 1, 1);
  80. ret= o;
  81. }
  82. }
  83. return ret;
  84. }
  85. //FIXME use eval.c maybe?
  86. AVOption *av_set_string(void *obj, const char *name, const char *val){
  87. AVOption *o= find_opt(obj, name, NULL);
  88. if(o && o->offset==0 && o->type == FF_OPT_TYPE_CONST && o->unit){
  89. return set_all_opt(obj, o->unit, o->default_val);
  90. }
  91. if(!o || !val || o->offset<=0)
  92. return NULL;
  93. if(o->type != FF_OPT_TYPE_STRING){
  94. for(;;){
  95. int i;
  96. char buf[256], *tail;
  97. int cmd=0;
  98. double d;
  99. if(*val == '+' || *val == '-')
  100. cmd= *(val++);
  101. for(i=0; i<sizeof(buf)-1 && val[i] && val[i]!='+' && val[i]!='-'; i++)
  102. buf[i]= val[i];
  103. buf[i]=0;
  104. val+= i;
  105. d= av_parse_num(buf, &tail);
  106. if(tail <= buf){
  107. AVOption *o_named= find_opt(obj, buf, o->unit);
  108. if(o_named && o_named->type == FF_OPT_TYPE_CONST)
  109. d= o_named->default_val;
  110. else if(!strcmp(buf, "default")) d= o->default_val;
  111. else if(!strcmp(buf, "max" )) d= o->max;
  112. else if(!strcmp(buf, "min" )) d= o->min;
  113. else return NULL;
  114. }
  115. if(o->type == FF_OPT_TYPE_FLAGS){
  116. if (cmd=='+') d= av_get_int(obj, name, NULL) | (int64_t)d;
  117. else if(cmd=='-') d= av_get_int(obj, name, NULL) &~(int64_t)d;
  118. }else if(cmd=='-')
  119. d= -d;
  120. av_set_number(obj, name, d, 1, 1);
  121. if(!*val)
  122. return o;
  123. }
  124. return NULL;
  125. }
  126. memcpy(((uint8_t*)obj) + o->offset, val, sizeof(val));
  127. return o;
  128. }
  129. AVOption *av_set_double(void *obj, const char *name, double n){
  130. return av_set_number(obj, name, n, 1, 1);
  131. }
  132. AVOption *av_set_q(void *obj, const char *name, AVRational n){
  133. return av_set_number(obj, name, n.num, n.den, 1);
  134. }
  135. AVOption *av_set_int(void *obj, const char *name, int64_t n){
  136. return av_set_number(obj, name, 1, 1, n);
  137. }
  138. /**
  139. *
  140. * @param buf a buffer which is used for returning non string values as strings, can be NULL
  141. * @param buf_len allocated length in bytes of buf
  142. */
  143. const char *av_get_string(void *obj, const char *name, AVOption **o_out, char *buf, int buf_len){
  144. AVOption *o= find_opt(obj, name, NULL);
  145. void *dst;
  146. if(!o || o->offset<=0)
  147. return NULL;
  148. if(o->type != FF_OPT_TYPE_STRING && (!buf || !buf_len))
  149. return NULL;
  150. dst= ((uint8_t*)obj) + o->offset;
  151. if(o_out) *o_out= o;
  152. if(o->type == FF_OPT_TYPE_STRING)
  153. return dst;
  154. switch(o->type){
  155. case FF_OPT_TYPE_FLAGS: snprintf(buf, buf_len, "0x%08X",*(int *)dst);break;
  156. case FF_OPT_TYPE_INT: snprintf(buf, buf_len, "%d" , *(int *)dst);break;
  157. case FF_OPT_TYPE_INT64: snprintf(buf, buf_len, "%Ld", *(int64_t*)dst);break;
  158. case FF_OPT_TYPE_FLOAT: snprintf(buf, buf_len, "%f" , *(float *)dst);break;
  159. case FF_OPT_TYPE_DOUBLE: snprintf(buf, buf_len, "%f" , *(double *)dst);break;
  160. case FF_OPT_TYPE_RATIONAL: snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
  161. default: return NULL;
  162. }
  163. return buf;
  164. }
  165. static int av_get_number(void *obj, const char *name, AVOption **o_out, double *num, int *den, int64_t *intnum){
  166. AVOption *o= find_opt(obj, name, NULL);
  167. void *dst;
  168. if(!o || o->offset<=0)
  169. goto error;
  170. dst= ((uint8_t*)obj) + o->offset;
  171. if(o_out) *o_out= o;
  172. switch(o->type){
  173. case FF_OPT_TYPE_FLAGS:
  174. case FF_OPT_TYPE_INT: *intnum= *(int *)dst;return 0;
  175. case FF_OPT_TYPE_INT64: *intnum= *(int64_t*)dst;return 0;
  176. case FF_OPT_TYPE_FLOAT: *num= *(float *)dst;return 0;
  177. case FF_OPT_TYPE_DOUBLE: *num= *(double *)dst;return 0;
  178. case FF_OPT_TYPE_RATIONAL: *intnum= ((AVRational*)dst)->num;
  179. *den = ((AVRational*)dst)->den;
  180. return 0;
  181. }
  182. error:
  183. *den=*intnum=0;
  184. return -1;
  185. }
  186. double av_get_double(void *obj, const char *name, AVOption **o_out){
  187. int64_t intnum=1;
  188. double num=1;
  189. int den=1;
  190. av_get_number(obj, name, o_out, &num, &den, &intnum);
  191. return num*intnum/den;
  192. }
  193. AVRational av_get_q(void *obj, const char *name, AVOption **o_out){
  194. int64_t intnum=1;
  195. double num=1;
  196. int den=1;
  197. av_get_number(obj, name, o_out, &num, &den, &intnum);
  198. if(num == 1.0 && (int)intnum == intnum)
  199. return (AVRational){intnum, den};
  200. else
  201. return av_d2q(num*intnum/den, 1<<24);
  202. }
  203. int64_t av_get_int(void *obj, const char *name, AVOption **o_out){
  204. int64_t intnum=1;
  205. double num=1;
  206. int den=1;
  207. av_get_number(obj, name, o_out, &num, &den, &intnum);
  208. return num*intnum/den;
  209. }
  210. int av_opt_show(void *obj, void *av_log_obj){
  211. AVOption *opt=NULL;
  212. if(!obj)
  213. return -1;
  214. av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
  215. while((opt= av_next_option(obj, opt))){
  216. if(!(opt->flags & (AV_OPT_FLAG_ENCODING_PARAM|AV_OPT_FLAG_DECODING_PARAM)))
  217. continue;
  218. av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name);
  219. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
  220. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
  221. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
  222. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
  223. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
  224. av_log(av_log_obj, AV_LOG_INFO, " %s\n", opt->help);
  225. }
  226. return 0;
  227. }