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.

302 lines
9.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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. */
  20. /**
  21. * @file opt.c
  22. * AVOptions
  23. * @author Michael Niedermayer <michaelni@gmx.at>
  24. */
  25. #include "avcodec.h"
  26. #include "opt.h"
  27. static double av_parse_num(const char *name, char **tail){
  28. double d;
  29. d= strtod(name, tail);
  30. if(*tail>name && (**tail=='/' || **tail==':'))
  31. d/=strtod((*tail)+1, tail);
  32. return d;
  33. }
  34. //FIXME order them and do a bin search
  35. static AVOption *find_opt(void *v, const char *name, const char *unit){
  36. AVClass *c= *(AVClass**)v; //FIXME silly way of storing AVClass
  37. AVOption *o= c->option;
  38. for(;o && o->name; o++){
  39. if(!strcmp(o->name, name) && (!unit || !strcmp(o->unit, unit)) )
  40. return o;
  41. }
  42. return NULL;
  43. }
  44. AVOption *av_next_option(void *obj, AVOption *last){
  45. if(last && last[1].name) return ++last;
  46. else if(last) return NULL;
  47. else return (*(AVClass**)obj)->option;
  48. }
  49. static AVOption *av_set_number(void *obj, const char *name, double num, int den, int64_t intnum){
  50. AVOption *o= find_opt(obj, name, NULL);
  51. void *dst;
  52. if(!o || o->offset<=0)
  53. return NULL;
  54. if(o->max*den < num*intnum || o->min*den > num*intnum)
  55. return NULL;
  56. dst= ((uint8_t*)obj) + o->offset;
  57. switch(o->type){
  58. case FF_OPT_TYPE_FLAGS:
  59. case FF_OPT_TYPE_INT: *(int *)dst= lrintf(num/den)*intnum; break;
  60. case FF_OPT_TYPE_INT64: *(int64_t *)dst= lrintf(num/den)*intnum; break;
  61. case FF_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break;
  62. case FF_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break;
  63. case FF_OPT_TYPE_RATIONAL:
  64. if((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
  65. else *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
  66. default:
  67. return NULL;
  68. }
  69. return o;
  70. }
  71. static AVOption *set_all_opt(void *v, const char *unit, double d){
  72. AVClass *c= *(AVClass**)v; //FIXME silly way of storing AVClass
  73. AVOption *o= c->option;
  74. AVOption *ret=NULL;
  75. for(;o && o->name; o++){
  76. if(o->type != FF_OPT_TYPE_CONST && o->unit && !strcmp(o->unit, unit)){
  77. double tmp= d;
  78. if(o->type == FF_OPT_TYPE_FLAGS)
  79. tmp= av_get_int(v, o->name, NULL) | (int64_t)d;
  80. av_set_number(v, o->name, tmp, 1, 1);
  81. ret= o;
  82. }
  83. }
  84. return ret;
  85. }
  86. //FIXME use eval.c maybe?
  87. AVOption *av_set_string(void *obj, const char *name, const char *val){
  88. AVOption *o= find_opt(obj, name, NULL);
  89. if(o && o->offset==0 && o->type == FF_OPT_TYPE_CONST && o->unit){
  90. return set_all_opt(obj, o->unit, o->default_val);
  91. }
  92. if(!o || !val || o->offset<=0)
  93. return NULL;
  94. if(o->type != FF_OPT_TYPE_STRING){
  95. for(;;){
  96. int i;
  97. char buf[256], *tail;
  98. int cmd=0;
  99. double d;
  100. if(*val == '+' || *val == '-')
  101. cmd= *(val++);
  102. for(i=0; i<sizeof(buf)-1 && val[i] && val[i]!='+' && val[i]!='-'; i++)
  103. buf[i]= val[i];
  104. buf[i]=0;
  105. val+= i;
  106. d= av_parse_num(buf, &tail);
  107. if(tail <= buf){
  108. AVOption *o_named= find_opt(obj, buf, o->unit);
  109. if(o_named && o_named->type == FF_OPT_TYPE_CONST)
  110. d= o_named->default_val;
  111. else if(!strcmp(buf, "default")) d= o->default_val;
  112. else if(!strcmp(buf, "max" )) d= o->max;
  113. else if(!strcmp(buf, "min" )) d= o->min;
  114. else return NULL;
  115. }
  116. if(o->type == FF_OPT_TYPE_FLAGS){
  117. if (cmd=='+') d= av_get_int(obj, name, NULL) | (int64_t)d;
  118. else if(cmd=='-') d= av_get_int(obj, name, NULL) &~(int64_t)d;
  119. }else if(cmd=='-')
  120. d= -d;
  121. av_set_number(obj, name, d, 1, 1);
  122. if(!*val)
  123. return o;
  124. }
  125. return NULL;
  126. }
  127. memcpy(((uint8_t*)obj) + o->offset, val, sizeof(val));
  128. return o;
  129. }
  130. AVOption *av_set_double(void *obj, const char *name, double n){
  131. return av_set_number(obj, name, n, 1, 1);
  132. }
  133. AVOption *av_set_q(void *obj, const char *name, AVRational n){
  134. return av_set_number(obj, name, n.num, n.den, 1);
  135. }
  136. AVOption *av_set_int(void *obj, const char *name, int64_t n){
  137. return av_set_number(obj, name, 1, 1, n);
  138. }
  139. /**
  140. *
  141. * @param buf a buffer which is used for returning non string values as strings, can be NULL
  142. * @param buf_len allocated length in bytes of buf
  143. */
  144. const char *av_get_string(void *obj, const char *name, AVOption **o_out, char *buf, int buf_len){
  145. AVOption *o= find_opt(obj, name, NULL);
  146. void *dst;
  147. if(!o || o->offset<=0)
  148. return NULL;
  149. if(o->type != FF_OPT_TYPE_STRING && (!buf || !buf_len))
  150. return NULL;
  151. dst= ((uint8_t*)obj) + o->offset;
  152. if(o_out) *o_out= o;
  153. if(o->type == FF_OPT_TYPE_STRING)
  154. return dst;
  155. switch(o->type){
  156. case FF_OPT_TYPE_FLAGS: snprintf(buf, buf_len, "0x%08X",*(int *)dst);break;
  157. case FF_OPT_TYPE_INT: snprintf(buf, buf_len, "%d" , *(int *)dst);break;
  158. case FF_OPT_TYPE_INT64: snprintf(buf, buf_len, "%"PRId64, *(int64_t*)dst);break;
  159. case FF_OPT_TYPE_FLOAT: snprintf(buf, buf_len, "%f" , *(float *)dst);break;
  160. case FF_OPT_TYPE_DOUBLE: snprintf(buf, buf_len, "%f" , *(double *)dst);break;
  161. case FF_OPT_TYPE_RATIONAL: snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
  162. default: return NULL;
  163. }
  164. return buf;
  165. }
  166. static int av_get_number(void *obj, const char *name, AVOption **o_out, double *num, int *den, int64_t *intnum){
  167. AVOption *o= find_opt(obj, name, NULL);
  168. void *dst;
  169. if(!o || o->offset<=0)
  170. goto error;
  171. dst= ((uint8_t*)obj) + o->offset;
  172. if(o_out) *o_out= o;
  173. switch(o->type){
  174. case FF_OPT_TYPE_FLAGS:
  175. case FF_OPT_TYPE_INT: *intnum= *(int *)dst;return 0;
  176. case FF_OPT_TYPE_INT64: *intnum= *(int64_t*)dst;return 0;
  177. case FF_OPT_TYPE_FLOAT: *num= *(float *)dst;return 0;
  178. case FF_OPT_TYPE_DOUBLE: *num= *(double *)dst;return 0;
  179. case FF_OPT_TYPE_RATIONAL: *intnum= ((AVRational*)dst)->num;
  180. *den = ((AVRational*)dst)->den;
  181. return 0;
  182. }
  183. error:
  184. *den=*intnum=0;
  185. return -1;
  186. }
  187. double av_get_double(void *obj, const char *name, AVOption **o_out){
  188. int64_t intnum=1;
  189. double num=1;
  190. int den=1;
  191. av_get_number(obj, name, o_out, &num, &den, &intnum);
  192. return num*intnum/den;
  193. }
  194. AVRational av_get_q(void *obj, const char *name, AVOption **o_out){
  195. int64_t intnum=1;
  196. double num=1;
  197. int den=1;
  198. av_get_number(obj, name, o_out, &num, &den, &intnum);
  199. if(num == 1.0 && (int)intnum == intnum)
  200. return (AVRational){intnum, den};
  201. else
  202. return av_d2q(num*intnum/den, 1<<24);
  203. }
  204. int64_t av_get_int(void *obj, const char *name, AVOption **o_out){
  205. int64_t intnum=1;
  206. double num=1;
  207. int den=1;
  208. av_get_number(obj, name, o_out, &num, &den, &intnum);
  209. return num*intnum/den;
  210. }
  211. int av_opt_show(void *obj, void *av_log_obj){
  212. AVOption *opt=NULL;
  213. if(!obj)
  214. return -1;
  215. av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
  216. while((opt= av_next_option(obj, opt))){
  217. if(!(opt->flags & (AV_OPT_FLAG_ENCODING_PARAM|AV_OPT_FLAG_DECODING_PARAM)))
  218. continue;
  219. av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name);
  220. switch( opt->type )
  221. {
  222. case FF_OPT_TYPE_FLAGS:
  223. av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<flags>" );
  224. break;
  225. case FF_OPT_TYPE_INT:
  226. av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<int>" );
  227. break;
  228. case FF_OPT_TYPE_INT64:
  229. av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<int64>" );
  230. break;
  231. case FF_OPT_TYPE_DOUBLE:
  232. av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<double>" );
  233. break;
  234. case FF_OPT_TYPE_FLOAT:
  235. av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<float>" );
  236. break;
  237. case FF_OPT_TYPE_STRING:
  238. av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<string>" );
  239. break;
  240. case FF_OPT_TYPE_RATIONAL:
  241. av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<rational>" );
  242. break;
  243. case FF_OPT_TYPE_CONST:
  244. default:
  245. av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "" );
  246. break;
  247. }
  248. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
  249. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
  250. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
  251. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
  252. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
  253. if(opt->help)
  254. av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
  255. av_log(av_log_obj, AV_LOG_INFO, "\n");
  256. }
  257. return 0;
  258. }