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.

372 lines
12KB

  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. /**
  28. * strtod() function extended with 'k', 'M' and 'B' postfixes.
  29. * This allows using kB, MB, k, M and B as a postfix. This function
  30. * assumes that the unit of numbers is bits not bytes.
  31. */
  32. static double av_strtod(const char *name, char **tail) {
  33. double d;
  34. d= strtod(name, tail);
  35. if(*tail>name && (**tail=='k')) {
  36. d*=1000;
  37. (*tail)++;
  38. }
  39. else if(*tail && (**tail=='M')) {
  40. d*=1000000;
  41. (*tail)++;
  42. }
  43. else if(*tail && (**tail=='G')) {
  44. d*=1000000000;
  45. (*tail)++;
  46. }
  47. if(*tail && (**tail=='B')) {
  48. d*=8;
  49. (*tail)++;
  50. }
  51. return d;
  52. }
  53. static double av_parse_num(const char *name, char **tail){
  54. double d;
  55. d= av_strtod(name, tail);
  56. if(*tail>name && (**tail=='/' || **tail==':'))
  57. d/=av_strtod((*tail)+1, tail);
  58. return d;
  59. }
  60. //FIXME order them and do a bin search
  61. static AVOption *find_opt(void *v, const char *name, const char *unit){
  62. AVClass *c= *(AVClass**)v; //FIXME silly way of storing AVClass
  63. AVOption *o= c->option;
  64. for(;o && o->name; o++){
  65. if(!strcmp(o->name, name) && (!unit || !strcmp(o->unit, unit)) )
  66. return o;
  67. }
  68. return NULL;
  69. }
  70. AVOption *av_next_option(void *obj, AVOption *last){
  71. if(last && last[1].name) return ++last;
  72. else if(last) return NULL;
  73. else return (*(AVClass**)obj)->option;
  74. }
  75. static AVOption *av_set_number(void *obj, const char *name, double num, int den, int64_t intnum){
  76. AVOption *o= find_opt(obj, name, NULL);
  77. void *dst;
  78. if(!o || o->offset<=0)
  79. return NULL;
  80. if(o->max*den < num*intnum || o->min*den > num*intnum)
  81. return NULL;
  82. dst= ((uint8_t*)obj) + o->offset;
  83. switch(o->type){
  84. case FF_OPT_TYPE_FLAGS:
  85. case FF_OPT_TYPE_INT: *(int *)dst= lrintf(num/den)*intnum; break;
  86. case FF_OPT_TYPE_INT64: *(int64_t *)dst= lrintf(num/den)*intnum; break;
  87. case FF_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break;
  88. case FF_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break;
  89. case FF_OPT_TYPE_RATIONAL:
  90. if((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
  91. else *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
  92. default:
  93. return NULL;
  94. }
  95. return o;
  96. }
  97. static AVOption *set_all_opt(void *v, const char *unit, double d){
  98. AVClass *c= *(AVClass**)v; //FIXME silly way of storing AVClass
  99. AVOption *o= c->option;
  100. AVOption *ret=NULL;
  101. for(;o && o->name; o++){
  102. if(o->type != FF_OPT_TYPE_CONST && o->unit && !strcmp(o->unit, unit)){
  103. double tmp= d;
  104. if(o->type == FF_OPT_TYPE_FLAGS)
  105. tmp= av_get_int(v, o->name, NULL) | (int64_t)d;
  106. av_set_number(v, o->name, tmp, 1, 1);
  107. ret= o;
  108. }
  109. }
  110. return ret;
  111. }
  112. //FIXME use eval.c maybe?
  113. AVOption *av_set_string(void *obj, const char *name, const char *val){
  114. AVOption *o= find_opt(obj, name, NULL);
  115. if(o && o->offset==0 && o->type == FF_OPT_TYPE_CONST && o->unit){
  116. return set_all_opt(obj, o->unit, o->default_val);
  117. }
  118. if(!o || !val || o->offset<=0)
  119. return NULL;
  120. if(o->type != FF_OPT_TYPE_STRING){
  121. for(;;){
  122. int i;
  123. char buf[256], *tail;
  124. int cmd=0;
  125. double d;
  126. if(*val == '+' || *val == '-')
  127. cmd= *(val++);
  128. for(i=0; i<sizeof(buf)-1 && val[i] && val[i]!='+' && val[i]!='-'; i++)
  129. buf[i]= val[i];
  130. buf[i]=0;
  131. val+= i;
  132. d= av_parse_num(buf, &tail);
  133. if(tail <= buf){
  134. AVOption *o_named= find_opt(obj, buf, o->unit);
  135. if(o_named && o_named->type == FF_OPT_TYPE_CONST)
  136. d= o_named->default_val;
  137. else if(!strcmp(buf, "default")) d= o->default_val;
  138. else if(!strcmp(buf, "max" )) d= o->max;
  139. else if(!strcmp(buf, "min" )) d= o->min;
  140. else return NULL;
  141. }
  142. if(o->type == FF_OPT_TYPE_FLAGS){
  143. if (cmd=='+') d= av_get_int(obj, name, NULL) | (int64_t)d;
  144. else if(cmd=='-') d= av_get_int(obj, name, NULL) &~(int64_t)d;
  145. }else if(cmd=='-')
  146. d= -d;
  147. av_set_number(obj, name, d, 1, 1);
  148. if(!*val)
  149. return o;
  150. }
  151. return NULL;
  152. }
  153. memcpy(((uint8_t*)obj) + o->offset, val, sizeof(val));
  154. return o;
  155. }
  156. AVOption *av_set_double(void *obj, const char *name, double n){
  157. return av_set_number(obj, name, n, 1, 1);
  158. }
  159. AVOption *av_set_q(void *obj, const char *name, AVRational n){
  160. return av_set_number(obj, name, n.num, n.den, 1);
  161. }
  162. AVOption *av_set_int(void *obj, const char *name, int64_t n){
  163. return av_set_number(obj, name, 1, 1, n);
  164. }
  165. /**
  166. *
  167. * @param buf a buffer which is used for returning non string values as strings, can be NULL
  168. * @param buf_len allocated length in bytes of buf
  169. */
  170. const char *av_get_string(void *obj, const char *name, AVOption **o_out, char *buf, int buf_len){
  171. AVOption *o= find_opt(obj, name, NULL);
  172. void *dst;
  173. if(!o || o->offset<=0)
  174. return NULL;
  175. if(o->type != FF_OPT_TYPE_STRING && (!buf || !buf_len))
  176. return NULL;
  177. dst= ((uint8_t*)obj) + o->offset;
  178. if(o_out) *o_out= o;
  179. if(o->type == FF_OPT_TYPE_STRING)
  180. return dst;
  181. switch(o->type){
  182. case FF_OPT_TYPE_FLAGS: snprintf(buf, buf_len, "0x%08X",*(int *)dst);break;
  183. case FF_OPT_TYPE_INT: snprintf(buf, buf_len, "%d" , *(int *)dst);break;
  184. case FF_OPT_TYPE_INT64: snprintf(buf, buf_len, "%"PRId64, *(int64_t*)dst);break;
  185. case FF_OPT_TYPE_FLOAT: snprintf(buf, buf_len, "%f" , *(float *)dst);break;
  186. case FF_OPT_TYPE_DOUBLE: snprintf(buf, buf_len, "%f" , *(double *)dst);break;
  187. case FF_OPT_TYPE_RATIONAL: snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
  188. default: return NULL;
  189. }
  190. return buf;
  191. }
  192. static int av_get_number(void *obj, const char *name, AVOption **o_out, double *num, int *den, int64_t *intnum){
  193. AVOption *o= find_opt(obj, name, NULL);
  194. void *dst;
  195. if(!o || o->offset<=0)
  196. goto error;
  197. dst= ((uint8_t*)obj) + o->offset;
  198. if(o_out) *o_out= o;
  199. switch(o->type){
  200. case FF_OPT_TYPE_FLAGS:
  201. case FF_OPT_TYPE_INT: *intnum= *(int *)dst;return 0;
  202. case FF_OPT_TYPE_INT64: *intnum= *(int64_t*)dst;return 0;
  203. case FF_OPT_TYPE_FLOAT: *num= *(float *)dst;return 0;
  204. case FF_OPT_TYPE_DOUBLE: *num= *(double *)dst;return 0;
  205. case FF_OPT_TYPE_RATIONAL: *intnum= ((AVRational*)dst)->num;
  206. *den = ((AVRational*)dst)->den;
  207. return 0;
  208. }
  209. error:
  210. *den=*intnum=0;
  211. return -1;
  212. }
  213. double av_get_double(void *obj, const char *name, AVOption **o_out){
  214. int64_t intnum=1;
  215. double num=1;
  216. int den=1;
  217. av_get_number(obj, name, o_out, &num, &den, &intnum);
  218. return num*intnum/den;
  219. }
  220. AVRational av_get_q(void *obj, const char *name, AVOption **o_out){
  221. int64_t intnum=1;
  222. double num=1;
  223. int den=1;
  224. av_get_number(obj, name, o_out, &num, &den, &intnum);
  225. if(num == 1.0 && (int)intnum == intnum)
  226. return (AVRational){intnum, den};
  227. else
  228. return av_d2q(num*intnum/den, 1<<24);
  229. }
  230. int64_t av_get_int(void *obj, const char *name, AVOption **o_out){
  231. int64_t intnum=1;
  232. double num=1;
  233. int den=1;
  234. av_get_number(obj, name, o_out, &num, &den, &intnum);
  235. return num*intnum/den;
  236. }
  237. int av_opt_show(void *obj, void *av_log_obj){
  238. AVOption *opt=NULL;
  239. if(!obj)
  240. return -1;
  241. av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
  242. while((opt= av_next_option(obj, opt))){
  243. if(!(opt->flags & (AV_OPT_FLAG_ENCODING_PARAM|AV_OPT_FLAG_DECODING_PARAM)))
  244. continue;
  245. av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name);
  246. switch( opt->type )
  247. {
  248. case FF_OPT_TYPE_FLAGS:
  249. av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<flags>" );
  250. break;
  251. case FF_OPT_TYPE_INT:
  252. av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<int>" );
  253. break;
  254. case FF_OPT_TYPE_INT64:
  255. av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<int64>" );
  256. break;
  257. case FF_OPT_TYPE_DOUBLE:
  258. av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<double>" );
  259. break;
  260. case FF_OPT_TYPE_FLOAT:
  261. av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<float>" );
  262. break;
  263. case FF_OPT_TYPE_STRING:
  264. av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<string>" );
  265. break;
  266. case FF_OPT_TYPE_RATIONAL:
  267. av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "<rational>" );
  268. break;
  269. case FF_OPT_TYPE_CONST:
  270. default:
  271. av_log( av_log_obj, AV_LOG_INFO, "%-7s ", "" );
  272. break;
  273. }
  274. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
  275. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
  276. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
  277. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
  278. av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
  279. if(opt->help)
  280. av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
  281. av_log(av_log_obj, AV_LOG_INFO, "\n");
  282. }
  283. return 0;
  284. }
  285. /** Set the values of the AVCodecContext or AVFormatContext structure.
  286. * They are set to the defaults specified in the according AVOption options
  287. * array default_val field.
  288. *
  289. * @param s AVCodecContext or AVFormatContext for which the defaults will be set
  290. */
  291. void av_opt_set_defaults(void *s)
  292. {
  293. AVOption *opt = NULL;
  294. while ((opt = av_next_option(s, opt)) != NULL) {
  295. switch(opt->type) {
  296. case FF_OPT_TYPE_CONST:
  297. /* Nothing to be done here */
  298. break;
  299. case FF_OPT_TYPE_FLAGS:
  300. case FF_OPT_TYPE_INT: {
  301. int val;
  302. val = opt->default_val;
  303. av_set_int(s, opt->name, val);
  304. }
  305. break;
  306. case FF_OPT_TYPE_FLOAT: {
  307. double val;
  308. val = opt->default_val;
  309. av_set_double(s, opt->name, val);
  310. }
  311. break;
  312. case FF_OPT_TYPE_RATIONAL: {
  313. AVRational val;
  314. val = av_d2q(opt->default_val, INT_MAX);
  315. av_set_q(s, opt->name, val);
  316. }
  317. break;
  318. case FF_OPT_TYPE_STRING:
  319. /* Cannot set default for string as default_val is of type * double */
  320. break;
  321. default:
  322. av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
  323. }
  324. }
  325. }